1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 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.
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"
62 struct request
*mk_request_parse(struct client_request
*cr
)
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
;
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 */
87 cr_buf
->method
= cr
->first_method
;
90 cr_buf
->method
= mk_http_method_get(cr_buf
->body
.data
);
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;
108 cr
->request
= cr_buf
;
111 cr_search
= cr
->request
;
113 if (cr_search
->next
== NULL
) {
114 cr_search
->next
= cr_buf
;
118 cr_search
= cr_search
->next
;
129 cr_search = cr->request;
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
;
144 /* Pipelining request must use GET or HEAD methods */
145 if (cr_search
->method
!= HTTP_METHOD_GET
&&
146 cr_search
->method
!= HTTP_METHOD_HEAD
) {
149 cr_search
= cr_search
->next
;
152 cr
->pipelined
= TRUE
;
158 int mk_handler_read(int socket
, struct client_request
*cr
)
162 bytes
= mk_socket_read(socket
, (void *)cr
->body
+ cr
->body_length
, MAX_REQUEST_BODY
- cr
->body_length
);
165 if (errno
== EAGAIN
) {
169 mk_request_client_remove(socket
);
174 mk_request_client_remove(socket
);
179 cr
->body_length
+= bytes
;
180 cr
->body
[cr
->body_length
] = '\0';
186 int mk_handler_write(int socket
, struct client_request
*cr
)
188 int bytes
, final_status
= 0;
192 * Get node from schedule list node which contains
193 * the information regarding to the current thread
200 if (!mk_request_parse(cr
)) {
207 /* Request not processed also no plugin has take some action */
208 if (sr
->bytes_to_send
< 0 && !sr
->handled_by
) {
209 final_status
= mk_request_process(cr
, sr
);
211 /* Request with data to send by static file sender */
212 else if (sr
->bytes_to_send
> 0 && !sr
->handled_by
) {
213 final_status
= bytes
= mk_http_send_file(cr
, sr
);
217 * If we got an error, we don't want to parse
218 * and send information for another pipelined request
220 if (final_status
> 0) {
223 else if (final_status
<= 0) {
224 /* STAGE_40, request has ended */
225 mk_plugin_stage_run(MK_PLUGIN_STAGE_40
, cr
->socket
,
227 switch (final_status
) {
230 if (sr
->close_now
== VAR_ON
) {
242 /* If we are here, is because all pipelined request were
243 * processed successfully, let's return 0;
248 int mk_request_process(struct client_request
*cr
, struct request
*s_request
)
253 status
= mk_request_header_process(s_request
);
259 switch (s_request
->method
) {
260 case METHOD_NOT_ALLOWED
:
261 mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED
, cr
, s_request
, 1);
263 case METHOD_NOT_FOUND
:
264 mk_request_error(M_SERVER_NOT_IMPLEMENTED
, cr
, s_request
, 1);
268 s_request
->user_home
= VAR_OFF
;
270 /* Valid request URI? */
271 if (s_request
->uri_processed
== NULL
) {
272 mk_request_error(M_CLIENT_BAD_REQUEST
, cr
, s_request
, 1);
276 /* HTTP/1.1 needs Host header */
277 if (!s_request
->host
.data
&& s_request
->protocol
== HTTP_PROTOCOL_11
) {
278 mk_request_error(M_CLIENT_BAD_REQUEST
, cr
, s_request
, 1);
282 /* Method not allowed ? */
283 if (s_request
->method
== METHOD_NOT_ALLOWED
) {
284 mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED
, cr
, s_request
, 1);
288 /* Validating protocol version */
289 if (s_request
->protocol
== HTTP_PROTOCOL_UNKNOWN
) {
290 mk_request_error(M_SERVER_HTTP_VERSION_UNSUP
, cr
, s_request
, 1);
294 if (s_request
->host
.data
) {
295 host
= mk_config_host_find(s_request
->host
);
297 s_request
->host_conf
= host
;
300 s_request
->host_conf
= config
->hosts
;
304 s_request
->host_conf
= config
->hosts
;
307 /* is requesting an user home directory ? */
308 if (config
->user_dir
) {
309 if (strncmp(s_request
->uri_processed
,
310 mk_user_home
.data
, mk_user_home
.len
) == 0) {
311 if (mk_user_init(cr
, s_request
) != 0) {
317 /* Handling method requested */
318 if (s_request
->method
== HTTP_METHOD_POST
) {
319 if ((status
= mk_method_post(cr
, s_request
)) == -1) {
324 /* Plugins Stage 20 */
326 ret
= mk_plugin_stage_run(MK_PLUGIN_STAGE_20
, cr
->socket
, NULL
,
329 if (ret
== MK_PLUGIN_RET_CLOSE_CONX
) {
331 MK_TRACE("STAGE 20 requested close conexion");
336 /* Normal HTTP process */
337 status
= mk_http_init(cr
, s_request
);
340 MK_TRACE("HTTP Init returning %i", status
);
346 /* Return a struct with method, URI , protocol version
347 and all static headers defined here sent in request */
348 int mk_request_header_process(struct request
*sr
)
350 int uri_init
= 0, uri_end
= 0;
351 char *query_init
= 0;
352 int prot_init
= 0, prot_end
= 0, pos_sep
= 0;
358 /* If verification fails it will return always
359 * a bad request status
361 //sr->log->final_response = M_CLIENT_BAD_REQUEST;
364 sr
->method_p
= mk_http_method_check_str(sr
->method
);
367 uri_init
= (index(sr
->body
.data
, ' ') - sr
->body
.data
) + 1;
368 fh_limit
= (index(sr
->body
.data
, '\n') - sr
->body
.data
);
370 uri_end
= mk_string_search_r(sr
->body
.data
, ' ', fh_limit
) - 1;
376 prot_init
= uri_end
+ 2;
378 if (uri_end
< uri_init
) {
383 query_init
= index(sr
->body
.data
+ uri_init
, '?');
387 init
= (int) (query_init
- (sr
->body
.data
+ uri_init
)) + uri_init
;
388 if (init
<= uri_end
) {
392 sr
->query_string
= mk_pointer_create(sr
->body
.data
,
397 /* Request URI Part 2 */
398 sr
->uri
= mk_pointer_create(sr
->body
.data
, uri_init
, uri_end
+ 1);
400 if (sr
->uri
.len
< 1) {
406 prot_end
= fh_limit
- 1;
407 if (prot_end
!= prot_init
&& prot_end
> 0) {
408 sr
->protocol
= 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
= mk_utils_hexuri_to_ascii(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 int headers_len
= sr
->body
.len
- (prot_end
+ mk_crlf
.len
);
426 struct header_toc
*toc
= mk_request_header_toc_create(toc_len
);
427 mk_request_header_toc_parse(toc
, toc_len
, headers
, headers_len
);
430 host
= mk_request_header_find(toc
, toc_len
, headers
, mk_rh_host
);
433 if ((pos_sep
= mk_string_char_search(host
.data
, ':', host
.len
)) >= 0) {
434 sr
->host
.data
= host
.data
;
435 sr
->host
.len
= pos_sep
;
437 port
= mk_string_copy_substr(host
.data
, pos_sep
+ 1, host
.len
);
438 sr
->port
= atoi(port
);
442 sr
->host
= host
; /* maybe null */
443 sr
->port
= config
->standard_port
;
447 sr
->host
.data
= NULL
;
450 /* Looking for headers */
451 sr
->accept
= mk_request_header_find(toc
, toc_len
, headers
, mk_rh_accept
);
452 sr
->accept_charset
= mk_request_header_find(toc
, toc_len
, headers
,
453 mk_rh_accept_charset
);
454 sr
->accept_encoding
= mk_request_header_find(toc
, toc_len
, headers
,
455 mk_rh_accept_encoding
);
458 sr
->accept_language
= mk_request_header_find(toc
, toc_len
, headers
,
459 mk_rh_accept_language
);
460 sr
->cookies
= mk_request_header_find(toc
, toc_len
, headers
, mk_rh_cookie
);
461 sr
->connection
= mk_request_header_find(toc
, toc_len
, headers
,
463 sr
->referer
= mk_request_header_find(toc
, toc_len
, headers
,
465 sr
->user_agent
= mk_request_header_find(toc
, toc_len
, headers
,
467 sr
->range
= mk_request_header_find(toc
, toc_len
, headers
, mk_rh_range
);
468 sr
->if_modified_since
= mk_request_header_find(toc
, toc_len
, headers
,
469 mk_rh_if_modified_since
);
471 /* Default Keepalive is off */
472 if (sr
->protocol
== HTTP_PROTOCOL_10
) {
473 sr
->keep_alive
= VAR_OFF
;
474 sr
->close_now
= VAR_ON
;
476 else if(sr
->protocol
== HTTP_PROTOCOL_11
) {
477 sr
->keep_alive
= VAR_ON
;
478 sr
->close_now
= VAR_OFF
;
481 if (sr
->connection
.data
) {
482 if (mk_string_casestr(sr
->connection
.data
, "Keep-Alive")) {
483 sr
->keep_alive
= VAR_ON
;
484 sr
->close_now
= VAR_OFF
;
486 else if(mk_string_casestr(sr
->connection
.data
, "Close")) {
487 sr
->keep_alive
= VAR_OFF
;
488 sr
->close_now
= VAR_ON
;
491 /* Set as a non-valid connection header value */
492 sr
->connection
.len
= 0;
499 /* Return value of some variable sent in request */
500 mk_pointer
mk_request_header_find(struct header_toc
* toc
, int toc_len
,
501 char *request_body
, mk_pointer header
)
510 for (i
= 0; i
< toc_len
; i
++) {
511 /* status = 1 means that the toc entry was already
514 if (toc
[i
].status
== 1) {
521 if (strncasecmp(toc
[i
].init
, header
.data
, header
.len
) == 0) {
522 var
.data
= toc
[i
].init
+ header
.len
+ 1;
523 var
.len
= toc
[i
].end
- var
.data
;
533 /* FIXME: IMPROVE access */
534 /* Look for some index.xxx in pathfile */
535 mk_pointer
mk_request_index(char *pathfile
)
540 struct indexfile
*aux_index
;
542 mk_pointer_reset(&f
);
544 aux_index
= first_index
;
547 mk_string_build(&file_aux
, &len
, "%s%s",
548 pathfile
, aux_index
->indexname
);
550 if (access(file_aux
, F_OK
) == 0) {
555 mk_mem_free(file_aux
);
556 aux_index
= aux_index
->next
;
562 /* Send error responses */
563 void mk_request_error(int num_error
, struct client_request
*cr
,
564 struct request
*s_request
, int debug
)
566 char *aux_message
= 0;
567 mk_pointer message
, *page
= 0;
571 * fixme s_log->error_details.data = NULL;
575 case M_CLIENT_BAD_REQUEST
:
576 page
= mk_request_set_default_page("Bad Request",
578 s_request
->host_conf
->
582 case M_CLIENT_FORBIDDEN
:
583 page
= mk_request_set_default_page("Forbidden",
585 s_request
->host_conf
->
587 //s_log->error_msg = request_error_msg_403;
588 //s_log->error_details = s_request->uri;
591 case M_CLIENT_NOT_FOUND
:
592 mk_string_build(&message
.data
, &message
.len
,
593 "The requested URL was not found on this server.");
594 page
= mk_request_set_default_page("Not Found",
596 s_request
->host_conf
->
598 //s_log->error_msg = request_error_msg_404;
599 //s_log->error_details = s_request->uri;
601 mk_pointer_free(&message
);
604 case M_CLIENT_METHOD_NOT_ALLOWED
:
605 page
= mk_request_set_default_page("Method Not Allowed",
607 s_request
->host_conf
->
610 //s_log->final_response = M_CLIENT_METHOD_NOT_ALLOWED;
611 //s_log->error_msg = request_error_msg_405;
612 //s_log->error_details = s_request->method_p;
615 case M_CLIENT_REQUEST_TIMEOUT
:
616 //s_log->status = S_LOG_OFF;
617 //s_log->error_msg = request_error_msg_408;
620 case M_CLIENT_LENGTH_REQUIRED
:
621 //s_log->error_msg = request_error_msg_411;
624 case M_SERVER_NOT_IMPLEMENTED
:
625 page
= mk_request_set_default_page("Method Not Implemented",
627 s_request
->host_conf
->
629 //s_log->final_response = M_SERVER_NOT_IMPLEMENTED;
630 //s_log->error_msg = request_error_msg_501;
631 //s_log->error_details = s_request->method_p;
634 case M_SERVER_INTERNAL_ERROR
:
635 mk_string_build(&message
.data
, &message
.len
,
636 "Problems found running %s ", s_request
->uri
);
637 page
= mk_request_set_default_page("Internal Server Error",
639 s_request
->host_conf
->
641 //s_log->error_msg = request_error_msg_500;
643 mk_pointer_free(&message
);
646 case M_SERVER_HTTP_VERSION_UNSUP
:
647 mk_pointer_reset(&message
);
648 page
= mk_request_set_default_page("HTTP Version Not Supported",
650 s_request
->host_conf
->
652 //s_log->error_msg = request_error_msg_505;
656 //s_log->final_response = num_error;
658 s_request
->headers
->status
= num_error
;
660 s_request
->headers
->content_length
= page
->len
;
663 s_request
->headers
->location
= NULL
;
664 s_request
->headers
->cgi
= SH_NOCGI
;
665 s_request
->headers
->pconnections_left
= 0;
666 mk_pointer_reset(&s_request
->headers
->last_modified
);
669 mk_mem_free(aux_message
);
672 mk_pointer_reset(&s_request
->headers
->content_type
);
675 mk_pointer_set(&s_request
->headers
->content_type
, "text/html\r\n");
678 mk_header_send(cr
->socket
, cr
, s_request
);
681 n
= write(cr
->socket
, page
->data
, page
->len
);
682 mk_pointer_free(page
);
687 /* Build error page */
688 mk_pointer
*mk_request_set_default_page(char *title
, mk_pointer message
,
694 p
= mk_mem_malloc(sizeof(mk_pointer
));
696 temp
= mk_pointer_to_buf(message
);
697 mk_string_build(&p
->data
, &p
->len
,
698 MK_REQUEST_DEFAULT_PAGE
, title
, temp
, signature
);
704 /* Create a memory allocation in order to handle the request data */
705 struct request
*mk_request_alloc()
707 struct request
*request
= 0;
709 request
= mk_mem_malloc(sizeof(struct request
));
710 request
->status
= VAR_OFF
; /* Request not processed yet */
711 request
->make_log
= VAR_ON
; /* build log file of this request ? */
712 request
->close_now
= VAR_OFF
;
714 mk_pointer_reset(&request
->body
);
715 request
->status
= VAR_ON
;
716 request
->method
= METHOD_NOT_FOUND
;
718 mk_pointer_reset(&request
->uri
);
719 request
->uri_processed
= NULL
;
720 request
->uri_twin
= VAR_OFF
;
722 request
->accept
.data
= NULL
;
723 request
->accept_language
.data
= NULL
;
724 request
->accept_encoding
.data
= NULL
;
725 request
->accept_charset
.data
= NULL
;
726 request
->content_length
= 0;
727 request
->content_type
.data
= NULL
;
728 request
->connection
.data
= NULL
;
729 request
->cookies
.data
= NULL
;
730 request
->host
.data
= NULL
;
731 request
->if_modified_since
.data
= NULL
;
732 request
->last_modified_since
.data
= NULL
;
733 request
->range
.data
= NULL
;
734 request
->referer
.data
= NULL
;
735 request
->resume
.data
= NULL
;
736 request
->user_agent
.data
= NULL
;
738 request
->post_variables
.data
= NULL
;
740 request
->user_uri
= NULL
;
741 mk_pointer_reset(&request
->query_string
);
743 request
->file_info
= NULL
;
744 request
->virtual_user
= NULL
;
745 request
->script_filename
= NULL
;
746 mk_pointer_reset(&request
->real_path
);
747 request
->host_conf
= config
->hosts
;
750 request
->bytes_to_send
= -1;
751 request
->bytes_offset
= 0;
752 request
->fd_file
= -1;
754 /* Response Headers */
755 request
->headers
= mk_header_create();
758 request
->handled_by
= NULL
;
763 void mk_request_free_list(struct client_request
*cr
)
765 struct request
*sr
= 0, *before
= 0;
769 while (cr
->request
) {
770 sr
= before
= cr
->request
;
776 if (sr
!= cr
->request
) {
777 while (before
->next
!= sr
) {
778 before
= before
->next
;
790 void mk_request_free(struct request
*sr
)
792 if (sr
->fd_file
> 0) {
796 mk_mem_free(sr
->headers
->location
);
797 mk_pointer_free(&sr
->headers
->last_modified
);
798 mk_mem_free(sr
->headers
);
801 mk_pointer_reset(&sr
->body
);
802 mk_pointer_reset(&sr
->uri
);
804 if (sr
->uri_twin
== VAR_ON
) {
805 mk_mem_free(sr
->uri_processed
);
808 mk_pointer_free(&sr
->post_variables
);
809 mk_mem_free(sr
->user_uri
);
810 mk_pointer_reset(&sr
->query_string
);
812 mk_mem_free(sr
->file_info
);
813 mk_mem_free(sr
->virtual_user
);
814 mk_mem_free(sr
->script_filename
);
815 mk_pointer_free(&sr
->real_path
);
819 /* Create a client request struct and put it on the
822 struct client_request
*mk_request_client_create(int socket
)
824 struct request_idx
*request_index
;
825 struct client_request
*cr
;
826 struct sched_connection
*sc
;
828 sc
= mk_sched_get_connection(NULL
, socket
);
829 cr
= mk_mem_malloc(sizeof(struct client_request
));
832 cr
->ipv4
= &sc
->ipv4
;
834 cr
->pipelined
= FALSE
;
835 cr
->counter_connections
= 0;
837 cr
->status
= MK_REQUEST_STATUS_INCOMPLETE
;
840 /* creation time in unix time */
841 cr
->init_time
= sc
->arrive_time
;
844 cr
->body
= mk_mem_malloc(MAX_REQUEST_BODY
);
846 cr
->body_pos_end
= -1;
847 cr
->first_method
= HTTP_METHOD_UNKNOWN
;
849 /* Add this request to the thread request list */
850 request_index
= mk_sched_get_request_index();
851 if (!request_index
->first
) {
852 request_index
->first
= request_index
->last
= cr
;
855 request_index
->last
->next
= cr
;
856 request_index
->last
= cr
;
859 /* Set again the global list */
860 mk_sched_set_request_index(request_index
);
865 struct client_request
*mk_request_client_get(int socket
)
867 struct request_idx
*request_index
;
868 struct client_request
*cr
= NULL
;
870 request_index
= mk_sched_get_request_index();
871 cr
= request_index
->first
;
873 if (cr
->socket
== socket
) {
883 * From thread sched_list_node "list", remove the client_request
886 void mk_request_client_remove(int socket
)
888 struct request_idx
*request_index
;
889 struct client_request
*cr
, *aux
;
891 request_index
= mk_sched_get_request_index();
892 cr
= request_index
->first
;
895 if (cr
->socket
== socket
) {
896 if (cr
== request_index
->first
) {
897 request_index
->first
= cr
->next
;
900 aux
= request_index
->first
;
901 while (aux
->next
!= cr
) {
904 aux
->next
= cr
->next
;
906 request_index
->last
= aux
;
914 mk_mem_free(cr
->body
);
917 /* Update thread index */
918 mk_sched_set_request_index(request_index
);
921 struct header_toc
*mk_request_header_toc_create(int len
)
924 struct header_toc
*p
;
926 p
= (struct header_toc
*) pthread_getspecific(mk_cache_header_toc
);
928 for (i
= 0; i
< len
; i
++) {
936 void mk_request_header_toc_parse(struct header_toc
*toc
, int toc_len
, char *data
, int len
)
942 for (i
= 0; i
< toc_len
&& p
&& l
< data
+ len
; i
++) {
943 l
= strstr(p
, MK_CRLF
);
955 void mk_request_ka_next(struct client_request
*cr
)
957 bzero(cr
->body
, sizeof(cr
->body
));
958 cr
->first_method
= -1;
959 cr
->body_pos_end
= -1;
961 cr
->counter_connections
++;
963 /* Update data for scheduler */
964 cr
->init_time
= log_current_utime
;
965 cr
->status
= MK_REQUEST_STATUS_INCOMPLETE
;