From 50735544c1caee405c3bdf0845bed78522b92f0c Mon Sep 17 00:00:00 2001 From: Andrey Uzunov Date: Sat, 20 Jul 2013 19:21:10 +0000 Subject: mhd2spdy: code formatting done and old code removed --- src/examples/mhd2spdy.c | 82 +++------ src/examples/mhd2spdy_http.c | 178 ++------------------ src/examples/mhd2spdy_http.h | 9 +- src/examples/mhd2spdy_spdy.c | 329 ++++++++++++++++++++----------------- src/examples/mhd2spdy_spdy.h | 71 +++++--- src/examples/mhd2spdy_structures.c | 26 +-- src/examples/mhd2spdy_structures.h | 72 ++++---- 7 files changed, 321 insertions(+), 446 deletions(-) diff --git a/src/examples/mhd2spdy.c b/src/examples/mhd2spdy.c index d921aa27..0426a276 100644 --- a/src/examples/mhd2spdy.c +++ b/src/examples/mhd2spdy.c @@ -31,16 +31,20 @@ #include "mhd2spdy_spdy.h" #include "mhd2spdy_http.h" + static int run = 1; //static int spdy_close = 0; -static void catch_signal(int signal) + +static void +catch_signal(int signal) { (void)signal; //spdy_close = 1; run = 0; } + int run_everything () { @@ -54,10 +58,7 @@ run_everything () int maxfd_s = -1; struct MHD_Daemon *daemon; nfds_t spdy_npollfds = 1; - //struct pollfd spdy_pollfds[MAX_SPDY_CONNECTIONS]; struct URI * spdy2http_uri = NULL; - //int spdy_nfds; - //int spdylay_timeout = 0; struct SPDY_Connection *connection; struct SPDY_Connection *connections[MAX_SPDY_CONNECTIONS]; struct SPDY_Connection *connection_for_delete; @@ -68,18 +69,15 @@ run_everything () if (signal(SIGINT, catch_signal) == SIG_ERR) PRINT_INFO("signal failed"); - //spdy2http_url = argv[2]; glob_opt.streams_opened = 0; glob_opt.responses_pending = 0; glob_opt.global_memory = 0; - //spdy_proto_version = 0; srand(time(NULL)); if(init_parse_uri(&glob_opt.uri_preg)) DIE("Regexp compilation failed"); - if(NULL != glob_opt.spdy2http_str) { ret = parse_uri(&glob_opt.uri_preg, glob_opt.spdy2http_str, &spdy2http_uri); @@ -105,12 +103,10 @@ run_everything () if(NULL==daemon) DIE("MHD_start_daemon failed"); - do { - - timeout.tv_sec = 0; - timeout.tv_usec = 0; + timeout.tv_sec = 0; + timeout.tv_usec = 0; if(NULL == glob_opt.spdy_connection && NULL != glob_opt.spdy2http_str) { @@ -119,32 +115,19 @@ run_everything () PRINT_INFO("cannot connect to the proxy"); } - //PRINT_INFO("while1"); FD_ZERO(&rs); FD_ZERO(&ws); FD_ZERO(&es); - - /*if(glob_opt.spdy_data_received) - { - timeout.tv_sec = 0; - timeout.tv_usec = 0; - glob_opt.spdy_data_received = false; -} -else{*/ - /*if(glob_opt.responses_pending || glob_opt.streams_opened)// TODO only streams_opened true? - timeout.tv_usec = 0; //return immediately + + ret = MHD_get_timeout(daemon, &timeoutlong); + if(MHD_NO == ret || timeoutlong > 5000) + timeout.tv_sec = 5; else - {*/ - ret = MHD_get_timeout(daemon, &timeoutlong); - if(MHD_NO == ret || timeoutlong > 5000) - timeout.tv_sec = 5; - else - { - timeout.tv_sec = timeoutlong / 1000; - timeout.tv_usec = (timeoutlong % 1000) * 1000; - } - //} -//} + { + timeout.tv_sec = timeoutlong / 1000; + timeout.tv_usec = (timeoutlong % 1000) * 1000; + } + if(MHD_NO == MHD_get_fdset (daemon, &rs, &ws, @@ -160,12 +143,10 @@ else{*/ &ws, &es, connections, MAX_SPDY_CONNECTIONS, &spdy_npollfds); - if(maxfd_s > maxfd) maxfd = maxfd_s; + if(maxfd_s > maxfd) + maxfd = maxfd_s; PRINT_INFO2("MHD timeout %lld %lld", (unsigned long long)timeout.tv_sec, (unsigned long long)timeout.tv_usec); - //TODO - //timeout.tv_sec = 0; - //timeout.tv_usec = 0; glob_opt.spdy_data_received = false; @@ -181,42 +162,20 @@ else{*/ break; default: PRINT_INFO("run"); - //MHD_run_from_select(daemon,&rs, &ws, &es); //not closing FDs + //MHD_run_from_select(daemon,&rs, &ws, &es); //not closing FDs at some time in past MHD_run(daemon); spdy_run_select(&rs, &ws, &es, connections, spdy_npollfds); if(glob_opt.spdy_data_received) { PRINT_INFO("MHD run again"); - //MHD_run_from_select(daemon,&rs, &ws, &es); //not closing FDs + //MHD_run_from_select(daemon,&rs, &ws, &es); //not closing FDs at some time in past MHD_run(daemon); } break; } - - /* - //if(glob_opt.streams_opened) spdylay_timeout = 500; - //if(glob_opt.responses_pending || glob_opt.streams_opened) spdylay_timeout = 0; - //else spdylay_timeout = 0; - //else spdylay_timeout = 0; - - spdy_get_pollfdset(spdy_pollfds, connections, MAX_SPDY_CONNECTIONS, &spdy_npollfds); - - //TODO - //spdylay_timeout = 0; - - PRINT_INFO2("spdylay timeout %i", spdylay_timeout); - ret = poll(spdy_pollfds, spdy_npollfds, spdylay_timeout); - if(ret == -1) - DIE("poll"); - if(ret > 0){ - PRINT_INFO("spdy_run"); - spdy_run(spdy_pollfds, connections, spdy_npollfds); - }*/ } while(run); - //TODO exit from loop and clean - MHD_stop_daemon (daemon); //TODO SSL_free brakes @@ -246,6 +205,7 @@ else{*/ return 0; } + void display_usage() { diff --git a/src/examples/mhd2spdy_http.c b/src/examples/mhd2spdy_http.c index 4761c086..7881d57a 100644 --- a/src/examples/mhd2spdy_http.c +++ b/src/examples/mhd2spdy_http.c @@ -26,7 +26,9 @@ #include "mhd2spdy_spdy.h" -void * http_log_cb(void * cls, const char * uri) +void * +http_log_cb(void * cls, +const char * uri) { (void)cls; @@ -34,29 +36,18 @@ void * http_log_cb(void * cls, const char * uri) PRINT_INFO2("log uri '%s'\n", uri); - //TODO free if(NULL == (http_uri = au_malloc(sizeof(struct HTTP_URI )))) DIE("no memory"); - //memset(http_uri, 0 , sizeof(struct HTTP_URI)); http_uri->uri = strdup(uri); return http_uri; } -/* -static int -http_query_iterate_cb(void *cls, - enum MHD_ValueKind kind, - const char *name, const char *value) -{ - -}*/ - - static int http_iterate_cb(void *cls, - enum MHD_ValueKind kind, - const char *name, const char *value) + enum MHD_ValueKind kind, + const char *name, + const char *value) { (void)kind; @@ -85,9 +76,9 @@ http_iterate_cb(void *cls, static ssize_t http_response_callback (void *cls, - uint64_t pos, - char *buffer, - size_t max) + uint64_t pos, + char *buffer, + size_t max) { (void)pos; @@ -96,15 +87,6 @@ http_response_callback (void *cls, void *newbody; const union MHD_ConnectionInfo *info; int val = 1; - - //max=16; - - //PRINT_INFO2("response_callback, pos: %i, max is %i, len is %i",pos,max,proxy->length); - - //assert(0 != proxy->length); - - //if(MHD_CONTENT_READER_END_OF_STREAM == proxy->length) - // return MHD_CONTENT_READER_END_OF_STREAM; PRINT_INFO2("http_response_callback for %s", proxy->url); @@ -116,7 +98,6 @@ http_response_callback (void *cls, return MHD_CONTENT_READER_END_OF_STREAM; } - //*more = true; if(!proxy->http_body_size)//nothing to write now { //flush data @@ -154,13 +135,6 @@ http_response_callback (void *cls, if(proxy->length >= 0) { proxy->length -= ret; - //printf("pr len %i", proxy->length); - /*if(proxy->length <= 0) - { - // *more = false; - //last frame - proxy->length = MHD_CONTENT_READER_END_OF_STREAM; - }*/ } PRINT_INFO2("response_callback, size: %i",ret); @@ -175,27 +149,7 @@ http_response_done_callback(void *cls) struct Proxy *proxy = (struct Proxy *)cls; PRINT_INFO2("http_response_done_callback for %s", proxy->url); - //int ret; - - //printf("response_done_callback\n"); - - //printf("answer for %s was sent\n", (char *)cls); - - /*if(SPDY_RESPONSE_RESULT_SUCCESS != status) - { - printf("answer was NOT sent, %i\n",status); - }*/ - /*if(CURLM_OK != (ret = curl_multi_remove_handle(multi_handle, proxy->curl_handle))) - { - PRINT_INFO2("curl_multi_remove_handle failed (%i)", ret); - } - curl_slist_free_all(proxy->curl_headers); - curl_easy_cleanup(proxy->curl_handle); - */ - //SPDY_destroy_request(request); - //SPDY_destroy_response(response); - //if(!strcmp("/close",proxy->path)) run = 0; - //free(proxy->path); + if(proxy->spdy_active) proxy->http_active = false; else @@ -219,12 +173,8 @@ http_cb_request (void *cls, (void)upload_data; (void)upload_data_size; - //struct MHD_Response *response; int ret; struct Proxy *proxy; - //struct URI *spdy_uri; - //char **nv; - //int num_headers; struct SPDY_Headers spdy_headers; //PRINT_INFO2("request cb %i; %s", *ptr,url); @@ -240,7 +190,7 @@ http_cb_request (void *cls, free(http_uri->uri); free(http_uri); PRINT_INFO2("unexpected method %s", method); - return MHD_NO; /* unexpected method */ + return MHD_NO; } if(NULL == (proxy = au_malloc(sizeof(struct Proxy)))) @@ -250,17 +200,14 @@ http_cb_request (void *cls, } ++glob_opt.responses_pending; - //memset(proxy, 0, sizeof(struct Proxy)); proxy->id = rand(); proxy->http_active = true; - //PRINT_INFO2("proxy obj with id %i created (%i)", proxy->id, proxy); proxy->http_connection = connection; http_uri->proxy = proxy; return MHD_YES; } proxy = http_uri->proxy; - //*ptr = NULL; /* reset when done */ if(proxy->spdy_active) { @@ -270,45 +217,15 @@ http_cb_request (void *cls, } PRINT_INFO2("received request for '%s %s %s'\n", method, http_uri->uri, version); - /* - proxy->http_response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, - 8096, - &http_response_callback, - proxy, - &http_response_done_callback); - - if (proxy->http_response == NULL) - DIE("no response"); - */ proxy->url = http_uri->uri; - //if(NULL == (proxy->url = strdup(http_uri->uri))) - // DIE("no memory"); - -//TODO HTTP headers - /*MHD_get_connection_values (connection, - MHD_HEADER_KIND, - &http_iterate_cb, - proxy); - */ - //proxy->url = strdup(url); - //if(NULL == (spdy_uri = au_malloc(sizeof(struct URI)))) - // DIE("no memory"); ret = parse_uri(&glob_opt.uri_preg, proxy->url, &proxy->uri); if(ret != 0) DIE("parse_uri failed"); - //proxy->uri = spdy_uri; proxy->http_uri = http_uri; proxy->spdy_active = true; - //proxy->spdy_request = au_malloc(sizeof(struct SPDY_Request)); - //if(NULL == proxy->spdy_request) - // DIE("no memory"); - //memset(proxy->spdy_request,0,sizeof(struct SPDY_Request)); - //spdy_request_init(proxy->spdy_request, &spdy_uri); - //spdy_submit_request(spdy_connection, proxy); - spdy_headers.num = MHD_get_connection_values (connection, MHD_HEADER_KIND, NULL, @@ -331,16 +248,10 @@ http_cb_request (void *cls, if(NULL == spdy_headers.nv[9]) spdy_headers.nv[9] = proxy->uri->host_and_port; - /*int i; - for(i=0; ihttp_response); - free_proxy(proxy);//TODO call it here or in done_callback + free_proxy(proxy); return MHD_NO; } @@ -364,60 +275,20 @@ http_cb_request (void *cls, if(MHD_NO == MHD_add_response_header (proxy->http_response, "Keep-Alive", "timeout=5, max=100")) PRINT_INFO("SPDY_name_value_add failed: "); - /* - const union MHD_ConnectionInfo *info; - info = MHD_get_connection_info (connection, - MHD_CONNECTION_INFO_CONNECTION_FD); - int val = 1; - int rv; - rv = setsockopt(info->connect_fd, IPPROTO_TCP, TCP_NODELAY, &val, (socklen_t)sizeof(val)); - if(rv == -1) { - DIE("setsockopt"); - }*/ + return MHD_YES; } + void -http_create_response(struct Proxy* proxy, char **nv) +http_create_response(struct Proxy* proxy, + char **nv) { size_t i; - //uint64_t response_size=MHD_SIZE_UNKNOWN; - - /*for(i = 0; nv[i]; i += 2) { - if(0 == strcmp("content-length", nv[i])) - { - response_size = atoi(nv[i+1]); - break; - } - }*/ - /* - proxy->http_response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, - 4096, - &http_response_callback, - proxy, - &http_response_done_callback); - - if (proxy->http_response == NULL) - DIE("no response"); - if(MHD_NO == MHD_add_response_header (proxy->http_response, - "Proxy-Connection", "keep-alive")) - PRINT_INFO("SPDY_name_value_add failed: "); - if(MHD_NO == MHD_add_response_header (proxy->http_response, - "Connection", "Keep-Alive")) - PRINT_INFO("SPDY_name_value_add failed: "); - if(MHD_NO == MHD_add_response_header (proxy->http_response, - "Keep-Alive", "timeout=5, max=100")) - PRINT_INFO("SPDY_name_value_add failed: "); - */ for(i = 0; nv[i]; i += 2) { - //printf(" %s: %s\n", nv[i], nv[i+1]); - //int j; - if(0 == strcmp(":status", nv[i])) { - //raise(SIGINT); - //proxy->status_msg = nv[i+1]; char tmp[4]; memcpy(&tmp,nv[i+1],3); tmp[3]=0; @@ -431,35 +302,22 @@ http_create_response(struct Proxy* proxy, char **nv) } else if(0 == strcmp("content-length", nv[i])) { - //proxy->length = atoi(nv[i+1]); - //response_size = atoi(nv[i+1]); continue; } - //for(j=0; jhttp_response, header, nv[i+1])) { PRINT_INFO2("SPDY_name_value_add failed: '%s' '%s'", header, nv[i+1]); - //abort(); } PRINT_INFO2("adding '%s: %s'",header, nv[i+1]); } - //PRINT_INFO2("%i", MHD_get_response_headers(proxy->http_response, NULL, NULL)); - //PRINT_INFO2("state before %i", proxy->http_connection->state); - //PRINT_INFO2("loop before %i", proxy->http_connection->event_loop_info); if(MHD_NO == MHD_queue_response (proxy->http_connection, proxy->status, proxy->http_response)){ PRINT_INFO("No queue"); abort(); } - //PRINT_INFO2("state after %i", proxy->http_connection->state); - //PRINT_INFO2("loop after %i", proxy->http_connection->event_loop_info); - //MHD_destroy_response (proxy->http_response); - //PRINT_INFO2("state after %i", proxy->http_connection->state); - //PRINT_INFO2("loop after %i", proxy->http_connection->event_loop_info); - MHD_destroy_response (proxy->http_response); + + MHD_destroy_response (proxy->http_response); } diff --git a/src/examples/mhd2spdy_http.h b/src/examples/mhd2spdy_http.h index 78685c69..240959cf 100644 --- a/src/examples/mhd2spdy_http.h +++ b/src/examples/mhd2spdy_http.h @@ -36,10 +36,13 @@ http_cb_request (void *cls, const char *upload_data, size_t *upload_data_size, void **ptr); - + + void * http_log_cb(void * cls, const char * uri); - + + void http_create_response(struct Proxy* proxy, char **nv); - + + #endif diff --git a/src/examples/mhd2spdy_spdy.c b/src/examples/mhd2spdy_spdy.c index 8640df35..413de7f3 100644 --- a/src/examples/mhd2spdy_spdy.c +++ b/src/examples/mhd2spdy_spdy.c @@ -35,29 +35,27 @@ #include "mhd2spdy_spdy.h" #include "mhd2spdy_http.h" -enum -{ - IO_NONE, - WANT_READ, - WANT_WRITE -}; - /* * Prints error containing the function name |func| and message |msg| * and exit. */ -static void spdy_dief(const char *func, const char *msg) +static void +spdy_dief(const char *func, + const char *msg) { fprintf(stderr, "FATAL: %s: %s\n", func, msg); exit(EXIT_FAILURE); } + /* * Prints error containing the function name |func| and error code * |error_code| and exit. */ -void spdy_diec(const char *func, int error_code) +void +spdy_diec(const char *func, + int error_code) { fprintf(stderr, "FATAL: %s: error_code=%d, msg=%s\n", func, error_code, spdylay_strerror(error_code)); @@ -71,11 +69,12 @@ void spdy_diec(const char *func, int error_code) * bytes actually written. See the documentation of * spdylay_send_callback for the details. */ -static ssize_t spdy_cb_send(spdylay_session *session, - const uint8_t *data, - size_t length, - int flags, - void *user_data) +static ssize_t +spdy_cb_send(spdylay_session *session, + const uint8_t *data, + size_t length, + int flags, + void *user_data) { (void)session; (void)flags; @@ -126,15 +125,19 @@ static ssize_t spdy_cb_send(spdylay_session *session, return rv; } + /* * The implementation of spdylay_recv_callback type. Here we read data * from the network and write them in |buf|. The capacity of |buf| is * |length| bytes. Returns the number of bytes stored in |buf|. See * the documentation of spdylay_recv_callback for the details. */ -static ssize_t spdy_cb_recv(spdylay_session *session, - uint8_t *buf, size_t length, int flags, - void *user_data) +static ssize_t +spdy_cb_recv(spdylay_session *session, + uint8_t *buf, + size_t length, + int flags, + void *user_data) { (void)session; (void)flags; @@ -191,63 +194,47 @@ static ssize_t spdy_cb_recv(spdylay_session *session, return rv; } -/* - * The implementation of spdylay_before_ctrl_send_callback type. We - * use this function to get stream ID of the request. This is because - * stream ID is not known when we submit the request - * (spdylay_spdy_submit_request). - */ -/*static void spdy_cb_before_ctrl_send(spdylay_session *session, - spdylay_frame_type type, - spdylay_frame *frame, - void *user_data) -{ -}*/ - -static void spdy_cb_on_ctrl_send(spdylay_session *session, - spdylay_frame_type type, - spdylay_frame *frame, void *user_data) +static void +spdy_cb_on_ctrl_send(spdylay_session *session, + spdylay_frame_type type, + spdylay_frame *frame, + void *user_data) { (void)user_data; - //char **nv; - //const char *name = NULL; int32_t stream_id; - //size_t i; struct Proxy *proxy; switch(type) { - case SPDYLAY_SYN_STREAM: - //nv = frame->syn_stream.nv; - //name = "SYN_STREAM"; - stream_id = frame->syn_stream.stream_id; - proxy = spdylay_session_get_stream_user_data(session, stream_id); - ++glob_opt.streams_opened; - ++proxy->spdy_connection->streams_opened; - PRINT_INFO2("opening stream: str open %i; %s", glob_opt.streams_opened, proxy->url); - break; - default: - break; + case SPDYLAY_SYN_STREAM: + stream_id = frame->syn_stream.stream_id; + proxy = spdylay_session_get_stream_user_data(session, stream_id); + ++glob_opt.streams_opened; + ++proxy->spdy_connection->streams_opened; + PRINT_INFO2("opening stream: str open %i; %s", glob_opt.streams_opened, proxy->url); + break; + default: + break; } } -void spdy_cb_on_ctrl_recv(spdylay_session *session, - spdylay_frame_type type, - spdylay_frame *frame, void *user_data) + +void +spdy_cb_on_ctrl_recv(spdylay_session *session, + spdylay_frame_type type, + spdylay_frame *frame, + void *user_data) { (void)user_data; - //struct SPDY_Request *req; char **nv; - //const char *name = NULL; int32_t stream_id; struct Proxy * proxy; switch(type) { case SPDYLAY_SYN_REPLY: nv = frame->syn_reply.nv; - //name = "SYN_REPLY"; stream_id = frame->syn_reply.stream_id; break; case SPDYLAY_RST_STREAM: @@ -255,7 +242,6 @@ void spdy_cb_on_ctrl_recv(spdylay_session *session, break; case SPDYLAY_HEADERS: nv = frame->headers.nv; - //name = "HEADERS"; stream_id = frame->headers.stream_id; break; default: @@ -269,34 +255,37 @@ void spdy_cb_on_ctrl_recv(spdylay_session *session, switch(type) { case SPDYLAY_SYN_REPLY: - PRINT_INFO2("received headers for %s", proxy->url); - http_create_response(proxy, nv); + PRINT_INFO2("received headers for %s", proxy->url); + http_create_response(proxy, nv); break; case SPDYLAY_RST_STREAM: - PRINT_INFO2("received reset stream for %s", proxy->url); - proxy->error = true; + PRINT_INFO2("received reset stream for %s", proxy->url); + proxy->error = true; break; case SPDYLAY_HEADERS: - PRINT_INFO2("received headers for %s", proxy->url); - http_create_response(proxy, nv); + PRINT_INFO2("received headers for %s", proxy->url); + http_create_response(proxy, nv); break; default: return; break; } + glob_opt.spdy_data_received = true; } + /* * The implementation of spdylay_on_stream_close_callback type. We use * this function to know the response is fully received. Since we just * fetch 1 resource in this program, after reception of the response, * we submit GOAWAY and close the session. */ -static void spdy_cb_on_stream_close(spdylay_session *session, - int32_t stream_id, - spdylay_status_code status_code, - void *user_data) +static void +spdy_cb_on_stream_close(spdylay_session *session, + int32_t stream_id, + spdylay_status_code status_code, + void *user_data) { (void)status_code; (void)user_data; @@ -315,24 +304,24 @@ static void spdy_cb_on_stream_close(spdylay_session *session, proxy->spdy_active = false; else free_proxy(proxy); - return; } -#define SPDY_MAX_OUTLEN 4096 /* * The implementation of spdylay_on_data_chunk_recv_callback type. We * use this function to print the received response body. */ -static void spdy_cb_on_data_chunk_recv(spdylay_session *session, uint8_t flags, - int32_t stream_id, - const uint8_t *data, size_t len, - void *user_data) +static void +spdy_cb_on_data_chunk_recv(spdylay_session *session, + uint8_t flags, + int32_t stream_id, + const uint8_t *data, + size_t len, + void *user_data) { (void)flags; (void)user_data; - //struct SPDY_Request *req; struct Proxy *proxy; proxy = spdylay_session_get_stream_user_data(session, stream_id); @@ -348,12 +337,17 @@ static void spdy_cb_on_data_chunk_recv(spdylay_session *session, uint8_t flags, memcpy(proxy->http_body + proxy->http_body_size, data, len); proxy->http_body_size += len; - PRINT_INFO2("received data for %s; %zu bytes", proxy->url, len); + PRINT_INFO2("received data for %s; %zu bytes", proxy->url, len); glob_opt.spdy_data_received = true; } -static void spdy_cb_on_data_recv(spdylay_session *session, - uint8_t flags, int32_t stream_id, int32_t length, void *user_data) + +static void +spdy_cb_on_data_recv(spdylay_session *session, + uint8_t flags, + int32_t stream_id, + int32_t length, + void *user_data) { (void)length; (void)user_data; @@ -367,18 +361,19 @@ static void spdy_cb_on_data_recv(spdylay_session *session, } } + /* * Setup callback functions. Spdylay API offers many callback * functions, but most of them are optional. The send_callback is * always required. Since we use spdylay_session_recv(), the * recv_callback is also required. */ -static void spdy_setup_spdylay_callbacks(spdylay_session_callbacks *callbacks) +static void +spdy_setup_spdylay_callbacks(spdylay_session_callbacks *callbacks) { memset(callbacks, 0, sizeof(spdylay_session_callbacks)); callbacks->send_callback = spdy_cb_send; callbacks->recv_callback = spdy_cb_recv; - //callbacks->before_ctrl_send_callback = spdy_cb_before_ctrl_send; callbacks->on_ctrl_send_callback = spdy_cb_on_ctrl_send; callbacks->on_ctrl_recv_callback = spdy_cb_on_ctrl_recv; callbacks->on_stream_close_callback = spdy_cb_on_stream_close; @@ -386,21 +381,25 @@ static void spdy_setup_spdylay_callbacks(spdylay_session_callbacks *callbacks) callbacks->on_data_recv_callback = spdy_cb_on_data_recv; } + /* * Callback function for SSL/TLS NPN. Since this program only supports * SPDY protocol, if server does not offer SPDY protocol the Spdylay * library supports, we terminate program. */ -static int spdy_cb_ssl_select_next_proto(SSL* ssl, - unsigned char **out, unsigned char *outlen, - const unsigned char *in, unsigned int inlen, +static int +spdy_cb_ssl_select_next_proto(SSL* ssl, + unsigned char **out, + unsigned char *outlen, + const unsigned char *in, + unsigned int inlen, void *arg) { (void)ssl; - //PRINT_INFO("spdy_cb_ssl_select_next_proto"); int rv; uint16_t *spdy_proto_version; + /* spdylay_select_next_protocol() selects SPDY protocol version the Spdylay library supports. */ rv = spdylay_select_next_protocol(out, outlen, in, inlen); @@ -413,11 +412,14 @@ static int spdy_cb_ssl_select_next_proto(SSL* ssl, return SSL_TLSEXT_ERR_OK; } + /* * Setup SSL context. We pass |spdy_proto_version| to get negotiated * SPDY protocol version in NPN callback. */ -void spdy_ssl_init_ssl_ctx(SSL_CTX *ssl_ctx, uint16_t *spdy_proto_version) +void +spdy_ssl_init_ssl_ctx(SSL_CTX *ssl_ctx, + uint16_t *spdy_proto_version) { /* Disable SSLv2 and enable all workarounds for buggy servers */ SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL|SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION); @@ -428,153 +430,184 @@ void spdy_ssl_init_ssl_ctx(SSL_CTX *ssl_ctx, uint16_t *spdy_proto_version) spdy_proto_version); } -static int spdy_ssl_handshake(SSL *ssl, int fd) + +static int +spdy_ssl_handshake(SSL *ssl, + int fd) { int rv; - if(SSL_set_fd(ssl, fd) == 0) { + + if(SSL_set_fd(ssl, fd) == 0) spdy_dief("SSL_set_fd", ERR_error_string(ERR_get_error(), NULL)); - } + ERR_clear_error(); rv = SSL_connect(ssl); - if(rv <= 0) { + if(rv <= 0) PRINT_INFO2("SSL_connect %s", ERR_error_string(ERR_get_error(), NULL)); - } return rv; } + /* * Connects to the host |host| and port |port|. This function returns * the file descriptor of the client socket. */ -static int spdy_socket_connect_to(const char *host, uint16_t port) +static int +spdy_socket_connect_to(const char *host, + uint16_t port) { struct addrinfo hints; int fd = -1; int rv; char service[NI_MAXSERV]; struct addrinfo *res, *rp; + + //TODO checks snprintf(service, sizeof(service), "%u", port); memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; rv = getaddrinfo(host, service, &hints, &res); - if(rv != 0) { + if(rv != 0) + { printf("%s\n",host); spdy_dief("getaddrinfo", gai_strerror(rv)); } - for(rp = res; rp; rp = rp->ai_next) { + for(rp = res; rp; rp = rp->ai_next) + { fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - if(fd == -1) { + if(fd == -1) continue; - } while((rv = connect(fd, rp->ai_addr, rp->ai_addrlen)) == -1 && errno == EINTR); - if(rv == 0) { + if(rv == 0) break; - } close(fd); fd = -1; } freeaddrinfo(res); + return fd; } -static void spdy_socket_make_non_block(int fd) + +static void +spdy_socket_make_non_block(int fd) { - int flags, rv; + int flags; + int rv; + while((flags = fcntl(fd, F_GETFL, 0)) == -1 && errno == EINTR); - if(flags == -1) { + + if(flags == -1) spdy_dief("fcntl", strerror(errno)); - } + while((rv = fcntl(fd, F_SETFL, flags | O_NONBLOCK)) == -1 && errno == EINTR); - if(rv == -1) { + + if(rv == -1) spdy_dief("fcntl", strerror(errno)); - } } + /* * Setting TCP_NODELAY is not mandatory for the SPDY protocol. */ -static void spdy_socket_set_tcp_nodelay(int fd) +static void +spdy_socket_set_tcp_nodelay(int fd) { int val = 1; int rv; + rv = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, (socklen_t)sizeof(val)); - if(rv == -1) { + if(rv == -1) spdy_dief("setsockopt", strerror(errno)); - } } /* * Update |pollfd| based on the state of |connection|. */ -void spdy_ctl_poll(struct pollfd *pollfd, struct SPDY_Connection *connection) +void +spdy_ctl_poll(struct pollfd *pollfd, + struct SPDY_Connection *connection) { pollfd->events = 0; if(spdylay_session_want_read(connection->session) || - connection->want_io == WANT_READ) { + connection->want_io == WANT_READ) + { pollfd->events |= POLLIN; } if(spdylay_session_want_write(connection->session) || - connection->want_io == WANT_WRITE) { + connection->want_io == WANT_WRITE) + { pollfd->events |= POLLOUT; } } + /* * Update |selectfd| based on the state of |connection|. */ -bool spdy_ctl_select(fd_set * read_fd_set, - fd_set * write_fd_set, - fd_set * except_fd_set, - struct SPDY_Connection *connection) +bool +spdy_ctl_select(fd_set * read_fd_set, + fd_set * write_fd_set, + fd_set * except_fd_set, + struct SPDY_Connection *connection) { (void)except_fd_set; bool ret = false; if(spdylay_session_want_read(connection->session) || - connection->want_io == WANT_READ) { + connection->want_io == WANT_READ) + { FD_SET(connection->fd, read_fd_set); ret = true; } if(spdylay_session_want_write(connection->session) || - connection->want_io == WANT_WRITE) { + connection->want_io == WANT_WRITE) + { FD_SET(connection->fd, write_fd_set); ret = true; } + return ret; } + /* * Performs the network I/O. */ -int spdy_exec_io(struct SPDY_Connection *connection) +int +spdy_exec_io(struct SPDY_Connection *connection) { int rv; + rv = spdylay_session_recv(connection->session); - if(rv != 0) { + if(rv != 0) + { PRINT_INFO2("spdylay_session_recv %i", rv); return rv; } rv = spdylay_session_send(connection->session); - if(rv != 0) { + if(rv != 0) PRINT_INFO2("spdylay_session_send %i", rv); - } + return rv; } + /* * Fetches the resource denoted by |uri|. */ -struct SPDY_Connection * spdy_connect(const struct URI *uri, uint16_t port, bool is_tls) +struct SPDY_Connection * +spdy_connect(const struct URI *uri, + uint16_t port, + bool is_tls) { spdylay_session_callbacks callbacks; int fd; - //SSL_CTX *ssl_ctx; SSL *ssl=NULL; - //struct SPDY_Request req; struct SPDY_Connection * connection; int rv; @@ -583,19 +616,14 @@ struct SPDY_Connection * spdy_connect(const struct URI *uri, uint16_t port, bool /* Establish connection and setup SSL */ PRINT_INFO2("connecting to %s:%i", uri->host, port); fd = spdy_socket_connect_to(uri->host, port); - if(fd == -1) { + if(fd == -1) + { PRINT_INFO("Could not open file descriptor"); - return NULL;//glob_opt.spdy_connection; + return NULL; } if(is_tls) { - /*ssl_ctx = SSL_CTX_new(SSLv23_client_method()); - if(ssl_ctx == NULL) { - spdy_dief("SSL_CTX_new", ERR_error_string(ERR_get_error(), NULL)); - } - spdy_ssl_init_ssl_ctx(ssl_ctx, &spdy_proto_version); - */ ssl = SSL_new(glob_opt.ssl_ctx); if(ssl == NULL) { spdy_dief("SSL_new", ERR_error_string(ERR_get_error(), NULL)); @@ -624,7 +652,6 @@ struct SPDY_Connection * spdy_connect(const struct URI *uri, uint16_t port, bool if(NULL == (connection = au_malloc(sizeof(struct SPDY_Connection)))) return NULL; - //memset(connection, 0 , sizeof(struct SPDY_Connection)); connection->is_tls = is_tls; connection->ssl = ssl; @@ -647,6 +674,7 @@ struct SPDY_Connection * spdy_connect(const struct URI *uri, uint16_t port, bool return connection; } + void spdy_free_connection(struct SPDY_Connection * connection) { @@ -659,8 +687,10 @@ spdy_free_connection(struct SPDY_Connection * connection) } } + int -spdy_request(const char **nv, struct Proxy *proxy) +spdy_request(const char **nv, + struct Proxy *proxy) { int ret; uint16_t port; @@ -714,13 +744,18 @@ spdy_request(const char **nv, struct Proxy *proxy) void -spdy_get_pollfdset(struct pollfd fds[], struct SPDY_Connection *connections[], unsigned int max_size, nfds_t *real_size) +spdy_get_pollfdset(struct pollfd fds[], + struct SPDY_Connection *connections[], + unsigned int max_size, + nfds_t *real_size) { struct SPDY_Connection *connection; struct Proxy *proxy; *real_size = 0; - if(max_size<1) return; + if(max_size<1) + return; + if(NULL != glob_opt.spdy_connection) { spdy_ctl_poll(&(fds[*real_size]), glob_opt.spdy_connection); @@ -781,9 +816,11 @@ spdy_get_pollfdset(struct pollfd fds[], struct SPDY_Connection *connections[], u int spdy_get_selectfdset(fd_set * read_fd_set, - fd_set * write_fd_set, - fd_set * except_fd_set, - struct SPDY_Connection *connections[], unsigned int max_size, nfds_t *real_size) + fd_set * write_fd_set, + fd_set * except_fd_set, + struct SPDY_Connection *connections[], + unsigned int max_size, + nfds_t *real_size) { struct SPDY_Connection *connection; struct Proxy *proxy; @@ -791,7 +828,9 @@ spdy_get_selectfdset(fd_set * read_fd_set, int maxfd = 0; *real_size = 0; - if(max_size<1) return 0; + if(max_size<1) + return 0; + if(NULL != glob_opt.spdy_connection) { ret = spdy_ctl_select(read_fd_set, @@ -857,12 +896,13 @@ spdy_get_selectfdset(fd_set * read_fd_set, void -spdy_run(struct pollfd fds[], struct SPDY_Connection *connections[], int size) +spdy_run(struct pollfd fds[], + struct SPDY_Connection *connections[], + int size) { int i; int ret; struct Proxy *proxy; - //PRINT_INFO2("size is %i", size); for(i=0; ifd, read_fd_set) || FD_ISSET(connections[i]->fd, write_fd_set) || FD_ISSET(connections[i]->fd, except_fd_set)) { ret = spdy_exec_io(connections[i]); - //PRINT_INFO2("%i",ret); - //if((spdy_pollfds[i].revents & POLLHUP) || (spdy_pollfds[0].revents & POLLERR)) - // PRINT_INFO("SPDY SPDY_Connection error"); - - //TODO POLLRDHUP - // always close on ret != 0? if(0 != ret) { @@ -948,8 +981,6 @@ spdy_run_select(fd_set * read_fd_set, } } else - { PRINT_INFO("not called"); - } } } diff --git a/src/examples/mhd2spdy_spdy.h b/src/examples/mhd2spdy_spdy.h index fa27e7f6..980ee1d1 100644 --- a/src/examples/mhd2spdy_spdy.h +++ b/src/examples/mhd2spdy_spdy.h @@ -26,43 +26,76 @@ #include "mhd2spdy_structures.h" -struct SPDY_Connection * spdy_connect(const struct URI *uri, uint16_t port, bool is_tls); -void spdy_ctl_poll(struct pollfd *pollfd, struct SPDY_Connection *connection); +struct SPDY_Connection * +spdy_connect(const struct URI *uri, + uint16_t port, + bool is_tls); -bool spdy_ctl_select(fd_set * read_fd_set, - fd_set * write_fd_set, - fd_set * except_fd_set, - struct SPDY_Connection *connection); -int spdy_exec_io(struct SPDY_Connection *connection); +void +spdy_ctl_poll(struct pollfd *pollfd, + struct SPDY_Connection *connection); + + +bool +spdy_ctl_select(fd_set * read_fd_set, + fd_set * write_fd_set, + fd_set * except_fd_set, + struct SPDY_Connection *connection); + + +int +spdy_exec_io(struct SPDY_Connection *connection); + + +void +spdy_diec(const char *func, + int error_code); -void spdy_diec(const char *func, int error_code); int -spdy_request(const char **nv, struct Proxy *proxy); +spdy_request(const char **nv, + struct Proxy *proxy); + + +void +spdy_ssl_init_ssl_ctx(SSL_CTX *ssl_ctx, + uint16_t *spdy_proto_version); -void spdy_ssl_init_ssl_ctx(SSL_CTX *ssl_ctx, uint16_t *spdy_proto_version); void spdy_free_connection(struct SPDY_Connection * connection); + void -spdy_get_pollfdset(struct pollfd fds[], struct SPDY_Connection *connections[], unsigned int max_size, nfds_t *real_size); +spdy_get_pollfdset(struct pollfd fds[], + struct SPDY_Connection *connections[], + unsigned int max_size, + nfds_t *real_size); int spdy_get_selectfdset(fd_set * read_fd_set, - fd_set * write_fd_set, - fd_set * except_fd_set, - struct SPDY_Connection *connections[], unsigned int max_size, nfds_t *real_size); - + fd_set * write_fd_set, + fd_set * except_fd_set, + struct SPDY_Connection *connections[], + unsigned int max_size, + nfds_t *real_size); + + void -spdy_run(struct pollfd fds[], struct SPDY_Connection *connections[], int size); +spdy_run(struct pollfd fds[], + struct SPDY_Connection *connections[], + int size); + void spdy_run_select(fd_set * read_fd_set, - fd_set * write_fd_set, - fd_set * except_fd_set, struct SPDY_Connection *connections[], int size); - + fd_set * write_fd_set, + fd_set * except_fd_set, + struct SPDY_Connection *connections[], + int size); + + #endif diff --git a/src/examples/mhd2spdy_structures.c b/src/examples/mhd2spdy_structures.c index da29581c..2982b2a4 100644 --- a/src/examples/mhd2spdy_structures.c +++ b/src/examples/mhd2spdy_structures.c @@ -32,7 +32,6 @@ free_uri(struct URI * uri) free(uri->full_uri); free(uri->scheme); free(uri->host_and_port); - //free(uri->host_and_port_for_connecting); free(uri->host); free(uri->path); free(uri->path_and_more); @@ -43,6 +42,7 @@ free_uri(struct URI * uri) } } + int init_parse_uri(regex_t * preg) { @@ -66,7 +66,9 @@ deinit_parse_uri(regex_t * preg) } int -parse_uri(regex_t * preg, char * full_uri, struct URI ** uri) +parse_uri(regex_t * preg, + char * full_uri, + struct URI ** uri) { int ret; char *colon; @@ -94,22 +96,8 @@ parse_uri(regex_t * preg, char * full_uri, struct URI ** uri) if(NULL == colon) { (*uri)->host = strdup((*uri)->host_and_port); - /*if(0 == strcasecmp("http", uri->scheme)) - { - uri->port = 80; - asprintf(&(uri->host_and_port_for_connecting), "%s:80", uri->host_and_port); - } - else if(0 == strcasecmp("https", uri->scheme)) - { - uri->port = 443; - asprintf(&(uri->host_and_port_for_connecting), "%s:443", uri->host_and_port); - } - else - { - PRINT_INFO("no standard scheme!"); - */(*uri)->port = 0; - /*uri->host_and_port_for_connecting = strdup(uri->host_and_port); - }*/ + (*uri)->port = 0; + return 0; } @@ -125,6 +113,7 @@ parse_uri(regex_t * preg, char * full_uri, struct URI ** uri) return 0; } + void free_proxy(struct Proxy *proxy) { @@ -136,6 +125,7 @@ free_proxy(struct Proxy *proxy) free(proxy); } +//TODO remove this void *au_malloc(size_t size) { void *new_memory; diff --git a/src/examples/mhd2spdy_structures.h b/src/examples/mhd2spdy_structures.h index 6d6d9769..88c28c64 100644 --- a/src/examples/mhd2spdy_structures.h +++ b/src/examples/mhd2spdy_structures.h @@ -49,23 +49,32 @@ #include #include + +/* WANT_READ if SSL connection needs more input; or WANT_WRITE if it + needs more output; or IO_NONE. This is necessary because SSL/TLS + re-negotiation is possible at any time. Spdylay API offers + similar functions like spdylay_session_want_read() and + spdylay_session_want_write() but they do not take into account + SSL connection. */ +enum +{ + IO_NONE, + WANT_READ, + WANT_WRITE +}; + + struct Proxy; + struct SPDY_Connection { SSL *ssl; - //SSL_CTX *ssl_ctx; spdylay_session *session; struct SPDY_Connection *prev; struct SPDY_Connection *next; struct Proxy *proxies_head; struct Proxy *proxies_tail; char *host; - /* WANT_READ if SSL connection needs more input; or WANT_WRITE if it - needs more output; or IO_NONE. This is necessary because SSL/TLS - re-negotiation is possible at any time. Spdylay API offers - similar functions like spdylay_session_want_read() and - spdylay_session_want_write() but they do not take into account - SSL connection. */ int fd; int want_io; uint counter; @@ -79,7 +88,6 @@ struct URI char * full_uri; char * scheme; char * host_and_port; - //char * host_and_port_for_connecting; char * host; char * path; char * path_and_more; @@ -88,26 +96,21 @@ struct URI uint16_t port; }; + struct HTTP_URI; + struct Proxy { struct MHD_Connection *http_connection; struct MHD_Response *http_response; struct URI *uri; - struct HTTP_URI *http_uri; //TODO remove me + struct HTTP_URI *http_uri; struct SPDY_Connection *spdy_connection; struct Proxy *next; struct Proxy *prev; - //char *path; char *url; - //struct SPDY_Request *request; - //struct SPDY_Response *response; - //CURL *curl_handle; - //struct curl_slist *curl_headers; - //struct SPDY_NameValue *headers; char *version; - //char *status_msg; void *http_body; size_t http_body_size; ssize_t length; @@ -126,6 +129,7 @@ struct HTTP_URI struct Proxy * proxy; }; + struct SPDY_Headers { const char **nv; @@ -133,6 +137,7 @@ struct SPDY_Headers int cnt; }; + struct global_options { char *spdy2http_str; @@ -150,24 +155,9 @@ struct global_options bool verbose; bool only_proxy; bool spdy_data_received; -} glob_opt; - -/* - -#define SOCK_ADDR_IN_PTR(sa) ((struct sockaddr_in *)(sa)) -#define SOCK_ADDR_IN_FAMILY(sa) SOCK_ADDR_IN_PTR(sa)->sin_family -#define SOCK_ADDR_IN_PORT(sa) SOCK_ADDR_IN_PTR(sa)->sin_port -#define SOCK_ADDR_IN_ADDR(sa) SOCK_ADDR_IN_PTR(sa)->sin_addr +} +glob_opt; -#ifdef HAS_IPV6 - -#define SOCK_ADDR_IN6_PTR(sa) ((struct sockaddr_in6 *)(sa)) -#define SOCK_ADDR_IN6_FAMILY(sa) SOCK_ADDR_IN6_PTR(sa)->sin6_family -#define SOCK_ADDR_IN6_PORT(sa) SOCK_ADDR_IN6_PTR(sa)->sin6_port -#define SOCK_ADDR_IN6_ADDR(sa) SOCK_ADDR_IN6_PTR(sa)->sin6_addr - -#endif -*/ //forbidden headers #define SPDY_HTTP_HEADER_TRANSFER_ENCODING "transfer-encoding" @@ -177,6 +167,7 @@ struct global_options #define MAX_SPDY_CONNECTIONS 100 +#define SPDY_MAX_OUTLEN 4096 /** * Insert an element at the head of a DLL. Assumes that head, tail and @@ -251,18 +242,27 @@ struct global_options void free_uri(struct URI * uri); + int init_parse_uri(regex_t * preg); + void deinit_parse_uri(regex_t * preg); - + + int -parse_uri(regex_t * preg, char * full_uri, struct URI ** uri); +parse_uri(regex_t * preg, + char * full_uri, + struct URI ** uri); + void free_proxy(struct Proxy *proxy); -void *au_malloc(size_t size); + +void * +au_malloc(size_t size); + #endif -- cgit v1.2.3