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

Contents of /hydra/src/request.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.22 - (show annotations)
Wed Oct 2 19:26:15 2002 UTC (21 years, 6 months ago) by nmav
Branch: MAIN
Changes since 1.21: +5 -2 lines
File MIME type: text/plain
Better use of limits. If getrlimit() returns a cur limit less than max limit, we increase the cur limit.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26