1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <sys/socket.h>
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
39 #include <sys/types.h>
44 #include "http_status.h"
48 #include "scheduler.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;
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();
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
;
81 cr_buf
->method
= mk_http_method_get(cr_buf
->body
.data
);
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
;
101 cr_search
= cr
->request
;
103 if (cr_search
->next
== NULL
) {
104 cr_search
->next
= cr_buf
;
108 cr_search
= cr_search
->next
;
113 n
= mk_string_search(cr
->body
+ i
, mk_endblock
.data
);
122 /* Checking pipelining connection */
123 cr_search
= cr
->request
;
128 if (cr_search
->method
!= HTTP_METHOD_GET
&&
129 cr_search
->method
!= HTTP_METHOD_HEAD
) {
133 cr_search
= cr_search
->next
;
136 if (pipelined
== FALSE
) {
137 /* All pipelined requests must use GET method */
141 cr
->pipelined
= TRUE
;
146 printf("*****************************************");
148 cr_search = cr->request;
150 printf("\n---BLOCK---:\n%s---END BLOCK---\n\n", cr_search->body);
152 cr_search = cr_search->next;
159 int mk_handler_read(int socket
, struct client_request
*cr
)
163 bytes
= read(socket
, cr
->body
+ cr
->body_length
,
164 MAX_REQUEST_BODY
- cr
->body_length
);
167 if (errno
== EAGAIN
) {
171 mk_request_client_remove(socket
);
176 mk_request_client_remove(socket
);
181 cr
->body_length
+= bytes
;
182 cr
->body
[cr
->body_length
] = '\0';
188 int mk_handler_write(int socket
, struct client_request
*cr
)
190 int bytes
, final_status
= 0;
194 * Get node from schedule list node which contains
195 * the information regarding to the current thread
202 if (!mk_request_parse(cr
)) {
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");
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) {
238 else if (final_status
<= 0) {
239 mk_logger_write_log(cr
, sr
->log
, sr
->host_conf
);
245 /* If we are here, is because all pipelined request were
246 * processed successfully, let's return 0;
251 int mk_request_process(struct client_request
*cr
, struct request
*s_request
)
256 status
= mk_request_header_process(s_request
);
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
);
267 case METHOD_NOT_FOUND
:
268 mk_request_error(M_SERVER_NOT_IMPLEMENTED
, cr
,
269 s_request
, 1, s_request
->log
);
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,
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,
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,
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,
308 if (s_request
->host
.data
) {
309 host
= mk_config_host_find(s_request
->host
);
311 s_request
->host_conf
= host
;
314 s_request
->host_conf
= config
->hosts
;
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) {
332 /* Handling method requested */
333 if (s_request
->method
== HTTP_METHOD_POST
) {
334 if ((status
= mk_method_post(cr
, s_request
)) == -1) {
339 status
= mk_http_init(cr
, s_request
);
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;
356 /* If verification fails it will return always
357 * a bad request status
359 sr
->log
->final_response
= M_CLIENT_BAD_REQUEST
;
362 sr
->method_p
= mk_http_method_check_str(sr
->method
);
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;
374 prot_init
= uri_end
+ 2;
376 if (uri_end
< uri_init
) {
381 query_init
= index(sr
->body
.data
+ uri_init
, '?');
385 init
= (int) (query_init
- (sr
->body
.data
+ uri_init
)) + uri_init
;
386 if (init
<= uri_end
) {
390 sr
->query_string
= mk_pointer_create(sr
->body
.data
,
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) {
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
;
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
);
428 host
= mk_request_header_find(toc
, toc_len
, headers
, mk_rh_host
);
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
);
440 sr
->host
= host
; /* maybe null */
441 sr
->port
= config
->standard_port
;
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
,
461 sr
->referer
= mk_request_header_find(toc
, toc_len
, headers
,
463 sr
->user_agent
= mk_request_header_find(toc
, toc_len
, headers
,
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
;
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
;
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
)
500 for (i
= 0; i
< toc_len
; i
++) {
501 /* status = 1 means that the toc entry was already
504 if (toc
[i
].status
== 1) {
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
;
523 /* FIXME: IMPROVE access */
524 /* Look for some index.xxx in pathfile */
525 mk_pointer
mk_request_index(char *pathfile
)
530 struct indexfile
*aux_index
;
532 mk_pointer_reset(&f
);
534 aux_index
= first_index
;
537 m_build_buffer(&file_aux
, &len
, "%s%s",
538 pathfile
, aux_index
->indexname
);
540 if (access(file_aux
, F_OK
) == 0) {
545 mk_mem_free(file_aux
);
546 aux_index
= aux_index
->next
;
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;
562 case M_CLIENT_BAD_REQUEST
:
563 page
= mk_request_set_default_page("Bad Request",
565 s_request
->host_conf
->
567 s_log
->error_msg
= request_error_msg_400
;
570 case M_CLIENT_FORBIDDEN
:
571 page
= mk_request_set_default_page("Forbidden",
573 s_request
->host_conf
->
575 s_log
->error_msg
= request_error_msg_403
;
576 // req s_request->uri;
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",
584 s_request
->host_conf
->
586 s_log
->error_msg
= request_error_msg_404
;
588 mk_pointer_free(&message
);
591 case M_CLIENT_METHOD_NOT_ALLOWED
:
592 page
= mk_request_set_default_page("Method Not Allowed",
594 s_request
->host_conf
->
597 s_log
->final_response
= M_CLIENT_METHOD_NOT_ALLOWED
;
598 s_log
->error_msg
= request_error_msg_405
;
601 case M_CLIENT_REQUEST_TIMEOUT
:
602 s_log
->status
= S_LOG_OFF
;
603 s_log
->error_msg
= request_error_msg_408
;
606 case M_CLIENT_LENGTH_REQUIRED
:
607 s_log
->error_msg
= request_error_msg_411
;
610 case M_SERVER_NOT_IMPLEMENTED
:
611 page
= mk_request_set_default_page("Method Not Implemented",
613 s_request
->host_conf
->
615 s_log
->final_response
= M_SERVER_NOT_IMPLEMENTED
;
616 s_log
->error_msg
= request_error_msg_501
;
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",
624 s_request
->host_conf
->
626 s_log
->error_msg
= request_error_msg_500
;
628 mk_pointer_free(&message
);
631 case M_SERVER_HTTP_VERSION_UNSUP
:
632 mk_pointer_reset(&message
);
633 page
= mk_request_set_default_page("HTTP Version Not Supported",
635 s_request
->host_conf
->
637 s_log
->error_msg
= request_error_msg_505
;
641 s_log
->final_response
= num_error
;
643 s_request
->headers
->status
= num_error
;
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
);
655 mk_mem_free(aux_message
);
658 mk_pointer_reset(&s_request
->headers
->content_type
);
661 mk_pointer_set(&s_request
->headers
->content_type
, "text/html");
664 mk_header_send(cr
->socket
, cr
, s_request
, s_log
);
667 n
= write(cr
->socket
, page
->data
, page
->len
);
668 mk_pointer_free(page
);
673 /* Build error page */
674 mk_pointer
*mk_request_set_default_page(char *title
, mk_pointer message
,
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
);
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
;
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
;
754 void mk_request_free_list(struct client_request
*cr
)
756 struct request
*sr
= 0, *before
= 0;
760 while (cr
->request
) {
761 sr
= before
= cr
->request
;
767 if (sr
!= cr
->request
) {
768 while (before
->next
!= sr
) {
769 before
= before
->next
;
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) {
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
);
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
);
841 /* Create a client request struct and put it on the
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
));
854 cr
->ipv4
= (char *) sc
->ipv4
;
856 cr
->pipelined
= FALSE
;
857 cr
->counter_connections
= 0;
859 cr
->status
= MK_REQUEST_STATUS_INCOMPLETE
;
862 /* creation time in unix time */
863 cr
->init_time
= sc
->arrive_time
;
866 cr
->body
= mk_mem_malloc(MAX_REQUEST_BODY
);
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
;
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
);
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
;
897 if (cr
->socket
== socket
) {
907 * From thread sched_list_node "list", remove the client_request
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
;
919 if (cr
->socket
== socket
) {
920 if (cr
== request_index
->first
) {
921 request_index
->first
= cr
->next
;
924 aux
= request_index
->first
;
925 while (aux
->next
!= cr
) {
928 aux
->next
= cr
->next
;
930 request_index
->last
= aux
;
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
);
951 struct header_toc
*mk_request_header_toc_create(int len
)
954 struct header_toc
*p
;
956 p
= (struct header_toc
*) pthread_getspecific(mk_cache_header_toc
);
958 for (i
= 0; i
< len
; i
++) {
966 void mk_request_header_toc_parse(struct header_toc
*toc
, char *data
, int len
)
972 for (i
= 0; i
< len
&& p
; i
++) {
973 l
= strstr(p
, MK_CRLF
);
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;
991 cr
->counter_connections
++;