Fix 'FIXME' code
[MonkeyD.git] / src / request.c
blob457a19146c71db0217bae002661b9ac4fcec5cb0
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-2010, Eduardo Silva P. <edsiper@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <sys/socket.h>
28 #include <sys/time.h>
29 #include <time.h>
30 #include <netdb.h>
31 #include <sys/wait.h>
32 #include <signal.h>
33 #include <errno.h>
35 #include <string.h>
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
39 #include <sys/types.h>
41 #include "request.h"
42 #include "monkey.h"
43 #include "http.h"
44 #include "http_status.h"
45 #include "string.h"
46 #include "str.h"
47 #include "config.h"
48 #include "scheduler.h"
49 #include "epoll.h"
50 #include "socket.h"
51 #include "utils.h"
52 #include "header.h"
53 #include "user.h"
54 #include "method.h"
55 #include "memory.h"
56 #include "socket.h"
57 #include "cache.h"
58 #include "clock.h"
59 #include "utils.h"
60 #include "plugin.h"
62 struct request *mk_request_parse(struct client_request *cr)
64 int i, end;
65 int blocks = 0;
66 struct request *cr_buf = 0, *cr_search = 0;
68 for (i = 0; i <= cr->body_pos_end; i++) {
69 /* Look for CRLFCRLF (\r\n\r\n), maybe some pipelining
70 * request can be involved.
72 end = mk_string_search(cr->body + i, mk_endblock.data) + i;
74 if (end < 0) {
75 return NULL;
78 /* Allocating request block */
79 cr_buf = mk_request_alloc();
81 /* We point the block with a mk_pointer */
82 cr_buf->body.data = cr->body + i;
83 cr_buf->body.len = end - i;
85 /* Method, previous catch in mk_http_pending_request */
86 if (i == 0) {
87 cr_buf->method = cr->first_method;
89 else {
90 cr_buf->method = mk_http_method_get(cr_buf->body.data);
92 cr_buf->next = NULL;
94 /* Looking for POST data */
95 if (cr_buf->method == HTTP_METHOD_POST) {
96 cr_buf->post_variables = mk_method_post_get_vars(cr->body,
97 end + mk_endblock.len);
98 if (cr_buf->post_variables.len >= 0) {
99 i += cr_buf->post_variables.len;
103 /* Increase index to the end of the current block */
104 i = (end + mk_endblock.len) - 1;
106 /* Link block */
107 if (!cr->request) {
108 cr->request = cr_buf;
110 else {
111 cr_search = cr->request;
112 while (cr_search) {
113 if (cr_search->next == NULL) {
114 cr_search->next = cr_buf;
115 break;
117 else {
118 cr_search = cr_search->next;
123 /* Update counter */
124 blocks++;
128 /* DEBUG BLOCKS
129 cr_search = cr->request;
130 while(cr_search){
131 printf("\n");
132 MK_TRACE("BLOCK INIT");
133 mk_pointer_print(cr_search->body);
134 MK_TRACE("BLOCK_END");
136 cr_search = cr_search->next;
140 /* Checking pipelining connection */
141 cr_search = cr->request;
142 if (blocks > 1) {
143 while (cr_search) {
144 /* Pipelining request must use GET or HEAD methods */
145 if (cr_search->method != HTTP_METHOD_GET &&
146 cr_search->method != HTTP_METHOD_HEAD) {
147 return NULL;
149 cr_search = cr_search->next;
152 cr->pipelined = TRUE;
155 return cr->request;
158 int mk_handler_read(int socket, struct client_request *cr)
160 int bytes;
162 bytes = mk_socket_read(socket, (void *)cr->body + cr->body_length, MAX_REQUEST_BODY - cr->body_length);
164 if (bytes < 0) {
165 if (errno == EAGAIN) {
166 return 1;
168 else {
169 mk_request_client_remove(socket);
170 return -1;
173 if (bytes == 0) {
174 mk_request_client_remove(socket);
175 return -1;
178 if (bytes > 0) {
179 cr->body_length += bytes;
180 cr->body[cr->body_length] = '\0';
183 return bytes;
186 int mk_handler_write(int socket, struct client_request *cr)
188 int bytes, final_status = 0;
189 struct request *sr;
192 * Get node from schedule list node which contains
193 * the information regarding to the current thread
195 if (!cr) {
196 return -1;
199 if (!cr->request) {
200 if (!mk_request_parse(cr)) {
201 return -1;
205 sr = cr->request;
206 while (sr) {
207 /* Request not processed also no plugin has take some action */
208 if (sr->bytes_to_send < 0 && !sr->handled_by) {
209 final_status = mk_request_process(cr, sr);
211 /* Request with data to send by static file sender */
212 else if (sr->bytes_to_send > 0 && !sr->handled_by) {
213 final_status = bytes = mk_http_send_file(cr, sr);
217 * If we got an error, we don't want to parse
218 * and send information for another pipelined request
220 if (final_status > 0) {
221 return final_status;
223 else if (final_status <= 0) {
224 /* STAGE_40, request has ended */
225 mk_plugin_stage_run(MK_PLUGIN_STAGE_40, cr->socket,
226 NULL, cr, sr);
227 switch (final_status) {
228 case EXIT_NORMAL:
229 case EXIT_ERROR:
230 if (sr->close_now == VAR_ON) {
231 return -1;
233 break;
234 case EXIT_ABORT:
235 return -1;
239 sr = sr->next;
242 /* If we are here, is because all pipelined request were
243 * processed successfully, let's return 0;
245 return 0;
248 int mk_request_process(struct client_request *cr, struct request *s_request)
250 int status = 0;
251 struct host *host;
253 status = mk_request_header_process(s_request);
255 if (status < 0) {
256 return EXIT_ABORT;
259 switch (s_request->method) {
260 case METHOD_NOT_ALLOWED:
261 mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED, cr, s_request, 1);
262 return EXIT_NORMAL;
263 case METHOD_NOT_FOUND:
264 mk_request_error(M_SERVER_NOT_IMPLEMENTED, cr, s_request, 1);
265 return EXIT_NORMAL;
268 s_request->user_home = VAR_OFF;
270 /* Valid request URI? */
271 if (s_request->uri_processed == NULL) {
272 mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request, 1);
273 return EXIT_NORMAL;
276 /* HTTP/1.1 needs Host header */
277 if (!s_request->host.data && s_request->protocol == HTTP_PROTOCOL_11) {
278 mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request, 1);
279 return EXIT_NORMAL;
282 /* Method not allowed ? */
283 if (s_request->method == METHOD_NOT_ALLOWED) {
284 mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED, cr, s_request, 1);
285 return EXIT_NORMAL;
288 /* Validating protocol version */
289 if (s_request->protocol == HTTP_PROTOCOL_UNKNOWN) {
290 mk_request_error(M_SERVER_HTTP_VERSION_UNSUP, cr, s_request, 1);
291 return EXIT_NORMAL;
294 if (s_request->host.data) {
295 host = mk_config_host_find(s_request->host);
296 if (host) {
297 s_request->host_conf = host;
299 else {
300 s_request->host_conf = config->hosts;
303 else {
304 s_request->host_conf = config->hosts;
307 /* is requesting an user home directory ? */
308 if (config->user_dir) {
309 if (strncmp(s_request->uri_processed,
310 mk_user_home.data, mk_user_home.len) == 0) {
311 if (mk_user_init(cr, s_request) != 0) {
312 return EXIT_NORMAL;
317 /* Handling method requested */
318 if (s_request->method == HTTP_METHOD_POST) {
319 if ((status = mk_method_post(cr, s_request)) == -1) {
320 return status;
324 /* Plugins Stage 20 */
325 int ret;
326 ret = mk_plugin_stage_run(MK_PLUGIN_STAGE_20, cr->socket, NULL,
327 cr, s_request);
329 if (ret == MK_PLUGIN_RET_CLOSE_CONX) {
330 #ifdef TRACE
331 MK_TRACE("STAGE 20 requested close conexion");
332 #endif
333 return EXIT_ABORT;
336 /* Normal HTTP process */
337 status = mk_http_init(cr, s_request);
339 #ifdef TRACE
340 MK_TRACE("HTTP Init returning %i", status);
341 #endif
343 return status;
346 /* Return a struct with method, URI , protocol version
347 and all static headers defined here sent in request */
348 int mk_request_header_process(struct request *sr)
350 int uri_init = 0, uri_end = 0;
351 char *query_init = 0;
352 int prot_init = 0, prot_end = 0, pos_sep = 0;
353 int fh_limit;
354 char *port = 0;
355 char *headers;
356 mk_pointer host;
358 /* Method */
359 sr->method_p = mk_http_method_check_str(sr->method);
361 /* Request URI */
362 uri_init = (index(sr->body.data, ' ') - sr->body.data) + 1;
363 fh_limit = (index(sr->body.data, '\n') - sr->body.data);
365 uri_end = mk_string_search_r(sr->body.data, ' ', fh_limit) - 1;
367 if (uri_end <= 0) {
368 return -1;
371 prot_init = uri_end + 2;
373 if (uri_end < uri_init) {
374 return -1;
377 /* Query String */
378 query_init = index(sr->body.data + uri_init, '?');
379 if (query_init) {
380 int init, end;
382 init = (int) (query_init - (sr->body.data + uri_init)) + uri_init;
383 if (init <= uri_end) {
384 end = uri_end;
385 uri_end = init - 1;
387 sr->query_string = mk_pointer_create(sr->body.data,
388 init + 1, end + 1);
392 /* Request URI Part 2 */
393 sr->uri = mk_pointer_create(sr->body.data, uri_init, uri_end + 1);
395 if (sr->uri.len < 1) {
396 return -1;
400 /* HTTP Version */
401 prot_end = fh_limit - 1;
402 if (prot_end != prot_init && prot_end > 0) {
403 sr->protocol = mk_http_protocol_check(sr->body.data + prot_init,
404 prot_end - prot_init);
405 sr->protocol_p = mk_http_protocol_check_str(sr->protocol);
408 headers = sr->body.data + prot_end + mk_crlf.len;
410 /* URI processed */
411 sr->uri_processed = mk_utils_hexuri_to_ascii(sr->uri);
413 if (!sr->uri_processed) {
414 sr->uri_processed = mk_pointer_to_buf(sr->uri);
415 sr->uri_twin = VAR_ON;
418 /* Creating table of content (index) for request headers */
419 int toc_len = MK_KNOWN_HEADERS;
420 int headers_len = sr->body.len - (prot_end + mk_crlf.len);
422 struct header_toc *toc = mk_request_header_toc_create(toc_len);
423 mk_request_header_toc_parse(toc, toc_len, headers, headers_len);
425 /* Host */
426 host = mk_request_header_find(toc, toc_len, headers, mk_rh_host);
428 if (host.data) {
429 if ((pos_sep = mk_string_char_search(host.data, ':', host.len)) >= 0) {
430 sr->host.data = host.data;
431 sr->host.len = pos_sep;
433 port = mk_string_copy_substr(host.data, pos_sep + 1, host.len);
434 sr->port = atoi(port);
435 mk_mem_free(port);
437 else {
438 sr->host = host; /* maybe null */
439 sr->port = config->standard_port;
442 else {
443 sr->host.data = NULL;
446 /* Looking for headers */
447 sr->accept = mk_request_header_find(toc, toc_len, headers, mk_rh_accept);
448 sr->accept_charset = mk_request_header_find(toc, toc_len, headers,
449 mk_rh_accept_charset);
450 sr->accept_encoding = mk_request_header_find(toc, toc_len, headers,
451 mk_rh_accept_encoding);
454 sr->accept_language = mk_request_header_find(toc, toc_len, headers,
455 mk_rh_accept_language);
456 sr->cookies = mk_request_header_find(toc, toc_len, headers, mk_rh_cookie);
457 sr->connection = mk_request_header_find(toc, toc_len, headers,
458 mk_rh_connection);
459 sr->referer = mk_request_header_find(toc, toc_len, headers,
460 mk_rh_referer);
461 sr->user_agent = mk_request_header_find(toc, toc_len, headers,
462 mk_rh_user_agent);
463 sr->range = mk_request_header_find(toc, toc_len, headers, mk_rh_range);
464 sr->if_modified_since = mk_request_header_find(toc, toc_len, headers,
465 mk_rh_if_modified_since);
467 /* Default Keepalive is off */
468 if (sr->protocol == HTTP_PROTOCOL_10) {
469 sr->keep_alive = VAR_OFF;
470 sr->close_now = VAR_ON;
472 else if(sr->protocol == HTTP_PROTOCOL_11) {
473 sr->keep_alive = VAR_ON;
474 sr->close_now = VAR_OFF;
477 if (sr->connection.data) {
478 if (mk_string_casestr(sr->connection.data, "Keep-Alive")) {
479 sr->keep_alive = VAR_ON;
480 sr->close_now = VAR_OFF;
482 else if(mk_string_casestr(sr->connection.data, "Close")) {
483 sr->keep_alive = VAR_OFF;
484 sr->close_now = VAR_ON;
486 else {
487 /* Set as a non-valid connection header value */
488 sr->connection.len = 0;
492 return 0;
495 /* Return value of some variable sent in request */
496 mk_pointer mk_request_header_find(struct header_toc * toc, int toc_len,
497 char *request_body, mk_pointer header)
499 int i;
500 mk_pointer var;
502 var.data = NULL;
503 var.len = 0;
505 if (toc) {
506 for (i = 0; i < toc_len; i++) {
507 /* status = 1 means that the toc entry was already
508 * checked by monkey
510 if (toc[i].status == 1) {
511 continue;
514 if (!toc[i].init)
515 break;
517 if (strncasecmp(toc[i].init, header.data, header.len) == 0) {
518 var.data = toc[i].init + header.len + 1;
519 var.len = toc[i].end - var.data;
520 toc[i].status = 1;
521 return var;
526 return var;
529 /* Look for some index.xxx in pathfile */
530 mk_pointer mk_request_index(char *pathfile)
532 unsigned long len;
533 char *file_aux = 0;
534 mk_pointer f;
535 struct indexfile *aux_index;
537 mk_pointer_reset(&f);
539 aux_index = first_index;
541 while (aux_index) {
542 mk_string_build(&file_aux, &len, "%s%s",
543 pathfile, aux_index->indexname);
545 if (access(file_aux, F_OK) == 0) {
546 f.data = file_aux;
547 f.len = len;
548 return f;
550 mk_mem_free(file_aux);
551 aux_index = aux_index->next;
554 return f;
557 /* Send error responses */
558 void mk_request_error(int http_status, struct client_request *cr,
559 struct request *sr, int debug){
560 char *aux_message = 0;
561 mk_pointer message, *page = 0;
562 long n;
565 * fixme s_log->error_details.data = NULL;
568 switch (http_status) {
569 case M_CLIENT_BAD_REQUEST:
570 page = mk_request_set_default_page("Bad Request",
571 sr->uri,
572 sr->host_conf->host_signature);
573 break;
575 case M_CLIENT_FORBIDDEN:
576 page = mk_request_set_default_page("Forbidden",
577 sr->uri,
578 sr->host_conf->host_signature);
579 //s_log->error_msg = request_error_msg_403;
580 //s_log->error_details = s_request->uri;
581 break;
583 case M_CLIENT_NOT_FOUND:
584 mk_string_build(&message.data, &message.len,
585 "The requested URL was not found on this server.");
586 page = mk_request_set_default_page("Not Found",
587 message,
588 sr->host_conf->host_signature);
589 //s_log->error_msg = request_error_msg_404;
590 //s_log->error_details = s_request->uri;
592 mk_pointer_free(&message);
593 break;
595 case M_CLIENT_METHOD_NOT_ALLOWED:
596 page = mk_request_set_default_page("Method Not Allowed",
597 sr->uri,
598 sr->host_conf->host_signature);
600 //s_log->final_response = M_CLIENT_METHOD_NOT_ALLOWED;
601 //s_log->error_msg = request_error_msg_405;
602 //s_log->error_details = s_request->method_p;
603 break;
605 case M_CLIENT_REQUEST_TIMEOUT:
606 //s_log->status = S_LOG_OFF;
607 //s_log->error_msg = request_error_msg_408;
608 break;
610 case M_CLIENT_LENGTH_REQUIRED:
611 //s_log->error_msg = request_error_msg_411;
612 break;
614 case M_SERVER_NOT_IMPLEMENTED:
615 page = mk_request_set_default_page("Method Not Implemented",
616 sr->uri,
617 sr->host_conf->host_signature);
618 //s_log->final_response = M_SERVER_NOT_IMPLEMENTED;
619 //s_log->error_msg = request_error_msg_501;
620 //s_log->error_details = s_request->method_p;
621 break;
623 case M_SERVER_INTERNAL_ERROR:
624 mk_string_build(&message.data, &message.len,
625 "Problems found running %s ", sr->uri);
626 page = mk_request_set_default_page("Internal Server Error",
627 message,
628 sr->host_conf->host_signature);
629 //s_log->error_msg = request_error_msg_500;
631 mk_pointer_free(&message);
632 break;
634 case M_SERVER_HTTP_VERSION_UNSUP:
635 mk_pointer_reset(&message);
636 page = mk_request_set_default_page("HTTP Version Not Supported",
637 message,
638 sr->host_conf->host_signature);
639 //s_log->error_msg = request_error_msg_505;
640 break;
643 //s_log->final_response = num_error;
645 mk_header_set_http_status(sr, http_status);
646 if (page) {
647 sr->headers->content_length = page->len;
650 sr->headers->location = NULL;
651 sr->headers->cgi = SH_NOCGI;
652 sr->headers->pconnections_left = 0;
653 mk_pointer_reset(&sr->headers->last_modified);
655 if (aux_message)
656 mk_mem_free(aux_message);
658 if (!page) {
659 mk_pointer_reset(&sr->headers->content_type);
661 else {
662 mk_pointer_set(&sr->headers->content_type, "text/html\r\n");
665 mk_header_send(cr->socket, cr, sr);
667 if (debug == 1) {
668 n = write(cr->socket, page->data, page->len);
669 mk_pointer_free(page);
670 mk_mem_free(page);
674 /* Build error page */
675 mk_pointer *mk_request_set_default_page(char *title, mk_pointer message,
676 char *signature)
678 char *temp;
679 mk_pointer *p;
681 p = mk_mem_malloc(sizeof(mk_pointer));
683 temp = mk_pointer_to_buf(message);
684 mk_string_build(&p->data, &p->len,
685 MK_REQUEST_DEFAULT_PAGE, title, temp, signature);
686 mk_mem_free(temp);
688 return p;
691 /* Create a memory allocation in order to handle the request data */
692 struct request *mk_request_alloc()
694 struct request *request = 0;
696 request = mk_mem_malloc(sizeof(struct request));
697 request->status = VAR_OFF; /* Request not processed yet */
698 request->close_now = VAR_OFF;
700 mk_pointer_reset(&request->body);
701 request->status = VAR_ON;
702 request->method = METHOD_NOT_FOUND;
704 mk_pointer_reset(&request->uri);
705 request->uri_processed = NULL;
706 request->uri_twin = VAR_OFF;
708 request->accept.data = NULL;
709 request->accept_language.data = NULL;
710 request->accept_encoding.data = NULL;
711 request->accept_charset.data = NULL;
712 request->content_length = 0;
713 request->content_type.data = NULL;
714 request->connection.data = NULL;
715 request->cookies.data = NULL;
716 request->host.data = NULL;
717 request->if_modified_since.data = NULL;
718 request->last_modified_since.data = NULL;
719 request->range.data = NULL;
720 request->referer.data = NULL;
721 request->resume.data = NULL;
722 request->user_agent.data = NULL;
724 request->post_variables.data = NULL;
726 request->user_uri = NULL;
727 mk_pointer_reset(&request->query_string);
729 request->file_info = NULL;
730 request->virtual_user = NULL;
731 request->script_filename = NULL;
732 mk_pointer_reset(&request->real_path);
733 request->host_conf = config->hosts;
735 request->loop = 0;
736 request->bytes_to_send = -1;
737 request->bytes_offset = 0;
738 request->fd_file = -1;
740 /* Response Headers */
741 request->headers = mk_header_create();
743 /* Plugin handler */
744 request->handled_by = NULL;
746 return request;
749 void mk_request_free_list(struct client_request *cr)
751 struct request *sr = 0, *before = 0;
753 /* sr = last node */
754 #ifdef TRACE
755 MK_TRACE("Free struct client_request [FD %i]", cr->socket);
756 #endif
758 while (cr->request) {
759 sr = before = cr->request;
761 while (sr->next) {
762 sr = sr->next;
765 if (sr != cr->request) {
766 while (before->next != sr) {
767 before = before->next;
769 before->next = NULL;
771 else {
772 cr->request = NULL;
774 mk_request_free(sr);
776 cr->request = NULL;
779 void mk_request_free(struct request *sr)
781 if (sr->fd_file > 0) {
782 close(sr->fd_file);
784 if (sr->headers) {
785 mk_mem_free(sr->headers->location);
786 mk_pointer_free(&sr->headers->last_modified);
787 mk_mem_free(sr->headers);
789 if (sr->headers->content_length >= 0) {
790 mk_pointer_free(&sr->headers->content_length_p);
794 mk_pointer_reset(&sr->body);
795 mk_pointer_reset(&sr->uri);
797 if (sr->uri_twin == VAR_ON) {
798 mk_mem_free(sr->uri_processed);
801 mk_pointer_free(&sr->post_variables);
802 mk_mem_free(sr->user_uri);
803 mk_pointer_reset(&sr->query_string);
805 mk_mem_free(sr->file_info);
806 mk_mem_free(sr->virtual_user);
807 mk_mem_free(sr->script_filename);
808 mk_pointer_free(&sr->real_path);
809 mk_mem_free(sr);
812 /* Create a client request struct and put it on the
813 * main list
815 struct client_request *mk_request_client_create(int socket)
817 struct request_idx *request_index;
818 struct client_request *cr;
819 struct sched_connection *sc;
821 sc = mk_sched_get_connection(NULL, socket);
822 cr = mk_mem_malloc(sizeof(struct client_request));
824 /* IPv4 Address */
825 cr->ipv4 = &sc->ipv4;
827 cr->pipelined = FALSE;
828 cr->counter_connections = 0;
829 cr->socket = socket;
830 cr->status = MK_REQUEST_STATUS_INCOMPLETE;
831 cr->request = NULL;
833 /* creation time in unix time */
834 cr->init_time = sc->arrive_time;
836 cr->next = NULL;
837 cr->body = mk_mem_malloc(MAX_REQUEST_BODY);
838 cr->body_length = 0;
839 cr->body_pos_end = -1;
840 cr->first_method = HTTP_METHOD_UNKNOWN;
842 /* Add this request to the thread request list */
843 request_index = mk_sched_get_request_index();
844 if (!request_index->first) {
845 request_index->first = request_index->last = cr;
847 else {
848 request_index->last->next = cr;
849 request_index->last = cr;
852 /* Set again the global list */
853 mk_sched_set_request_index(request_index);
855 return cr;
858 struct client_request *mk_request_client_get(int socket)
860 struct request_idx *request_index;
861 struct client_request *cr = NULL;
863 request_index = mk_sched_get_request_index();
864 cr = request_index->first;
865 while (cr != NULL) {
866 if (cr->socket == socket) {
867 break;
869 cr = cr->next;
872 return cr;
876 * From thread sched_list_node "list", remove the client_request
877 * struct information
879 void mk_request_client_remove(int socket)
881 struct request_idx *request_index;
882 struct client_request *cr, *aux;
884 request_index = mk_sched_get_request_index();
885 cr = request_index->first;
887 while (cr) {
888 if (cr->socket == socket) {
889 if (cr == request_index->first) {
890 request_index->first = cr->next;
892 else {
893 aux = request_index->first;
894 while (aux->next != cr) {
895 aux = aux->next;
897 aux->next = cr->next;
898 if (!aux->next) {
899 request_index->last = aux;
902 break;
904 cr = cr->next;
907 mk_mem_free(cr->body);
908 mk_mem_free(cr);
910 /* Update thread index */
911 mk_sched_set_request_index(request_index);
914 struct header_toc *mk_request_header_toc_create(int len)
916 int i;
917 struct header_toc *p;
919 p = (struct header_toc *) pthread_getspecific(mk_cache_header_toc);
921 for (i = 0; i < len; i++) {
922 p[i].init = NULL;
923 p[i].end = NULL;
924 p[i].status = 0;
926 return p;
929 void mk_request_header_toc_parse(struct header_toc *toc, int toc_len, char *data, int len)
931 char *p, *l = 0;
932 int i;
934 p = data;
935 for (i = 0; i < toc_len && p && l < data + len; i++) {
936 l = strstr(p, MK_CRLF);
937 if (l) {
938 toc[i].init = p;
939 toc[i].end = l;
940 p = l + mk_crlf.len;
942 else {
943 break;
948 void mk_request_ka_next(struct client_request *cr)
950 bzero(cr->body, sizeof(cr->body));
951 cr->first_method = -1;
952 cr->body_pos_end = -1;
953 cr->body_length = 0;
954 cr->counter_connections++;
956 /* Update data for scheduler */
957 cr->init_time = log_current_utime;
958 cr->status = MK_REQUEST_STATUS_INCOMPLETE;