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.14 2002/09/28 10:52:46 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(¶ms->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 *) ¶ms->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(¶ms->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(¶ms->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(¶ms->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->status = READ_HEADER; |
370 |
#ifdef ENABLE_SSL |
371 |
conn->ssl_state = NULL; |
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(¶ms->request_block, conn); |
399 |
|
400 |
BOA_FD_SET(conn->fd, ¶ms->block_read_fdset); |
401 |
|
402 |
enqueue(¶ms->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(¶ms->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(¤t_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, ¶ms->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 == -1) |
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 = 0; |
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 |
*p2 = boa_atol( &value[1]); |
770 |
return 0; |
771 |
} else |
772 |
ret = sscanf( value, "%lu-%lu", p1, p2); |
773 |
|
774 |
if (ret == 1) { |
775 |
/* This one accepts ranges of both "-stop" and "start-" |
776 |
*/ |
777 |
return 0; |
778 |
} |
779 |
|
780 |
if (ret == 2) |
781 |
return 0; |
782 |
|
783 |
return -1; |
784 |
} |
785 |
|
786 |
inline |
787 |
static void init_range_stuff( request* req, char* value) |
788 |
{ |
789 |
unsigned long int p1, p2; |
790 |
if (parse_range( value, &p1, &p2) == 0) { |
791 |
req->range_start = p1; |
792 |
req->range_stop = p2; |
793 |
} else { |
794 |
req->range_start = 0; |
795 |
req->range_stop = 0; |
796 |
log_error_time(); |
797 |
fprintf(stderr, "bogus range: \"%s\"\n", value); |
798 |
send_r_bad_request(req); |
799 |
} |
800 |
} |
801 |
|
802 |
inline |
803 |
static void init_vhost_stuff( request* req, char* value) |
804 |
{ |
805 |
virthost* vhost; |
806 |
int valuelen; |
807 |
|
808 |
valuelen = strlen(value); |
809 |
|
810 |
vhost = find_virthost( value, valuelen); |
811 |
|
812 |
if ( vhost && ( vhost->ip == NULL || !memcmp( vhost->ip, req->local_ip_addr, vhost->ip_len) )) |
813 |
{ |
814 |
req->hostname = value; |
815 |
memcpy( req->document_root, vhost->document_root, vhost->document_root_len + 1); |
816 |
if (vhost->user_dir) |
817 |
memcpy( req->user_dir, vhost->user_dir, vhost->user_dir_len + 1); |
818 |
|
819 |
} else { /* No virtual host found. use defaults */ |
820 |
if ( default_document_root) |
821 |
memcpy( req->document_root, default_document_root, default_document_root_size + 1); |
822 |
} |
823 |
} |
824 |
|
825 |
/* |
826 |
* Name: process_option_line |
827 |
* |
828 |
* Description: Parses the contents of req->header_line and takes |
829 |
* appropriate action. |
830 |
*/ |
831 |
|
832 |
int process_option_line(request * req) |
833 |
{ |
834 |
char c, *value, *line = req->header_line; |
835 |
|
836 |
/* Start by aggressively hacking the in-place copy of the header line */ |
837 |
|
838 |
#ifdef FASCIST_LOGGING |
839 |
log_error_time(); |
840 |
fprintf(stderr, "%s:%d - Parsing \"%s\"\n", __FILE__, __LINE__, line); |
841 |
#endif |
842 |
|
843 |
value = strchr(line, ':'); |
844 |
if (value == NULL) |
845 |
return 0; |
846 |
*value++ = '\0'; /* overwrite the : */ |
847 |
to_upper(line); /* header types are case-insensitive */ |
848 |
while ((c = *value) && (c == ' ' || c == '\t')) |
849 |
value++; |
850 |
|
851 |
if (!memcmp(line, "IF_MODIFIED_SINCE", 18) && !req->if_modified_since) |
852 |
req->if_modified_since = value; |
853 |
|
854 |
else if (!memcmp(line, "CONTENT_TYPE", 13) && !req->content_type) |
855 |
req->content_type = value; |
856 |
|
857 |
else if (!memcmp(line, "CONTENT_LENGTH", 15) && !req->content_length) |
858 |
req->content_length = value; |
859 |
|
860 |
else if (!memcmp(line, "CONNECTION", 11) && |
861 |
ka_max && req->keepalive != KA_STOPPED) { |
862 |
req->keepalive = (!strncasecmp(value, "Keep-Alive", 10) ? |
863 |
KA_ACTIVE : KA_STOPPED); |
864 |
} |
865 |
/* #ifdef ACCEPT_ON */ |
866 |
else if (!memcmp(line, "ACCEPT", 7)) |
867 |
add_accept_header(req, value); |
868 |
/* #endif */ |
869 |
|
870 |
/* Need agent and referer for logs */ |
871 |
else if (!memcmp(line, "REFERER", 8)) { |
872 |
req->header_referer = value; |
873 |
if (!add_cgi_env(req, "REFERER", value, 1)) |
874 |
return 0; |
875 |
} else if (!memcmp(line, "USER_AGENT", 11)) { |
876 |
req->header_user_agent = value; |
877 |
if (!add_cgi_env(req, "USER_AGENT", value, 1)) |
878 |
return 0; |
879 |
} else if (!memcmp(line, "RANGE", 5)) { |
880 |
init_range_stuff( req, value); |
881 |
} else if (!memcmp(line, "HOST", 4)) { |
882 |
init_vhost_stuff( req, value); |
883 |
if (!add_cgi_env(req, "HOST", value, 1)) |
884 |
return 0; |
885 |
} else { |
886 |
if (!add_cgi_env(req, line, value, 1)) |
887 |
return 0; |
888 |
} |
889 |
return 1; |
890 |
} |
891 |
|
892 |
/* |
893 |
* Name: add_accept_header |
894 |
* Description: Adds a mime_type to a requests accept char buffer |
895 |
* silently ignore any that don't fit - |
896 |
* shouldn't happen because of relative buffer sizes |
897 |
*/ |
898 |
|
899 |
void add_accept_header(request * req, char *mime_type) |
900 |
{ |
901 |
#ifdef ACCEPT_ON |
902 |
int l = strlen(req->accept); |
903 |
int l2 = strlen(mime_type); |
904 |
|
905 |
if ((l + l2 + 2) >= MAX_HEADER_LENGTH) |
906 |
return; |
907 |
|
908 |
if (req->accept[0] == '\0') |
909 |
strcpy(req->accept, mime_type); |
910 |
else { |
911 |
req->accept[l] = ','; |
912 |
req->accept[l + 1] = ' '; |
913 |
memcpy(req->accept + l + 2, mime_type, l2 + 1); |
914 |
/* the +1 is for the '\0' */ |
915 |
/* |
916 |
sprintf(req->accept + l, ", %s", mime_type); |
917 |
*/ |
918 |
} |
919 |
#endif |
920 |
} |
921 |
|
922 |
void free_requests(server_params* params) |
923 |
{ |
924 |
request *ptr, *next; |
925 |
|
926 |
ptr = params->request_free; |
927 |
while (ptr != NULL) { |
928 |
next = ptr->next; |
929 |
free(ptr); |
930 |
ptr = next; |
931 |
} |
932 |
params->request_free = NULL; |
933 |
} |