Change the FSF address and update LICENSE with the new address and some texts
[MonkeyD.git] / src / request.c
blob7e3a1dd4a0d4eaa2d294d31bd802de50bc3ef87b
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-2008, Eduardo Silva P.
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"
61 struct request *mk_request_parse(struct client_request *cr)
63 int i, n, init_block = 0, n_blocks = 0;
64 int pipelined = FALSE;
65 struct request *cr_buf = 0, *cr_search = 0;
67 init_block = 0;
69 for (i = cr->first_block_end; i <= cr->body_length - mk_endblock.len; i++) {
70 /* Allocating request block */
71 cr_buf = mk_request_alloc();
73 /* mk_pointer */
74 cr_buf->body.data = cr->body + init_block;
75 cr_buf->body.len = i - init_block;
77 if (i == cr->first_block_end) {
78 cr_buf->method = cr->first_method;
80 else {
81 cr_buf->method = mk_http_method_get(cr_buf->body.data);
84 cr_buf->next = NULL;
86 i = init_block = i + mk_endblock.len;
88 /* Looking for POST data */
89 if (cr_buf->method == HTTP_METHOD_POST) {
90 cr_buf->post_variables = mk_method_post_get_vars(cr->body, i);
92 if (cr_buf->post_variables.len >= 0) {
93 i = init_block = i + cr_buf->post_variables.len;
97 if (!cr->request) {
98 cr->request = cr_buf;
100 else {
101 cr_search = cr->request;
102 while (cr_search) {
103 if (cr_search->next == NULL) {
104 cr_search->next = cr_buf;
105 break;
107 else {
108 cr_search = cr_search->next;
112 n_blocks++;
113 n = mk_string_search(cr->body + i, mk_endblock.data);
114 if (n <= 0) {
115 break;
117 else {
118 i = i + n;
122 /* Checking pipelining connection */
123 cr_search = cr->request;
124 if (n_blocks > 1) {
125 pipelined = TRUE;
127 while (cr_search) {
128 if (cr_search->method != HTTP_METHOD_GET &&
129 cr_search->method != HTTP_METHOD_HEAD) {
130 pipelined = FALSE;
131 break;
133 cr_search = cr_search->next;
136 if (pipelined == FALSE) {
137 /* All pipelined requests must use GET method */
138 return NULL;
140 else {
141 cr->pipelined = TRUE;
145 /* DEBUG BLOCKS
146 printf("*****************************************");
147 fflush(stdout);
148 cr_search = cr->request;
149 while(cr_search){
150 printf("\n---BLOCK---:\n%s---END BLOCK---\n\n", cr_search->body);
151 fflush(stdout);
152 cr_search = cr_search->next;
156 return cr->request;
159 int mk_handler_read(int socket, struct client_request *cr)
161 int bytes;
163 bytes = read(socket, cr->body + cr->body_length,
164 MAX_REQUEST_BODY - cr->body_length);
166 if (bytes < 0) {
167 if (errno == EAGAIN) {
168 return 1;
170 else {
171 mk_request_client_remove(socket);
172 return -1;
175 if (bytes == 0) {
176 mk_request_client_remove(socket);
177 return -1;
180 if (bytes > 0) {
181 cr->body_length += bytes;
182 cr->body[cr->body_length] = '\0';
185 return bytes;
188 int mk_handler_write(int socket, struct client_request *cr)
190 int bytes, final_status = 0;
191 struct request *sr;
194 * Get node from schedule list node which contains
195 * the information regarding to the current thread
197 if (!cr) {
198 return -1;
201 if (!cr->request) {
202 if (!mk_request_parse(cr)) {
203 return -1;
207 sr = cr->request;
209 while (sr) {
210 /* Request not processed also no plugin has take some action */
211 if (sr->bytes_to_send < 0 && !sr->handled_by) {
212 final_status = mk_request_process(cr, sr);
214 /* Request with data to send by static file sender */
215 else if (sr->bytes_to_send > 0 && !sr->handled_by) {
216 bytes = SendFile(socket, cr, sr);
217 final_status = bytes;
219 else if (sr->handled_by){
220 struct handler *handler = sr->handled_by;
222 printf("\nhandled by");
223 fflush(stdout);
225 while(handler){
227 handler = handler->next;
232 * If we got an error, we don't want to parse
233 * and send information for another pipelined request
235 if (final_status > 0) {
236 return final_status;
238 else if (final_status <= 0) {
239 mk_logger_write_log(cr, sr->log, sr->host_conf);
242 sr = sr->next;
245 /* If we are here, is because all pipelined request were
246 * processed successfully, let's return 0;
248 return 0;
251 int mk_request_process(struct client_request *cr, struct request *s_request)
253 int status = 0;
254 struct host *host;
256 status = mk_request_header_process(s_request);
258 if (status < 0) {
259 return EXIT_NORMAL;
262 switch (s_request->method) {
263 case METHOD_NOT_ALLOWED:
264 mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED, cr,
265 s_request, 1, s_request->log);
266 return EXIT_NORMAL;
267 case METHOD_NOT_FOUND:
268 mk_request_error(M_SERVER_NOT_IMPLEMENTED, cr,
269 s_request, 1, s_request->log);
270 return EXIT_NORMAL;
273 s_request->user_home = VAR_OFF;
274 s_request->log->method = s_request->method;
276 /* Valid request URI? */
277 if (s_request->uri_processed == NULL) {
278 mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request, 1,
279 s_request->log);
280 return EXIT_NORMAL;
283 /* HTTP/1.1 needs Host header */
284 if (!s_request->host.data && s_request->protocol == HTTP_PROTOCOL_11) {
285 s_request->log->final_response = M_CLIENT_BAD_REQUEST;
286 mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request, 1,
287 s_request->log);
288 return EXIT_NORMAL;
291 /* Method not allowed ? */
292 if (s_request->method == METHOD_NOT_ALLOWED) {
293 s_request->log->final_response = M_CLIENT_METHOD_NOT_ALLOWED;
294 mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED, cr, s_request, 1,
295 s_request->log);
296 return EXIT_NORMAL;
299 /* Validating protocol version */
300 if (s_request->protocol == HTTP_PROTOCOL_UNKNOWN) {
302 s_request->log->final_response = M_SERVER_HTTP_VERSION_UNSUP;
303 mk_request_error(M_SERVER_HTTP_VERSION_UNSUP, cr, s_request, 1,
304 s_request->log);
305 return EXIT_NORMAL;
308 if (s_request->host.data) {
309 host = mk_config_host_find(s_request->host);
310 if (host) {
311 s_request->host_conf = host;
313 else {
314 s_request->host_conf = config->hosts;
317 else {
318 s_request->host_conf = config->hosts;
320 s_request->log->host_conf = s_request->host_conf;
322 /* is requesting an user home directory ? */
323 if (config->user_dir) {
324 if (strncmp(s_request->uri_processed,
325 mk_user_home.data, mk_user_home.len) == 0) {
326 if (mk_user_init(cr, s_request) != 0) {
327 return EXIT_NORMAL;
332 /* Handling method requested */
333 if (s_request->method == HTTP_METHOD_POST) {
334 if ((status = mk_method_post(cr, s_request)) == -1) {
335 return status;
339 status = mk_http_init(cr, s_request);
341 return status;
344 /* Return a struct with method, URI , protocol version
345 and all static headers defined here sent in request */
346 int mk_request_header_process(struct request *sr)
348 int uri_init = 0, uri_end = 0;
349 char *query_init = 0;
350 int prot_init = 0, prot_end = 0, pos_sep = 0;
351 int fh_limit;
352 char *port = 0;
353 char *headers;
354 mk_pointer host;
356 /* If verification fails it will return always
357 * a bad request status
359 sr->log->final_response = M_CLIENT_BAD_REQUEST;
361 /* Method */
362 sr->method_p = mk_http_method_check_str(sr->method);
364 /* Request URI */
365 uri_init = (index(sr->body.data, ' ') - sr->body.data) + 1;
366 fh_limit = (index(sr->body.data, '\n') - sr->body.data);
368 uri_end = mk_string_search_r(sr->body.data, ' ', fh_limit) - 1;
370 if (uri_end <= 0) {
371 return -1;
374 prot_init = uri_end + 2;
376 if (uri_end < uri_init) {
377 return -1;
380 /* Query String */
381 query_init = index(sr->body.data + uri_init, '?');
382 if (query_init) {
383 int init, end;
385 init = (int) (query_init - (sr->body.data + uri_init)) + uri_init;
386 if (init <= uri_end) {
387 end = uri_end;
388 uri_end = init - 1;
390 sr->query_string = mk_pointer_create(sr->body.data,
391 init + 1, end + 1);
395 /* Request URI Part 2 */
396 sr->uri = sr->log->uri = mk_pointer_create(sr->body.data,
397 uri_init, uri_end + 1);
399 if (sr->uri.len < 1) {
400 return -1;
404 /* HTTP Version */
405 prot_end = fh_limit - 1;
406 if (prot_end != prot_init && prot_end > 0) {
407 sr->protocol = sr->log->protocol =
408 mk_http_protocol_check(sr->body.data + prot_init,
409 prot_end - prot_init);
412 headers = sr->body.data + prot_end + mk_crlf.len;
414 /* URI processed */
415 sr->uri_processed = get_real_string(sr->uri);
417 if (!sr->uri_processed) {
418 sr->uri_processed = mk_pointer_to_buf(sr->uri);
419 sr->uri_twin = VAR_ON;
422 /* Creating table of content (index) for request headers */
423 int toc_len = MK_KNOWN_HEADERS;
424 struct header_toc *toc = mk_request_header_toc_create(toc_len);
425 mk_request_header_toc_parse(toc, headers, toc_len);
427 /* Host */
428 host = mk_request_header_find(toc, toc_len, headers, mk_rh_host);
430 if (host.data) {
431 if ((pos_sep = mk_string_char_search(host.data, ':', host.len)) >= 0) {
432 sr->host.data = host.data;
433 sr->host.len = pos_sep;
435 port = mk_string_copy_substr(host.data, pos_sep + 1, host.len);
436 sr->port = atoi(port);
437 mk_mem_free(port);
439 else {
440 sr->host = host; /* maybe null */
441 sr->port = config->standard_port;
444 else {
445 sr->host.data = NULL;
448 /* Looking for headers */
449 sr->accept = mk_request_header_find(toc, toc_len, headers, mk_rh_accept);
450 sr->accept_charset = mk_request_header_find(toc, toc_len, headers,
451 mk_rh_accept_charset);
452 sr->accept_encoding = mk_request_header_find(toc, toc_len, headers,
453 mk_rh_accept_encoding);
456 sr->accept_language = mk_request_header_find(toc, toc_len, headers,
457 mk_rh_accept_language);
458 sr->cookies = mk_request_header_find(toc, toc_len, headers, mk_rh_cookie);
459 sr->connection = mk_request_header_find(toc, toc_len, headers,
460 mk_rh_connection);
461 sr->referer = mk_request_header_find(toc, toc_len, headers,
462 mk_rh_referer);
463 sr->user_agent = mk_request_header_find(toc, toc_len, headers,
464 mk_rh_user_agent);
465 sr->range = mk_request_header_find(toc, toc_len, headers, mk_rh_range);
466 sr->if_modified_since = mk_request_header_find(toc, toc_len, headers,
467 mk_rh_if_modified_since);
469 /* Default Keepalive is off */
470 sr->keep_alive = VAR_OFF;
471 if (sr->connection.data) {
472 if (mk_string_casestr(sr->connection.data, "Keep-Alive")) {
473 sr->keep_alive = VAR_ON;
476 else {
477 /* Default value for HTTP/1.1 */
478 if (sr->protocol == HTTP_PROTOCOL_11) {
479 /* Assume keep-alive connection */
480 sr->keep_alive = VAR_ON;
483 sr->log->final_response = M_HTTP_OK;
485 return 0;
488 /* Return value of some variable sent in request */
489 mk_pointer mk_request_header_find(struct header_toc * toc, int toc_len,
490 char *request_body, mk_pointer header)
492 int i;
493 mk_pointer var;
495 var.data = NULL;
496 var.len = 0;
498 /* new code */
499 if (toc) {
500 for (i = 0; i < toc_len; i++) {
501 /* status = 1 means that the toc entry was already
502 * checked by monkey
504 if (toc[i].status == 1) {
505 continue;
508 if (!toc[i].init)
509 break;
511 if (strncasecmp(toc[i].init, header.data, header.len) == 0) {
512 var.data = toc[i].init + header.len + 1;
513 var.len = toc[i].end - var.data;
514 toc[i].status = 1;
515 return var;
520 return var;
523 /* FIXME: IMPROVE access */
524 /* Look for some index.xxx in pathfile */
525 mk_pointer mk_request_index(char *pathfile)
527 unsigned long len;
528 char *file_aux = 0;
529 mk_pointer f;
530 struct indexfile *aux_index;
532 mk_pointer_reset(&f);
534 aux_index = first_index;
536 while (aux_index) {
537 m_build_buffer(&file_aux, &len, "%s%s",
538 pathfile, aux_index->indexname);
540 if (access(file_aux, F_OK) == 0) {
541 f.data = file_aux;
542 f.len = len;
543 return f;
545 mk_mem_free(file_aux);
546 aux_index = aux_index->next;
549 return f;
552 /* Send error responses */
553 void mk_request_error(int num_error, struct client_request *cr,
554 struct request *s_request, int debug,
555 struct log_info *s_log)
557 char *aux_message = 0;
558 mk_pointer message, *page = 0;
559 long n;
561 switch (num_error) {
562 case M_CLIENT_BAD_REQUEST:
563 page = mk_request_set_default_page("Bad Request",
564 s_request->uri,
565 s_request->host_conf->
566 host_signature);
567 s_log->error_msg = request_error_msg_400;
568 break;
570 case M_CLIENT_FORBIDDEN:
571 page = mk_request_set_default_page("Forbidden",
572 s_request->uri,
573 s_request->host_conf->
574 host_signature);
575 s_log->error_msg = request_error_msg_403;
576 // req s_request->uri;
577 break;
579 case M_CLIENT_NOT_FOUND:
580 m_build_buffer(&message.data, &message.len,
581 "The requested URL was not found on this server.");
582 page = mk_request_set_default_page("Not Found",
583 message,
584 s_request->host_conf->
585 host_signature);
586 s_log->error_msg = request_error_msg_404;
587 // req uri;
588 mk_pointer_free(&message);
589 break;
591 case M_CLIENT_METHOD_NOT_ALLOWED:
592 page = mk_request_set_default_page("Method Not Allowed",
593 s_request->uri,
594 s_request->host_conf->
595 host_signature);
597 s_log->final_response = M_CLIENT_METHOD_NOT_ALLOWED;
598 s_log->error_msg = request_error_msg_405;
599 break;
601 case M_CLIENT_REQUEST_TIMEOUT:
602 s_log->status = S_LOG_OFF;
603 s_log->error_msg = request_error_msg_408;
604 break;
606 case M_CLIENT_LENGTH_REQUIRED:
607 s_log->error_msg = request_error_msg_411;
608 break;
610 case M_SERVER_NOT_IMPLEMENTED:
611 page = mk_request_set_default_page("Method Not Implemented",
612 s_request->uri,
613 s_request->host_conf->
614 host_signature);
615 s_log->final_response = M_SERVER_NOT_IMPLEMENTED;
616 s_log->error_msg = request_error_msg_501;
617 break;
619 case M_SERVER_INTERNAL_ERROR:
620 m_build_buffer(&message.data, &message.len,
621 "Problems found running %s ", s_request->uri);
622 page = mk_request_set_default_page("Internal Server Error",
623 message,
624 s_request->host_conf->
625 host_signature);
626 s_log->error_msg = request_error_msg_500;
628 mk_pointer_free(&message);
629 break;
631 case M_SERVER_HTTP_VERSION_UNSUP:
632 mk_pointer_reset(&message);
633 page = mk_request_set_default_page("HTTP Version Not Supported",
634 message,
635 s_request->host_conf->
636 host_signature);
637 s_log->error_msg = request_error_msg_505;
638 break;
641 s_log->final_response = num_error;
643 s_request->headers->status = num_error;
644 if (page) {
645 s_request->headers->content_length = page->len;
646 s_request->headers->content_length_p = mk_utils_int2mkp(page->len);
649 s_request->headers->location = NULL;
650 s_request->headers->cgi = SH_NOCGI;
651 s_request->headers->pconnections_left = 0;
652 mk_pointer_reset(&s_request->headers->last_modified);
654 if (aux_message)
655 mk_mem_free(aux_message);
657 if (!page) {
658 mk_pointer_reset(&s_request->headers->content_type);
660 else {
661 mk_pointer_set(&s_request->headers->content_type, "text/html");
664 mk_header_send(cr->socket, cr, s_request, s_log);
666 if (debug == 1) {
667 n = write(cr->socket, page->data, page->len);
668 mk_pointer_free(page);
669 mk_mem_free(page);
673 /* Build error page */
674 mk_pointer *mk_request_set_default_page(char *title, mk_pointer message,
675 char *signature)
677 char *temp;
678 mk_pointer *p;
680 p = mk_mem_malloc(sizeof(mk_pointer));
682 temp = mk_pointer_to_buf(message);
683 m_build_buffer(&p->data, &p->len,
684 MK_REQUEST_DEFAULT_PAGE, title, temp, signature);
685 mk_mem_free(temp);
687 return p;
690 /* Create a memory allocation in order to handle the request data */
691 struct request *mk_request_alloc()
693 struct request *request = 0;
695 request = mk_mem_malloc(sizeof(struct request));
696 request->log = mk_mem_malloc(sizeof(struct log_info));
698 request->status = VAR_OFF; /* Request not processed yet */
699 request->make_log = VAR_ON; /* build log file of this request ? */
701 mk_pointer_reset(&request->body);
703 request->log->final_response = M_HTTP_OK;
704 request->log->status = S_LOG_ON;
705 mk_pointer_reset(&request->log->size_p);
706 mk_pointer_reset(&request->log->error_msg);
708 request->status = VAR_ON;
709 request->method = METHOD_NOT_FOUND;
711 mk_pointer_reset(&request->uri);
712 request->uri_processed = NULL;
713 request->uri_twin = VAR_OFF;
715 request->accept.data = NULL;
716 request->accept_language.data = NULL;
717 request->accept_encoding.data = NULL;
718 request->accept_charset.data = NULL;
719 request->content_length = 0;
720 request->content_type.data = NULL;
721 request->connection.data = NULL;
722 request->cookies.data = NULL;
723 request->host.data = NULL;
724 request->if_modified_since.data = NULL;
725 request->last_modified_since.data = NULL;
726 request->range.data = NULL;
727 request->referer.data = NULL;
728 request->resume.data = NULL;
729 request->user_agent.data = NULL;
731 request->post_variables.data = NULL;
733 request->user_uri = NULL;
734 mk_pointer_reset(&request->query_string);
736 request->file_info = NULL;
737 request->virtual_user = NULL;
738 request->script_filename = NULL;
739 mk_pointer_reset(&request->real_path);
740 request->host_conf = config->hosts;
742 request->loop = 0;
743 request->bytes_to_send = -1;
744 request->bytes_offset = 0;
745 request->fd_file = -1;
747 /* Response Headers */
748 request->headers = mk_header_create();
750 request->handled_by = NULL;
751 return request;
754 void mk_request_free_list(struct client_request *cr)
756 struct request *sr = 0, *before = 0;
758 /* sr = last node */
760 while (cr->request) {
761 sr = before = cr->request;
763 while (sr->next) {
764 sr = sr->next;
767 if (sr != cr->request) {
768 while (before->next != sr) {
769 before = before->next;
771 before->next = NULL;
773 else {
774 cr->request = NULL;
776 mk_request_free(sr);
778 cr->request = NULL;
781 void mk_request_free(struct request *sr)
783 /* I hate it, but I don't know another light way :( */
784 if (sr->fd_file > 0) {
785 close(sr->fd_file);
787 if (sr->headers) {
788 mk_mem_free(sr->headers->location);
789 mk_pointer_free(&sr->headers->content_length_p);
790 mk_pointer_free(&sr->headers->last_modified);
792 mk_mem_free(sr->headers->content_type);
793 headers->content_type never it's allocated
794 with malloc or something, so we don't need
795 to free it, the value has been freed before
796 in M_METHOD_Get_and_Head(struct request *sr)
798 this BUG was reported by gentoo team.. thanks guys XD
801 mk_mem_free(sr->headers);
805 if (sr->log) {
807 * We do not free log->size_p, as if it was
808 * used due to an error, it points to the
809 * same memory block than header->content_length_p
810 * points to, we just reset it.
812 mk_pointer_reset(&sr->log->size_p);
815 * sr->log->error_msg just point to
816 * local data on request.c, no
817 * dynamic allocation is made
820 mk_mem_free(sr->log);
823 mk_pointer_reset(&sr->body);
824 mk_pointer_reset(&sr->uri);
826 if (sr->uri_twin == VAR_ON) {
827 mk_mem_free(sr->uri_processed);
830 mk_pointer_free(&sr->post_variables);
831 mk_mem_free(sr->user_uri);
832 mk_pointer_reset(&sr->query_string);
834 mk_mem_free(sr->file_info);
835 mk_mem_free(sr->virtual_user);
836 mk_mem_free(sr->script_filename);
837 mk_pointer_free(&sr->real_path);
838 mk_mem_free(sr);
841 /* Create a client request struct and put it on the
842 * main list
844 struct client_request *mk_request_client_create(int socket)
846 struct request_idx *request_index;
847 struct client_request *cr;
848 struct sched_connection *sc;
850 sc = mk_sched_get_connection(NULL, socket);
851 cr = mk_mem_malloc(sizeof(struct client_request));
853 /* IPv4 Address */
854 cr->ipv4 = (char *) sc->ipv4;
856 cr->pipelined = FALSE;
857 cr->counter_connections = 0;
858 cr->socket = socket;
859 cr->status = MK_REQUEST_STATUS_INCOMPLETE;
860 cr->request = NULL;
862 /* creation time in unix time */
863 cr->init_time = sc->arrive_time;
865 cr->next = NULL;
866 cr->body = mk_mem_malloc(MAX_REQUEST_BODY);
867 cr->body_length = 0;
868 cr->first_block_end = -1;
869 cr->first_method = HTTP_METHOD_UNKNOWN;
871 request_index = mk_sched_get_request_index();
872 if (!request_index->first) {
873 request_index->first = request_index->last = cr;
875 else {
876 request_index->last->next = cr;
877 request_index->last = cr;
879 mk_sched_set_request_index(request_index);
882 mk_sched_update_thread_status(NULL,
883 MK_SCHEDULER_ACTIVE_UP,
884 MK_SCHEDULER_CLOSED_NONE);
886 return cr;
889 struct client_request *mk_request_client_get(int socket)
891 struct request_idx *request_index;
892 struct client_request *cr = NULL;
894 request_index = mk_sched_get_request_index();
895 cr = request_index->first;
896 while (cr != NULL) {
897 if (cr->socket == socket) {
898 break;
900 cr = cr->next;
903 return cr;
907 * From thread sched_list_node "list", remove the client_request
908 * struct information
910 struct client_request *mk_request_client_remove(int socket)
912 struct request_idx *request_index;
913 struct client_request *cr, *aux;
915 request_index = mk_sched_get_request_index();
916 cr = request_index->first;
918 while (cr) {
919 if (cr->socket == socket) {
920 if (cr == request_index->first) {
921 request_index->first = cr->next;
923 else {
924 aux = request_index->first;
925 while (aux->next != cr) {
926 aux = aux->next;
928 aux->next = cr->next;
929 if (!aux->next) {
930 request_index->last = aux;
933 break;
935 cr = cr->next;
938 /* No keep alive connection */
939 if (cr->counter_connections == 0) {
940 mk_sched_update_thread_status(NULL,
941 MK_SCHEDULER_ACTIVE_DOWN,
942 MK_SCHEDULER_CLOSED_UP);
944 //mk_pointer_free(&cr->ip);
945 mk_mem_free(cr->body);
946 mk_mem_free(cr);
948 return NULL;
951 struct header_toc *mk_request_header_toc_create(int len)
953 int i;
954 struct header_toc *p;
956 p = (struct header_toc *) pthread_getspecific(mk_cache_header_toc);
958 for (i = 0; i < len; i++) {
959 p[i].init = NULL;
960 p[i].end = NULL;
961 p[i].status = 0;
963 return p;
966 void mk_request_header_toc_parse(struct header_toc *toc, char *data, int len)
968 char *p, *l;
969 int i;
971 p = data;
972 for (i = 0; i < len && p; i++) {
973 l = strstr(p, MK_CRLF);
974 if (l) {
975 toc[i].init = p;
976 toc[i].end = l;
977 p = l + mk_crlf.len;
979 else {
980 break;
985 void mk_request_ka_next(struct client_request *cr)
987 bzero(cr->body, sizeof(cr->body));
988 cr->first_method = -1;
989 cr->first_block_end = -1;
990 cr->body_length = 0;
991 cr->counter_connections++;