/[hydra]/hydra/src/request.c
ViewVC logotype

Contents of /hydra/src/request.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.25 - (show annotations)
Thu Oct 3 11:26:15 2002 UTC (18 years, 11 months ago) by nmav
Branch: MAIN
CVS Tags: hydra_0_0_6
Changes since 1.24: +57 -23 lines
File MIME type: text/plain
Improved the max_connections stuff.

1 /*
2 * Hydra, an http server
3 * Copyright (C) 1995 Paul Phillips <paulp@go2net.com>
4 * Some changes Copyright (C) 1996,97 Larry Doolittle <ldoolitt@boa.org>
5 * Some changes Copyright (C) 1996-2002 Jon Nelson <jnelson@boa.org>
6 * Portions Copyright (C) 2002 Nikos Mavroyanopoulos <nmav@gnutls.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 1, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 */
23
24 /* $Id: request.c,v 1.24 2002/10/02 20:20:45 nmav Exp $*/
25
26 #include "boa.h"
27 #include <stddef.h> /* for offsetof */
28 #include "ssl.h"
29 #include "socket.h"
30
31 extern int boa_ssl;
32
33 /* function prototypes located in this file only */
34 static void free_request( server_params* params, request ** list_head_addr,
35 request * req);
36
37 /*
38 * Name: new_request
39 * Description: Obtains a request struct off the free list, or if the
40 * free list is empty, allocates memory
41 *
42 * Return value: pointer to initialized request
43 */
44
45 request *new_request(server_params* params)
46 {
47 request *req;
48
49 if (params->request_free) {
50 req = params->request_free; /* first on free list */
51 dequeue(&params->request_free, params->request_free); /* dequeue the head */
52 } else {
53 req = (request *) malloc(sizeof (request));
54 if (!req) {
55 log_error_time();
56 perror("malloc for new request");
57 return NULL;
58 }
59 }
60
61 memset(req, 0, offsetof(request, buffer) + 1);
62 req->data_fd = -1;
63 req->post_data_fd = -1;
64
65 return req;
66 }
67
68 #ifdef ENABLE_SMP
69 static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER;
70 #endif
71
72 static int total_global_connections = 0;
73
74 /* Decreases total_global_connections, but does some locking
75 * too.
76 */
77 inline
78 static void decrease_global_total_connections()
79 {
80 /* if we do want to serv as much as possible, then
81 * don't bother counting connections.
82 */
83 if (max_connections == INT_MAX) return;
84
85 #ifdef ENABLE_SMP
86 pthread_mutex_lock( &accept_mutex);
87 #endif
88 total_global_connections--;
89 #ifdef ENABLE_SMP
90 pthread_mutex_unlock( &accept_mutex);
91 #endif
92
93 }
94
95 /*
96 * Name: get_request
97 *
98 * Description: Polls the server socket for a request. If one exists,
99 * does some basic initialization and adds it to the ready queue;.
100 */
101
102 void get_request(server_params* params, socket_type *server_s)
103 {
104 int fd; /* socket */
105 struct SOCKADDR remote_addr; /* address */
106 struct SOCKADDR salocal;
107 int remote_addrlen = sizeof (struct SOCKADDR);
108 request *conn; /* connection */
109 size_t len;
110 static int system_bufsize = 0; /* Default size of SNDBUF given by system */
111 #ifdef ENABLE_SSL
112 gnutls_session ssl_state = NULL;
113 #endif
114
115 remote_addr.S_FAMILY = 0xdead;
116
117 #ifdef ENABLE_SMP
118 /* here we make use of the fact that server_s.secure is
119 * 0 or 1, and we have 2 mutexes, one for the secure port,
120 * and one of the normal http port.
121 */
122 pthread_mutex_lock( &accept_mutex);
123 #endif
124
125 /* If we have reached our max connections limit
126 */
127 if (total_global_connections >= max_connections) {
128 server_s->pending_requests = 0;
129 goto unlock;
130 }
131
132 fd = accept(server_s->socket, (struct sockaddr *) &remote_addr,
133 &remote_addrlen);
134
135 if (fd == -1) {
136 if (errno != EAGAIN && errno != EWOULDBLOCK)
137 /* abnormal error */
138 WARN("accept");
139 else
140 /* no requests */
141 server_s->pending_requests = 0;
142 goto unlock;
143 }
144
145 /* only count, if we have enabled a connection limit */
146 if (max_connections != INT_MAX)
147 total_global_connections++;
148
149 #ifdef ENABLE_SMP
150 /* No dead lock conditions here, since accept() is non blocking.
151 */
152 pthread_mutex_unlock( &accept_mutex);
153 #endif
154
155 if (fd >= FD_SETSIZE) {
156 WARN("Got fd >= FD_SETSIZE.");
157 close(fd);
158 return;
159 }
160 #ifdef DEBUGNONINET
161 /* This shows up due to race conditions in some Linux kernels
162 when the client closes the socket sometime between
163 the select() and accept() syscalls.
164 Code and description by Larry Doolittle <ldoolitt@boa.org>
165 */
166 #define HEX(x) (((x)>9)?(('a'-10)+(x)):('0'+(x)))
167 if (remote_addr.sin_family != AF_INET) {
168 struct sockaddr *bogus = (struct sockaddr *) &remote_addr;
169 char *ap, ablock[44];
170 int i;
171 close(fd);
172 log_error_time();
173 for (ap = ablock, i = 0; i < remote_addrlen && i < 14; i++) {
174 *ap++ = ' ';
175 *ap++ = HEX((bogus->sa_data[i] >> 4) & 0x0f);
176 *ap++ = HEX(bogus->sa_data[i] & 0x0f);
177 }
178 *ap = '\0';
179 fprintf(stderr, "non-INET connection attempt: socket %d, "
180 "sa_family = %hu, sa_data[%d] = %s\n",
181 fd, bogus->sa_family, remote_addrlen, ablock);
182 return;
183 }
184 #endif
185
186 /* XXX Either delete this, or document why it's needed */
187 /* Pointed out 3-Oct-1999 by Paul Saab <paul@mu.org> */
188 #ifdef REUSE_EACH_CLIENT_CONNECTION_SOCKET
189 if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt,
190 sizeof (sock_opt))) == -1) {
191 DIE("setsockopt: unable to set SO_REUSEADDR");
192 }
193 #endif
194
195 #ifdef ENABLE_SSL
196 if ( server_s->secure) {
197 ssl_state = initialize_ssl_session();
198 if (ssl_state == NULL) {
199 WARN("Could not initialize ssl state.");
200 close(fd);
201 return;
202 }
203
204 gnutls_transport_set_ptr( ssl_state, fd);
205 }
206 #endif
207
208 len = sizeof(salocal);
209
210 if (getsockname(fd, (struct sockaddr *) &salocal, &len) != 0) {
211 WARN("getsockname");
212 close(fd);
213 return;
214 }
215
216 conn = new_request( params);
217 if (!conn) {
218 close(fd);
219 return;
220 }
221 conn->fd = fd;
222 #ifdef ENABLE_SSL
223 conn->ssl_state = ssl_state;
224 #endif
225
226 if (server_s->secure != 0) conn->secure = 1;
227 else conn->secure = 0;
228
229 if ( server_s->secure != 0)
230 conn->status = FINISH_HANDSHAKE;
231 else conn->status = READ_HEADER;
232
233 conn->header_line = conn->client_stream;
234 conn->time_last = current_time;
235 conn->kacount = ka_max;
236
237 ascii_sockaddr(&salocal, conn->local_ip_addr, NI_MAXHOST);
238
239 if (default_document_root)
240 memcpy( conn->document_root, default_document_root, default_document_root_size + 1);
241
242 if (server_name)
243 conn->hostname = server_name;
244
245 /* nonblocking socket */
246 if (set_nonblock_fd(conn->fd) == -1)
247 WARN("fcntl: unable to set new socket to non-block");
248
249 /* set close on exec to true */
250 if (fcntl(conn->fd, F_SETFD, 1) == -1)
251 WARN("fctnl: unable to set close-on-exec for new socket");
252
253 /* Increase buffer size if we have to.
254 * Only ask the system the buffer size on the first request,
255 * and assume all subsequent sockets have the same size.
256 */
257 if (system_bufsize == 0) {
258 len = sizeof (system_bufsize);
259 if (getsockopt
260 (conn->fd, SOL_SOCKET, SO_SNDBUF, &system_bufsize, &len) == 0
261 && len == sizeof (system_bufsize)) {
262 /*
263 fprintf(stderr, "%sgetsockopt reports SNDBUF %d\n",
264 get_commonlog_time(), system_bufsize);
265 */
266 ;
267 } else {
268 WARN("getsockopt(SNDBUF)");
269 system_bufsize = 1;
270 }
271 }
272 if (system_bufsize < params->sockbufsize) {
273 if (setsockopt
274 (conn->fd, SOL_SOCKET, SO_SNDBUF, (void *) &params->sockbufsize,
275 sizeof (params->sockbufsize)) == -1) {
276 WARN("setsockopt: unable to set socket buffer size");
277 #ifdef DIE_ON_ERROR_TUNING_SNDBUF
278 exit(errno);
279 #endif
280 }
281 }
282
283 /* for log file and possible use by CGI programs */
284 ascii_sockaddr(&remote_addr, conn->remote_ip_addr, NI_MAXHOST);
285
286 /* for possible use by CGI programs */
287 conn->remote_port = net_port(&remote_addr);
288
289 params->status.requests++;
290
291 #ifdef HAVE_TCP_CORK
292 {
293 int one = 1;
294 if (setsockopt(conn->fd, IPPROTO_TCP, TCP_CORK,
295 (void *) &one, sizeof (one)) == -1) {
296 WARN("setsockopt: unable to set TCP_CORK");
297 }
298
299 }
300 #endif /* TCP_CORK */
301
302 params->total_connections++;
303
304 enqueue(&params->request_ready, conn);
305
306 return;
307
308 unlock:
309 #ifdef ENABLE_SMP
310 pthread_mutex_unlock( &accept_mutex);
311 #endif
312 return;
313 }
314
315
316 /*
317 * Name: free_request
318 *
319 * Description: Deallocates memory for a finished request and closes
320 * down socket.
321 */
322
323 static void free_request( server_params *params, request ** list_head_addr, request * req)
324 {
325 int i;
326 /* free_request should *never* get called by anything but
327 process_requests */
328
329 if (req->buffer_end && req->status != DEAD) {
330 req->status = DONE;
331 return;
332 }
333 /* put request on the free list */
334 dequeue(list_head_addr, req); /* dequeue from ready or block list */
335
336 if (req->logline) /* access log */
337 log_access(req);
338
339 if (req->mmap_entry_var)
340 release_mmap( req->mmap_entry_var);
341 /* FIXME: Why is it needed? */ else if (req->data_mem)
342 munmap(req->data_mem, req->filesize);
343
344 if (req->data_fd != -1)
345 close(req->data_fd);
346
347 if (req->post_data_fd != -1)
348 close(req->post_data_fd);
349
350 if (req->response_status >= 400)
351 params->status.errors++;
352
353 for (i = COMMON_CGI_COUNT; i < req->cgi_env_index; ++i) {
354 if (req->cgi_env[i]) {
355 free(req->cgi_env[i]);
356 } else {
357 log_error_time();
358 fprintf(stderr, "Warning: CGI Environment contains NULL value" \
359 "(index %d of %d).\n", i, req->cgi_env_index);
360 }
361 }
362
363 if (req->pathname)
364 free(req->pathname);
365 if (req->query_string)
366 free(req->query_string);
367 if (req->path_info)
368 free(req->path_info);
369 if (req->path_translated)
370 free(req->path_translated);
371 if (req->script_name)
372 free(req->script_name);
373
374 if ((req->keepalive == KA_ACTIVE) &&
375 (req->response_status < 500) && req->kacount > 0) {
376 int bytes_to_move;
377
378 request *conn = new_request( params);
379 if (!conn) {
380 /* errors already reported */
381 enqueue(&params->request_free, req);
382 close(req->fd);
383 params->total_connections--;
384 decrease_global_total_connections();
385 return;
386 }
387 conn->fd = req->fd;
388
389 #ifdef ENABLE_SSL
390 if ( req->secure != 0) {
391 conn->ssl_state = initialize_ssl_session();
392 conn->secure = 1;
393
394 if (conn->ssl_state == NULL) {
395 enqueue(&params->request_free, req);
396 close(req->fd);
397 params->total_connections--;
398 decrease_global_total_connections();
399 return;
400 }
401
402 gnutls_transport_set_ptr( conn->ssl_state, conn->fd);
403 conn->status = FINISH_HANDSHAKE;
404 } else {
405 #endif
406 conn->secure = 0;
407 conn->status = READ_HEADER;
408 #ifdef ENABLE_SSL
409 conn->ssl_state = NULL;
410 }
411 #endif
412
413 conn->header_line = conn->client_stream;
414 conn->kacount = req->kacount - 1;
415
416 /* close enough and we avoid a call to time(NULL) */
417 conn->time_last = req->time_last;
418
419 /* for log file and possible use by CGI programs */
420 memcpy(conn->remote_ip_addr, req->remote_ip_addr, NI_MAXHOST);
421 memcpy(conn->local_ip_addr, req->local_ip_addr, NI_MAXHOST);
422
423 /* for possible use by CGI programs */
424 conn->remote_port = req->remote_port;
425
426 params->status.requests++;
427
428 /* we haven't parsed beyond req->parse_pos, so... */
429 bytes_to_move = req->client_stream_pos - req->parse_pos;
430
431 if (bytes_to_move) {
432 memcpy(conn->client_stream,
433 req->client_stream + req->parse_pos, bytes_to_move);
434 conn->client_stream_pos = bytes_to_move;
435 }
436 enqueue(&params->request_block, conn);
437
438 BOA_FD_SET(conn->fd, &params->block_read_fdset);
439
440 enqueue(&params->request_free, req);
441
442 return;
443 }
444
445 /*
446 While debugging some weird errors, Jon Nelson learned that
447 some versions of Netscape Navigator break the
448 HTTP specification.
449
450 Some research on the issue brought up:
451
452 http://www.apache.org/docs/misc/known_client_problems.html
453
454 As quoted here:
455
456 "
457 Trailing CRLF on POSTs
458
459 This is a legacy issue. The CERN webserver required POST
460 data to have an extra CRLF following it. Thus many
461 clients send an extra CRLF that is not included in the
462 Content-Length of the request. Apache works around this
463 problem by eating any empty lines which appear before a
464 request.
465 "
466
467 Boa will (for now) hack around this stupid bug in Netscape
468 (and Internet Exploder)
469 by reading up to 32k after the connection is all but closed.
470 This should eliminate any remaining spurious crlf sent
471 by the client.
472
473 Building bugs *into* software to be compatable is
474 just plain wrong
475 */
476
477 if (req->method == M_POST) {
478 char buf[32768];
479
480 socket_recv( req, buf, sizeof(buf));
481 }
482
483 #ifdef ENABLE_SSL
484 if ( req->secure) {
485 gnutls_bye(req->ssl_state, GNUTLS_SHUT_WR);
486 gnutls_deinit( req->ssl_state);
487 }
488 #endif
489 /* Needed when TCP_CORK is used... */
490 socket_flush( req->fd);
491
492 close(req->fd);
493
494 params->total_connections--;
495 decrease_global_total_connections();
496
497 enqueue(&params->request_free, req);
498
499 return;
500 }
501
502 /*
503 * Name: process_requests
504 *
505 * Description: Iterates through the ready queue, passing each request
506 * to the appropriate handler for processing. It monitors the
507 * return value from handler functions, all of which return -1
508 * to indicate a block, 0 on completion and 1 to remain on the
509 * ready list for more procesing.
510 */
511
512 void process_requests(server_params* params, socket_type *server_s)
513 {
514 int retval = 0;
515 request *current, *trailer;
516
517 if (server_s->pending_requests) {
518 get_request(params, server_s);
519 #ifdef ORIGINAL_BEHAVIOR
520 server_s->pending_requests = 0;
521 #endif
522 }
523
524 current = params->request_ready;
525
526 while (current) {
527 time(&current_time);
528 if (current->buffer_end && /* there is data in the buffer */
529 current->status != DEAD && current->status != DONE) {
530 retval = req_flush(current);
531 /*
532 * retval can be -2=error, -1=blocked, or bytes left
533 */
534 if (retval == -2) { /* error */
535 current->status = DEAD;
536 retval = 0;
537 } else if (retval >= 0) {
538 /* notice the >= which is different from below?
539 Here, we may just be flushing headers.
540 We don't want to return 0 because we are not DONE
541 or DEAD */
542
543 retval = 1;
544 }
545 } else {
546 switch (current->status) {
547 #ifdef ENABLE_SSL
548 case FINISH_HANDSHAKE:
549 retval = finish_handshake( current);
550 break;
551 case SEND_ALERT:
552 retval = send_alert( current);
553 break;
554 #endif
555 case READ_HEADER:
556 case ONE_CR:
557 case ONE_LF:
558 case TWO_CR:
559 retval = read_header(params, current);
560 break;
561 case BODY_READ:
562 retval = read_body(current);
563 break;
564 case BODY_WRITE:
565 retval = write_body(current);
566 break;
567 case WRITE:
568 retval = process_get(params, current);
569 break;
570 case PIPE_READ:
571 retval = read_from_pipe(current);
572 break;
573 case PIPE_WRITE:
574 retval = write_from_pipe(current);
575 break;
576 case DONE:
577 /* a non-status that will terminate the request */
578 retval = req_flush(current);
579 /*
580 * retval can be -2=error, -1=blocked, or bytes left
581 */
582 if (retval == -2) { /* error */
583 current->status = DEAD;
584 retval = 0;
585 } else if (retval > 0) {
586 retval = 1;
587 }
588 break;
589 case DEAD:
590 retval = 0;
591 current->buffer_end = 0;
592 SQUASH_KA(current);
593 break;
594 default:
595 retval = 0;
596 fprintf(stderr, "Unknown status (%d), "
597 "closing!\n", current->status);
598 current->status = DEAD;
599 break;
600 }
601
602 }
603
604 if (params->sigterm_flag)
605 SQUASH_KA(current);
606
607 /* we put this here instead of after the switch so that
608 * if we are on the last request, and get_request is successful,
609 * current->next is valid!
610 */
611 if (server_s->pending_requests)
612 get_request(params, server_s);
613
614 switch (retval) {
615 case -1: /* request blocked */
616 trailer = current;
617 current = current->next;
618 block_request(params, trailer);
619 break;
620 case 0: /* request complete */
621 current->time_last = current_time;
622 trailer = current;
623 current = current->next;
624 free_request(params, &params->request_ready, trailer);
625 break;
626 case 1: /* more to do */
627 current->time_last = current_time;
628 current = current->next;
629 break;
630 default:
631 log_error_time();
632 fprintf(stderr, "Unknown retval in process.c - "
633 "Status: %d, retval: %d\n", current->status, retval);
634 current = current->next;
635 break;
636 }
637 }
638 }
639
640 /*
641 * Name: process_logline
642 *
643 * Description: This is called with the first req->header_line received
644 * by a request, called "logline" because it is logged to a file.
645 * It is parsed to determine request type and method, then passed to
646 * translate_uri for further parsing. Also sets up CGI environment if
647 * needed.
648 */
649 #define SIMPLE_HTTP_VERSION "HTTP/0.9"
650 int process_logline(request * req)
651 {
652 char *stop, *stop2;
653
654 req->logline = req->client_stream;
655 if (!memcmp(req->logline, "GET ", 4))
656 req->method = M_GET;
657 else if (!memcmp(req->logline, "HEAD ", 5))
658 /* head is just get w/no body */
659 req->method = M_HEAD;
660 else if (!memcmp(req->logline, "POST ", 5))
661 req->method = M_POST;
662 else {
663 log_error_time();
664 fprintf(stderr, "malformed request: \"%s\"\n", req->logline);
665 send_r_not_implemented(req);
666 return 0;
667 }
668
669 req->http_version = SIMPLE_HTTP_VERSION;
670 req->simple = 1;
671
672 /* Guaranteed to find ' ' since we matched a method above */
673 stop = req->logline + 3;
674 if (*stop != ' ')
675 ++stop;
676
677 /* scan to start of non-whitespace */
678 while (*(++stop) == ' ');
679
680 stop2 = stop;
681
682 /* scan to end of non-whitespace */
683 while (*stop2 != '\0' && *stop2 != ' ')
684 ++stop2;
685
686 if (stop2 - stop > MAX_HEADER_LENGTH) {
687 log_error_time();
688 fprintf(stderr, "URI too long %d: \"%s\"\n", MAX_HEADER_LENGTH,
689 req->logline);
690 send_r_bad_request(req);
691 return 0;
692 }
693 memcpy(req->request_uri, stop, stop2 - stop);
694 req->request_uri[stop2 - stop] = '\0';
695
696 if (*stop2 == ' ') {
697 /* if found, we should get an HTTP/x.x */
698 unsigned int p1, p2;
699
700 /* scan to end of whitespace */
701 ++stop2;
702 while (*stop2 == ' ' && *stop2 != '\0')
703 ++stop2;
704
705 /* scan in HTTP/major.minor */
706 if (sscanf(stop2, "HTTP/%u.%u", &p1, &p2) == 2) {
707 /* HTTP/{0.9,1.0,1.1} */
708 if (p1 == 1) { /* We accept all HTTP/1.x versions */
709 req->http_version = stop2;
710 req->simple = 0;
711 } else if (p1 > 1) { /* major number > 1 is invalid for us */
712 goto BAD_VERSION;
713 }
714 } else {
715 goto BAD_VERSION;
716 }
717 }
718
719 if (req->method == M_HEAD && req->simple) {
720 send_r_bad_request(req);
721 return 0;
722 }
723 req->cgi_env_index = COMMON_CGI_COUNT;
724
725 return 1;
726
727 BAD_VERSION:
728 log_error_time();
729 fprintf(stderr, "bogus HTTP version: \"%s\"\n", stop2);
730 send_r_bad_request(req);
731 return 0;
732 }
733
734 /*
735 * Name: process_header_end
736 *
737 * Description: takes a request and performs some final checking before
738 * init_cgi or init_get
739 * Returns 0 for error or NPH, or 1 for success
740 */
741
742 int process_header_end(server_params* params, request * req)
743 {
744 char *p = NULL;
745
746 if (!req->logline) {
747 send_r_error(req);
748 return 0;
749 }
750
751 /* Percent-decode request */
752 if (unescape_uri(req->request_uri, &p) == 0) {
753 log_error_doc(req);
754 fputs("Problem unescaping uri\n", stderr);
755 send_r_bad_request(req);
756 return 0;
757 }
758
759 if (p) {
760 req->query_string = strdup( p);
761 if (req->query_string == NULL) {
762 send_r_error( req);
763 return 0;
764 }
765 }
766
767 /* clean pathname */
768 clean_pathname(req->request_uri);
769
770 if (req->request_uri[0] != '/') {
771 send_r_bad_request(req);
772 return 0;
773 }
774
775 if (translate_uri(req) == 0) { /* unescape, parse uri */
776 SQUASH_KA(req);
777 return 0; /* failure, close down */
778 }
779
780 if (req->method == M_POST) {
781 req->post_data_fd = create_temporary_file(1, NULL, 0);
782 if (req->post_data_fd == -1)
783 return(0);
784 return(1); /* success */
785 }
786
787 if (req->is_cgi) {
788 return init_cgi(req);
789 }
790
791 req->status = WRITE;
792 return init_get(params, req); /* get and head */
793 }
794
795 /* Parses HTTP/1.1 range values.
796 */
797 static int parse_range( const char* value, unsigned long *p1, unsigned long *p2)
798 {
799 int ret = 0;
800 int len;
801
802 *p1 = *p2 = 0;
803
804 len = strlen( value);
805 if (len < 7) return -1;
806
807 /* we do not accept ranges of the form 10-20,21-30
808 */
809 if (strchr( value, ',') != NULL) return -1;
810
811 if ( memcmp("bytes=", value, 6) != 0) {
812 return -1;
813 } else value += 6;
814
815 if (strchr( value, '-') == NULL) return -1;
816
817 if (value[0] == '-') {
818 *p2 = boa_atol( &value[1]);
819 return 0;
820 } else
821 ret = sscanf( value, "%lu-%lu", p1, p2);
822
823 if (ret == 1) {
824 /* This one accepts ranges of both "-stop" and "start-"
825 */
826 return 0;
827 }
828
829 if (ret == 2)
830 return 0;
831
832 return -1;
833 }
834
835 inline
836 static void init_range_stuff( request* req, char* value)
837 {
838 unsigned long int p1, p2;
839 if (parse_range( value, &p1, &p2) == 0) {
840 req->range_start = p1;
841 req->range_stop = p2;
842 } else {
843 req->range_start = 0;
844 req->range_stop = 0;
845 log_error_time();
846 fprintf(stderr, "bogus range: \"%s\"\n", value);
847 /* we just ignore a bogus range */
848 /* send_r_bad_request(req); */
849 }
850 }
851
852 inline
853 static void init_vhost_stuff( request* req, char* value)
854 {
855 virthost* vhost;
856 int valuelen;
857
858 valuelen = strlen(value);
859
860 vhost = find_virthost( value, valuelen);
861
862 if ( vhost && ( vhost->ip == NULL || !memcmp( vhost->ip, req->local_ip_addr, vhost->ip_len) ))
863 {
864 req->hostname = value;
865 memcpy( req->document_root, vhost->document_root, vhost->document_root_len + 1);
866 if (vhost->user_dir)
867 memcpy( req->user_dir, vhost->user_dir, vhost->user_dir_len + 1);
868
869 } else { /* No virtual host found. use defaults */
870 if ( default_document_root)
871 memcpy( req->document_root, default_document_root, default_document_root_size + 1);
872 }
873 }
874
875 /*
876 * Name: process_option_line
877 *
878 * Description: Parses the contents of req->header_line and takes
879 * appropriate action.
880 */
881
882 int process_option_line(request * req)
883 {
884 char c, *value, *line = req->header_line;
885
886 /* Start by aggressively hacking the in-place copy of the header line */
887
888 #ifdef FASCIST_LOGGING
889 log_error_time();
890 fprintf(stderr, "%s:%d - Parsing \"%s\"\n", __FILE__, __LINE__, line);
891 #endif
892
893 value = strchr(line, ':');
894 if (value == NULL)
895 return 0;
896 *value++ = '\0'; /* overwrite the : */
897 to_upper(line); /* header types are case-insensitive */
898 while ((c = *value) && (c == ' ' || c == '\t'))
899 value++;
900
901
902 if (!memcmp(line, "CONTENT_TYPE", 13) && !req->content_type)
903 req->content_type = value;
904
905 else if (!memcmp(line, "CONTENT_LENGTH", 15) && !req->content_length)
906 req->content_length = value;
907
908 else if (!memcmp(line, "CONNECTION", 11) &&
909 ka_max && req->keepalive != KA_STOPPED) {
910 req->keepalive = (!strncasecmp(value, "Keep-Alive", 10) ?
911 KA_ACTIVE : KA_STOPPED);
912 }
913 /* #ifdef ACCEPT_ON */
914 else if (!memcmp(line, "ACCEPT", 7))
915 add_accept_header(req, value);
916 /* #endif */
917
918 /* Need agent and referer for logs */
919 else if (!memcmp(line, "REFERER", 8)) {
920 req->header_referer = value;
921 if (!add_cgi_env(req, "REFERER", value, 1))
922 return 0;
923 } else if (!memcmp(line, "USER_AGENT", 11)) {
924 req->header_user_agent = value;
925 if (!add_cgi_env(req, "USER_AGENT", value, 1))
926 return 0;
927 } else if (!memcmp(line, "RANGE", 5)) {
928 init_range_stuff( req, value);
929 } else if (!memcmp(line, "HOST", 4)) {
930 init_vhost_stuff( req, value);
931 if (!add_cgi_env(req, "HOST", value, 1))
932 return 0;
933 } else if (!memcmp(line, "IF_", 3)) {
934 char *p = line+3;
935
936 if (!memcmp( p, "MODIFIED_SINCE", 15) && !req->if_modified_since) {
937 req->if_types |= IF_MODIFIED_SINCE;
938 req->if_modified_since = value;
939
940 } else if (!memcmp( p, "MATCH", 5) && !req->if_match_etag) {
941 req->if_types |= IF_MATCH;
942 req->if_match_etag = value;
943
944 } else if (!memcmp( p, "NONE_MATCH", 10) && !req->if_none_match_etag) {
945 req->if_types |= IF_NONE_MATCH;
946 req->if_none_match_etag = value;
947
948 } else if (!memcmp( p, "RANGE", 5) && !req->if_range_etag) {
949 req->if_types |= IF_RANGE;
950 req->if_range_etag = value;
951 }
952
953 if (!add_cgi_env(req, line, value, 1))
954 return 0;
955 } else {
956 if (!add_cgi_env(req, line, value, 1))
957 return 0;
958 }
959 return 1;
960 }
961
962 /*
963 * Name: add_accept_header
964 * Description: Adds a mime_type to a requests accept char buffer
965 * silently ignore any that don't fit -
966 * shouldn't happen because of relative buffer sizes
967 */
968
969 void add_accept_header(request * req, char *mime_type)
970 {
971 #ifdef ACCEPT_ON
972 int l = strlen(req->accept);
973 int l2 = strlen(mime_type);
974
975 if ((l + l2 + 2) >= MAX_HEADER_LENGTH)
976 return;
977
978 if (req->accept[0] == '\0')
979 strcpy(req->accept, mime_type);
980 else {
981 req->accept[l] = ',';
982 req->accept[l + 1] = ' ';
983 memcpy(req->accept + l + 2, mime_type, l2 + 1);
984 /* the +1 is for the '\0' */
985 /*
986 sprintf(req->accept + l, ", %s", mime_type);
987 */
988 }
989 #endif
990 }
991
992 void free_requests(server_params* params)
993 {
994 request *ptr, *next;
995
996 ptr = params->request_free;
997 while (ptr != NULL) {
998 next = ptr->next;
999 free(ptr);
1000 ptr = next;
1001 }
1002 params->request_free = NULL;
1003 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26