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

Contents of /hydra/src/request.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations)
Tue Sep 24 21:47:26 2002 UTC (21 years, 6 months ago) by nmav
Branch: MAIN
Changes since 1.6: +11 -18 lines
File MIME type: text/plain
cleaned up the new virtual hosting code.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26