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

Contents of /hydra/src/get.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (show annotations)
Sun Sep 29 15:34:23 2002 UTC (21 years, 6 months ago) by nmav
Branch: MAIN
Changes since 1.16: +187 -110 lines
File MIME type: text/plain
Corrected If-None-Match behaviour, added support for Dates in the If-Range field, and added support for multiple ETags.

1 /*
2 * Hydra, an http server
3 * Copyright (C) 1995 Paul Phillips <paulp@go2net.com>
4 * Some changes Copyright (C) 1996,99 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: get.c,v 1.16 2002/09/29 12:02:57 nmav Exp $*/
25
26 #include "boa.h"
27 #include "socket.h"
28
29 /* local prototypes */
30 int get_cachedir_file(request * req, struct stat *statbuf);
31 int index_directory(request * req, char *dest_filename);
32 static int check_if_stuff(request * req);
33
34 /*
35 * Name: init_get
36 * Description: Initializes a non-script GET or HEAD request.
37 *
38 * Return values:
39 * 0: finished or error, request will be freed
40 * 1: successfully initialized, added to ready queue
41 */
42
43 int init_get(server_params * params, request * req)
44 {
45 int data_fd, saved_errno;
46 struct stat statbuf;
47 volatile int bytes;
48
49 data_fd = open(req->pathname, O_RDONLY);
50 saved_errno = errno; /* might not get used */
51
52 if (data_fd == -1) {
53 log_error_doc(req);
54 errno = saved_errno;
55 perror("document open");
56
57 if (saved_errno == ENOENT)
58 send_r_not_found(req);
59 else if (saved_errno == EACCES)
60 send_r_forbidden(req);
61 else
62 send_r_bad_request(req);
63 return 0;
64 }
65
66 if (fstat(data_fd, &statbuf) == -1) {
67 /* this is quite impossible, since the file
68 * was opened before.
69 */
70 close(data_fd);
71 send_r_not_found(req);
72 return 0;
73 }
74
75 if (S_ISDIR(statbuf.st_mode)) { /* directory */
76 close(data_fd); /* close dir */
77
78 if (req->pathname[strlen(req->pathname) - 1] != '/') {
79 char buffer[3 * MAX_PATH_LENGTH + 128];
80
81 create_url(buffer, sizeof(buffer), req->secure, req->hostname,
82 params->server_s[req->secure].port, req->request_uri);
83
84 send_r_moved_perm(req, buffer);
85 return 0;
86 }
87 data_fd = get_dir(req, &statbuf); /* updates statbuf */
88
89 if (data_fd == -1) /* couldn't do it */
90 return 0; /* errors reported by get_dir */
91 else if (data_fd <= 1)
92 /* data_fd == 0 -> close it down, 1 -> continue */
93 return data_fd;
94 /* else, data_fd contains the fd of the file... */
95 }
96
97 req->filesize = statbuf.st_size;
98 req->last_modified = statbuf.st_mtime;
99
100 /* Check the If-Match, If-Modified etc stuff.
101 */
102 if (req->if_type)
103 if (check_if_stuff(req) == 0) {
104 close(data_fd);
105 return 0;
106 }
107 /* Move on */
108
109 if (req->range_stop == 0)
110 req->range_stop = statbuf.st_size;
111
112 /* out of range! */
113 if (req->range_start > statbuf.st_size ||
114 req->range_stop > statbuf.st_size ||
115 req->range_stop < req->range_start) {
116 send_r_range_unsatisfiable(req);
117 close(data_fd);
118 return 0;
119 }
120
121 if (req->method == M_HEAD || req->filesize == 0) {
122 send_r_request_ok(req);
123 close(data_fd);
124 return 0;
125 }
126
127 if (req->range_stop > max_file_size_cache) {
128
129 if (req->range_start == 0 && req->range_stop == statbuf.st_size)
130 send_r_request_ok(req); /* All's well */
131 else {
132 /* if ranges were used, then lseek to the start given
133 */
134 if (lseek(data_fd, req->range_start, SEEK_SET) == (off_t) - 1) {
135 close(data_fd);
136 send_r_not_found(req);
137 return 0;
138 }
139 send_r_request_partial(req); /* All's well */
140 }
141
142 req->status = PIPE_READ;
143 req->cgi_status = CGI_BUFFER;
144 req->data_fd = data_fd;
145 req_flush(req); /* this should *always* complete due to
146 the size of the I/O buffers */
147 req->header_line = req->header_end = req->buffer;
148 req->pipe_range_stop = req->range_stop;
149 return 1;
150 }
151
152 if (req->range_stop == 0) { /* done */
153 send_r_request_ok(req); /* All's well *so far* */
154 close(data_fd);
155 return 1;
156 }
157
158 /* NOTE: I (Jon Nelson) tried performing a read(2)
159 * into the output buffer provided the file data would
160 * fit, before mmapping, and if successful, writing that
161 * and stopping there -- all to avoid the cost
162 * of a mmap. Oddly, it was *slower* in benchmarks.
163 */
164 if (max_files_cache > 0) {
165 req->mmap_entry_var = find_mmap(data_fd, &statbuf);
166 if (req->mmap_entry_var == NULL) {
167 req->buffer_end = 0;
168 if (errno == ENOENT)
169 send_r_not_found(req);
170 else if (errno == EACCES)
171 send_r_forbidden(req);
172 else
173 send_r_bad_request(req);
174 close(data_fd);
175 return 0;
176 }
177 req->data_mem = req->mmap_entry_var->mmap;
178 } else { /* File caching is disabled.
179 */
180 req->data_mem =
181 mmap(0, req->range_stop, PROT_READ, MAP_OPTIONS, data_fd, 0);
182 }
183
184 close(data_fd); /* close data file */
185
186 if (req->data_mem == MAP_FAILED) {
187 boa_perror(req, "mmap");
188 return 0;
189 }
190
191 if (req->range_start == 0 && req->range_stop == statbuf.st_size)
192 send_r_request_ok(req); /* All's well */
193 else
194 send_r_request_partial(req); /* All's well */
195
196 bytes = BUFFER_SIZE - req->buffer_end;
197
198 /* bytes is now how much the buffer can hold
199 * after the headers
200 */
201 req->filepos = req->range_start;
202
203 if (bytes > 0) {
204 if (bytes > req->range_stop - req->range_start)
205 bytes = req->range_stop - req->range_start;
206
207 if (setjmp(params->env) == 0) {
208 params->handle_sigbus = 1;
209 memcpy(req->buffer + req->buffer_end,
210 &req->data_mem[req->filepos], bytes);
211 params->handle_sigbus = 0;
212 /* OK, SIGBUS **after** this point is very bad! */
213 } else {
214 char buf[30];
215 /* sigbus! */
216 log_error_doc(req);
217 reset_output_buffer(req);
218 send_r_error(req);
219 get_commonlog_time(buf);
220 fprintf(stderr, "%sGot SIGBUS in memcpy!\n", buf);
221 return 0;
222 }
223 req->buffer_end += bytes;
224 req->filepos += bytes;
225 if (req->range_stop == req->filepos) {
226 req_flush(req);
227 req->status = DONE;
228 }
229 }
230
231 /* We lose statbuf here, so make sure response has been sent */
232 return 1;
233 }
234
235 /* Breaks a list of "xxx", "yyy", to a character array, of
236 * MAX_ETAGS size; Note that the given string is modified.
237 */
238 #define MAX_COMMA_SEP_ELEMENTS 6
239 inline
240 static void break_comma_list( char* etag, char* broken_etag[MAX_COMMA_SEP_ELEMENTS], int* elements)
241 {
242 *elements = 0;
243 char *p = etag;
244
245 do {
246 broken_etag[*elements] = p;
247
248 (*elements)++;
249
250 p = strchr( p, ',');
251 if (p) {
252 *p = 0;
253 p++; /* move to next entry and skip white
254 * space.
255 */
256 while (*p == ' ') p++;
257 }
258 } while( p != NULL && *elements < MAX_COMMA_SEP_ELEMENTS);
259 }
260
261 /*
262 * Name: check_if_stuff
263 * Description: Checks the If-Match, If-None-Match headers
264 *
265 * req->last_modified, and req->filesize MUST have been set
266 * before calling this function. This function should be called
267 * if req->if_type != 0.
268 *
269 * Return values:
270 * 1: Successful, continue sending the file
271 * 0: unsuccessful. We send the appropriate stuff. Close the connection.
272 */
273
274 static int check_if_stuff(request * req)
275 {
276 int comp = 0;
277
278 /* Although we allow multiple If-* directives to be used, we
279 * actually use only one. The priority used is shown below.
280 */
281
282 /* First try IF_MODIFIED_SINCE
283 */
284 if (req->if_type & IF_MODIFIED_SINCE) {
285 if (!modified_since(req->last_modified, req->if_modified_since)) {
286 send_r_not_modified(req);
287 return 0;
288 }
289 return 1;
290 }
291
292 /* After that we need the etag
293 */
294 if (req->etag == NULL)
295 return 1;
296
297 if (req->etag[0] == '"') { /* ETag may contain a date, if If-Range
298 * was used.
299 */
300 char * broken_etag[MAX_COMMA_SEP_ELEMENTS];
301 int broken_etag_size, i;
302
303 /* Check for the "*"
304 */
305 if (strncmp(req->etag, "\"*\"", 3) == 0) {
306 comp = 0; /* comparison is always ok */
307 } else {
308 char new_etag[MAX_ETAG_LENGTH];
309
310 /* Create the current ETag
311 */
312 create_etag(req->filesize, req->last_modified, new_etag);
313
314 /* Check if one of the ETags sent, match ours
315 */
316
317 break_comma_list( req->etag, broken_etag, &broken_etag_size);
318
319 comp = 1;
320 for (i=0;i<broken_etag_size;i++) {
321 comp = strcmp(broken_etag[i], new_etag);
322 if (comp==0) /* matches! */
323 break;
324 }
325
326
327 }
328 }
329
330 /* Then try IF_MATCH
331 */
332 if (req->if_type & IF_MATCH) {
333 if (comp == 0)
334 return 1;
335 send_r_precondition_failed(req);
336 return 0;
337 }
338
339 /* Then try IF_RANGE
340 */
341 if (req->if_type & IF_RANGE) {
342 if (req->etag[0] != '"') { /* Not '"' there, so he has a
343 * date in the etag */
344 if (!modified_since(req->last_modified, req->etag)) {
345 return 1;
346 }
347 /* it was modified */
348 req->range_start = req->range_stop = 0;
349 return 1;
350 }
351
352 if (comp == 0)
353 return 1;
354
355 /* Ok, but send the whole file, because
356 * it has been changed.
357 */
358 req->range_start = req->range_stop = 0;
359 return 1;
360 }
361
362 /* Then try IF_NONE_MATCH
363 */
364 if (req->if_type & IF_NONE_MATCH) {
365 if (comp == 0) {
366 send_r_not_modified(req);
367 return 0;
368 } else { /* it was modified */
369 send_r_precondition_failed(req);
370 return 0;
371 }
372 }
373
374 /* Unsupported type ? */
375
376 return 1; /* do the request */
377 }
378
379 /*
380 * Name: process_get
381 * Description: Writes a chunk of data to the socket.
382 *
383 * Return values:
384 * -1: request blocked, move to blocked queue
385 * 0: EOF or error, close it down
386 * 1: successful write, recycle in ready queue
387 */
388
389 int process_get(server_params * params, request * req)
390 {
391 int bytes_written;
392 volatile int bytes_to_write;
393
394 bytes_to_write = req->range_stop - req->filepos;
395 if (bytes_to_write > SOCKETBUF_SIZE)
396 bytes_to_write = SOCKETBUF_SIZE;
397
398
399 if (setjmp(params->env) == 0) {
400 params->handle_sigbus = 1;
401
402 bytes_written =
403 socket_send(req, req->data_mem + req->filepos, bytes_to_write);
404
405 params->handle_sigbus = 0;
406 /* OK, SIGBUS **after** this point is very bad! */
407 } else {
408 char buf[30];
409 /* sigbus! */
410 log_error_doc(req);
411 /* sending an error here is inappropriate
412 * if we are here, the file is mmapped, and thus,
413 * a content-length has been sent. If we send fewer bytes
414 * the client knows there has been a problem.
415 * We run the risk of accidentally sending the right number
416 * of bytes (or a few too many) and the client
417 * won't be the wiser.
418 */
419 req->status = DEAD;
420 get_commonlog_time(buf);
421 fprintf(stderr, "%sGot SIGBUS in write(2)!\n", buf);
422 return 0;
423 }
424
425 if (bytes_written < 0) {
426 if (errno == EWOULDBLOCK || errno == EAGAIN)
427 return -1;
428 /* request blocked at the pipe level, but keep going */
429 else {
430 if (errno != EPIPE) {
431 log_error_doc(req);
432 /* Can generate lots of log entries, */
433 perror("write");
434 /* OK to disable if your logs get too big */
435 }
436 req->status = DEAD;
437 return 0;
438 }
439 }
440 req->filepos += bytes_written;
441
442 if (req->filepos == req->range_stop) { /* EOF */
443 return 0;
444 } else
445 return 1; /* more to do */
446 }
447
448 /*
449 * Name: get_dir
450 * Description: Called from process_get if the request is a directory.
451 * statbuf must describe directory on input, since we may need its
452 * device, inode, and mtime.
453 * statbuf is updated, since we may need to check mtimes of a cache.
454 * returns:
455 * -1 error
456 * 0 cgi (either gunzip or auto-generated)
457 * >0 file descriptor of file
458 */
459
460 int get_dir(request * req, struct stat *statbuf)
461 {
462
463 char *directory_index;
464 int data_fd;
465
466 directory_index =
467 find_and_open_directory_index(req->pathname, 0, &data_fd);
468
469 if (directory_index) { /* look for index.html first?? */
470 if (data_fd != -1) { /* user's index file */
471 int ret;
472
473 /* Check if we can execute the file
474 */
475
476 strcat(req->request_uri, directory_index);
477
478 ret = is_executable_cgi(req, directory_index);
479 if (ret != 0) { /* it is a CGI */
480 close(data_fd); /* we don't need it */
481 if (ret == -1) {
482 send_r_error(req);
483 return -1;
484 }
485 return init_cgi(req);
486 }
487
488 /* Not a cgi */
489
490 fstat(data_fd, statbuf);
491 return data_fd;
492 }
493 if (errno == EACCES) {
494 send_r_forbidden(req);
495 return -1;
496 } else if (errno != ENOENT) {
497 /* if there is an error *other* than EACCES or ENOENT */
498 send_r_not_found(req);
499 return -1;
500 }
501 }
502
503 /* only here if index.html, index.html.gz don't exist */
504 if (dirmaker != NULL) { /* don't look for index.html... maybe automake? */
505 req->response_status = R_REQUEST_OK;
506 SQUASH_KA(req);
507
508 /* the indexer should take care of all headers */
509 if (!req->simple) {
510 req_write(req, HTTP_VERSION " 200 OK\r\n");
511 print_http_headers(req);
512 print_last_modified(req);
513 req_write(req, "Content-Type: " TEXT_HTML CRLF CRLF);
514 req_flush(req);
515 }
516 if (req->method == M_HEAD)
517 return 0;
518
519 req->is_cgi = INDEXER_CGI;
520 return init_cgi(req);
521 /* in this case, 0 means success */
522 } else if (cachedir) {
523 return get_cachedir_file(req, statbuf);
524 } else { /* neither index.html nor autogenerate are allowed */
525 send_r_forbidden(req);
526 return -1; /* nothing worked */
527 }
528 }
529
530 int get_cachedir_file(request * req, struct stat *statbuf)
531 {
532
533 char pathname_with_index[MAX_PATH_LENGTH];
534 int data_fd;
535 time_t real_dir_mtime;
536
537 real_dir_mtime = statbuf->st_mtime;
538 sprintf(pathname_with_index, "%s/dir.%d.%ld",
539 cachedir, (int) statbuf->st_dev, statbuf->st_ino);
540 data_fd = open(pathname_with_index, O_RDONLY);
541
542 if (data_fd != -1) { /* index cache */
543
544 fstat(data_fd, statbuf);
545 if (statbuf->st_mtime > real_dir_mtime) {
546 statbuf->st_mtime = real_dir_mtime; /* lie */
547 strcpy(req->request_uri, find_default_directory_index()); /* for mimetype */
548 return data_fd;
549 }
550 close(data_fd);
551 unlink(pathname_with_index); /* cache is stale, delete it */
552 }
553 if (index_directory(req, pathname_with_index) == -1)
554 return -1;
555
556 data_fd = open(pathname_with_index, O_RDONLY); /* Last chance */
557 if (data_fd != -1) {
558 strcpy(req->request_uri, find_default_directory_index()); /* for mimetype */
559 fstat(data_fd, statbuf);
560 statbuf->st_mtime = real_dir_mtime; /* lie */
561 return data_fd;
562 }
563
564 boa_perror(req, "re-opening dircache");
565 return -1; /* Nothing worked. */
566
567 }
568
569 /*
570 * Name: index_directory
571 * Description: Called from get_cachedir_file if a directory html
572 * has to be generated on the fly
573 * returns -1 for problem, else 0
574 * This version is the fastest, ugliest, and most accurate yet.
575 * It solves the "stale size or type" problem by not ever giving
576 * the size or type. This also speeds it up since no per-file
577 * stat() is required.
578 */
579
580 int index_directory(request * req, char *dest_filename)
581 {
582 DIR *request_dir;
583 FILE *fdstream;
584 struct dirent *dirbuf;
585 int bytes = 0;
586 char *escname = NULL;
587
588 if (chdir(req->pathname) == -1) {
589 if (errno == EACCES || errno == EPERM) {
590 send_r_forbidden(req);
591 } else {
592 log_error_doc(req);
593 perror("chdir");
594 send_r_bad_request(req);
595 }
596 return -1;
597 }
598
599 request_dir = opendir(".");
600 if (request_dir == NULL) {
601 int errno_save = errno;
602 send_r_error(req);
603 log_error_time();
604 fprintf(stderr, "directory \"%s\": ", req->pathname);
605 errno = errno_save;
606 perror("opendir");
607 return -1;
608 }
609
610 fdstream = fopen(dest_filename, "w");
611 if (fdstream == NULL) {
612 boa_perror(req, "dircache fopen");
613 closedir(request_dir);
614 return -1;
615 }
616
617 bytes += fprintf(fdstream,
618 "<HTML><HEAD>\n<TITLE>Index of %s</TITLE>\n</HEAD>\n\n",
619 req->request_uri);
620 bytes +=
621 fprintf(fdstream, "<BODY>\n\n<H2>Index of %s</H2>\n\n<PRE>\n",
622 req->request_uri);
623
624 while ((dirbuf = readdir(request_dir))) {
625 if (!strcmp(dirbuf->d_name, "."))
626 continue;
627
628 if (!strcmp(dirbuf->d_name, "..")) {
629 bytes += fprintf(fdstream,
630 " [DIR] <A HREF=\"../\">Parent Directory</A>\n");
631 continue;
632 }
633
634 if ((escname = escape_string(dirbuf->d_name, NULL)) != NULL) {
635 bytes += fprintf(fdstream, " <A HREF=\"%s\">%s</A>\n",
636 escname, dirbuf->d_name);
637 free(escname);
638 escname = NULL;
639 }
640 }
641 closedir(request_dir);
642 bytes += fprintf(fdstream, "</PRE>\n\n</BODY>\n</HTML>\n");
643
644 fclose(fdstream);
645
646 chdir(server_root);
647
648 req->filesize = bytes; /* for logging transfer size */
649 return 0; /* success */
650 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26