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

Contents of /hydra/src/request.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (show annotations)
Wed Sep 25 06:42:34 2002 UTC (21 years, 6 months ago) by nmav
Branch: MAIN
CVS Tags: hydra_0_0_2
Changes since 1.7: +44 -33 lines
File MIME type: text/plain
Cleanups. Added support for 3 argument options in the grammar (I'll probably rewrite it soon). Changed the Alias, ScriptAlias and Redirect options, to use the virtual hosting stuff.

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.7 2002/09/24 21:47:26 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 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 if (conn->fd > max_connections) {
270 send_r_service_unavailable(conn);
271 conn->status = DONE;
272 server_s->pending_requests = 0;
273 }
274 #endif /* NO_RATE_LIMIT */
275
276 params->total_connections++;
277
278 enqueue(&params->request_ready, conn);
279 }
280
281
282 /*
283 * Name: free_request
284 *
285 * Description: Deallocates memory for a finished request and closes
286 * down socket.
287 */
288
289 static void free_request( server_params *params, request ** list_head_addr, request * req)
290 {
291 int i;
292 /* free_request should *never* get called by anything but
293 process_requests */
294
295 if (req->buffer_end && req->status != DEAD) {
296 req->status = DONE;
297 return;
298 }
299 /* put request on the free list */
300 dequeue(list_head_addr, req); /* dequeue from ready or block list */
301
302 if (req->logline) /* access log */
303 log_access(req);
304
305 if (req->mmap_entry_var)
306 release_mmap( req->mmap_entry_var);
307 /* FIXME: Why is it needed? */ else if (req->data_mem)
308 munmap(req->data_mem, req->filesize);
309
310 if (req->data_fd != -1)
311 close(req->data_fd);
312
313 if (req->post_data_fd != -1)
314 close(req->post_data_fd);
315
316 if (req->response_status >= 400)
317 params->status.errors++;
318
319 for (i = COMMON_CGI_COUNT; i < req->cgi_env_index; ++i) {
320 if (req->cgi_env[i]) {
321 free(req->cgi_env[i]);
322 } else {
323 log_error_time();
324 fprintf(stderr, "Warning: CGI Environment contains NULL value" \
325 "(index %d of %d).\n", i, req->cgi_env_index);
326 }
327 }
328
329 if (req->pathname)
330 free(req->pathname);
331 if (req->path_info)
332 free(req->path_info);
333 if (req->path_translated)
334 free(req->path_translated);
335 if (req->script_name)
336 free(req->script_name);
337
338 if ((req->keepalive == KA_ACTIVE) &&
339 (req->response_status < 500) && req->kacount > 0) {
340 int bytes_to_move;
341
342 request *conn = new_request( params);
343 if (!conn) {
344 /* errors already reported */
345 enqueue(&params->request_free, req);
346 close(req->fd);
347 params->total_connections--;
348 return;
349 }
350 conn->fd = req->fd;
351
352 #ifdef ENABLE_SSL
353 if ( req->secure != 0) {
354 conn->ssl_state = initialize_ssl_session();
355 conn->secure = 1;
356
357 if (conn->ssl_state == NULL) {
358 enqueue(&params->request_free, req);
359 close(req->fd);
360 params->total_connections--;
361 return;
362 }
363
364 gnutls_transport_set_ptr( conn->ssl_state, conn->fd);
365 conn->status = FINISH_HANDSHAKE;
366 } else {
367 #endif
368 conn->secure = 0;
369 conn->ssl_state = NULL;
370 conn->status = READ_HEADER;
371 #ifdef ENABLE_SSL
372 }
373 #endif
374
375 conn->header_line = conn->client_stream;
376 conn->kacount = req->kacount - 1;
377
378 /* close enough and we avoid a call to time(NULL) */
379 conn->time_last = req->time_last;
380
381 /* for log file and possible use by CGI programs */
382 memcpy(conn->remote_ip_addr, req->remote_ip_addr, NI_MAXHOST);
383 memcpy(conn->local_ip_addr, req->local_ip_addr, NI_MAXHOST);
384
385 /* for possible use by CGI programs */
386 conn->remote_port = req->remote_port;
387
388 params->status.requests++;
389
390 /* we haven't parsed beyond req->parse_pos, so... */
391 bytes_to_move = req->client_stream_pos - req->parse_pos;
392
393 if (bytes_to_move) {
394 memcpy(conn->client_stream,
395 req->client_stream + req->parse_pos, bytes_to_move);
396 conn->client_stream_pos = bytes_to_move;
397 }
398 enqueue(&params->request_block, conn);
399
400 BOA_FD_SET(conn->fd, &params->block_read_fdset);
401
402 enqueue(&params->request_free, req);
403
404 return;
405 }
406
407 /*
408 While debugging some weird errors, Jon Nelson learned that
409 some versions of Netscape Navigator break the
410 HTTP specification.
411
412 Some research on the issue brought up:
413
414 http://www.apache.org/docs/misc/known_client_problems.html
415
416 As quoted here:
417
418 "
419 Trailing CRLF on POSTs
420
421 This is a legacy issue. The CERN webserver required POST
422 data to have an extra CRLF following it. Thus many
423 clients send an extra CRLF that is not included in the
424 Content-Length of the request. Apache works around this
425 problem by eating any empty lines which appear before a
426 request.
427 "
428
429 Boa will (for now) hack around this stupid bug in Netscape
430 (and Internet Exploder)
431 by reading up to 32k after the connection is all but closed.
432 This should eliminate any remaining spurious crlf sent
433 by the client.
434
435 Building bugs *into* software to be compatable is
436 just plain wrong
437 */
438
439 if (req->method == M_POST) {
440 char buf[32768];
441
442 socket_recv( req, buf, sizeof(buf));
443 }
444
445 #ifdef ENABLE_SSL
446 if ( req->secure) {
447 gnutls_bye(req->ssl_state, GNUTLS_SHUT_WR);
448 gnutls_deinit( req->ssl_state);
449 }
450 #endif
451 /* Needed when TCP_CORK is used... */
452 socket_flush( req->fd);
453
454 close(req->fd);
455
456 params->total_connections--;
457
458 enqueue(&params->request_free, req);
459
460 return;
461 }
462
463 /*
464 * Name: process_requests
465 *
466 * Description: Iterates through the ready queue, passing each request
467 * to the appropriate handler for processing. It monitors the
468 * return value from handler functions, all of which return -1
469 * to indicate a block, 0 on completion and 1 to remain on the
470 * ready list for more procesing.
471 */
472
473 void process_requests(server_params* params, socket_type *server_s)
474 {
475 int retval = 0;
476 request *current, *trailer;
477
478 if (server_s->pending_requests) {
479 get_request(params, server_s);
480 #ifdef ORIGINAL_BEHAVIOR
481 server_s->pending_requests = 0;
482 #endif
483 }
484
485 current = params->request_ready;
486
487 while (current) {
488 time(&current_time);
489 if (current->buffer_end && /* there is data in the buffer */
490 current->status != DEAD && current->status != DONE) {
491 retval = req_flush(current);
492 /*
493 * retval can be -2=error, -1=blocked, or bytes left
494 */
495 if (retval == -2) { /* error */
496 current->status = DEAD;
497 retval = 0;
498 } else if (retval >= 0) {
499 /* notice the >= which is different from below?
500 Here, we may just be flushing headers.
501 We don't want to return 0 because we are not DONE
502 or DEAD */
503
504 retval = 1;
505 }
506 } else {
507 switch (current->status) {
508 #ifdef ENABLE_SSL
509 case FINISH_HANDSHAKE:
510 retval = finish_handshake( current);
511 break;
512 case SEND_ALERT:
513 retval = send_alert( current);
514 break;
515 #endif
516 case READ_HEADER:
517 case ONE_CR:
518 case ONE_LF:
519 case TWO_CR:
520 retval = read_header(params, current);
521 break;
522 case BODY_READ:
523 retval = read_body(current);
524 break;
525 case BODY_WRITE:
526 retval = write_body(current);
527 break;
528 case WRITE:
529 retval = process_get(params, current);
530 break;
531 case PIPE_READ:
532 retval = read_from_pipe(current);
533 break;
534 case PIPE_WRITE:
535 retval = write_from_pipe(current);
536 break;
537 case DONE:
538 /* a non-status that will terminate the request */
539 retval = req_flush(current);
540 /*
541 * retval can be -2=error, -1=blocked, or bytes left
542 */
543 if (retval == -2) { /* error */
544 current->status = DEAD;
545 retval = 0;
546 } else if (retval > 0) {
547 retval = 1;
548 }
549 break;
550 case DEAD:
551 retval = 0;
552 current->buffer_end = 0;
553 SQUASH_KA(current);
554 break;
555 default:
556 retval = 0;
557 fprintf(stderr, "Unknown status (%d), "
558 "closing!\n", current->status);
559 current->status = DEAD;
560 break;
561 }
562
563 }
564
565 if (params->sigterm_flag)
566 SQUASH_KA(current);
567
568 /* we put this here instead of after the switch so that
569 * if we are on the last request, and get_request is successful,
570 * current->next is valid!
571 */
572 if (server_s->pending_requests)
573 get_request(params, server_s);
574
575 switch (retval) {
576 case -1: /* request blocked */
577 trailer = current;
578 current = current->next;
579 block_request(params, trailer);
580 break;
581 case 0: /* request complete */
582 current->time_last = current_time;
583 trailer = current;
584 current = current->next;
585 free_request(params, &params->request_ready, trailer);
586 break;
587 case 1: /* more to do */
588 current->time_last = current_time;
589 current = current->next;
590 break;
591 default:
592 log_error_time();
593 fprintf(stderr, "Unknown retval in process.c - "
594 "Status: %d, retval: %d\n", current->status, retval);
595 current = current->next;
596 break;
597 }
598 }
599 }
600
601 /*
602 * Name: process_logline
603 *
604 * Description: This is called with the first req->header_line received
605 * by a request, called "logline" because it is logged to a file.
606 * It is parsed to determine request type and method, then passed to
607 * translate_uri for further parsing. Also sets up CGI environment if
608 * needed.
609 */
610 #define SIMPLE_HTTP_VERSION "HTTP/0.9"
611 int process_logline(request * req)
612 {
613 char *stop, *stop2;
614
615 req->logline = req->client_stream;
616 if (!memcmp(req->logline, "GET ", 4))
617 req->method = M_GET;
618 else if (!memcmp(req->logline, "HEAD ", 5))
619 /* head is just get w/no body */
620 req->method = M_HEAD;
621 else if (!memcmp(req->logline, "POST ", 5))
622 req->method = M_POST;
623 else {
624 log_error_time();
625 fprintf(stderr, "malformed request: \"%s\"\n", req->logline);
626 send_r_not_implemented(req);
627 return 0;
628 }
629
630 req->http_version = SIMPLE_HTTP_VERSION;
631 req->simple = 1;
632
633 /* Guaranteed to find ' ' since we matched a method above */
634 stop = req->logline + 3;
635 if (*stop != ' ')
636 ++stop;
637
638 /* scan to start of non-whitespace */
639 while (*(++stop) == ' ');
640
641 stop2 = stop;
642
643 /* scan to end of non-whitespace */
644 while (*stop2 != '\0' && *stop2 != ' ')
645 ++stop2;
646
647 if (stop2 - stop > MAX_HEADER_LENGTH) {
648 log_error_time();
649 fprintf(stderr, "URI too long %d: \"%s\"\n", MAX_HEADER_LENGTH,
650 req->logline);
651 send_r_bad_request(req);
652 return 0;
653 }
654 memcpy(req->request_uri, stop, stop2 - stop);
655 req->request_uri[stop2 - stop] = '\0';
656
657 if (*stop2 == ' ') {
658 /* if found, we should get an HTTP/x.x */
659 unsigned int p1, p2;
660
661 /* scan to end of whitespace */
662 ++stop2;
663 while (*stop2 == ' ' && *stop2 != '\0')
664 ++stop2;
665
666 /* scan in HTTP/major.minor */
667 if (sscanf(stop2, "HTTP/%u.%u", &p1, &p2) == 2) {
668 /* HTTP/{0.9,1.0,1.1} */
669 if (p1 == 1 && (p2 == 0 || p2 == 1)) {
670 req->http_version = stop2;
671 req->simple = 0;
672 } else if (p1 > 1 || (p1 != 0 && p2 > 1)) {
673 goto BAD_VERSION;
674 }
675 } else {
676 goto BAD_VERSION;
677 }
678 }
679
680 if (req->method == M_HEAD && req->simple) {
681 send_r_bad_request(req);
682 return 0;
683 }
684 req->cgi_env_index = COMMON_CGI_COUNT;
685
686 return 1;
687
688 BAD_VERSION:
689 log_error_time();
690 fprintf(stderr, "bogus HTTP version: \"%s\"\n", stop2);
691 send_r_bad_request(req);
692 return 0;
693 }
694
695 /*
696 * Name: process_header_end
697 *
698 * Description: takes a request and performs some final checking before
699 * init_cgi or init_get
700 * Returns 0 for error or NPH, or 1 for success
701 */
702
703 int process_header_end(server_params* params, request * req)
704 {
705 if (!req->logline) {
706 send_r_error(req);
707 return 0;
708 }
709
710 /* Percent-decode request */
711 if (unescape_uri(req->request_uri, &(req->query_string)) == 0) {
712 log_error_doc(req);
713 fputs("Problem unescaping uri\n", stderr);
714 send_r_bad_request(req);
715 return 0;
716 }
717
718 /* clean pathname */
719 clean_pathname(req->request_uri);
720
721 if (req->request_uri[0] != '/') {
722 send_r_bad_request(req);
723 return 0;
724 }
725
726 if (translate_uri(req) == 0) { /* unescape, parse uri */
727 SQUASH_KA(req);
728 return 0; /* failure, close down */
729 }
730
731 if (req->method == M_POST) {
732 req->post_data_fd = create_temporary_file(1, NULL, 0);
733 if (req->post_data_fd == 0)
734 return(0);
735 return(1); /* success */
736 }
737
738 if (req->is_cgi) {
739 return init_cgi(req);
740 }
741
742 req->status = WRITE;
743 return init_get(params, req); /* get and head */
744 }
745
746 /* Parses HTTP/1.1 range values.
747 */
748 static int parse_range( const char* value, unsigned long *p1, unsigned long *p2)
749 {
750 int ret;
751 int len;
752
753 *p1 = *p2 = 0;
754
755 len = strlen( value);
756 if (len < 7) return -1;
757
758 /* we do not accept ranges of the form 10-20,21-30
759 */
760 if (strchr( value, ',') != NULL) return -1;
761
762 if ( memcmp("bytes=", value, 6) != 0) {
763 return -1;
764 } else value += 6;
765
766 if (strchr( value, '-') == NULL) return -1;
767
768 if (value[0] == '-')
769 ret = sscanf( value, "-%lu", p2);
770 else
771 ret = sscanf( value, "%lu-%lu", p1, p2);
772
773 if (ret == 1) {
774 /* This one accepts ranges of both "-stop" and "start-"
775 */
776 return 0;
777 }
778
779 if (ret == 2)
780 return 0;
781
782 return -1;
783 }
784
785 inline
786 static void init_range_stuff( request* req, char* value)
787 {
788 unsigned long int p1, p2;
789 if (parse_range( value, &p1, &p2) == 0) {
790 req->range_start = p1;
791 req->range_stop = p2;
792 } else {
793 req->range_start = 0;
794 req->range_stop = 0;
795 log_error_time();
796 fprintf(stderr, "bogus range: \"%s\"\n", value);
797 send_r_bad_request(req);
798 }
799 }
800
801 inline
802 static void init_vhost_stuff( request* req, char* value)
803 {
804 virthost* vhost;
805 int valuelen;
806
807 valuelen = strlen(value);
808
809 vhost = find_virthost( value, valuelen);
810
811 if ( vhost && ( vhost->ip == NULL || !memcmp( vhost->ip, req->local_ip_addr, vhost->ip_len) ))
812 {
813 req->hostname = value;
814 memcpy( req->document_root, vhost->document_root, vhost->document_root_len + 1);
815 if (vhost->user_dir)
816 memcpy( req->user_dir, vhost->user_dir, vhost->user_dir_len + 1);
817
818 } else { /* No virtual host found. use defaults */
819 if ( default_document_root)
820 memcpy( req->document_root, default_document_root, default_document_root_size + 1);
821 }
822 }
823
824 /*
825 * Name: process_option_line
826 *
827 * Description: Parses the contents of req->header_line and takes
828 * appropriate action.
829 */
830
831 int process_option_line(request * req)
832 {
833 char c, *value, *line = req->header_line;
834
835 /* Start by aggressively hacking the in-place copy of the header line */
836
837 #ifdef FASCIST_LOGGING
838 log_error_time();
839 fprintf(stderr, "%s:%d - Parsing \"%s\"\n", __FILE__, __LINE__, line);
840 #endif
841
842 value = strchr(line, ':');
843 if (value == NULL)
844 return 0;
845 *value++ = '\0'; /* overwrite the : */
846 to_upper(line); /* header types are case-insensitive */
847 while ((c = *value) && (c == ' ' || c == '\t'))
848 value++;
849
850 if (!memcmp(line, "IF_MODIFIED_SINCE", 18) && !req->if_modified_since)
851 req->if_modified_since = value;
852
853 else if (!memcmp(line, "CONTENT_TYPE", 13) && !req->content_type)
854 req->content_type = value;
855
856 else if (!memcmp(line, "CONTENT_LENGTH", 15) && !req->content_length)
857 req->content_length = value;
858
859 else if (!memcmp(line, "CONNECTION", 11) &&
860 ka_max && req->keepalive != KA_STOPPED) {
861 req->keepalive = (!strncasecmp(value, "Keep-Alive", 10) ?
862 KA_ACTIVE : KA_STOPPED);
863 }
864 /* #ifdef ACCEPT_ON */
865 else if (!memcmp(line, "ACCEPT", 7))
866 add_accept_header(req, value);
867 /* #endif */
868
869 /* Need agent and referer for logs */
870 else if (!memcmp(line, "REFERER", 8)) {
871 req->header_referer = value;
872 if (!add_cgi_env(req, "REFERER", value, 1))
873 return 0;
874 } else if (!memcmp(line, "USER_AGENT", 11)) {
875 req->header_user_agent = value;
876 if (!add_cgi_env(req, "USER_AGENT", value, 1))
877 return 0;
878 } else if (!memcmp(line, "RANGE", 5)) {
879 init_range_stuff( req, value);
880 } else if (!memcmp(line, "HOST", 4)) {
881 init_vhost_stuff( req, value);
882 } else {
883 if (!add_cgi_env(req, line, value, 1))
884 return 0;
885 }
886 return 1;
887 }
888
889 /*
890 * Name: add_accept_header
891 * Description: Adds a mime_type to a requests accept char buffer
892 * silently ignore any that don't fit -
893 * shouldn't happen because of relative buffer sizes
894 */
895
896 void add_accept_header(request * req, char *mime_type)
897 {
898 #ifdef ACCEPT_ON
899 int l = strlen(req->accept);
900 int l2 = strlen(mime_type);
901
902 if ((l + l2 + 2) >= MAX_HEADER_LENGTH)
903 return;
904
905 if (req->accept[0] == '\0')
906 strcpy(req->accept, mime_type);
907 else {
908 req->accept[l] = ',';
909 req->accept[l + 1] = ' ';
910 memcpy(req->accept + l + 2, mime_type, l2 + 1);
911 /* the +1 is for the '\0' */
912 /*
913 sprintf(req->accept + l, ", %s", mime_type);
914 */
915 }
916 #endif
917 }
918
919 void free_requests(server_params* params)
920 {
921 request *ptr, *next;
922
923 ptr = params->request_free;
924 while (ptr != NULL) {
925 next = ptr->next;
926 free(ptr);
927 ptr = next;
928 }
929 params->request_free = NULL;
930 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26