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