diff options
Diffstat (limited to 'src/examples/mhd2spdy_http.c')
-rw-r--r-- | src/examples/mhd2spdy_http.c | 178 |
1 files changed, 18 insertions, 160 deletions
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 @@ | |||
26 | #include "mhd2spdy_spdy.h" | 26 | #include "mhd2spdy_spdy.h" |
27 | 27 | ||
28 | 28 | ||
29 | void * http_log_cb(void * cls, const char * uri) | 29 | void * |
30 | http_log_cb(void * cls, | ||
31 | const char * uri) | ||
30 | { | 32 | { |
31 | (void)cls; | 33 | (void)cls; |
32 | 34 | ||
@@ -34,29 +36,18 @@ void * http_log_cb(void * cls, const char * uri) | |||
34 | 36 | ||
35 | PRINT_INFO2("log uri '%s'\n", uri); | 37 | PRINT_INFO2("log uri '%s'\n", uri); |
36 | 38 | ||
37 | //TODO free | ||
38 | if(NULL == (http_uri = au_malloc(sizeof(struct HTTP_URI )))) | 39 | if(NULL == (http_uri = au_malloc(sizeof(struct HTTP_URI )))) |
39 | DIE("no memory"); | 40 | DIE("no memory"); |
40 | //memset(http_uri, 0 , sizeof(struct HTTP_URI)); | ||
41 | http_uri->uri = strdup(uri); | 41 | http_uri->uri = strdup(uri); |
42 | return http_uri; | 42 | return http_uri; |
43 | } | 43 | } |
44 | 44 | ||
45 | 45 | ||
46 | /* | ||
47 | static int | ||
48 | http_query_iterate_cb(void *cls, | ||
49 | enum MHD_ValueKind kind, | ||
50 | const char *name, const char *value) | ||
51 | { | ||
52 | |||
53 | }*/ | ||
54 | |||
55 | |||
56 | static int | 46 | static int |
57 | http_iterate_cb(void *cls, | 47 | http_iterate_cb(void *cls, |
58 | enum MHD_ValueKind kind, | 48 | enum MHD_ValueKind kind, |
59 | const char *name, const char *value) | 49 | const char *name, |
50 | const char *value) | ||
60 | { | 51 | { |
61 | (void)kind; | 52 | (void)kind; |
62 | 53 | ||
@@ -85,9 +76,9 @@ http_iterate_cb(void *cls, | |||
85 | 76 | ||
86 | static ssize_t | 77 | static ssize_t |
87 | http_response_callback (void *cls, | 78 | http_response_callback (void *cls, |
88 | uint64_t pos, | 79 | uint64_t pos, |
89 | char *buffer, | 80 | char *buffer, |
90 | size_t max) | 81 | size_t max) |
91 | { | 82 | { |
92 | (void)pos; | 83 | (void)pos; |
93 | 84 | ||
@@ -96,15 +87,6 @@ http_response_callback (void *cls, | |||
96 | void *newbody; | 87 | void *newbody; |
97 | const union MHD_ConnectionInfo *info; | 88 | const union MHD_ConnectionInfo *info; |
98 | int val = 1; | 89 | int val = 1; |
99 | |||
100 | //max=16; | ||
101 | |||
102 | //PRINT_INFO2("response_callback, pos: %i, max is %i, len is %i",pos,max,proxy->length); | ||
103 | |||
104 | //assert(0 != proxy->length); | ||
105 | |||
106 | //if(MHD_CONTENT_READER_END_OF_STREAM == proxy->length) | ||
107 | // return MHD_CONTENT_READER_END_OF_STREAM; | ||
108 | 90 | ||
109 | PRINT_INFO2("http_response_callback for %s", proxy->url); | 91 | PRINT_INFO2("http_response_callback for %s", proxy->url); |
110 | 92 | ||
@@ -116,7 +98,6 @@ http_response_callback (void *cls, | |||
116 | return MHD_CONTENT_READER_END_OF_STREAM; | 98 | return MHD_CONTENT_READER_END_OF_STREAM; |
117 | } | 99 | } |
118 | 100 | ||
119 | //*more = true; | ||
120 | if(!proxy->http_body_size)//nothing to write now | 101 | if(!proxy->http_body_size)//nothing to write now |
121 | { | 102 | { |
122 | //flush data | 103 | //flush data |
@@ -154,13 +135,6 @@ http_response_callback (void *cls, | |||
154 | if(proxy->length >= 0) | 135 | if(proxy->length >= 0) |
155 | { | 136 | { |
156 | proxy->length -= ret; | 137 | proxy->length -= ret; |
157 | //printf("pr len %i", proxy->length); | ||
158 | /*if(proxy->length <= 0) | ||
159 | { | ||
160 | // *more = false; | ||
161 | //last frame | ||
162 | proxy->length = MHD_CONTENT_READER_END_OF_STREAM; | ||
163 | }*/ | ||
164 | } | 138 | } |
165 | 139 | ||
166 | PRINT_INFO2("response_callback, size: %i",ret); | 140 | PRINT_INFO2("response_callback, size: %i",ret); |
@@ -175,27 +149,7 @@ http_response_done_callback(void *cls) | |||
175 | struct Proxy *proxy = (struct Proxy *)cls; | 149 | struct Proxy *proxy = (struct Proxy *)cls; |
176 | 150 | ||
177 | PRINT_INFO2("http_response_done_callback for %s", proxy->url); | 151 | PRINT_INFO2("http_response_done_callback for %s", proxy->url); |
178 | //int ret; | 152 | |
179 | |||
180 | //printf("response_done_callback\n"); | ||
181 | |||
182 | //printf("answer for %s was sent\n", (char *)cls); | ||
183 | |||
184 | /*if(SPDY_RESPONSE_RESULT_SUCCESS != status) | ||
185 | { | ||
186 | printf("answer was NOT sent, %i\n",status); | ||
187 | }*/ | ||
188 | /*if(CURLM_OK != (ret = curl_multi_remove_handle(multi_handle, proxy->curl_handle))) | ||
189 | { | ||
190 | PRINT_INFO2("curl_multi_remove_handle failed (%i)", ret); | ||
191 | } | ||
192 | curl_slist_free_all(proxy->curl_headers); | ||
193 | curl_easy_cleanup(proxy->curl_handle); | ||
194 | */ | ||
195 | //SPDY_destroy_request(request); | ||
196 | //SPDY_destroy_response(response); | ||
197 | //if(!strcmp("/close",proxy->path)) run = 0; | ||
198 | //free(proxy->path); | ||
199 | if(proxy->spdy_active) | 153 | if(proxy->spdy_active) |
200 | proxy->http_active = false; | 154 | proxy->http_active = false; |
201 | else | 155 | else |
@@ -219,12 +173,8 @@ http_cb_request (void *cls, | |||
219 | (void)upload_data; | 173 | (void)upload_data; |
220 | (void)upload_data_size; | 174 | (void)upload_data_size; |
221 | 175 | ||
222 | //struct MHD_Response *response; | ||
223 | int ret; | 176 | int ret; |
224 | struct Proxy *proxy; | 177 | struct Proxy *proxy; |
225 | //struct URI *spdy_uri; | ||
226 | //char **nv; | ||
227 | //int num_headers; | ||
228 | struct SPDY_Headers spdy_headers; | 178 | struct SPDY_Headers spdy_headers; |
229 | 179 | ||
230 | //PRINT_INFO2("request cb %i; %s", *ptr,url); | 180 | //PRINT_INFO2("request cb %i; %s", *ptr,url); |
@@ -240,7 +190,7 @@ http_cb_request (void *cls, | |||
240 | free(http_uri->uri); | 190 | free(http_uri->uri); |
241 | free(http_uri); | 191 | free(http_uri); |
242 | PRINT_INFO2("unexpected method %s", method); | 192 | PRINT_INFO2("unexpected method %s", method); |
243 | return MHD_NO; /* unexpected method */ | 193 | return MHD_NO; |
244 | } | 194 | } |
245 | 195 | ||
246 | if(NULL == (proxy = au_malloc(sizeof(struct Proxy)))) | 196 | if(NULL == (proxy = au_malloc(sizeof(struct Proxy)))) |
@@ -250,17 +200,14 @@ http_cb_request (void *cls, | |||
250 | } | 200 | } |
251 | 201 | ||
252 | ++glob_opt.responses_pending; | 202 | ++glob_opt.responses_pending; |
253 | //memset(proxy, 0, sizeof(struct Proxy)); | ||
254 | proxy->id = rand(); | 203 | proxy->id = rand(); |
255 | proxy->http_active = true; | 204 | proxy->http_active = true; |
256 | //PRINT_INFO2("proxy obj with id %i created (%i)", proxy->id, proxy); | ||
257 | proxy->http_connection = connection; | 205 | proxy->http_connection = connection; |
258 | http_uri->proxy = proxy; | 206 | http_uri->proxy = proxy; |
259 | return MHD_YES; | 207 | return MHD_YES; |
260 | } | 208 | } |
261 | 209 | ||
262 | proxy = http_uri->proxy; | 210 | proxy = http_uri->proxy; |
263 | //*ptr = NULL; /* reset when done */ | ||
264 | 211 | ||
265 | if(proxy->spdy_active) | 212 | if(proxy->spdy_active) |
266 | { | 213 | { |
@@ -270,45 +217,15 @@ http_cb_request (void *cls, | |||
270 | } | 217 | } |
271 | 218 | ||
272 | PRINT_INFO2("received request for '%s %s %s'\n", method, http_uri->uri, version); | 219 | PRINT_INFO2("received request for '%s %s %s'\n", method, http_uri->uri, version); |
273 | /* | ||
274 | proxy->http_response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, | ||
275 | 8096, | ||
276 | &http_response_callback, | ||
277 | proxy, | ||
278 | &http_response_done_callback); | ||
279 | |||
280 | if (proxy->http_response == NULL) | ||
281 | DIE("no response"); | ||
282 | */ | ||
283 | 220 | ||
284 | proxy->url = http_uri->uri; | 221 | proxy->url = http_uri->uri; |
285 | //if(NULL == (proxy->url = strdup(http_uri->uri))) | ||
286 | // DIE("no memory"); | ||
287 | |||
288 | //TODO HTTP headers | ||
289 | /*MHD_get_connection_values (connection, | ||
290 | MHD_HEADER_KIND, | ||
291 | &http_iterate_cb, | ||
292 | proxy); | ||
293 | */ | ||
294 | //proxy->url = strdup(url); | ||
295 | //if(NULL == (spdy_uri = au_malloc(sizeof(struct URI)))) | ||
296 | // DIE("no memory"); | ||
297 | 222 | ||
298 | ret = parse_uri(&glob_opt.uri_preg, proxy->url, &proxy->uri); | 223 | ret = parse_uri(&glob_opt.uri_preg, proxy->url, &proxy->uri); |
299 | if(ret != 0) | 224 | if(ret != 0) |
300 | DIE("parse_uri failed"); | 225 | DIE("parse_uri failed"); |
301 | //proxy->uri = spdy_uri; | ||
302 | proxy->http_uri = http_uri; | 226 | proxy->http_uri = http_uri; |
303 | proxy->spdy_active = true; | 227 | proxy->spdy_active = true; |
304 | 228 | ||
305 | //proxy->spdy_request = au_malloc(sizeof(struct SPDY_Request)); | ||
306 | //if(NULL == proxy->spdy_request) | ||
307 | // DIE("no memory"); | ||
308 | //memset(proxy->spdy_request,0,sizeof(struct SPDY_Request)); | ||
309 | //spdy_request_init(proxy->spdy_request, &spdy_uri); | ||
310 | //spdy_submit_request(spdy_connection, proxy); | ||
311 | |||
312 | spdy_headers.num = MHD_get_connection_values (connection, | 229 | spdy_headers.num = MHD_get_connection_values (connection, |
313 | MHD_HEADER_KIND, | 230 | MHD_HEADER_KIND, |
314 | NULL, | 231 | NULL, |
@@ -331,16 +248,10 @@ http_cb_request (void *cls, | |||
331 | if(NULL == spdy_headers.nv[9]) | 248 | if(NULL == spdy_headers.nv[9]) |
332 | spdy_headers.nv[9] = proxy->uri->host_and_port; | 249 | spdy_headers.nv[9] = proxy->uri->host_and_port; |
333 | 250 | ||
334 | /*int i; | ||
335 | for(i=0; i<spdy_headers.cnt; i+=2) | ||
336 | printf("%s: %s\n", spdy_headers.nv[i], spdy_headers.nv[i+1]); | ||
337 | */ | ||
338 | if(0 != spdy_request(spdy_headers.nv, proxy)) | 251 | if(0 != spdy_request(spdy_headers.nv, proxy)) |
339 | { | 252 | { |
340 | //--glob_opt.responses_pending; | ||
341 | free(spdy_headers.nv); | 253 | free(spdy_headers.nv); |
342 | //MHD_destroy_response (proxy->http_response); | 254 | free_proxy(proxy); |
343 | free_proxy(proxy);//TODO call it here or in done_callback | ||
344 | 255 | ||
345 | return MHD_NO; | 256 | return MHD_NO; |
346 | } | 257 | } |
@@ -364,60 +275,20 @@ http_cb_request (void *cls, | |||
364 | if(MHD_NO == MHD_add_response_header (proxy->http_response, | 275 | if(MHD_NO == MHD_add_response_header (proxy->http_response, |
365 | "Keep-Alive", "timeout=5, max=100")) | 276 | "Keep-Alive", "timeout=5, max=100")) |
366 | PRINT_INFO("SPDY_name_value_add failed: "); | 277 | PRINT_INFO("SPDY_name_value_add failed: "); |
367 | /* | 278 | |
368 | const union MHD_ConnectionInfo *info; | ||
369 | info = MHD_get_connection_info (connection, | ||
370 | MHD_CONNECTION_INFO_CONNECTION_FD); | ||
371 | int val = 1; | ||
372 | int rv; | ||
373 | rv = setsockopt(info->connect_fd, IPPROTO_TCP, TCP_NODELAY, &val, (socklen_t)sizeof(val)); | ||
374 | if(rv == -1) { | ||
375 | DIE("setsockopt"); | ||
376 | }*/ | ||
377 | return MHD_YES; | 279 | return MHD_YES; |
378 | } | 280 | } |
379 | 281 | ||
282 | |||
380 | void | 283 | void |
381 | http_create_response(struct Proxy* proxy, char **nv) | 284 | http_create_response(struct Proxy* proxy, |
285 | char **nv) | ||
382 | { | 286 | { |
383 | size_t i; | 287 | size_t i; |
384 | //uint64_t response_size=MHD_SIZE_UNKNOWN; | ||
385 | |||
386 | /*for(i = 0; nv[i]; i += 2) { | ||
387 | if(0 == strcmp("content-length", nv[i])) | ||
388 | { | ||
389 | response_size = atoi(nv[i+1]); | ||
390 | break; | ||
391 | } | ||
392 | }*/ | ||
393 | /* | ||
394 | proxy->http_response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, | ||
395 | 4096, | ||
396 | &http_response_callback, | ||
397 | proxy, | ||
398 | &http_response_done_callback); | ||
399 | |||
400 | if (proxy->http_response == NULL) | ||
401 | DIE("no response"); | ||
402 | 288 | ||
403 | if(MHD_NO == MHD_add_response_header (proxy->http_response, | ||
404 | "Proxy-Connection", "keep-alive")) | ||
405 | PRINT_INFO("SPDY_name_value_add failed: "); | ||
406 | if(MHD_NO == MHD_add_response_header (proxy->http_response, | ||
407 | "Connection", "Keep-Alive")) | ||
408 | PRINT_INFO("SPDY_name_value_add failed: "); | ||
409 | if(MHD_NO == MHD_add_response_header (proxy->http_response, | ||
410 | "Keep-Alive", "timeout=5, max=100")) | ||
411 | PRINT_INFO("SPDY_name_value_add failed: "); | ||
412 | */ | ||
413 | for(i = 0; nv[i]; i += 2) { | 289 | for(i = 0; nv[i]; i += 2) { |
414 | //printf(" %s: %s\n", nv[i], nv[i+1]); | ||
415 | //int j; | ||
416 | |||
417 | if(0 == strcmp(":status", nv[i])) | 290 | if(0 == strcmp(":status", nv[i])) |
418 | { | 291 | { |
419 | //raise(SIGINT); | ||
420 | //proxy->status_msg = nv[i+1]; | ||
421 | char tmp[4]; | 292 | char tmp[4]; |
422 | memcpy(&tmp,nv[i+1],3); | 293 | memcpy(&tmp,nv[i+1],3); |
423 | tmp[3]=0; | 294 | tmp[3]=0; |
@@ -431,35 +302,22 @@ http_create_response(struct Proxy* proxy, char **nv) | |||
431 | } | 302 | } |
432 | else if(0 == strcmp("content-length", nv[i])) | 303 | else if(0 == strcmp("content-length", nv[i])) |
433 | { | 304 | { |
434 | //proxy->length = atoi(nv[i+1]); | ||
435 | //response_size = atoi(nv[i+1]); | ||
436 | continue; | 305 | continue; |
437 | } | 306 | } |
438 | 307 | ||
439 | //for(j=0; j<strlen(nv[i]) && ':'==nv[i][j]; ++j); | ||
440 | |||
441 | char *header = *(nv+i); | 308 | char *header = *(nv+i); |
442 | //header[0] = toupper(header[0]); | ||
443 | if(MHD_NO == MHD_add_response_header (proxy->http_response, | 309 | if(MHD_NO == MHD_add_response_header (proxy->http_response, |
444 | header, nv[i+1])) | 310 | header, nv[i+1])) |
445 | { | 311 | { |
446 | PRINT_INFO2("SPDY_name_value_add failed: '%s' '%s'", header, nv[i+1]); | 312 | PRINT_INFO2("SPDY_name_value_add failed: '%s' '%s'", header, nv[i+1]); |
447 | //abort(); | ||
448 | } | 313 | } |
449 | PRINT_INFO2("adding '%s: %s'",header, nv[i+1]); | 314 | PRINT_INFO2("adding '%s: %s'",header, nv[i+1]); |
450 | } | 315 | } |
451 | 316 | ||
452 | //PRINT_INFO2("%i", MHD_get_response_headers(proxy->http_response, NULL, NULL)); | ||
453 | //PRINT_INFO2("state before %i", proxy->http_connection->state); | ||
454 | //PRINT_INFO2("loop before %i", proxy->http_connection->event_loop_info); | ||
455 | if(MHD_NO == MHD_queue_response (proxy->http_connection, proxy->status, proxy->http_response)){ | 317 | if(MHD_NO == MHD_queue_response (proxy->http_connection, proxy->status, proxy->http_response)){ |
456 | PRINT_INFO("No queue"); | 318 | PRINT_INFO("No queue"); |
457 | abort(); | 319 | abort(); |
458 | } | 320 | } |
459 | //PRINT_INFO2("state after %i", proxy->http_connection->state); | 321 | |
460 | //PRINT_INFO2("loop after %i", proxy->http_connection->event_loop_info); | 322 | MHD_destroy_response (proxy->http_response); |
461 | //MHD_destroy_response (proxy->http_response); | ||
462 | //PRINT_INFO2("state after %i", proxy->http_connection->state); | ||
463 | //PRINT_INFO2("loop after %i", proxy->http_connection->event_loop_info); | ||
464 | MHD_destroy_response (proxy->http_response); | ||
465 | } | 323 | } |