aboutsummaryrefslogtreecommitdiff
path: root/src/examples/mhd2spdy_http.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/examples/mhd2spdy_http.c')
-rw-r--r--src/examples/mhd2spdy_http.c104
1 files changed, 57 insertions, 47 deletions
diff --git a/src/examples/mhd2spdy_http.c b/src/examples/mhd2spdy_http.c
index cf9f6c52..063b6d58 100644
--- a/src/examples/mhd2spdy_http.c
+++ b/src/examples/mhd2spdy_http.c
@@ -38,7 +38,7 @@ const char * uri)
38 38
39 //TODO not freed once in a while 39 //TODO not freed once in a while
40 if(NULL == (http_uri = au_malloc(sizeof(struct HTTP_URI )))) 40 if(NULL == (http_uri = au_malloc(sizeof(struct HTTP_URI ))))
41 DIE("no memory"); 41 return NULL;
42 http_uri->uri = strdup(uri); 42 http_uri->uri = strdup(uri);
43 return http_uri; 43 return http_uri;
44} 44}
@@ -94,7 +94,8 @@ http_cb_response (void *cls,
94 if(proxy->spdy_error) 94 if(proxy->spdy_error)
95 return MHD_CONTENT_READER_END_WITH_ERROR; 95 return MHD_CONTENT_READER_END_WITH_ERROR;
96 96
97 if(0 == proxy->http_body_size &&( proxy->done || !proxy->spdy_active)){ 97 if(0 == proxy->http_body_size && (proxy->done || !proxy->spdy_active))
98 {
98 PRINT_INFO("sent end of stream"); 99 PRINT_INFO("sent end of stream");
99 return MHD_CONTENT_READER_END_OF_STREAM; 100 return MHD_CONTENT_READER_END_OF_STREAM;
100 } 101 }
@@ -124,7 +125,7 @@ http_cb_response (void *cls,
124 if(NULL == (newbody = au_malloc(proxy->http_body_size - max))) 125 if(NULL == (newbody = au_malloc(proxy->http_body_size - max)))
125 { 126 {
126 PRINT_INFO("no memory"); 127 PRINT_INFO("no memory");
127 return -2; 128 return MHD_CONTENT_READER_END_WITH_ERROR;
128 } 129 }
129 memcpy(newbody, proxy->http_body + max, proxy->http_body_size - max); 130 memcpy(newbody, proxy->http_body + max, proxy->http_body_size - max);
130 } 131 }
@@ -147,12 +148,6 @@ http_cb_response (void *cls,
147static void 148static void
148http_cb_response_done(void *cls) 149http_cb_response_done(void *cls)
149{ 150{
150
151 //TODO
152 /*struct Proxy *proxy = (struct Proxy *)cls;
153
154 PRINT_INFO2("http_cb_response_done for %s", proxy->url);
155 */
156 151
157} 152}
158 153
@@ -175,15 +170,16 @@ http_cb_request (void *cls,
175 struct Proxy *proxy; 170 struct Proxy *proxy;
176 struct SPDY_Headers spdy_headers; 171 struct SPDY_Headers spdy_headers;
177 bool with_body = false; 172 bool with_body = false;
178 173 struct HTTP_URI *http_uri;
179 //PRINT_INFO2("request cb %i; %s", *ptr,url);
180 174
181 if (NULL == *ptr) 175 if (NULL == ptr || NULL == *ptr)
182 DIE("ptr is null"); 176 return MHD_NO;
183 struct HTTP_URI *http_uri = (struct HTTP_URI *)*ptr; 177
178 http_uri = (struct HTTP_URI *)*ptr;
184 179
185 if(NULL == http_uri->proxy) 180 if(NULL == http_uri->proxy)
186 { 181 {
182 //first call for this request
187 if (0 != strcmp (method, MHD_HTTP_METHOD_GET) && 0 != strcmp (method, MHD_HTTP_METHOD_POST)) 183 if (0 != strcmp (method, MHD_HTTP_METHOD_GET) && 0 != strcmp (method, MHD_HTTP_METHOD_POST))
188 { 184 {
189 free(http_uri->uri); 185 free(http_uri->uri);
@@ -207,12 +203,15 @@ http_cb_request (void *cls,
207 } 203 }
208 204
209 proxy = http_uri->proxy; 205 proxy = http_uri->proxy;
206
207 if(proxy->spdy_error || proxy->http_error)
208 return MHD_NO; // handled at different place TODO? leaks?
210 209
211 if(proxy->spdy_active) 210 if(proxy->spdy_active)
212 { 211 {
213 if(0 == strcmp (method, MHD_HTTP_METHOD_POST)) 212 if(0 == strcmp (method, MHD_HTTP_METHOD_POST))
214 { 213 {
215 PRINT_INFO("POST processing"); 214 PRINT_INFO("POST processing");
216 215
217 int rc= spdylay_session_resume_data(proxy->spdy_connection->session, proxy->stream_id); 216 int rc= spdylay_session_resume_data(proxy->spdy_connection->session, proxy->stream_id);
218 PRINT_INFO2("rc is %i stream is %i", rc, proxy->stream_id); 217 PRINT_INFO2("rc is %i stream is %i", rc, proxy->stream_id);
@@ -231,24 +230,8 @@ http_cb_request (void *cls,
231 PRINT_INFO("not enough memory (malloc/realloc returned NULL)"); 230 PRINT_INFO("not enough memory (malloc/realloc returned NULL)");
232 return MHD_NO; 231 return MHD_NO;
233 } 232 }
234 /*
235 if(NULL == proxy->received_body)
236 proxy->received_body = malloc(*upload_data_size);
237 else
238 proxy->received_body = realloc(proxy->received_body, proxy->received_body_size + *upload_data_size);
239 if(NULL == proxy->received_body)
240 {
241 //TODO handle it better?
242 PRINT_INFO("not enough memory (realloc returned NULL)");
243 return MHD_NO;
244 }
245
246 memcpy(proxy->received_body + proxy->received_body_size, upload_data, *upload_data_size);
247 proxy->received_body_size += *upload_data_size;
248 */
249 *upload_data_size = 0;
250 233
251 //raise(SIGINT); 234 *upload_data_size = 0;
252 235
253 return MHD_YES; 236 return MHD_YES;
254 } 237 }
@@ -257,12 +240,15 @@ http_cb_request (void *cls,
257 PRINT_INFO("unnecessary call to http_cb_request"); 240 PRINT_INFO("unnecessary call to http_cb_request");
258 return MHD_YES; 241 return MHD_YES;
259 } 242 }
243
244 //second call for this request
260 245
261 PRINT_INFO2("received request for '%s %s %s'", method, http_uri->uri, version); 246 PRINT_INFO2("received request for '%s %s %s'", method, http_uri->uri, version);
262 247
263 proxy->url = http_uri->uri; 248 proxy->url = http_uri->uri;
264 249
265 with_body = 0 == strcmp (method, MHD_HTTP_METHOD_POST) && 0 != strcmp ("0", 250 with_body = 0 == strcmp (method, MHD_HTTP_METHOD_POST)
251 && 0 != strcmp ("0",
266 MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH)); 252 MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH));
267 253
268 PRINT_INFO2("body will be sent %i", with_body); 254 PRINT_INFO2("body will be sent %i", with_body);
@@ -271,7 +257,6 @@ http_cb_request (void *cls,
271 if(ret != 0) 257 if(ret != 0)
272 DIE("parse_uri failed"); 258 DIE("parse_uri failed");
273 proxy->http_uri = http_uri; 259 proxy->http_uri = http_uri;
274 proxy->spdy_active = true;
275 260
276 spdy_headers.num = MHD_get_connection_values (connection, 261 spdy_headers.num = MHD_get_connection_values (connection,
277 MHD_HEADER_KIND, 262 MHD_HEADER_KIND,
@@ -298,7 +283,7 @@ http_cb_request (void *cls,
298 if(0 != spdy_request(spdy_headers.nv, proxy, with_body)) 283 if(0 != spdy_request(spdy_headers.nv, proxy, with_body))
299 { 284 {
300 free(spdy_headers.nv); 285 free(spdy_headers.nv);
301 free_proxy(proxy); 286 //free_proxy(proxy);
302 287
303 return MHD_NO; 288 return MHD_NO;
304 } 289 }
@@ -310,7 +295,7 @@ http_cb_request (void *cls,
310 proxy, 295 proxy,
311 &http_cb_response_done); 296 &http_cb_response_done);
312 297
313 if (proxy->http_response == NULL) 298 if (NULL == proxy->http_response)
314 DIE("no response"); 299 DIE("no response");
315 300
316 if(MHD_NO == MHD_add_response_header (proxy->http_response, 301 if(MHD_NO == MHD_add_response_header (proxy->http_response,
@@ -323,6 +308,8 @@ http_cb_request (void *cls,
323 "Keep-Alive", "timeout=5, max=100")) 308 "Keep-Alive", "timeout=5, max=100"))
324 PRINT_INFO("SPDY_name_value_add failed: "); 309 PRINT_INFO("SPDY_name_value_add failed: ");
325 310
311 proxy->spdy_active = true;
312
326 return MHD_YES; 313 return MHD_YES;
327} 314}
328 315
@@ -333,6 +320,9 @@ http_create_response(struct Proxy* proxy,
333{ 320{
334 size_t i; 321 size_t i;
335 322
323 if(!proxy->http_active)
324 return;
325
336 for(i = 0; nv[i]; i += 2) { 326 for(i = 0; nv[i]; i += 2) {
337 if(0 == strcmp(":status", nv[i])) 327 if(0 == strcmp(":status", nv[i]))
338 { 328 {
@@ -363,10 +353,13 @@ http_create_response(struct Proxy* proxy,
363 353
364 if(MHD_NO == MHD_queue_response (proxy->http_connection, proxy->status, proxy->http_response)){ 354 if(MHD_NO == MHD_queue_response (proxy->http_connection, proxy->status, proxy->http_response)){
365 PRINT_INFO("No queue"); 355 PRINT_INFO("No queue");
366 abort(); 356 //TODO
357 //abort();
358 proxy->http_error = true;
367 } 359 }
368 360
369 MHD_destroy_response (proxy->http_response); 361 MHD_destroy_response (proxy->http_response);
362 proxy->http_response = NULL;
370} 363}
371 364
372void 365void
@@ -375,11 +368,22 @@ http_cb_request_completed (void *cls,
375 void **con_cls, 368 void **con_cls,
376 enum MHD_RequestTerminationCode toe) 369 enum MHD_RequestTerminationCode toe)
377{ 370{
378 struct HTTP_URI *http_uri = (struct HTTP_URI *)*con_cls; 371 struct HTTP_URI *http_uri;
379 if(NULL == http_uri) return; 372 struct Proxy *proxy;
380 struct Proxy *proxy = (struct Proxy *)http_uri->proxy; 373
374 http_uri = (struct HTTP_URI *)*con_cls;
375 if(NULL == http_uri)
376 return;
377 proxy = (struct Proxy *)http_uri->proxy;
378 assert(NULL != proxy);
381 379
382 PRINT_INFO2("http_cb_request_completed %i for %s",toe, http_uri->uri); 380 PRINT_INFO2("http_cb_request_completed %i for %s; id %i",toe, http_uri->uri, proxy->id);
381
382 if(NULL != proxy->http_response)
383 {
384 MHD_destroy_response (proxy->http_response);
385 proxy->http_response = NULL;
386 }
383 387
384 if(proxy->spdy_active) 388 if(proxy->spdy_active)
385 { 389 {
@@ -387,14 +391,20 @@ http_cb_request_completed (void *cls,
387 if(MHD_REQUEST_TERMINATED_COMPLETED_OK != toe) 391 if(MHD_REQUEST_TERMINATED_COMPLETED_OK != toe)
388 { 392 {
389 proxy->http_error = true; 393 proxy->http_error = true;
390 assert(proxy->stream_id > 0); 394 if(proxy->stream_id > 0 && NULL != proxy->spdy_connection->session)
391 //send RST_STREAM_STATUS_CANCEL 395 {
392 PRINT_INFO("send rst_stream" ); 396 //send RST_STREAM_STATUS_CANCEL
393 spdylay_submit_rst_stream(proxy->spdy_connection->session, proxy->stream_id, 5); 397 PRINT_INFO2("send rst_stream %i",proxy->spdy_active );
398 spdylay_submit_rst_stream(proxy->spdy_connection->session, proxy->stream_id, 5);
399 }
394 } 400 }
395 } 401 }
396 else 402 else
397 free_proxy(proxy); 403 {
404 PRINT_INFO2("proxy free http id %i ", proxy->id);
405 //DLL_remove(proxy->spdy_connection->proxies_head, proxy->spdy_connection->proxies_tail, proxy);
406 //free_proxy(proxy);
407 }
398 408
399 --glob_opt.responses_pending; 409 --glob_opt.responses_pending;
400} 410}