aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/session.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/session.c')
-rw-r--r--src/daemon/session.c100
1 files changed, 50 insertions, 50 deletions
diff --git a/src/daemon/session.c b/src/daemon/session.c
index f69ec1ed..edcbb04f 100644
--- a/src/daemon/session.c
+++ b/src/daemon/session.c
@@ -37,7 +37,7 @@
37 * maybe NULL (then just count headers) 37 * maybe NULL (then just count headers)
38 * @param iterator_cls extra argument to iterator 38 * @param iterator_cls extra argument to iterator
39 * @return number of entries iterated over 39 * @return number of entries iterated over
40 */ 40 */
41int 41int
42MHD_get_session_values(struct MHD_Session * session, 42MHD_get_session_values(struct MHD_Session * session,
43 enum MHD_ValueKind kind, 43 enum MHD_ValueKind kind,
@@ -46,7 +46,7 @@ MHD_get_session_values(struct MHD_Session * session,
46 int ret; 46 int ret;
47 struct MHD_HTTP_Header * pos; 47 struct MHD_HTTP_Header * pos;
48 48
49 if (session == NULL) 49 if (session == NULL)
50 return -1; 50 return -1;
51 ret = 0; 51 ret = 0;
52 pos = session->headers_received; 52 pos = session->headers_received;
@@ -54,11 +54,11 @@ MHD_get_session_values(struct MHD_Session * session,
54 if (0 != (pos->kind & kind)) { 54 if (0 != (pos->kind & kind)) {
55 ret++; 55 ret++;
56 if ( (iterator != NULL) && 56 if ( (iterator != NULL) &&
57 (MHD_YES != iterator(iterator_cls, 57 (MHD_YES != iterator(iterator_cls,
58 kind, 58 kind,
59 pos->header, 59 pos->header,
60 pos->value)) ) 60 pos->value)) )
61 return ret; 61 return ret;
62 } 62 }
63 pos = pos->next; 63 pos = pos->next;
64 } 64 }
@@ -72,14 +72,14 @@ MHD_get_session_values(struct MHD_Session * session,
72 * 72 *
73 * @param key the header to look for 73 * @param key the header to look for
74 * @return NULL if no such item was found 74 * @return NULL if no such item was found
75 */ 75 */
76const char * 76const char *
77MHD_lookup_session_value(struct MHD_Session * session, 77MHD_lookup_session_value(struct MHD_Session * session,
78 enum MHD_ValueKind kind, 78 enum MHD_ValueKind kind,
79 const char * key) { 79 const char * key) {
80 struct MHD_HTTP_Header * pos; 80 struct MHD_HTTP_Header * pos;
81 81
82 if (session == NULL) 82 if (session == NULL)
83 return NULL; 83 return NULL;
84 pos = session->headers_received; 84 pos = session->headers_received;
85 while (pos != NULL) { 85 while (pos != NULL) {
@@ -95,18 +95,18 @@ MHD_lookup_session_value(struct MHD_Session * session,
95/** 95/**
96 * Queue a response to be transmitted to the client (as soon as 96 * Queue a response to be transmitted to the client (as soon as
97 * possible). 97 * possible).
98 * 98 *
99 * @param session the session identifying the client 99 * @param session the session identifying the client
100 * @param status_code HTTP status code (i.e. 200 for OK) 100 * @param status_code HTTP status code (i.e. 200 for OK)
101 * @param response response to transmit 101 * @param response response to transmit
102 * @return MHD_NO on error (i.e. reply already sent), 102 * @return MHD_NO on error (i.e. reply already sent),
103 * MHD_YES on success or if message has been queued 103 * MHD_YES on success or if message has been queued
104 */ 104 */
105int 105int
106MHD_queue_response(struct MHD_Session * session, 106MHD_queue_response(struct MHD_Session * session,
107 unsigned int status_code, 107 unsigned int status_code,
108 struct MHD_Response * response) { 108 struct MHD_Response * response) {
109 if ( (session == NULL) || 109 if ( (session == NULL) ||
110 (response == NULL) || 110 (response == NULL) ||
111 (session->response != NULL) || 111 (session->response != NULL) ||
112 (session->bodyReceived == 0) || 112 (session->bodyReceived == 0) ||
@@ -124,7 +124,7 @@ MHD_queue_response(struct MHD_Session * session,
124 * 124 *
125 * @return MHD_YES on success 125 * @return MHD_YES on success
126 */ 126 */
127int 127int
128MHD_session_get_fdset(struct MHD_Session * session, 128MHD_session_get_fdset(struct MHD_Session * session,
129 fd_set * read_fd_set, 129 fd_set * read_fd_set,
130 fd_set * write_fd_set, 130 fd_set * write_fd_set,
@@ -135,11 +135,11 @@ MHD_session_get_fdset(struct MHD_Session * session,
135 fd = session->socket_fd; 135 fd = session->socket_fd;
136 if (fd == -1) 136 if (fd == -1)
137 return MHD_YES; 137 return MHD_YES;
138 if ( (session->read_close == 0) && 138 if ( (session->read_close == 0) &&
139 ( (session->headersReceived == 0) || 139 ( (session->headersReceived == 0) ||
140 (session->readLoc < session->read_buffer_size) ) ) 140 (session->readLoc < session->read_buffer_size) ) )
141 FD_SET(fd, read_fd_set); 141 FD_SET(fd, read_fd_set);
142 if (session->response != NULL) 142 if (session->response != NULL)
143 FD_SET(fd, write_fd_set); 143 FD_SET(fd, write_fd_set);
144 if ( (fd > *max_fd) && 144 if ( (fd > *max_fd) &&
145 ( (session->headersReceived == 0) || 145 ( (session->headersReceived == 0) ||
@@ -157,7 +157,7 @@ MHD_session_get_fdset(struct MHD_Session * session,
157 * found (incomplete, buffer too small, line too long), 157 * found (incomplete, buffer too small, line too long),
158 * return NULL. Otherwise return a copy of the line. 158 * return NULL. Otherwise return a copy of the line.
159 */ 159 */
160static char * 160static char *
161MHD_get_next_header_line(struct MHD_Session * session) { 161MHD_get_next_header_line(struct MHD_Session * session) {
162 char * rbuf; 162 char * rbuf;
163 size_t pos; 163 size_t pos;
@@ -201,10 +201,10 @@ MHD_get_next_header_line(struct MHD_Session * session) {
201 session->read_buffer, 201 session->read_buffer,
202 pos); 202 pos);
203 rbuf[pos] = '\0'; 203 rbuf[pos] = '\0';
204 if ( (session->read_buffer[pos] == '\r') && 204 if ( (session->read_buffer[pos] == '\r') &&
205 (session->read_buffer[pos+1] == '\n') ) 205 (session->read_buffer[pos+1] == '\n') )
206 pos++; /* skip both r and n */ 206 pos++; /* skip both r and n */
207 pos++; 207 pos++;
208 memmove(session->read_buffer, 208 memmove(session->read_buffer,
209 &session->read_buffer[pos], 209 &session->read_buffer[pos],
210 session->readLoc - pos); 210 session->readLoc - pos);
@@ -212,7 +212,7 @@ MHD_get_next_header_line(struct MHD_Session * session) {
212 return rbuf; 212 return rbuf;
213} 213}
214 214
215static void 215static void
216MHD_session_add_header(struct MHD_Session * session, 216MHD_session_add_header(struct MHD_Session * session,
217 const char * key, 217 const char * key,
218 const char * value, 218 const char * value,
@@ -227,7 +227,7 @@ MHD_session_add_header(struct MHD_Session * session,
227 session->headers_received = hdr; 227 session->headers_received = hdr;
228} 228}
229 229
230static void 230static void
231MHD_http_unescape(char * val) { 231MHD_http_unescape(char * val) {
232 char * esc; 232 char * esc;
233 unsigned int num; 233 unsigned int num;
@@ -248,7 +248,7 @@ MHD_http_unescape(char * val) {
248 } 248 }
249} 249}
250 250
251static void 251static void
252MHD_parse_arguments(struct MHD_Session * session, 252MHD_parse_arguments(struct MHD_Session * session,
253 char * args) { 253 char * args) {
254 char * equals; 254 char * equals;
@@ -264,7 +264,7 @@ MHD_parse_arguments(struct MHD_Session * session,
264 if (amper != NULL) { 264 if (amper != NULL) {
265 amper[0] = '\0'; 265 amper[0] = '\0';
266 amper++; 266 amper++;
267 } 267 }
268 MHD_http_unescape(args); 268 MHD_http_unescape(args);
269 MHD_http_unescape(equals); 269 MHD_http_unescape(equals);
270 MHD_session_add_header(session, 270 MHD_session_add_header(session,
@@ -278,7 +278,7 @@ MHD_parse_arguments(struct MHD_Session * session,
278/** 278/**
279 * Parse the cookie header (see RFC 2109). 279 * Parse the cookie header (see RFC 2109).
280 */ 280 */
281static void 281static void
282MHD_parse_cookie_header(struct MHD_Session * session) { 282MHD_parse_cookie_header(struct MHD_Session * session) {
283 const char * hdr; 283 const char * hdr;
284 char * cpy; 284 char * cpy;
@@ -336,7 +336,7 @@ MHD_parse_cookie_header(struct MHD_Session * session) {
336 * This function is designed to parse the input buffer of a given session. 336 * This function is designed to parse the input buffer of a given session.
337 * 337 *
338 * Once the header is complete, it should have set the 338 * Once the header is complete, it should have set the
339 * headers_received, url and method values and set 339 * headers_received, url and method values and set
340 * headersReceived to 1. If no body is expected, it should 340 * headersReceived to 1. If no body is expected, it should
341 * also set "bodyReceived" to 1. Otherwise, it should 341 * also set "bodyReceived" to 1. Otherwise, it should
342 * set "uploadSize" to the expected size of the body. If the 342 * set "uploadSize" to the expected size of the body. If the
@@ -354,7 +354,7 @@ MHD_parse_session_headers(struct MHD_Session * session) {
354 354
355 if (session->bodyReceived == 1) 355 if (session->bodyReceived == 1)
356 abort(); 356 abort();
357 while (NULL != (line = MHD_get_next_header_line(session))) { 357 while (NULL != (line = MHD_get_next_header_line(session))) {
358 if (session->url == NULL) { 358 if (session->url == NULL) {
359 /* line must be request line */ 359 /* line must be request line */
360 uri = strstr(line, " "); 360 uri = strstr(line, " ");
@@ -387,7 +387,7 @@ MHD_parse_session_headers(struct MHD_Session * session) {
387 MHD_HEADER_KIND, 387 MHD_HEADER_KIND,
388 "Content-Length"); 388 "Content-Length");
389 if (clen != NULL) { 389 if (clen != NULL) {
390 if (1 != sscanf(clen, 390 if (1 != sscanf(clen,
391 "%llu", 391 "%llu",
392 &cval)) { 392 &cval)) {
393 MHD_DLOG(session->daemon, 393 MHD_DLOG(session->daemon,
@@ -409,7 +409,7 @@ MHD_parse_session_headers(struct MHD_Session * session) {
409 session->bodyReceived = 0; 409 session->bodyReceived = 0;
410 } 410 }
411 } 411 }
412 break; 412 break;
413 } 413 }
414 /* line should be normal header line, find colon */ 414 /* line should be normal header line, find colon */
415 colon = strstr(line, ": "); 415 colon = strstr(line, ": ");
@@ -438,8 +438,8 @@ MHD_parse_session_headers(struct MHD_Session * session) {
438 438
439/** 439/**
440 * Find the handler responsible for this request. 440 * Find the handler responsible for this request.
441 */ 441 */
442static struct MHD_Access_Handler * 442static struct MHD_Access_Handler *
443MHD_find_access_handler(struct MHD_Session * session) { 443MHD_find_access_handler(struct MHD_Session * session) {
444 struct MHD_Access_Handler * pos; 444 struct MHD_Access_Handler * pos;
445 445
@@ -455,7 +455,7 @@ MHD_find_access_handler(struct MHD_Session * session) {
455 455
456/** 456/**
457 * Call the handler of the application for this 457 * Call the handler of the application for this
458 * session. 458 * session.
459 */ 459 */
460void 460void
461MHD_call_session_handler(struct MHD_Session * session) { 461MHD_call_session_handler(struct MHD_Session * session) {
@@ -484,7 +484,7 @@ MHD_call_session_handler(struct MHD_Session * session) {
484 &session->read_buffer[session->readLoc - processed], 484 &session->read_buffer[session->readLoc - processed],
485 processed); 485 processed);
486 if (session->uploadSize != -1) 486 if (session->uploadSize != -1)
487 session->uploadSize -= (session->readLoc - processed); 487 session->uploadSize -= (session->readLoc - processed);
488 session->readLoc = processed; 488 session->readLoc = processed;
489 if ( (session->uploadSize == 0) || 489 if ( (session->uploadSize == 0) ||
490 ( (session->readLoc == 0) && 490 ( (session->readLoc == 0) &&
@@ -503,13 +503,13 @@ MHD_call_session_handler(struct MHD_Session * session) {
503 * This function handles a particular connection when it has been 503 * This function handles a particular connection when it has been
504 * determined that there is data to be read off a socket. All implementations 504 * determined that there is data to be read off a socket. All implementations
505 * (multithreaded, external select, internal select) call this function 505 * (multithreaded, external select, internal select) call this function
506 * to handle reads. 506 * to handle reads.
507 */ 507 */
508int 508int
509MHD_session_handle_read(struct MHD_Session * session) { 509MHD_session_handle_read(struct MHD_Session * session) {
510 int bytes_read; 510 int bytes_read;
511 void * tmp; 511 void * tmp;
512 512
513 if ( (session->readLoc >= session->read_buffer_size) && 513 if ( (session->readLoc >= session->read_buffer_size) &&
514 (session->headersReceived == 0) ) { 514 (session->headersReceived == 0) ) {
515 /* need to grow read buffer */ 515 /* need to grow read buffer */
@@ -526,7 +526,7 @@ MHD_session_handle_read(struct MHD_Session * session) {
526 MHD_DLOG(session->daemon, 526 MHD_DLOG(session->daemon,
527 "Unexpected call to %s.\n", 527 "Unexpected call to %s.\n",
528 __FUNCTION__); 528 __FUNCTION__);
529 return MHD_NO; 529 return MHD_NO;
530 } 530 }
531 bytes_read = RECV(session->socket_fd, 531 bytes_read = RECV(session->socket_fd,
532 &session->read_buffer[session->readLoc], 532 &session->read_buffer[session->readLoc],
@@ -544,15 +544,15 @@ MHD_session_handle_read(struct MHD_Session * session) {
544 } 544 }
545 if (bytes_read == 0) { 545 if (bytes_read == 0) {
546 /* other side closed connection */ 546 /* other side closed connection */
547 if (session->readLoc > 0) 547 if (session->readLoc > 0)
548 MHD_call_session_handler(session); 548 MHD_call_session_handler(session);
549 shutdown(session->socket_fd, SHUT_RD); 549 shutdown(session->socket_fd, SHUT_RD);
550 return MHD_YES; 550 return MHD_YES;
551 } 551 }
552 session->readLoc += bytes_read; 552 session->readLoc += bytes_read;
553 if (session->headersReceived == 0) 553 if (session->headersReceived == 0)
554 MHD_parse_session_headers(session); 554 MHD_parse_session_headers(session);
555 if (session->headersReceived == 1) 555 if (session->headersReceived == 1)
556 MHD_call_session_handler(session); 556 MHD_call_session_handler(session);
557 return MHD_YES; 557 return MHD_YES;
558} 558}
@@ -561,7 +561,7 @@ MHD_session_handle_read(struct MHD_Session * session) {
561 * Check if we need to set some additional headers 561 * Check if we need to set some additional headers
562 * for http-compiliance. 562 * for http-compiliance.
563 */ 563 */
564static void 564static void
565MHD_add_extra_headers(struct MHD_Session * session) { 565MHD_add_extra_headers(struct MHD_Session * session) {
566 const char * have; 566 const char * have;
567 char buf[128]; 567 char buf[128];
@@ -600,7 +600,7 @@ MHD_build_header_response(struct MHD_Session * session) {
600 600
601 MHD_add_extra_headers(session); 601 MHD_add_extra_headers(session);
602 SPRINTF(code, 602 SPRINTF(code,
603 "HTTP/1.1 %u\r\n", 603 "HTTP/1.1 %u\r\n",
604 session->responseCode); 604 session->responseCode);
605 off = strlen(code); 605 off = strlen(code);
606 /* estimate size */ 606 /* estimate size */
@@ -627,10 +627,10 @@ MHD_build_header_response(struct MHD_Session * session) {
627 sprintf(&data[off], 627 sprintf(&data[off],
628 "\r\n"); 628 "\r\n");
629 off += 2; 629 off += 2;
630 if (off != size) 630 if (off != size)
631 abort(); 631 abort();
632 session->write_buffer = data; 632 session->write_buffer = data;
633 session->write_buffer_size = size; 633 session->write_buffer_size = size;
634} 634}
635 635
636/** 636/**
@@ -654,7 +654,7 @@ MHD_session_handle_write(struct MHD_Session * session) {
654 if (! session->headersSent) { 654 if (! session->headersSent) {
655 if (session->write_buffer == NULL) 655 if (session->write_buffer == NULL)
656 MHD_build_header_response(session); 656 MHD_build_header_response(session);
657 ret = SEND(session->socket_fd, 657 ret = SEND(session->socket_fd,
658 &session->write_buffer[session->writeLoc], 658 &session->write_buffer[session->writeLoc],
659 session->write_buffer_size - session->writeLoc, 659 session->write_buffer_size - session->writeLoc,
660 0); 660 0);
@@ -678,10 +678,10 @@ MHD_session_handle_write(struct MHD_Session * session) {
678 } 678 }
679 return MHD_YES; 679 return MHD_YES;
680 } 680 }
681 if (response->total_size <= session->messagePos) 681 if (response->total_size <= session->messagePos)
682 abort(); /* internal error */ 682 abort(); /* internal error */
683 if (response->crc != NULL) 683 if (response->crc != NULL)
684 pthread_mutex_lock(&response->mutex); 684 pthread_mutex_lock(&response->mutex);
685 685
686 /* prepare send buffer */ 686 /* prepare send buffer */
687 if ( (response->data == NULL) || 687 if ( (response->data == NULL) ||
@@ -708,16 +708,16 @@ MHD_session_handle_write(struct MHD_Session * session) {
708 response->data_start = session->messagePos; 708 response->data_start = session->messagePos;
709 response->data_size = ret; 709 response->data_size = ret;
710 if (ret == 0) 710 if (ret == 0)
711 return MHD_YES; 711 return MHD_YES;
712 } 712 }
713 713
714 /* transmit */ 714 /* transmit */
715 ret = SEND(session->socket_fd, 715 ret = SEND(session->socket_fd,
716 &response->data[session->messagePos - response->data_start], 716 &response->data[session->messagePos - response->data_start],
717 response->data_size - (session->messagePos - response->data_start), 717 response->data_size - (session->messagePos - response->data_start),
718 0); 718 0);
719 if (response->crc != NULL) 719 if (response->crc != NULL)
720 pthread_mutex_unlock(&response->mutex); 720 pthread_mutex_unlock(&response->mutex);
721 if (ret < 0) { 721 if (ret < 0) {
722 if (errno == EINTR) 722 if (errno == EINTR)
723 return MHD_YES; 723 return MHD_YES;
@@ -729,7 +729,7 @@ MHD_session_handle_write(struct MHD_Session * session) {
729 return MHD_YES; 729 return MHD_YES;
730 } 730 }
731 session->messagePos += ret; 731 session->messagePos += ret;
732 if (session->messagePos > response->data_size) 732 if (session->messagePos > response->data_size)
733 abort(); /* internal error */ 733 abort(); /* internal error */
734 if (session->messagePos == response->data_size) { 734 if (session->messagePos == response->data_size) {
735 if ( (session->bodyReceived == 0) || 735 if ( (session->bodyReceived == 0) ||