Improve comment
[MonkeyD.git] / src / request.c
blobf702eba546fc5a333a671b8efe717aa88e6196e3
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 "logfile.h"
52 #include "utils.h"
53 #include "header.h"
54 #include "user.h"
55 #include "method.h"
56 #include "memory.h"
57 #include "socket.h"
58 #include "cache.h"
59 #include "clock.h"
60 #include "utils.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 = read(socket, cr->body + cr->body_length,
163 MAX_REQUEST_BODY - cr->body_length);
165 if (bytes < 0) {
166 if (errno == EAGAIN) {
167 return 1;
169 else {
170 mk_request_client_remove(socket);
171 return -1;
174 if (bytes == 0) {
175 mk_request_client_remove(socket);
176 return -1;
179 if (bytes > 0) {
180 cr->body_length += bytes;
181 cr->body[cr->body_length] = '\0';
184 return bytes;
187 int mk_handler_write(int socket, struct client_request *cr)
189 int bytes, final_status = 0;
190 struct request *sr;
193 * Get node from schedule list node which contains
194 * the information regarding to the current thread
196 if (!cr) {
197 return -1;
200 if (!cr->request) {
201 if (!mk_request_parse(cr)) {
202 return -1;
206 sr = cr->request;
208 while (sr) {
209 /* Request not processed also no plugin has take some action */
210 if (sr->bytes_to_send < 0 && !sr->handled_by) {
211 final_status = mk_request_process(cr, sr);
213 /* Request with data to send by static file sender */
214 else if (sr->bytes_to_send > 0 && !sr->handled_by) {
215 bytes = SendFile(socket, cr, sr);
216 final_status = bytes;
218 else if (sr->handled_by){
219 /* FIXME: Look for loops
220 * sr->handled_by; */
224 * If we got an error, we don't want to parse
225 * and send information for another pipelined request
227 if (final_status > 0) {
228 return final_status;
230 else if (final_status <= 0) {
231 switch (final_status) {
232 case EXIT_NORMAL:
233 mk_logger_write_log(cr, sr->log, sr->host_conf);
234 if (sr->close_now == VAR_ON) {
235 return -1;
237 break;
238 case EXIT_ABORT:
239 return -1;
240 break;
244 sr = sr->next;
247 /* If we are here, is because all pipelined request were
248 * processed successfully, let's return 0;
250 return 0;
253 int mk_request_process(struct client_request *cr, struct request *s_request)
255 int status = 0;
256 struct host *host;
258 status = mk_request_header_process(s_request);
260 if (status < 0) {
261 return EXIT_ABORT;
264 switch (s_request->method) {
265 case METHOD_NOT_ALLOWED:
266 mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED, cr,
267 s_request, 1, s_request->log);
268 return EXIT_NORMAL;
269 case METHOD_NOT_FOUND:
270 mk_request_error(M_SERVER_NOT_IMPLEMENTED, cr,
271 s_request, 1, s_request->log);
272 return EXIT_NORMAL;
275 s_request->user_home = VAR_OFF;
276 s_request->log->method = s_request->method;
278 /* Valid request URI? */
279 if (s_request->uri_processed == NULL) {
280 mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request, 1,
281 s_request->log);
282 return EXIT_NORMAL;
285 /* HTTP/1.1 needs Host header */
286 if (!s_request->host.data && s_request->protocol == HTTP_PROTOCOL_11) {
287 s_request->log->final_response = M_CLIENT_BAD_REQUEST;
288 mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request, 1,
289 s_request->log);
290 return EXIT_NORMAL;
293 /* Method not allowed ? */
294 if (s_request->method == METHOD_NOT_ALLOWED) {
295 s_request->log->final_response = M_CLIENT_METHOD_NOT_ALLOWED;
296 mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED, cr, s_request, 1,
297 s_request->log);
298 return EXIT_NORMAL;
301 /* Validating protocol version */
302 if (s_request->protocol == HTTP_PROTOCOL_UNKNOWN) {
304 s_request->log->final_response = M_SERVER_HTTP_VERSION_UNSUP;
305 mk_request_error(M_SERVER_HTTP_VERSION_UNSUP, cr, s_request, 1,
306 s_request->log);
307 return EXIT_NORMAL;
310 if (s_request->host.data) {
311 host = mk_config_host_find(s_request->host);
312 if (host) {
313 s_request->host_conf = host;
315 else {
316 s_request->host_conf = config->hosts;
319 else {
320 s_request->host_conf = config->hosts;
322 s_request->log->host_conf = s_request->host_conf;
324 /* is requesting an user home directory ? */
325 if (config->user_dir) {
326 if (strncmp(s_request->uri_processed,
327 mk_user_home.data, mk_user_home.len) == 0) {
328 if (mk_user_init(cr, s_request) != 0) {
329 return EXIT_NORMAL;
334 /* Handling method requested */
335 if (s_request->method == HTTP_METHOD_POST) {
336 if ((status = mk_method_post(cr, s_request)) == -1) {
337 return status;
341 status = mk_http_init(cr, s_request);
343 #ifdef TRACE
344 MK_TRACE("HTTP Init returning %i", status);
345 #endif
347 return status;
350 /* Return a struct with method, URI , protocol version
351 and all static headers defined here sent in request */
352 int mk_request_header_process(struct request *sr)
354 int uri_init = 0, uri_end = 0;
355 char *query_init = 0;
356 int prot_init = 0, prot_end = 0, pos_sep = 0;
357 int fh_limit;
358 char *port = 0;
359 char *headers;
360 mk_pointer host;
362 /* If verification fails it will return always
363 * a bad request status
365 sr->log->final_response = M_CLIENT_BAD_REQUEST;
367 /* Method */
368 sr->method_p = mk_http_method_check_str(sr->method);
370 /* Request URI */
371 uri_init = (index(sr->body.data, ' ') - sr->body.data) + 1;
372 fh_limit = (index(sr->body.data, '\n') - sr->body.data);
374 uri_end = mk_string_search_r(sr->body.data, ' ', fh_limit) - 1;
376 if (uri_end <= 0) {
377 return -1;
380 prot_init = uri_end + 2;
382 if (uri_end < uri_init) {
383 return -1;
386 /* Query String */
387 query_init = index(sr->body.data + uri_init, '?');
388 if (query_init) {
389 int init, end;
391 init = (int) (query_init - (sr->body.data + uri_init)) + uri_init;
392 if (init <= uri_end) {
393 end = uri_end;
394 uri_end = init - 1;
396 sr->query_string = mk_pointer_create(sr->body.data,
397 init + 1, end + 1);
401 /* Request URI Part 2 */
402 sr->uri = sr->log->uri = mk_pointer_create(sr->body.data,
403 uri_init, uri_end + 1);
405 if (sr->uri.len < 1) {
406 return -1;
410 /* HTTP Version */
411 prot_end = fh_limit - 1;
412 if (prot_end != prot_init && prot_end > 0) {
413 sr->protocol = sr->log->protocol =
414 mk_http_protocol_check(sr->body.data + prot_init,
415 prot_end - prot_init);
418 headers = sr->body.data + prot_end + mk_crlf.len;
420 /* URI processed */
421 sr->uri_processed = mk_utils_hexuri_to_ascii(sr->uri);
423 if (!sr->uri_processed) {
424 sr->uri_processed = mk_pointer_to_buf(sr->uri);
425 sr->uri_twin = VAR_ON;
428 /* Creating table of content (index) for request headers */
429 int toc_len = MK_KNOWN_HEADERS;
430 int headers_len = sr->body.len - (prot_end + mk_crlf.len);
432 struct header_toc *toc = mk_request_header_toc_create(toc_len);
433 mk_request_header_toc_parse(toc, toc_len, headers, headers_len);
435 /* Host */
436 host = mk_request_header_find(toc, toc_len, headers, mk_rh_host);
438 if (host.data) {
439 if ((pos_sep = mk_string_char_search(host.data, ':', host.len)) >= 0) {
440 sr->host.data = host.data;
441 sr->host.len = pos_sep;
443 port = mk_string_copy_substr(host.data, pos_sep + 1, host.len);
444 sr->port = atoi(port);
445 mk_mem_free(port);
447 else {
448 sr->host = host; /* maybe null */
449 sr->port = config->standard_port;
452 else {
453 sr->host.data = NULL;
456 /* Looking for headers */
457 sr->accept = mk_request_header_find(toc, toc_len, headers, mk_rh_accept);
458 sr->accept_charset = mk_request_header_find(toc, toc_len, headers,
459 mk_rh_accept_charset);
460 sr->accept_encoding = mk_request_header_find(toc, toc_len, headers,
461 mk_rh_accept_encoding);
464 sr->accept_language = mk_request_header_find(toc, toc_len, headers,
465 mk_rh_accept_language);
466 sr->cookies = mk_request_header_find(toc, toc_len, headers, mk_rh_cookie);
467 sr->connection = mk_request_header_find(toc, toc_len, headers,
468 mk_rh_connection);
469 sr->referer = mk_request_header_find(toc, toc_len, headers,
470 mk_rh_referer);
471 sr->user_agent = mk_request_header_find(toc, toc_len, headers,
472 mk_rh_user_agent);
473 sr->range = mk_request_header_find(toc, toc_len, headers, mk_rh_range);
474 sr->if_modified_since = mk_request_header_find(toc, toc_len, headers,
475 mk_rh_if_modified_since);
477 /* Default Keepalive is off */
478 if (sr->protocol == HTTP_PROTOCOL_10) {
479 sr->keep_alive = VAR_OFF;
480 sr->close_now = VAR_ON;
482 else if(sr->protocol == HTTP_PROTOCOL_11) {
483 sr->keep_alive = VAR_ON;
484 sr->close_now = VAR_OFF;
487 if (sr->connection.data) {
488 if (mk_string_casestr(sr->connection.data, "Keep-Alive")) {
489 sr->keep_alive = VAR_ON;
490 sr->close_now = VAR_OFF;
492 else if(mk_string_casestr(sr->connection.data, "Close")) {
493 sr->keep_alive = VAR_OFF;
494 sr->close_now = VAR_ON;
496 else {
497 /* Set as a non-valid connection header value */
498 sr->connection.len = 0;
501 sr->log->final_response = M_HTTP_OK;
503 return 0;
506 /* Return value of some variable sent in request */
507 mk_pointer mk_request_header_find(struct header_toc * toc, int toc_len,
508 char *request_body, mk_pointer header)
510 int i;
511 mk_pointer var;
513 var.data = NULL;
514 var.len = 0;
516 /* new code */
517 if (toc) {
518 for (i = 0; i < toc_len; i++) {
519 /* status = 1 means that the toc entry was already
520 * checked by monkey
522 if (toc[i].status == 1) {
523 continue;
526 if (!toc[i].init)
527 break;
529 if (strncasecmp(toc[i].init, header.data, header.len) == 0) {
530 var.data = toc[i].init + header.len + 1;
531 var.len = toc[i].end - var.data;
532 toc[i].status = 1;
533 return var;
538 return var;
541 /* FIXME: IMPROVE access */
542 /* Look for some index.xxx in pathfile */
543 mk_pointer mk_request_index(char *pathfile)
545 unsigned long len;
546 char *file_aux = 0;
547 mk_pointer f;
548 struct indexfile *aux_index;
550 mk_pointer_reset(&f);
552 aux_index = first_index;
554 while (aux_index) {
555 m_build_buffer(&file_aux, &len, "%s%s",
556 pathfile, aux_index->indexname);
558 if (access(file_aux, F_OK) == 0) {
559 f.data = file_aux;
560 f.len = len;
561 return f;
563 mk_mem_free(file_aux);
564 aux_index = aux_index->next;
567 return f;
570 /* Send error responses */
571 void mk_request_error(int num_error, struct client_request *cr,
572 struct request *s_request, int debug,
573 struct log_info *s_log)
575 char *aux_message = 0;
576 mk_pointer message, *page = 0;
577 long n;
579 s_log->error_details.data = NULL;
581 switch (num_error) {
582 case M_CLIENT_BAD_REQUEST:
583 page = mk_request_set_default_page("Bad Request",
584 s_request->uri,
585 s_request->host_conf->
586 host_signature);
587 s_log->error_msg = request_error_msg_400;
588 break;
590 case M_CLIENT_FORBIDDEN:
591 page = mk_request_set_default_page("Forbidden",
592 s_request->uri,
593 s_request->host_conf->
594 host_signature);
595 s_log->error_msg = request_error_msg_403;
596 s_log->error_details = s_request->uri;
597 break;
599 case M_CLIENT_NOT_FOUND:
600 m_build_buffer(&message.data, &message.len,
601 "The requested URL was not found on this server.");
602 page = mk_request_set_default_page("Not Found",
603 message,
604 s_request->host_conf->
605 host_signature);
606 s_log->error_msg = request_error_msg_404;
607 s_log->error_details = s_request->uri;
609 mk_pointer_free(&message);
610 break;
612 case M_CLIENT_METHOD_NOT_ALLOWED:
613 page = mk_request_set_default_page("Method Not Allowed",
614 s_request->uri,
615 s_request->host_conf->
616 host_signature);
618 s_log->final_response = M_CLIENT_METHOD_NOT_ALLOWED;
619 s_log->error_msg = request_error_msg_405;
620 s_log->error_details = s_request->method_p;
621 break;
623 case M_CLIENT_REQUEST_TIMEOUT:
624 s_log->status = S_LOG_OFF;
625 s_log->error_msg = request_error_msg_408;
626 break;
628 case M_CLIENT_LENGTH_REQUIRED:
629 s_log->error_msg = request_error_msg_411;
630 break;
632 case M_SERVER_NOT_IMPLEMENTED:
633 page = mk_request_set_default_page("Method Not Implemented",
634 s_request->uri,
635 s_request->host_conf->
636 host_signature);
637 s_log->final_response = M_SERVER_NOT_IMPLEMENTED;
638 s_log->error_msg = request_error_msg_501;
639 s_log->error_details = s_request->method_p;
640 break;
642 case M_SERVER_INTERNAL_ERROR:
643 m_build_buffer(&message.data, &message.len,
644 "Problems found running %s ", s_request->uri);
645 page = mk_request_set_default_page("Internal Server Error",
646 message,
647 s_request->host_conf->
648 host_signature);
649 s_log->error_msg = request_error_msg_500;
651 mk_pointer_free(&message);
652 break;
654 case M_SERVER_HTTP_VERSION_UNSUP:
655 mk_pointer_reset(&message);
656 page = mk_request_set_default_page("HTTP Version Not Supported",
657 message,
658 s_request->host_conf->
659 host_signature);
660 s_log->error_msg = request_error_msg_505;
661 break;
664 s_log->final_response = num_error;
666 s_request->headers->status = num_error;
667 if (page) {
668 s_request->headers->content_length = page->len;
669 s_request->headers->content_length_p = mk_utils_int2mkp(page->len);
672 s_request->headers->location = NULL;
673 s_request->headers->cgi = SH_NOCGI;
674 s_request->headers->pconnections_left = 0;
675 mk_pointer_reset(&s_request->headers->last_modified);
677 if (aux_message)
678 mk_mem_free(aux_message);
680 if (!page) {
681 mk_pointer_reset(&s_request->headers->content_type);
683 else {
684 mk_pointer_set(&s_request->headers->content_type, "text/html\r\n");
687 mk_header_send(cr->socket, cr, s_request, s_log);
689 if (debug == 1) {
690 n = write(cr->socket, page->data, page->len);
691 mk_pointer_free(page);
692 mk_mem_free(page);
696 /* Build error page */
697 mk_pointer *mk_request_set_default_page(char *title, mk_pointer message,
698 char *signature)
700 char *temp;
701 mk_pointer *p;
703 p = mk_mem_malloc(sizeof(mk_pointer));
705 temp = mk_pointer_to_buf(message);
706 m_build_buffer(&p->data, &p->len,
707 MK_REQUEST_DEFAULT_PAGE, title, temp, signature);
708 mk_mem_free(temp);
710 return p;
713 /* Create a memory allocation in order to handle the request data */
714 struct request *mk_request_alloc()
716 struct request *request = 0;
718 request = mk_mem_malloc(sizeof(struct request));
719 request->log = mk_mem_malloc(sizeof(struct log_info));
721 request->status = VAR_OFF; /* Request not processed yet */
722 request->make_log = VAR_ON; /* build log file of this request ? */
723 request->close_now = VAR_OFF;
725 mk_pointer_reset(&request->body);
727 request->log->final_response = M_HTTP_OK;
728 request->log->status = S_LOG_ON;
729 mk_pointer_reset(&request->log->size_p);
730 mk_pointer_reset(&request->log->error_msg);
732 request->status = VAR_ON;
733 request->method = METHOD_NOT_FOUND;
735 mk_pointer_reset(&request->uri);
736 request->uri_processed = NULL;
737 request->uri_twin = VAR_OFF;
739 request->accept.data = NULL;
740 request->accept_language.data = NULL;
741 request->accept_encoding.data = NULL;
742 request->accept_charset.data = NULL;
743 request->content_length = 0;
744 request->content_type.data = NULL;
745 request->connection.data = NULL;
746 request->cookies.data = NULL;
747 request->host.data = NULL;
748 request->if_modified_since.data = NULL;
749 request->last_modified_since.data = NULL;
750 request->range.data = NULL;
751 request->referer.data = NULL;
752 request->resume.data = NULL;
753 request->user_agent.data = NULL;
755 request->post_variables.data = NULL;
757 request->user_uri = NULL;
758 mk_pointer_reset(&request->query_string);
760 request->file_info = NULL;
761 request->virtual_user = NULL;
762 request->script_filename = NULL;
763 mk_pointer_reset(&request->real_path);
764 request->host_conf = config->hosts;
766 request->loop = 0;
767 request->bytes_to_send = -1;
768 request->bytes_offset = 0;
769 request->fd_file = -1;
771 /* Response Headers */
772 request->headers = mk_header_create();
774 request->handled_by = NULL;
775 return request;
778 void mk_request_free_list(struct client_request *cr)
780 struct request *sr = 0, *before = 0;
782 /* sr = last node */
784 while (cr->request) {
785 sr = before = cr->request;
787 while (sr->next) {
788 sr = sr->next;
791 if (sr != cr->request) {
792 while (before->next != sr) {
793 before = before->next;
795 before->next = NULL;
797 else {
798 cr->request = NULL;
800 mk_request_free(sr);
802 cr->request = NULL;
805 void mk_request_free(struct request *sr)
807 /* I hate it, but I don't know another light way :( */
808 if (sr->fd_file > 0) {
809 close(sr->fd_file);
811 if (sr->headers) {
812 mk_mem_free(sr->headers->location);
813 mk_pointer_free(&sr->headers->content_length_p);
814 mk_pointer_free(&sr->headers->last_modified);
816 mk_mem_free(sr->headers->content_type);
817 headers->content_type never it's allocated
818 with malloc or something, so we don't need
819 to free it, the value has been freed before
820 in M_METHOD_Get_and_Head(struct request *sr)
822 this BUG was reported by gentoo team.. thanks guys XD
825 mk_mem_free(sr->headers);
829 if (sr->log) {
831 * We do not free log->size_p, as if it was
832 * used due to an error, it points to the
833 * same memory block than header->content_length_p
834 * points to, we just reset it.
836 mk_pointer_reset(&sr->log->size_p);
839 * sr->log->error_msg just point to
840 * local data on request.c, no
841 * dynamic allocation is made
844 mk_mem_free(sr->log);
847 mk_pointer_reset(&sr->body);
848 mk_pointer_reset(&sr->uri);
850 if (sr->uri_twin == VAR_ON) {
851 mk_mem_free(sr->uri_processed);
854 mk_pointer_free(&sr->post_variables);
855 mk_mem_free(sr->user_uri);
856 mk_pointer_reset(&sr->query_string);
858 mk_mem_free(sr->file_info);
859 mk_mem_free(sr->virtual_user);
860 mk_mem_free(sr->script_filename);
861 mk_pointer_free(&sr->real_path);
862 mk_mem_free(sr);
865 /* Create a client request struct and put it on the
866 * main list
868 struct client_request *mk_request_client_create(int socket)
870 struct request_idx *request_index;
871 struct client_request *cr;
872 struct sched_connection *sc;
874 sc = mk_sched_get_connection(NULL, socket);
875 cr = mk_mem_malloc(sizeof(struct client_request));
877 /* IPv4 Address */
878 cr->ipv4 = &sc->ipv4;
880 cr->pipelined = FALSE;
881 cr->counter_connections = 0;
882 cr->socket = socket;
883 cr->status = MK_REQUEST_STATUS_INCOMPLETE;
884 cr->request = NULL;
886 /* creation time in unix time */
887 cr->init_time = sc->arrive_time;
889 cr->next = NULL;
890 cr->body = mk_mem_malloc(MAX_REQUEST_BODY);
891 cr->body_length = 0;
892 cr->body_pos_end = -1;
893 cr->first_method = HTTP_METHOD_UNKNOWN;
895 request_index = mk_sched_get_request_index();
896 if (!request_index->first) {
897 request_index->first = request_index->last = cr;
899 else {
900 request_index->last->next = cr;
901 request_index->last = cr;
903 mk_sched_set_request_index(request_index);
905 return cr;
908 struct client_request *mk_request_client_get(int socket)
910 struct request_idx *request_index;
911 struct client_request *cr = NULL;
913 request_index = mk_sched_get_request_index();
914 cr = request_index->first;
915 while (cr != NULL) {
916 if (cr->socket == socket) {
917 break;
919 cr = cr->next;
922 return cr;
926 * From thread sched_list_node "list", remove the client_request
927 * struct information
929 struct client_request *mk_request_client_remove(int socket)
931 struct request_idx *request_index;
932 struct client_request *cr, *aux;
934 request_index = mk_sched_get_request_index();
935 cr = request_index->first;
937 while (cr) {
938 if (cr->socket == socket) {
939 if (cr == request_index->first) {
940 request_index->first = cr->next;
942 else {
943 aux = request_index->first;
944 while (aux->next != cr) {
945 aux = aux->next;
947 aux->next = cr->next;
948 if (!aux->next) {
949 request_index->last = aux;
952 break;
954 cr = cr->next;
957 //mk_pointer_free(&cr->ip);
958 mk_mem_free(cr->body);
959 mk_mem_free(cr);
961 return NULL;
964 struct header_toc *mk_request_header_toc_create(int len)
966 int i;
967 struct header_toc *p;
969 p = (struct header_toc *) pthread_getspecific(mk_cache_header_toc);
971 for (i = 0; i < len; i++) {
972 p[i].init = NULL;
973 p[i].end = NULL;
974 p[i].status = 0;
976 return p;
979 void mk_request_header_toc_parse(struct header_toc *toc, int toc_len, char *data, int len)
981 char *p, *l = 0;
982 int i;
984 p = data;
985 for (i = 0; i < toc_len && p && l < data + len; i++) {
986 l = strstr(p, MK_CRLF);
987 if (l) {
988 toc[i].init = p;
989 toc[i].end = l;
990 p = l + mk_crlf.len;
992 else {
993 break;
998 void mk_request_ka_next(struct client_request *cr)
1000 bzero(cr->body, sizeof(cr->body));
1001 cr->first_method = -1;
1002 cr->body_pos_end = -1;
1003 cr->body_length = 0;
1004 cr->counter_connections++;
1006 /* Update data for scheduler */
1007 cr->init_time = log_current_utime;
1008 cr->status = MK_REQUEST_STATUS_INCOMPLETE;