libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit a4030d4473d4237aa9dad30d7812cd1c9e764dba
parent b457435a8845782708575236f2a7e67537330e6f
Author: Christian Grothoff <christian@grothoff.org>
Date:   Sat, 20 Dec 2014 00:25:22 +0000

-check RVals

Diffstat:
Msrc/testspdy/test_misc.c | 46++++++++++++++++++++++++----------------------
Msrc/testspdy/test_proxies.c | 24+++++++++++++-----------
Msrc/testspdy/test_request_response.c | 80+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Msrc/testspdy/test_session_timeout.c | 4++++
4 files changed, 83 insertions(+), 71 deletions(-)

diff --git a/src/testspdy/test_misc.c b/src/testspdy/test_misc.c @@ -68,7 +68,7 @@ create_child() child = fork(); if (-1 == child) - { + { fprintf(stderr, "can't fork, error %d\n", errno); exit(EXIT_FAILURE); } @@ -79,6 +79,8 @@ create_child() char *uri; fflush(stdout); devnull = open("/dev/null", O_WRONLY); + if (-1 == devnull) + abort (); if (1 != devnull) { dup2(devnull, 1); @@ -100,7 +102,7 @@ response_done_callback(void *cls, { (void)status; (void)streamopened; - + if(strcmp(cls,"/main.css")) { session1 = SPDY_get_session_for_request(request); @@ -109,7 +111,7 @@ response_done_callback(void *cls, printf("SPDY_get_session_for_request failed\n"); killchild(); } - + char *session_cls = strdup(SESSION_CLS); SPDY_set_cls_to_session(session1,session_cls); } @@ -122,7 +124,7 @@ response_done_callback(void *cls, killchild(); } printf("SPDY_get_session_for_request tested...\n"); - + void *session_cls = SPDY_get_cls_from_session(session2); if(NULL == session_cls || strcmp(session_cls, SESSION_CLS)) { @@ -131,7 +133,7 @@ response_done_callback(void *cls, } printf("SPDY_set_cls_to_session tested...\n"); printf("SPDY_get_cls_from_session tested...\n"); - + void *request_cls = SPDY_get_cls_from_request(request); if(NULL == request_cls || strcmp(request_cls, REQUEST_CLS)) { @@ -141,7 +143,7 @@ response_done_callback(void *cls, printf("SPDY_set_cls_to_request tested...\n"); printf("SPDY_get_cls_from_request tested...\n"); } - + SPDY_destroy_request(request); SPDY_destroy_response(response); free(cls); @@ -168,10 +170,10 @@ standard_request_handler(void *cls, (void)method; (void)version; (void)more; - + struct SPDY_Response *response=NULL; char *cls_path = strdup(path); - + if(strcmp(path,"/main.css")==0) { char *request_cls = strdup(REQUEST_CLS); @@ -182,12 +184,12 @@ standard_request_handler(void *cls, { response = SPDY_build_response(200,NULL,SPDY_HTTP_VERSION_1_1,NULL,HTML,strlen(HTML)); } - + if(NULL==response){ fprintf(stdout,"no response obj\n"); killchild(); } - + if(SPDY_queue_response(request,response,true,false,&response_done_callback,cls_path)!=SPDY_YES) { fprintf(stdout,"queue\n"); @@ -197,7 +199,7 @@ standard_request_handler(void *cls, int parentproc() -{ +{ int childstatus; unsigned long long timeoutlong=0; struct timeval timeout; @@ -207,7 +209,7 @@ parentproc() fd_set except_fd_set; int maxfd = -1; struct SPDY_Daemon *daemon; - + daemon = SPDY_start_daemon(port, DATA_DIR "cert-and-key.pem", DATA_DIR "cert-and-key.pem", @@ -219,12 +221,12 @@ parentproc() SPDY_DAEMON_OPTION_SESSION_TIMEOUT, 1800, SPDY_DAEMON_OPTION_END); - + if(NULL==daemon){ printf("no daemon\n"); return 1; } - + create_child(); do @@ -244,14 +246,14 @@ parentproc() timeout.tv_sec = timeoutlong / 1000; timeout.tv_usec = (timeoutlong % 1000) * 1000; } - + maxfd = SPDY_get_fdset (daemon, &read_fd_set, - &write_fd_set, + &write_fd_set, &except_fd_set); - + ret = select(maxfd+1, &read_fd_set, &write_fd_set, &except_fd_set, &timeout); - + switch(ret) { case -1: printf("select error: %i\n", errno); @@ -268,7 +270,7 @@ parentproc() while(waitpid(child,&childstatus,WNOHANG) != child); SPDY_stop_daemon(daemon); - + return WEXITSTATUS(childstatus); } @@ -278,10 +280,10 @@ main() { port = get_port(13123); SPDY_init(); - + int ret = parentproc(); - + SPDY_deinit(); - + return ret; } diff --git a/src/testspdy/test_proxies.c b/src/testspdy/test_proxies.c @@ -80,7 +80,7 @@ pid_t au_fork() if (child == -1) { killchildren(); - + killproc(parent,"fork failed\n"); } @@ -93,7 +93,7 @@ int main() //pid_t child; int childstatus; pid_t wpid; - + parent = getpid(); mhd_port = get_port(4000); spdy2http_port = get_port(4100); @@ -119,8 +119,8 @@ int main() //killchildren(); _exit(1); } - - + + child_spdy2http = au_fork(); if (child_spdy2http == 0) { @@ -144,7 +144,7 @@ int main() //killchildren(); _exit(1); } - + child_mhd2spdy = au_fork(); if (child_mhd2spdy == 0) { @@ -168,7 +168,7 @@ int main() //killchildren(); _exit(1); } - + child_curl = au_fork(); if (child_curl == 0) { @@ -181,12 +181,14 @@ int main() close(1); devnull = open("/dev/null", O_WRONLY); + if (-1 == devnull) + abort (); if (1 != devnull) { dup2(devnull, 1); close(devnull); } - + asprintf (&cmd, "curl --proxy http://127.0.0.1:%i http://127.0.0.1:%i/", mhd2spdy_port, mhd_port); sleep(3); p = popen(cmd, "r"); @@ -205,7 +207,7 @@ int main() //killchildren(); _exit(1); } - + do { wpid = waitpid(child_mhd,&childstatus,WNOHANG); @@ -215,7 +217,7 @@ int main() killchildren(); return 1; } - + wpid = waitpid(child_spdy2http,&childstatus,WNOHANG); if(wpid == child_spdy2http) { @@ -223,7 +225,7 @@ int main() killchildren(); return 1; } - + wpid = waitpid(child_mhd2spdy,&childstatus,WNOHANG); if(wpid == child_mhd2spdy) { @@ -231,7 +233,7 @@ int main() killchildren(); return 1; } - + if(waitpid(child_curl,&childstatus,WNOHANG) == child_curl) { killchildren(); diff --git a/src/testspdy/test_request_response.c b/src/testspdy/test_request_response.c @@ -39,7 +39,7 @@ char *rcvbuf; int rcvbuf_c = 0; int session_closed_called = 0; - + void killchild(int pid, char *message) { @@ -55,7 +55,7 @@ killparent(int pid, char *message) kill(pid, SIGKILL); _exit(1); } - + /***** * start of code needed to utilize spdylay @@ -131,6 +131,8 @@ static char* strcopy(const char *s, size_t len) { char *dst; dst = malloc(len+1); + if (NULL == dst) + abort (); memcpy(dst, s, len); dst[len] = '\0'; return dst; @@ -204,7 +206,7 @@ static ssize_t send_callback(spdylay_session *session, { (void)session; (void)flags; - + struct Connection *connection; ssize_t rv; connection = (struct Connection*)user_data; @@ -236,7 +238,7 @@ static ssize_t recv_callback(spdylay_session *session, { (void)session; (void)flags; - + struct Connection *connection; ssize_t rv; connection = (struct Connection*)user_data; @@ -270,7 +272,7 @@ static void before_ctrl_send_callback(spdylay_session *session, void *user_data) { (void)user_data; - + if(type == SPDYLAY_SYN_STREAM) { struct Request *req; int stream_id = frame->syn_stream.stream_id; @@ -287,7 +289,7 @@ static void on_ctrl_send_callback(spdylay_session *session, spdylay_frame *frame, void *user_data) { (void)user_data; - + char **nv; const char *name = NULL; int32_t stream_id; @@ -360,7 +362,7 @@ static void on_stream_close_callback(spdylay_session *session, { (void)user_data; (void)status_code; - + struct Request *req; req = spdylay_session_get_stream_user_data(session, stream_id); if(req) { @@ -385,7 +387,7 @@ static void on_data_chunk_recv_callback(spdylay_session *session, uint8_t flags, { (void)user_data; (void)flags; - + struct Request *req; req = spdylay_session_get_stream_user_data(session, stream_id); if(req) { @@ -409,13 +411,13 @@ static void on_data_chunk_recv_callback(spdylay_session *session, uint8_t flags, } else { /* TODO add support gzip */ fwrite(data, 1, len, stdout); - + //check if the data is correct //if(strcmp(RESPONSE_BODY, data) != 0) //killparent(parent, "\nreceived data is not the same"); if(len + rcvbuf_c > strlen(RESPONSE_BODY)) killparent(parent, "\nreceived data is not the same"); - + strcpy(rcvbuf + rcvbuf_c,(char*)data); rcvbuf_c+=len; } @@ -452,7 +454,7 @@ static int select_next_proto_cb(SSL* ssl, void *arg) { (void)ssl; - + int rv; uint16_t *spdy_proto_version; /* spdylay_select_next_protocol() selects SPDY protocol version the @@ -655,6 +657,8 @@ static void fetch_uri(const struct URI *uri) /* Establish connection and setup SSL */ fd = connect_to(req.host, req.port); + if (-1 == fd) + abort (); ssl_ctx = SSL_CTX_new(SSLv23_client_method()); if(ssl_ctx == NULL) { dief("SSL_CTX_new", ERR_error_string(ERR_get_error(), NULL)); @@ -799,7 +803,7 @@ static int parse_uri(struct URI *res, const char *uri) /***** * end of code needed to utilize spdylay */ - + /***** * start of code needed to utilize microspdy @@ -826,26 +830,26 @@ standard_request_handler(void *cls, (void)headers; (void)method; (void)version; - + struct SPDY_Response *response=NULL; - + if(strcmp(CLS,cls)!=0) { killchild(child,"wrong cls"); } - + if(false != more){ fprintf(stdout,"more has wrong value\n"); exit(5); } - + response = SPDY_build_response(200,NULL,SPDY_HTTP_VERSION_1_1,NULL,RESPONSE_BODY,strlen(RESPONSE_BODY)); - + if(NULL==response){ fprintf(stdout,"no response obj\n"); exit(3); } - + if(SPDY_queue_response(request,response,true,false,NULL,(void*)strdup(path))!=SPDY_YES) { fprintf(stdout,"queue\n"); @@ -859,12 +863,12 @@ session_closed_handler (void *cls, int by_client) { printf("session_closed_handler called\n"); - + if(strcmp(CLS,cls)!=0) { killchild(child,"wrong cls"); } - + if(SPDY_YES != by_client) { //killchild(child,"wrong by_client"); @@ -874,12 +878,12 @@ session_closed_handler (void *cls, { printf("session closed by client\n"); } - + if(NULL == session) { killchild(child,"session is NULL"); } - + session_closed_called = 1; } @@ -887,7 +891,7 @@ session_closed_handler (void *cls, /***** * end of code needed to utilize microspdy */ - + //child process void childproc(int port) @@ -896,7 +900,7 @@ childproc(int port) struct sigaction act; int rv; char *uristr; - + memset(&act, 0, sizeof(struct sigaction)); act.sa_handler = SIG_IGN; sigaction(SIGPIPE, &act, 0); @@ -913,7 +917,7 @@ childproc(int port) killparent(parent,"parse_uri failed"); } fetch_uri(&uri); - + if(strcmp(rcvbuf, RESPONSE_BODY)) killparent(parent,"received data is different"); } @@ -931,14 +935,14 @@ parentproc( int port) fd_set except_fd_set; int maxfd = -1; struct SPDY_Daemon *daemon; - + SPDY_init(); - + daemon = SPDY_start_daemon(port, DATA_DIR "cert-and-key.pem", DATA_DIR "cert-and-key.pem", NULL,&session_closed_handler,&standard_request_handler,NULL,CLS,SPDY_DAEMON_OPTION_END); - + if(NULL==daemon){ printf("no daemon\n"); return 1; @@ -961,14 +965,14 @@ parentproc( int port) timeout.tv_sec = timeoutlong / 1000; timeout.tv_usec = (timeoutlong % 1000) * 1000; } - + maxfd = SPDY_get_fdset (daemon, &read_fd_set, - &write_fd_set, + &write_fd_set, &except_fd_set); - + ret = select(maxfd+1, &read_fd_set, &write_fd_set, &except_fd_set, &timeout); - + switch(ret) { case -1: printf("select error: %i\n", errno); @@ -990,9 +994,9 @@ parentproc( int port) SPDY_run(daemon); SPDY_stop_daemon(daemon); - + SPDY_deinit(); - + return WEXITSTATUS(childstatus); } @@ -1000,21 +1004,21 @@ int main() { int port = get_port(12123); parent = getpid(); - + child = fork(); if (child == -1) - { + { fprintf(stderr, "can't fork, error %d\n", errno); exit(EXIT_FAILURE); } - + if (child == 0) { childproc(port); _exit(0); } else - { + { int ret = parentproc(port); if(1 == session_closed_called && 0 == ret) exit(0); diff --git a/src/testspdy/test_session_timeout.c b/src/testspdy/test_session_timeout.c @@ -283,12 +283,16 @@ childproc() close(devnull); }*/ devnull = open("/dev/null", O_WRONLY); + if (-1 == devnull) + abort (); if (1 != devnull) { dup2(devnull, 1); close(devnull); } devnull = open("/dev/null", O_WRONLY); + if (-1 == devnull) + abort (); if (2 != devnull) { dup2(devnull, 2);