Liana Plugin: remove cast
[MonkeyD.git] / src / request.c
blob44d9c3b9aa60c6c1e320e50461b157a800b9c9a4
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 = 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;
207 while (sr) {
208 /* Request not processed also no plugin has take some action */
209 if (sr->bytes_to_send < 0 && !sr->handled_by) {
210 final_status = mk_request_process(cr, sr);
212 /* Request with data to send by static file sender */
213 else if (sr->bytes_to_send > 0 && !sr->handled_by) {
214 final_status = bytes = mk_http_send_file(cr, sr);
218 * If we got an error, we don't want to parse
219 * and send information for another pipelined request
221 if (final_status > 0) {
222 return final_status;
224 else if (final_status <= 0) {
225 switch (final_status) {
226 case EXIT_NORMAL:
227 case EXIT_ERROR:
228 mk_logger_write_log(cr, sr->log, sr->host_conf);
229 if (sr->close_now == VAR_ON) {
230 return -1;
232 break;
233 case EXIT_ABORT:
234 return -1;
235 break;
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,
262 s_request, 1, s_request->log);
263 return EXIT_NORMAL;
264 case METHOD_NOT_FOUND:
265 mk_request_error(M_SERVER_NOT_IMPLEMENTED, cr,
266 s_request, 1, s_request->log);
267 return EXIT_NORMAL;
270 s_request->user_home = VAR_OFF;
271 s_request->log->method = s_request->method;
273 /* Valid request URI? */
274 if (s_request->uri_processed == NULL) {
275 mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request, 1,
276 s_request->log);
277 return EXIT_NORMAL;
280 /* HTTP/1.1 needs Host header */
281 if (!s_request->host.data && s_request->protocol == HTTP_PROTOCOL_11) {
282 s_request->log->final_response = M_CLIENT_BAD_REQUEST;
283 mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request, 1,
284 s_request->log);
285 return EXIT_NORMAL;
288 /* Method not allowed ? */
289 if (s_request->method == METHOD_NOT_ALLOWED) {
290 s_request->log->final_response = M_CLIENT_METHOD_NOT_ALLOWED;
291 mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED, cr, s_request, 1,
292 s_request->log);
293 return EXIT_NORMAL;
296 /* Validating protocol version */
297 if (s_request->protocol == HTTP_PROTOCOL_UNKNOWN) {
299 s_request->log->final_response = M_SERVER_HTTP_VERSION_UNSUP;
300 mk_request_error(M_SERVER_HTTP_VERSION_UNSUP, cr, s_request, 1,
301 s_request->log);
302 return EXIT_NORMAL;
305 if (s_request->host.data) {
306 host = mk_config_host_find(s_request->host);
307 if (host) {
308 s_request->host_conf = host;
310 else {
311 s_request->host_conf = config->hosts;
314 else {
315 s_request->host_conf = config->hosts;
317 s_request->log->host_conf = s_request->host_conf;
319 /* is requesting an user home directory ? */
320 if (config->user_dir) {
321 if (strncmp(s_request->uri_processed,
322 mk_user_home.data, mk_user_home.len) == 0) {
323 if (mk_user_init(cr, s_request) != 0) {
324 return EXIT_NORMAL;
329 /* Handling method requested */
330 if (s_request->method == HTTP_METHOD_POST) {
331 if ((status = mk_method_post(cr, s_request)) == -1) {
332 return status;
336 status = mk_http_init(cr, s_request);
338 #ifdef TRACE
339 MK_TRACE("HTTP Init returning %i", status);
340 #endif
342 return status;
345 /* Return a struct with method, URI , protocol version
346 and all static headers defined here sent in request */
347 int mk_request_header_process(struct request *sr)
349 int uri_init = 0, uri_end = 0;
350 char *query_init = 0;
351 int prot_init = 0, prot_end = 0, pos_sep = 0;
352 int fh_limit;
353 char *port = 0;
354 char *headers;
355 mk_pointer host;
357 /* If verification fails it will return always
358 * a bad request status
360 sr->log->final_response = M_CLIENT_BAD_REQUEST;
362 /* Method */
363 sr->method_p = mk_http_method_check_str(sr->method);
365 /* Request URI */
366 uri_init = (index(sr->body.data, ' ') - sr->body.data) + 1;
367 fh_limit = (index(sr->body.data, '\n') - sr->body.data);
369 uri_end = mk_string_search_r(sr->body.data, ' ', fh_limit) - 1;
371 if (uri_end <= 0) {
372 return -1;
375 prot_init = uri_end + 2;
377 if (uri_end < uri_init) {
378 return -1;
381 /* Query String */
382 query_init = index(sr->body.data + uri_init, '?');
383 if (query_init) {
384 int init, end;
386 init = (int) (query_init - (sr->body.data + uri_init)) + uri_init;
387 if (init <= uri_end) {
388 end = uri_end;
389 uri_end = init - 1;
391 sr->query_string = mk_pointer_create(sr->body.data,
392 init + 1, end + 1);
396 /* Request URI Part 2 */
397 sr->uri = sr->log->uri = mk_pointer_create(sr->body.data,
398 uri_init, uri_end + 1);
400 if (sr->uri.len < 1) {
401 return -1;
405 /* HTTP Version */
406 prot_end = fh_limit - 1;
407 if (prot_end != prot_init && prot_end > 0) {
408 sr->protocol = sr->log->protocol =
409 mk_http_protocol_check(sr->body.data + prot_init,
410 prot_end - prot_init);
413 headers = sr->body.data + prot_end + mk_crlf.len;
415 /* URI processed */
416 sr->uri_processed = mk_utils_hexuri_to_ascii(sr->uri);
418 if (!sr->uri_processed) {
419 sr->uri_processed = mk_pointer_to_buf(sr->uri);
420 sr->uri_twin = VAR_ON;
423 /* Creating table of content (index) for request headers */
424 int toc_len = MK_KNOWN_HEADERS;
425 int headers_len = sr->body.len - (prot_end + mk_crlf.len);
427 struct header_toc *toc = mk_request_header_toc_create(toc_len);
428 mk_request_header_toc_parse(toc, toc_len, headers, headers_len);
430 /* Host */
431 host = mk_request_header_find(toc, toc_len, headers, mk_rh_host);
433 if (host.data) {
434 if ((pos_sep = mk_string_char_search(host.data, ':', host.len)) >= 0) {
435 sr->host.data = host.data;
436 sr->host.len = pos_sep;
438 port = mk_string_copy_substr(host.data, pos_sep + 1, host.len);
439 sr->port = atoi(port);
440 mk_mem_free(port);
442 else {
443 sr->host = host; /* maybe null */
444 sr->port = config->standard_port;
447 else {
448 sr->host.data = NULL;
451 /* Looking for headers */
452 sr->accept = mk_request_header_find(toc, toc_len, headers, mk_rh_accept);
453 sr->accept_charset = mk_request_header_find(toc, toc_len, headers,
454 mk_rh_accept_charset);
455 sr->accept_encoding = mk_request_header_find(toc, toc_len, headers,
456 mk_rh_accept_encoding);
459 sr->accept_language = mk_request_header_find(toc, toc_len, headers,
460 mk_rh_accept_language);
461 sr->cookies = mk_request_header_find(toc, toc_len, headers, mk_rh_cookie);
462 sr->connection = mk_request_header_find(toc, toc_len, headers,
463 mk_rh_connection);
464 sr->referer = mk_request_header_find(toc, toc_len, headers,
465 mk_rh_referer);
466 sr->user_agent = mk_request_header_find(toc, toc_len, headers,
467 mk_rh_user_agent);
468 sr->range = mk_request_header_find(toc, toc_len, headers, mk_rh_range);
469 sr->if_modified_since = mk_request_header_find(toc, toc_len, headers,
470 mk_rh_if_modified_since);
472 /* Default Keepalive is off */
473 if (sr->protocol == HTTP_PROTOCOL_10) {
474 sr->keep_alive = VAR_OFF;
475 sr->close_now = VAR_ON;
477 else if(sr->protocol == HTTP_PROTOCOL_11) {
478 sr->keep_alive = VAR_ON;
479 sr->close_now = VAR_OFF;
482 if (sr->connection.data) {
483 if (mk_string_casestr(sr->connection.data, "Keep-Alive")) {
484 sr->keep_alive = VAR_ON;
485 sr->close_now = VAR_OFF;
487 else if(mk_string_casestr(sr->connection.data, "Close")) {
488 sr->keep_alive = VAR_OFF;
489 sr->close_now = VAR_ON;
491 else {
492 /* Set as a non-valid connection header value */
493 sr->connection.len = 0;
496 sr->log->final_response = M_HTTP_OK;
498 return 0;
501 /* Return value of some variable sent in request */
502 mk_pointer mk_request_header_find(struct header_toc * toc, int toc_len,
503 char *request_body, mk_pointer header)
505 int i;
506 mk_pointer var;
508 var.data = NULL;
509 var.len = 0;
511 if (toc) {
512 for (i = 0; i < toc_len; i++) {
513 /* status = 1 means that the toc entry was already
514 * checked by monkey
516 if (toc[i].status == 1) {
517 continue;
520 if (!toc[i].init)
521 break;
523 if (strncasecmp(toc[i].init, header.data, header.len) == 0) {
524 var.data = toc[i].init + header.len + 1;
525 var.len = toc[i].end - var.data;
526 toc[i].status = 1;
527 return var;
532 return var;
535 /* FIXME: IMPROVE access */
536 /* Look for some index.xxx in pathfile */
537 mk_pointer mk_request_index(char *pathfile)
539 unsigned long len;
540 char *file_aux = 0;
541 mk_pointer f;
542 struct indexfile *aux_index;
544 mk_pointer_reset(&f);
546 aux_index = first_index;
548 while (aux_index) {
549 mk_string_build(&file_aux, &len, "%s%s",
550 pathfile, aux_index->indexname);
552 if (access(file_aux, F_OK) == 0) {
553 f.data = file_aux;
554 f.len = len;
555 return f;
557 mk_mem_free(file_aux);
558 aux_index = aux_index->next;
561 return f;
564 /* Send error responses */
565 void mk_request_error(int num_error, struct client_request *cr,
566 struct request *s_request, int debug,
567 struct log_info *s_log)
569 char *aux_message = 0;
570 mk_pointer message, *page = 0;
571 long n;
573 s_log->error_details.data = NULL;
575 switch (num_error) {
576 case M_CLIENT_BAD_REQUEST:
577 page = mk_request_set_default_page("Bad Request",
578 s_request->uri,
579 s_request->host_conf->
580 host_signature);
581 s_log->error_msg = request_error_msg_400;
582 break;
584 case M_CLIENT_FORBIDDEN:
585 page = mk_request_set_default_page("Forbidden",
586 s_request->uri,
587 s_request->host_conf->
588 host_signature);
589 s_log->error_msg = request_error_msg_403;
590 s_log->error_details = s_request->uri;
591 break;
593 case M_CLIENT_NOT_FOUND:
594 mk_string_build(&message.data, &message.len,
595 "The requested URL was not found on this server.");
596 page = mk_request_set_default_page("Not Found",
597 message,
598 s_request->host_conf->
599 host_signature);
600 s_log->error_msg = request_error_msg_404;
601 s_log->error_details = s_request->uri;
603 mk_pointer_free(&message);
604 break;
606 case M_CLIENT_METHOD_NOT_ALLOWED:
607 page = mk_request_set_default_page("Method Not Allowed",
608 s_request->uri,
609 s_request->host_conf->
610 host_signature);
612 s_log->final_response = M_CLIENT_METHOD_NOT_ALLOWED;
613 s_log->error_msg = request_error_msg_405;
614 s_log->error_details = s_request->method_p;
615 break;
617 case M_CLIENT_REQUEST_TIMEOUT:
618 s_log->status = S_LOG_OFF;
619 s_log->error_msg = request_error_msg_408;
620 break;
622 case M_CLIENT_LENGTH_REQUIRED:
623 s_log->error_msg = request_error_msg_411;
624 break;
626 case M_SERVER_NOT_IMPLEMENTED:
627 page = mk_request_set_default_page("Method Not Implemented",
628 s_request->uri,
629 s_request->host_conf->
630 host_signature);
631 s_log->final_response = M_SERVER_NOT_IMPLEMENTED;
632 s_log->error_msg = request_error_msg_501;
633 s_log->error_details = s_request->method_p;
634 break;
636 case M_SERVER_INTERNAL_ERROR:
637 mk_string_build(&message.data, &message.len,
638 "Problems found running %s ", s_request->uri);
639 page = mk_request_set_default_page("Internal Server Error",
640 message,
641 s_request->host_conf->
642 host_signature);
643 s_log->error_msg = request_error_msg_500;
645 mk_pointer_free(&message);
646 break;
648 case M_SERVER_HTTP_VERSION_UNSUP:
649 mk_pointer_reset(&message);
650 page = mk_request_set_default_page("HTTP Version Not Supported",
651 message,
652 s_request->host_conf->
653 host_signature);
654 s_log->error_msg = request_error_msg_505;
655 break;
658 s_log->final_response = num_error;
660 s_request->headers->status = num_error;
661 if (page) {
662 s_request->headers->content_length = page->len;
663 s_request->headers->content_length_p = mk_utils_int2mkp(page->len);
666 s_request->headers->location = NULL;
667 s_request->headers->cgi = SH_NOCGI;
668 s_request->headers->pconnections_left = 0;
669 mk_pointer_reset(&s_request->headers->last_modified);
671 if (aux_message)
672 mk_mem_free(aux_message);
674 if (!page) {
675 mk_pointer_reset(&s_request->headers->content_type);
677 else {
678 mk_pointer_set(&s_request->headers->content_type, "text/html\r\n");
681 mk_header_send(cr->socket, cr, s_request, s_log);
683 if (debug == 1) {
684 n = write(cr->socket, page->data, page->len);
685 mk_pointer_free(page);
686 mk_mem_free(page);
690 /* Build error page */
691 mk_pointer *mk_request_set_default_page(char *title, mk_pointer message,
692 char *signature)
694 char *temp;
695 mk_pointer *p;
697 p = mk_mem_malloc(sizeof(mk_pointer));
699 temp = mk_pointer_to_buf(message);
700 mk_string_build(&p->data, &p->len,
701 MK_REQUEST_DEFAULT_PAGE, title, temp, signature);
702 mk_mem_free(temp);
704 return p;
707 /* Create a memory allocation in order to handle the request data */
708 struct request *mk_request_alloc()
710 struct request *request = 0;
712 request = mk_mem_malloc(sizeof(struct request));
713 request->log = mk_mem_malloc(sizeof(struct log_info));
715 request->status = VAR_OFF; /* Request not processed yet */
716 request->make_log = VAR_ON; /* build log file of this request ? */
717 request->close_now = VAR_OFF;
719 mk_pointer_reset(&request->body);
721 request->log->final_response = M_HTTP_OK;
722 request->log->status = S_LOG_ON;
723 mk_pointer_reset(&request->log->size_p);
724 mk_pointer_reset(&request->log->error_msg);
726 request->status = VAR_ON;
727 request->method = METHOD_NOT_FOUND;
729 mk_pointer_reset(&request->uri);
730 request->uri_processed = NULL;
731 request->uri_twin = VAR_OFF;
733 request->accept.data = NULL;
734 request->accept_language.data = NULL;
735 request->accept_encoding.data = NULL;
736 request->accept_charset.data = NULL;
737 request->content_length = 0;
738 request->content_type.data = NULL;
739 request->connection.data = NULL;
740 request->cookies.data = NULL;
741 request->host.data = NULL;
742 request->if_modified_since.data = NULL;
743 request->last_modified_since.data = NULL;
744 request->range.data = NULL;
745 request->referer.data = NULL;
746 request->resume.data = NULL;
747 request->user_agent.data = NULL;
749 request->post_variables.data = NULL;
751 request->user_uri = NULL;
752 mk_pointer_reset(&request->query_string);
754 request->file_info = NULL;
755 request->virtual_user = NULL;
756 request->script_filename = NULL;
757 mk_pointer_reset(&request->real_path);
758 request->host_conf = config->hosts;
760 request->loop = 0;
761 request->bytes_to_send = -1;
762 request->bytes_offset = 0;
763 request->fd_file = -1;
765 /* Response Headers */
766 request->headers = mk_header_create();
768 request->handled_by = NULL;
769 return request;
772 void mk_request_free_list(struct client_request *cr)
774 struct request *sr = 0, *before = 0;
776 /* sr = last node */
778 while (cr->request) {
779 sr = before = cr->request;
781 while (sr->next) {
782 sr = sr->next;
785 if (sr != cr->request) {
786 while (before->next != sr) {
787 before = before->next;
789 before->next = NULL;
791 else {
792 cr->request = NULL;
794 mk_request_free(sr);
796 cr->request = NULL;
799 void mk_request_free(struct request *sr)
801 /* I hate it, but I don't know another light way :( */
802 if (sr->fd_file > 0) {
803 close(sr->fd_file);
805 if (sr->headers) {
806 mk_mem_free(sr->headers->location);
807 mk_pointer_free(&sr->headers->content_length_p);
808 mk_pointer_free(&sr->headers->last_modified);
809 mk_mem_free(sr->headers);
813 if (sr->log) {
815 * We do not free log->size_p, as if it was
816 * used due to an error, it points to the
817 * same memory block than header->content_length_p
818 * points to, we just reset it.
820 mk_pointer_reset(&sr->log->size_p);
823 * sr->log->error_msg just point to
824 * local data on request.c, no
825 * dynamic allocation is made
828 mk_mem_free(sr->log);
831 mk_pointer_reset(&sr->body);
832 mk_pointer_reset(&sr->uri);
834 if (sr->uri_twin == VAR_ON) {
835 mk_mem_free(sr->uri_processed);
838 mk_pointer_free(&sr->post_variables);
839 mk_mem_free(sr->user_uri);
840 mk_pointer_reset(&sr->query_string);
842 mk_mem_free(sr->file_info);
843 mk_mem_free(sr->virtual_user);
844 mk_mem_free(sr->script_filename);
845 mk_pointer_free(&sr->real_path);
846 mk_mem_free(sr);
849 /* Create a client request struct and put it on the
850 * main list
852 struct client_request *mk_request_client_create(int socket)
854 struct request_idx *request_index;
855 struct client_request *cr;
856 struct sched_connection *sc;
858 sc = mk_sched_get_connection(NULL, socket);
859 cr = mk_mem_malloc(sizeof(struct client_request));
861 /* IPv4 Address */
862 cr->ipv4 = &sc->ipv4;
864 cr->pipelined = FALSE;
865 cr->counter_connections = 0;
866 cr->socket = socket;
867 cr->status = MK_REQUEST_STATUS_INCOMPLETE;
868 cr->request = NULL;
870 /* creation time in unix time */
871 cr->init_time = sc->arrive_time;
873 cr->next = NULL;
874 cr->body = mk_mem_malloc(MAX_REQUEST_BODY);
875 cr->body_length = 0;
876 cr->body_pos_end = -1;
877 cr->first_method = HTTP_METHOD_UNKNOWN;
879 request_index = mk_sched_get_request_index();
880 if (!request_index->first) {
881 request_index->first = request_index->last = cr;
883 else {
884 request_index->last->next = cr;
885 request_index->last = cr;
887 mk_sched_set_request_index(request_index);
889 return cr;
892 struct client_request *mk_request_client_get(int socket)
894 struct request_idx *request_index;
895 struct client_request *cr = NULL;
897 request_index = mk_sched_get_request_index();
898 cr = request_index->first;
899 while (cr != NULL) {
900 if (cr->socket == socket) {
901 break;
903 cr = cr->next;
906 return cr;
910 * From thread sched_list_node "list", remove the client_request
911 * struct information
913 struct client_request *mk_request_client_remove(int socket)
915 struct request_idx *request_index;
916 struct client_request *cr, *aux;
918 request_index = mk_sched_get_request_index();
919 cr = request_index->first;
921 while (cr) {
922 if (cr->socket == socket) {
923 if (cr == request_index->first) {
924 request_index->first = cr->next;
926 else {
927 aux = request_index->first;
928 while (aux->next != cr) {
929 aux = aux->next;
931 aux->next = cr->next;
932 if (!aux->next) {
933 request_index->last = aux;
936 break;
938 cr = cr->next;
941 mk_mem_free(cr->body);
942 mk_mem_free(cr);
944 return NULL;
947 struct header_toc *mk_request_header_toc_create(int len)
949 int i;
950 struct header_toc *p;
952 p = (struct header_toc *) pthread_getspecific(mk_cache_header_toc);
954 for (i = 0; i < len; i++) {
955 p[i].init = NULL;
956 p[i].end = NULL;
957 p[i].status = 0;
959 return p;
962 void mk_request_header_toc_parse(struct header_toc *toc, int toc_len, char *data, int len)
964 char *p, *l = 0;
965 int i;
967 p = data;
968 for (i = 0; i < toc_len && p && l < data + len; i++) {
969 l = strstr(p, MK_CRLF);
970 if (l) {
971 toc[i].init = p;
972 toc[i].end = l;
973 p = l + mk_crlf.len;
975 else {
976 break;
981 void mk_request_ka_next(struct client_request *cr)
983 bzero(cr->body, sizeof(cr->body));
984 cr->first_method = -1;
985 cr->body_pos_end = -1;
986 cr->body_length = 0;
987 cr->counter_connections++;
989 /* Update data for scheduler */
990 cr->init_time = log_current_utime;
991 cr->status = MK_REQUEST_STATUS_INCOMPLETE;