Fix data type for uri_len in http.c
[MonkeyD.git] / src / request.c
blob3cf08f422bbe4b81f7848a8faffd1a2e07fbf07e
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;
220 * If we got an error, we don't want to parse
221 * and send information for another pipelined request
223 if (final_status > 0) {
224 return final_status;
226 else if (final_status <= 0) {
227 switch (final_status) {
228 case EXIT_NORMAL:
229 case EXIT_ERROR:
230 mk_logger_write_log(cr, sr->log, sr->host_conf);
231 if (sr->close_now == VAR_ON) {
232 return -1;
234 break;
235 case EXIT_ABORT:
236 return -1;
237 break;
241 sr = sr->next;
244 /* If we are here, is because all pipelined request were
245 * processed successfully, let's return 0;
247 return 0;
250 int mk_request_process(struct client_request *cr, struct request *s_request)
252 int status = 0;
253 struct host *host;
255 status = mk_request_header_process(s_request);
257 if (status < 0) {
258 return EXIT_ABORT;
261 switch (s_request->method) {
262 case METHOD_NOT_ALLOWED:
263 mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED, cr,
264 s_request, 1, s_request->log);
265 return EXIT_NORMAL;
266 case METHOD_NOT_FOUND:
267 mk_request_error(M_SERVER_NOT_IMPLEMENTED, cr,
268 s_request, 1, s_request->log);
269 return EXIT_NORMAL;
272 s_request->user_home = VAR_OFF;
273 s_request->log->method = s_request->method;
275 /* Valid request URI? */
276 if (s_request->uri_processed == NULL) {
277 mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request, 1,
278 s_request->log);
279 return EXIT_NORMAL;
281 if (s_request->uri_processed[0] != '/') {
282 mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request, 1,
283 s_request->log);
284 return EXIT_NORMAL;
287 /* HTTP/1.1 needs Host header */
288 if (!s_request->host.data && s_request->protocol == HTTP_PROTOCOL_11) {
289 s_request->log->final_response = M_CLIENT_BAD_REQUEST;
290 mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request, 1,
291 s_request->log);
292 return EXIT_NORMAL;
295 /* Method not allowed ? */
296 if (s_request->method == METHOD_NOT_ALLOWED) {
297 s_request->log->final_response = M_CLIENT_METHOD_NOT_ALLOWED;
298 mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED, cr, s_request, 1,
299 s_request->log);
300 return EXIT_NORMAL;
303 /* Validating protocol version */
304 if (s_request->protocol == HTTP_PROTOCOL_UNKNOWN) {
306 s_request->log->final_response = M_SERVER_HTTP_VERSION_UNSUP;
307 mk_request_error(M_SERVER_HTTP_VERSION_UNSUP, cr, s_request, 1,
308 s_request->log);
309 return EXIT_NORMAL;
312 if (s_request->host.data) {
313 host = mk_config_host_find(s_request->host);
314 if (host) {
315 s_request->host_conf = host;
317 else {
318 s_request->host_conf = config->hosts;
321 else {
322 s_request->host_conf = config->hosts;
324 s_request->log->host_conf = s_request->host_conf;
326 /* is requesting an user home directory ? */
327 if (config->user_dir) {
328 if (strncmp(s_request->uri_processed,
329 mk_user_home.data, mk_user_home.len) == 0) {
330 if (mk_user_init(cr, s_request) != 0) {
331 return EXIT_NORMAL;
336 /* Handling method requested */
337 if (s_request->method == HTTP_METHOD_POST) {
338 if ((status = mk_method_post(cr, s_request)) == -1) {
339 return status;
343 status = mk_http_init(cr, s_request);
345 #ifdef TRACE
346 MK_TRACE("HTTP Init returning %i", status);
347 #endif
349 return status;
352 /* Return a struct with method, URI , protocol version
353 and all static headers defined here sent in request */
354 int mk_request_header_process(struct request *sr)
356 int uri_init = 0, uri_end = 0;
357 char *query_init = 0;
358 int prot_init = 0, prot_end = 0, pos_sep = 0;
359 int fh_limit;
360 char *port = 0;
361 char *headers;
362 mk_pointer host;
364 /* If verification fails it will return always
365 * a bad request status
367 sr->log->final_response = M_CLIENT_BAD_REQUEST;
369 /* Method */
370 sr->method_p = mk_http_method_check_str(sr->method);
372 /* Request URI */
373 uri_init = (index(sr->body.data, ' ') - sr->body.data) + 1;
374 fh_limit = (index(sr->body.data, '\n') - sr->body.data);
376 uri_end = mk_string_search_r(sr->body.data, ' ', fh_limit) - 1;
378 if (uri_end <= 0) {
379 #ifdef TRACE
380 MK_TRACE("Error, first header bad formed");
381 #endif
382 return -1;
385 prot_init = uri_end + 2;
387 if (uri_end < uri_init) {
388 return -1;
391 /* Query String */
392 query_init = index(sr->body.data + uri_init, '?');
393 if (query_init) {
394 int init, end;
396 init = (int) (query_init - (sr->body.data + uri_init)) + uri_init;
397 if (init <= uri_end) {
398 end = uri_end;
399 uri_end = init - 1;
401 sr->query_string = mk_pointer_create(sr->body.data,
402 init + 1, end + 1);
406 /* Request URI Part 2 */
407 sr->uri = sr->log->uri = mk_pointer_create(sr->body.data,
408 uri_init, uri_end + 1);
410 if (sr->uri.len < 1) {
411 return -1;
414 /* HTTP Version */
415 prot_end = fh_limit - 1;
416 if (prot_end != prot_init && prot_end > 0) {
417 sr->protocol = sr->log->protocol =
418 mk_http_protocol_check(sr->body.data + prot_init,
419 prot_end - prot_init);
422 headers = sr->body.data + prot_end + mk_crlf.len;
424 /* URI processed */
425 sr->uri_processed = mk_utils_hexuri_to_ascii(sr->uri);
427 if (!sr->uri_processed) {
428 sr->uri_processed = mk_pointer_to_buf(sr->uri);
429 sr->uri_twin = VAR_ON;
432 /* Creating table of content (index) for request headers */
433 int toc_len = MK_KNOWN_HEADERS;
434 int headers_len = sr->body.len - (prot_end + mk_crlf.len);
436 struct header_toc *toc = mk_request_header_toc_create(toc_len);
437 mk_request_header_toc_parse(toc, toc_len, headers, headers_len);
439 /* Host */
440 host = mk_request_header_find(toc, toc_len, headers, mk_rh_host);
442 if (host.data) {
443 if ((pos_sep = mk_string_char_search(host.data, ':', host.len)) >= 0) {
444 sr->host.data = host.data;
445 sr->host.len = pos_sep;
447 port = mk_string_copy_substr(host.data, pos_sep + 1, host.len);
448 sr->port = atoi(port);
449 mk_mem_free(port);
451 else {
452 sr->host = host; /* maybe null */
453 sr->port = config->standard_port;
456 else {
457 sr->host.data = NULL;
460 /* Looking for headers */
461 sr->accept = mk_request_header_find(toc, toc_len, headers, mk_rh_accept);
462 sr->accept_charset = mk_request_header_find(toc, toc_len, headers,
463 mk_rh_accept_charset);
464 sr->accept_encoding = mk_request_header_find(toc, toc_len, headers,
465 mk_rh_accept_encoding);
468 sr->accept_language = mk_request_header_find(toc, toc_len, headers,
469 mk_rh_accept_language);
470 sr->cookies = mk_request_header_find(toc, toc_len, headers, mk_rh_cookie);
471 sr->connection = mk_request_header_find(toc, toc_len, headers,
472 mk_rh_connection);
473 sr->referer = mk_request_header_find(toc, toc_len, headers,
474 mk_rh_referer);
475 sr->user_agent = mk_request_header_find(toc, toc_len, headers,
476 mk_rh_user_agent);
477 sr->range = mk_request_header_find(toc, toc_len, headers, mk_rh_range);
478 sr->if_modified_since = mk_request_header_find(toc, toc_len, headers,
479 mk_rh_if_modified_since);
481 /* Default Keepalive is off */
482 if (sr->protocol == HTTP_PROTOCOL_10) {
483 sr->keep_alive = VAR_OFF;
484 sr->close_now = VAR_ON;
486 else if(sr->protocol == HTTP_PROTOCOL_11) {
487 sr->keep_alive = VAR_ON;
488 sr->close_now = VAR_OFF;
491 if (sr->connection.data) {
492 if (mk_string_casestr(sr->connection.data, "Keep-Alive")) {
493 sr->keep_alive = VAR_ON;
494 sr->close_now = VAR_OFF;
496 else if(mk_string_casestr(sr->connection.data, "Close")) {
497 sr->keep_alive = VAR_OFF;
498 sr->close_now = VAR_ON;
500 else {
501 /* Set as a non-valid connection header value */
502 sr->connection.len = 0;
505 sr->log->final_response = M_HTTP_OK;
507 return 0;
510 /* Return value of some variable sent in request */
511 mk_pointer mk_request_header_find(struct header_toc * toc, int toc_len,
512 char *request_body, mk_pointer header)
514 int i;
515 mk_pointer var;
517 var.data = NULL;
518 var.len = 0;
520 if (toc) {
521 for (i = 0; i < toc_len; i++) {
522 /* status = 1 means that the toc entry was already
523 * checked by monkey
525 if (toc[i].status == 1) {
526 continue;
529 if (!toc[i].init)
530 break;
532 if (strncasecmp(toc[i].init, header.data, header.len) == 0) {
533 var.data = toc[i].init + header.len + 1;
534 var.len = toc[i].end - var.data;
535 toc[i].status = 1;
536 return var;
541 return var;
544 /* FIXME: IMPROVE access */
545 /* Look for some index.xxx in pathfile */
546 mk_pointer mk_request_index(char *pathfile)
548 unsigned long len;
549 char *file_aux = 0;
550 mk_pointer f;
551 struct indexfile *aux_index;
553 mk_pointer_reset(&f);
555 aux_index = first_index;
557 while (aux_index) {
558 m_build_buffer(&file_aux, &len, "%s%s",
559 pathfile, aux_index->indexname);
561 if (access(file_aux, F_OK) == 0) {
562 f.data = file_aux;
563 f.len = len;
564 return f;
566 mk_mem_free(file_aux);
567 aux_index = aux_index->next;
570 return f;
573 /* Send error responses */
574 void mk_request_error(int num_error, struct client_request *cr,
575 struct request *s_request, int debug,
576 struct log_info *s_log)
578 char *aux_message = 0;
579 mk_pointer message, *page = 0;
580 long n;
582 s_log->error_details.data = NULL;
584 switch (num_error) {
585 case M_CLIENT_BAD_REQUEST:
586 page = mk_request_set_default_page("Bad Request",
587 s_request->uri,
588 s_request->host_conf->
589 host_signature);
590 s_log->error_msg = request_error_msg_400;
591 break;
593 case M_CLIENT_FORBIDDEN:
594 page = mk_request_set_default_page("Forbidden",
595 s_request->uri,
596 s_request->host_conf->
597 host_signature);
598 s_log->error_msg = request_error_msg_403;
599 s_log->error_details = s_request->uri;
600 break;
602 case M_CLIENT_NOT_FOUND:
603 m_build_buffer(&message.data, &message.len,
604 "The requested URL was not found on this server.");
605 page = mk_request_set_default_page("Not Found",
606 message,
607 s_request->host_conf->
608 host_signature);
609 s_log->error_msg = request_error_msg_404;
610 s_log->error_details = s_request->uri;
612 mk_pointer_free(&message);
613 break;
615 case M_CLIENT_METHOD_NOT_ALLOWED:
616 page = mk_request_set_default_page("Method Not Allowed",
617 s_request->uri,
618 s_request->host_conf->
619 host_signature);
621 s_log->final_response = M_CLIENT_METHOD_NOT_ALLOWED;
622 s_log->error_msg = request_error_msg_405;
623 s_log->error_details = s_request->method_p;
624 break;
626 case M_CLIENT_REQUEST_TIMEOUT:
627 s_log->status = S_LOG_OFF;
628 s_log->error_msg = request_error_msg_408;
629 break;
631 case M_CLIENT_LENGTH_REQUIRED:
632 s_log->error_msg = request_error_msg_411;
633 break;
635 case M_SERVER_NOT_IMPLEMENTED:
636 page = mk_request_set_default_page("Method Not Implemented",
637 s_request->uri,
638 s_request->host_conf->
639 host_signature);
640 s_log->final_response = M_SERVER_NOT_IMPLEMENTED;
641 s_log->error_msg = request_error_msg_501;
642 s_log->error_details = s_request->method_p;
643 break;
645 case M_SERVER_INTERNAL_ERROR:
646 m_build_buffer(&message.data, &message.len,
647 "Problems found running %s ", s_request->uri);
648 page = mk_request_set_default_page("Internal Server Error",
649 message,
650 s_request->host_conf->
651 host_signature);
652 s_log->error_msg = request_error_msg_500;
654 mk_pointer_free(&message);
655 break;
657 case M_SERVER_HTTP_VERSION_UNSUP:
658 mk_pointer_reset(&message);
659 page = mk_request_set_default_page("HTTP Version Not Supported",
660 message,
661 s_request->host_conf->
662 host_signature);
663 s_log->error_msg = request_error_msg_505;
664 break;
667 s_log->final_response = num_error;
669 s_request->headers->status = num_error;
670 if (page) {
671 s_request->headers->content_length = page->len;
672 s_request->headers->content_length_p = mk_utils_int2mkp(page->len);
675 s_request->headers->location = NULL;
676 s_request->headers->cgi = SH_NOCGI;
677 s_request->headers->pconnections_left = 0;
678 mk_pointer_reset(&s_request->headers->last_modified);
680 if (aux_message)
681 mk_mem_free(aux_message);
683 if (!page) {
684 mk_pointer_reset(&s_request->headers->content_type);
686 else {
687 mk_pointer_set(&s_request->headers->content_type, "text/html\r\n");
690 mk_header_send(cr->socket, cr, s_request, s_log);
692 if (debug == 1) {
693 n = write(cr->socket, page->data, page->len);
694 mk_pointer_free(page);
695 mk_mem_free(page);
699 /* Build error page */
700 mk_pointer *mk_request_set_default_page(char *title, mk_pointer message,
701 char *signature)
703 char *temp;
704 mk_pointer *p;
706 p = mk_mem_malloc(sizeof(mk_pointer));
708 temp = mk_pointer_to_buf(message);
709 m_build_buffer(&p->data, &p->len,
710 MK_REQUEST_DEFAULT_PAGE, title, temp, signature);
711 mk_mem_free(temp);
713 return p;
716 /* Create a memory allocation in order to handle the request data */
717 struct request *mk_request_alloc()
719 struct request *request = 0;
721 request = mk_mem_malloc(sizeof(struct request));
722 request->log = mk_mem_malloc(sizeof(struct log_info));
724 request->status = VAR_OFF; /* Request not processed yet */
725 request->make_log = VAR_ON; /* build log file of this request ? */
726 request->close_now = VAR_OFF;
728 mk_pointer_reset(&request->body);
730 request->log->final_response = M_HTTP_OK;
731 request->log->status = S_LOG_ON;
732 mk_pointer_reset(&request->log->size_p);
733 mk_pointer_reset(&request->log->error_msg);
735 request->status = VAR_ON;
736 request->method = METHOD_NOT_FOUND;
738 mk_pointer_reset(&request->uri);
739 request->uri_processed = NULL;
740 request->uri_twin = VAR_OFF;
742 request->accept.data = NULL;
743 request->accept_language.data = NULL;
744 request->accept_encoding.data = NULL;
745 request->accept_charset.data = NULL;
746 request->content_length = 0;
747 request->content_type.data = NULL;
748 request->connection.data = NULL;
749 request->cookies.data = NULL;
750 request->host.data = NULL;
751 request->if_modified_since.data = NULL;
752 request->last_modified_since.data = NULL;
753 request->range.data = NULL;
754 request->referer.data = NULL;
755 request->resume.data = NULL;
756 request->user_agent.data = NULL;
758 request->post_variables.data = NULL;
760 request->user_uri = NULL;
761 mk_pointer_reset(&request->query_string);
763 request->file_info = NULL;
764 request->virtual_user = NULL;
765 request->script_filename = NULL;
766 mk_pointer_reset(&request->real_path);
767 request->host_conf = config->hosts;
769 request->loop = 0;
770 request->bytes_to_send = -1;
771 request->bytes_offset = 0;
772 request->fd_file = -1;
774 /* Response Headers */
775 request->headers = mk_header_create();
777 request->handled_by = NULL;
778 return request;
781 void mk_request_free_list(struct client_request *cr)
783 struct request *sr = 0, *before = 0;
785 /* sr = last node */
787 while (cr->request) {
788 sr = before = cr->request;
790 while (sr->next) {
791 sr = sr->next;
794 if (sr != cr->request) {
795 while (before->next != sr) {
796 before = before->next;
798 before->next = NULL;
800 else {
801 cr->request = NULL;
803 mk_request_free(sr);
805 cr->request = NULL;
808 void mk_request_free(struct request *sr)
810 /* I hate it, but I don't know another light way :( */
811 if (sr->fd_file > 0) {
812 close(sr->fd_file);
814 if (sr->headers) {
815 mk_mem_free(sr->headers->location);
816 mk_pointer_free(&sr->headers->content_length_p);
817 mk_pointer_free(&sr->headers->last_modified);
818 mk_mem_free(sr->headers);
822 if (sr->log) {
824 * We do not free log->size_p, as if it was
825 * used due to an error, it points to the
826 * same memory block than header->content_length_p
827 * points to, we just reset it.
829 mk_pointer_reset(&sr->log->size_p);
832 * sr->log->error_msg just point to
833 * local data on request.c, no
834 * dynamic allocation is made
837 mk_mem_free(sr->log);
840 mk_pointer_reset(&sr->body);
841 mk_pointer_reset(&sr->uri);
843 if (sr->uri_twin == VAR_ON) {
844 mk_mem_free(sr->uri_processed);
847 mk_pointer_free(&sr->post_variables);
848 mk_mem_free(sr->user_uri);
849 mk_pointer_reset(&sr->query_string);
851 mk_mem_free(sr->file_info);
852 mk_mem_free(sr->virtual_user);
853 mk_mem_free(sr->script_filename);
854 mk_pointer_free(&sr->real_path);
855 mk_mem_free(sr);
858 /* Create a client request struct and put it on the
859 * main list
861 struct client_request *mk_request_client_create(int socket)
863 struct request_idx *request_index;
864 struct client_request *cr;
865 struct sched_connection *sc;
867 sc = mk_sched_get_connection(NULL, socket);
868 cr = mk_mem_malloc(sizeof(struct client_request));
870 /* IPv4 Address */
871 cr->ipv4 = &sc->ipv4;
873 cr->pipelined = FALSE;
874 cr->counter_connections = 0;
875 cr->socket = socket;
876 cr->status = MK_REQUEST_STATUS_INCOMPLETE;
877 cr->request = NULL;
879 /* creation time in unix time */
880 cr->init_time = sc->arrive_time;
882 cr->next = NULL;
883 cr->body = mk_mem_malloc(MAX_REQUEST_BODY);
884 cr->body_length = 0;
885 cr->body_pos_end = -1;
886 cr->first_method = HTTP_METHOD_UNKNOWN;
888 request_index = mk_sched_get_request_index();
889 if (!request_index->first) {
890 request_index->first = request_index->last = cr;
892 else {
893 request_index->last->next = cr;
894 request_index->last = cr;
896 mk_sched_set_request_index(request_index);
898 return cr;
901 struct client_request *mk_request_client_get(int socket)
903 struct request_idx *request_index;
904 struct client_request *cr = NULL;
906 request_index = mk_sched_get_request_index();
907 cr = request_index->first;
908 while (cr != NULL) {
909 if (cr->socket == socket) {
910 break;
912 cr = cr->next;
915 return cr;
919 * From thread sched_list_node "list", remove the client_request
920 * struct information
922 struct client_request *mk_request_client_remove(int socket)
924 struct request_idx *request_index;
925 struct client_request *cr, *aux;
927 request_index = mk_sched_get_request_index();
928 cr = request_index->first;
930 while (cr) {
931 if (cr->socket == socket) {
932 if (cr == request_index->first) {
933 request_index->first = cr->next;
935 else {
936 aux = request_index->first;
937 while (aux->next != cr) {
938 aux = aux->next;
940 aux->next = cr->next;
941 if (!aux->next) {
942 request_index->last = aux;
945 break;
947 cr = cr->next;
950 mk_mem_free(cr->body);
951 mk_mem_free(cr);
953 return NULL;
956 struct header_toc *mk_request_header_toc_create(int len)
958 int i;
959 struct header_toc *p;
961 p = (struct header_toc *) pthread_getspecific(mk_cache_header_toc);
963 for (i = 0; i < len; i++) {
964 p[i].init = NULL;
965 p[i].end = NULL;
966 p[i].status = 0;
968 return p;
971 void mk_request_header_toc_parse(struct header_toc *toc, int toc_len, char *data, int len)
973 char *p, *l = 0;
974 int i;
976 p = data;
977 for (i = 0; i < toc_len && p && l < data + len; i++) {
978 l = strstr(p, MK_CRLF);
979 if (l) {
980 toc[i].init = p;
981 toc[i].end = l;
982 p = l + mk_crlf.len;
984 else {
985 break;
990 void mk_request_ka_next(struct client_request *cr)
992 bzero(cr->body, sizeof(cr->body));
993 cr->first_method = -1;
994 cr->body_pos_end = -1;
995 cr->body_length = 0;
996 cr->counter_connections++;
998 /* Update data for scheduler */
999 cr->init_time = log_current_utime;
1000 cr->status = MK_REQUEST_STATUS_INCOMPLETE;