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