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.17 2002/09/29 15:34:23 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_types) |
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, |
241 |
char *broken_etag[MAX_COMMA_SEP_ELEMENTS], |
242 |
int *elements) |
243 |
{ |
244 |
*elements = 0; |
245 |
char *p = etag; |
246 |
|
247 |
do { |
248 |
broken_etag[*elements] = p; |
249 |
|
250 |
(*elements)++; |
251 |
|
252 |
p = strchr(p, ','); |
253 |
if (p) { |
254 |
*p = 0; |
255 |
p++; /* move to next entry and skip white |
256 |
* space. |
257 |
*/ |
258 |
while (*p == ' ') |
259 |
p++; |
260 |
} |
261 |
} while (p != NULL && *elements < MAX_COMMA_SEP_ELEMENTS); |
262 |
} |
263 |
|
264 |
/* |
265 |
* Name: check_if_stuff |
266 |
* Description: Checks the If-Match, If-None-Match headers |
267 |
* |
268 |
* req->last_modified, and req->filesize MUST have been set |
269 |
* before calling this function. This function should be called |
270 |
* if req->if_types != 0. |
271 |
* |
272 |
* Return values: |
273 |
* 1: Successful, continue sending the file |
274 |
* 0: unsuccessful. We send the appropriate stuff. Close the connection. |
275 |
*/ |
276 |
|
277 |
static int check_if_stuff(request * req) |
278 |
{ |
279 |
int comp = 0; |
280 |
char *broken_etag[MAX_COMMA_SEP_ELEMENTS]; |
281 |
int broken_etag_size, i; |
282 |
char new_etag[MAX_ETAG_LENGTH]; |
283 |
|
284 |
/* Although we allow multiple If-* directives to be used, we |
285 |
* actually use only one. The priority used is shown below. |
286 |
*/ |
287 |
|
288 |
/* First try IF_MODIFIED_SINCE |
289 |
*/ |
290 |
if (req->if_types & IF_MODIFIED_SINCE) { |
291 |
if (!modified_since(req->last_modified, req->if_modified_since)) { |
292 |
send_r_not_modified(req); |
293 |
return 0; |
294 |
} |
295 |
return 1; |
296 |
} |
297 |
|
298 |
|
299 |
/* Then try IF_MATCH |
300 |
*/ |
301 |
if (req->if_types & IF_MATCH) { |
302 |
|
303 |
/* Check for the "*" |
304 |
*/ |
305 |
if (strncmp(req->if_match_etag, "\"*\"", 3) == 0) { |
306 |
comp = 0; /* comparison is always ok */ |
307 |
} else { |
308 |
|
309 |
/* Create the current ETag of the file. |
310 |
*/ |
311 |
create_etag(req->filesize, req->last_modified, new_etag); |
312 |
|
313 |
/* Check if one of the ETags sent, match ours |
314 |
*/ |
315 |
break_comma_list(req->if_match_etag, broken_etag, |
316 |
&broken_etag_size); |
317 |
|
318 |
comp = 1; |
319 |
for (i = 0; i < broken_etag_size; i++) { |
320 |
comp = strcmp(broken_etag[i], new_etag); |
321 |
if (comp == 0) /* matches! */ |
322 |
break; |
323 |
} |
324 |
|
325 |
|
326 |
} |
327 |
|
328 |
if (comp == 0) |
329 |
return 1; |
330 |
send_r_precondition_failed(req); |
331 |
return 0; |
332 |
} |
333 |
|
334 |
/* Then try IF_RANGE |
335 |
*/ |
336 |
if (req->if_types & IF_RANGE) { |
337 |
if (req->if_range_etag[0] == '"') { /* ETag may contain a date, if If-Range |
338 |
* was used. |
339 |
*/ |
340 |
/* Check for the "*" |
341 |
*/ |
342 |
if (strncmp(req->if_range_etag, "\"*\"", 3) == 0) { |
343 |
comp = 0; /* comparison is always ok */ |
344 |
} else { |
345 |
|
346 |
/* Create the current ETag |
347 |
*/ |
348 |
create_etag(req->filesize, req->last_modified, new_etag); |
349 |
|
350 |
/* Check if one of the ETags sent, match ours |
351 |
*/ |
352 |
|
353 |
break_comma_list(req->if_range_etag, broken_etag, |
354 |
&broken_etag_size); |
355 |
|
356 |
comp = 1; |
357 |
for (i = 0; i < broken_etag_size; i++) { |
358 |
comp = strcmp(broken_etag[i], new_etag); |
359 |
if (comp == 0) /* matches! */ |
360 |
break; |
361 |
} |
362 |
|
363 |
|
364 |
} |
365 |
} else { |
366 |
comp = modified_since(req->last_modified, req->if_range_etag); |
367 |
} |
368 |
|
369 |
|
370 |
/* File didn't change */ |
371 |
if (comp == 0) |
372 |
return 1; |
373 |
|
374 |
/* File has been changed, but it is Ok, so send the whole |
375 |
* file. |
376 |
*/ |
377 |
req->range_start = req->range_stop = 0; |
378 |
return 1; |
379 |
} |
380 |
|
381 |
/* Then try IF_NONE_MATCH |
382 |
*/ |
383 |
if (req->if_types & IF_NONE_MATCH) { |
384 |
/* Check for the "*" |
385 |
*/ |
386 |
if (strncmp(req->if_none_match_etag, "\"*\"", 3) == 0) { |
387 |
comp = 0; /* comparison is always ok */ |
388 |
} else { |
389 |
|
390 |
/* Create the current ETag |
391 |
*/ |
392 |
create_etag(req->filesize, req->last_modified, new_etag); |
393 |
|
394 |
/* Check if one of the ETags sent, match ours |
395 |
*/ |
396 |
|
397 |
break_comma_list(req->if_none_match_etag, broken_etag, |
398 |
&broken_etag_size); |
399 |
|
400 |
comp = 1; |
401 |
for (i = 0; i < broken_etag_size; i++) { |
402 |
comp = strcmp(broken_etag[i], new_etag); |
403 |
if (comp == 0) /* matches! */ |
404 |
break; |
405 |
} |
406 |
|
407 |
|
408 |
} |
409 |
|
410 |
if (comp == 0) { |
411 |
send_r_not_modified(req); |
412 |
return 0; |
413 |
} else { /* it was modified */ |
414 |
send_r_precondition_failed(req); |
415 |
return 0; |
416 |
} |
417 |
} |
418 |
|
419 |
/* Unsupported type ? */ |
420 |
|
421 |
return 1; /* do the request */ |
422 |
} |
423 |
|
424 |
/* |
425 |
* Name: process_get |
426 |
* Description: Writes a chunk of data to the socket. |
427 |
* |
428 |
* Return values: |
429 |
* -1: request blocked, move to blocked queue |
430 |
* 0: EOF or error, close it down |
431 |
* 1: successful write, recycle in ready queue |
432 |
*/ |
433 |
|
434 |
int process_get(server_params * params, request * req) |
435 |
{ |
436 |
int bytes_written; |
437 |
volatile int bytes_to_write; |
438 |
|
439 |
bytes_to_write = req->range_stop - req->filepos; |
440 |
if (bytes_to_write > SOCKETBUF_SIZE) |
441 |
bytes_to_write = SOCKETBUF_SIZE; |
442 |
|
443 |
|
444 |
if (setjmp(params->env) == 0) { |
445 |
params->handle_sigbus = 1; |
446 |
|
447 |
bytes_written = |
448 |
socket_send(req, req->data_mem + req->filepos, bytes_to_write); |
449 |
|
450 |
params->handle_sigbus = 0; |
451 |
/* OK, SIGBUS **after** this point is very bad! */ |
452 |
} else { |
453 |
char buf[30]; |
454 |
/* sigbus! */ |
455 |
log_error_doc(req); |
456 |
/* sending an error here is inappropriate |
457 |
* if we are here, the file is mmapped, and thus, |
458 |
* a content-length has been sent. If we send fewer bytes |
459 |
* the client knows there has been a problem. |
460 |
* We run the risk of accidentally sending the right number |
461 |
* of bytes (or a few too many) and the client |
462 |
* won't be the wiser. |
463 |
*/ |
464 |
req->status = DEAD; |
465 |
get_commonlog_time(buf); |
466 |
fprintf(stderr, "%sGot SIGBUS in write(2)!\n", buf); |
467 |
return 0; |
468 |
} |
469 |
|
470 |
if (bytes_written < 0) { |
471 |
if (errno == EWOULDBLOCK || errno == EAGAIN) |
472 |
return -1; |
473 |
/* request blocked at the pipe level, but keep going */ |
474 |
else { |
475 |
if (errno != EPIPE) { |
476 |
log_error_doc(req); |
477 |
/* Can generate lots of log entries, */ |
478 |
perror("write"); |
479 |
/* OK to disable if your logs get too big */ |
480 |
} |
481 |
req->status = DEAD; |
482 |
return 0; |
483 |
} |
484 |
} |
485 |
req->filepos += bytes_written; |
486 |
|
487 |
if (req->filepos == req->range_stop) { /* EOF */ |
488 |
return 0; |
489 |
} else |
490 |
return 1; /* more to do */ |
491 |
} |
492 |
|
493 |
/* |
494 |
* Name: get_dir |
495 |
* Description: Called from process_get if the request is a directory. |
496 |
* statbuf must describe directory on input, since we may need its |
497 |
* device, inode, and mtime. |
498 |
* statbuf is updated, since we may need to check mtimes of a cache. |
499 |
* returns: |
500 |
* -1 error |
501 |
* 0 cgi (either gunzip or auto-generated) |
502 |
* >0 file descriptor of file |
503 |
*/ |
504 |
|
505 |
int get_dir(request * req, struct stat *statbuf) |
506 |
{ |
507 |
|
508 |
char *directory_index; |
509 |
int data_fd; |
510 |
|
511 |
directory_index = |
512 |
find_and_open_directory_index(req->pathname, 0, &data_fd); |
513 |
|
514 |
if (directory_index) { /* look for index.html first?? */ |
515 |
if (data_fd != -1) { /* user's index file */ |
516 |
int ret; |
517 |
|
518 |
/* Check if we can execute the file |
519 |
*/ |
520 |
|
521 |
strcat(req->request_uri, directory_index); |
522 |
|
523 |
ret = is_executable_cgi(req, directory_index); |
524 |
if (ret != 0) { /* it is a CGI */ |
525 |
close(data_fd); /* we don't need it */ |
526 |
if (ret == -1) { |
527 |
send_r_error(req); |
528 |
return -1; |
529 |
} |
530 |
return init_cgi(req); |
531 |
} |
532 |
|
533 |
/* Not a cgi */ |
534 |
|
535 |
fstat(data_fd, statbuf); |
536 |
return data_fd; |
537 |
} |
538 |
if (errno == EACCES) { |
539 |
send_r_forbidden(req); |
540 |
return -1; |
541 |
} else if (errno != ENOENT) { |
542 |
/* if there is an error *other* than EACCES or ENOENT */ |
543 |
send_r_not_found(req); |
544 |
return -1; |
545 |
} |
546 |
} |
547 |
|
548 |
/* only here if index.html, index.html.gz don't exist */ |
549 |
if (dirmaker != NULL) { /* don't look for index.html... maybe automake? */ |
550 |
req->response_status = R_REQUEST_OK; |
551 |
SQUASH_KA(req); |
552 |
|
553 |
/* the indexer should take care of all headers */ |
554 |
if (!req->simple) { |
555 |
req_write(req, HTTP_VERSION " 200 OK\r\n"); |
556 |
print_http_headers(req); |
557 |
print_last_modified(req); |
558 |
req_write(req, "Content-Type: " TEXT_HTML CRLF CRLF); |
559 |
req_flush(req); |
560 |
} |
561 |
if (req->method == M_HEAD) |
562 |
return 0; |
563 |
|
564 |
req->is_cgi = INDEXER_CGI; |
565 |
return init_cgi(req); |
566 |
/* in this case, 0 means success */ |
567 |
} else if (cachedir) { |
568 |
return get_cachedir_file(req, statbuf); |
569 |
} else { /* neither index.html nor autogenerate are allowed */ |
570 |
send_r_forbidden(req); |
571 |
return -1; /* nothing worked */ |
572 |
} |
573 |
} |
574 |
|
575 |
int get_cachedir_file(request * req, struct stat *statbuf) |
576 |
{ |
577 |
|
578 |
char pathname_with_index[MAX_PATH_LENGTH]; |
579 |
int data_fd; |
580 |
time_t real_dir_mtime; |
581 |
|
582 |
real_dir_mtime = statbuf->st_mtime; |
583 |
sprintf(pathname_with_index, "%s/dir.%d.%ld", |
584 |
cachedir, (int) statbuf->st_dev, statbuf->st_ino); |
585 |
data_fd = open(pathname_with_index, O_RDONLY); |
586 |
|
587 |
if (data_fd != -1) { /* index cache */ |
588 |
|
589 |
fstat(data_fd, statbuf); |
590 |
if (statbuf->st_mtime > real_dir_mtime) { |
591 |
statbuf->st_mtime = real_dir_mtime; /* lie */ |
592 |
strcpy(req->request_uri, find_default_directory_index()); /* for mimetype */ |
593 |
return data_fd; |
594 |
} |
595 |
close(data_fd); |
596 |
unlink(pathname_with_index); /* cache is stale, delete it */ |
597 |
} |
598 |
if (index_directory(req, pathname_with_index) == -1) |
599 |
return -1; |
600 |
|
601 |
data_fd = open(pathname_with_index, O_RDONLY); /* Last chance */ |
602 |
if (data_fd != -1) { |
603 |
strcpy(req->request_uri, find_default_directory_index()); /* for mimetype */ |
604 |
fstat(data_fd, statbuf); |
605 |
statbuf->st_mtime = real_dir_mtime; /* lie */ |
606 |
return data_fd; |
607 |
} |
608 |
|
609 |
boa_perror(req, "re-opening dircache"); |
610 |
return -1; /* Nothing worked. */ |
611 |
|
612 |
} |
613 |
|
614 |
/* |
615 |
* Name: index_directory |
616 |
* Description: Called from get_cachedir_file if a directory html |
617 |
* has to be generated on the fly |
618 |
* returns -1 for problem, else 0 |
619 |
* This version is the fastest, ugliest, and most accurate yet. |
620 |
* It solves the "stale size or type" problem by not ever giving |
621 |
* the size or type. This also speeds it up since no per-file |
622 |
* stat() is required. |
623 |
*/ |
624 |
|
625 |
int index_directory(request * req, char *dest_filename) |
626 |
{ |
627 |
DIR *request_dir; |
628 |
FILE *fdstream; |
629 |
struct dirent *dirbuf; |
630 |
int bytes = 0; |
631 |
char *escname = NULL; |
632 |
|
633 |
if (chdir(req->pathname) == -1) { |
634 |
if (errno == EACCES || errno == EPERM) { |
635 |
send_r_forbidden(req); |
636 |
} else { |
637 |
log_error_doc(req); |
638 |
perror("chdir"); |
639 |
send_r_bad_request(req); |
640 |
} |
641 |
return -1; |
642 |
} |
643 |
|
644 |
request_dir = opendir("."); |
645 |
if (request_dir == NULL) { |
646 |
int errno_save = errno; |
647 |
send_r_error(req); |
648 |
log_error_time(); |
649 |
fprintf(stderr, "directory \"%s\": ", req->pathname); |
650 |
errno = errno_save; |
651 |
perror("opendir"); |
652 |
return -1; |
653 |
} |
654 |
|
655 |
fdstream = fopen(dest_filename, "w"); |
656 |
if (fdstream == NULL) { |
657 |
boa_perror(req, "dircache fopen"); |
658 |
closedir(request_dir); |
659 |
return -1; |
660 |
} |
661 |
|
662 |
bytes += fprintf(fdstream, |
663 |
"<HTML><HEAD>\n<TITLE>Index of %s</TITLE>\n</HEAD>\n\n", |
664 |
req->request_uri); |
665 |
bytes += |
666 |
fprintf(fdstream, "<BODY>\n\n<H2>Index of %s</H2>\n\n<PRE>\n", |
667 |
req->request_uri); |
668 |
|
669 |
while ((dirbuf = readdir(request_dir))) { |
670 |
if (!strcmp(dirbuf->d_name, ".")) |
671 |
continue; |
672 |
|
673 |
if (!strcmp(dirbuf->d_name, "..")) { |
674 |
bytes += fprintf(fdstream, |
675 |
" [DIR] <A HREF=\"../\">Parent Directory</A>\n"); |
676 |
continue; |
677 |
} |
678 |
|
679 |
if ((escname = escape_string(dirbuf->d_name, NULL)) != NULL) { |
680 |
bytes += fprintf(fdstream, " <A HREF=\"%s\">%s</A>\n", |
681 |
escname, dirbuf->d_name); |
682 |
free(escname); |
683 |
escname = NULL; |
684 |
} |
685 |
} |
686 |
closedir(request_dir); |
687 |
bytes += fprintf(fdstream, "</PRE>\n\n</BODY>\n</HTML>\n"); |
688 |
|
689 |
fclose(fdstream); |
690 |
|
691 |
chdir(server_root); |
692 |
|
693 |
req->filesize = bytes; /* for logging transfer size */ |
694 |
return 0; /* success */ |
695 |
} |