/src/h2o/lib/handler/file.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2014-2016 DeNA Co., Ltd., Kazuho Oku, Domingo Alvarez Duarte, |
3 | | * Tatsuhiko Kubo, Nick Desaulniers, Marc Hoersken, |
4 | | * Justin Zhu, Tatsuhiro Tsujikawa |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | * of this software and associated documentation files (the "Software"), to |
8 | | * deal in the Software without restriction, including without limitation the |
9 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
10 | | * sell copies of the Software, and to permit persons to whom the Software is |
11 | | * furnished to do so, subject to the following conditions: |
12 | | * |
13 | | * The above copyright notice and this permission notice shall be included in |
14 | | * all copies or substantial portions of the Software. |
15 | | * |
16 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
21 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
22 | | * IN THE SOFTWARE. |
23 | | */ |
24 | | #include <dirent.h> |
25 | | #include <errno.h> |
26 | | #include <fcntl.h> |
27 | | #include <limits.h> |
28 | | #include <stdio.h> |
29 | | #include <stdlib.h> |
30 | | #if defined(__linux__) |
31 | | #include <sys/sendfile.h> |
32 | | #endif |
33 | | #include <sys/stat.h> |
34 | | #include <sys/types.h> |
35 | | #include <time.h> |
36 | | #include <unistd.h> |
37 | | #include "h2o.h" |
38 | | #if H2O_USE_IO_URING |
39 | | #include "h2o/io_uring.h" |
40 | | #endif |
41 | | |
42 | 0 | #define MAX_BUF_SIZE 65000 |
43 | 0 | #define BOUNDARY_SIZE 20 |
44 | 0 | #define FIXED_PART_SIZE (sizeof("\r\n--") - 1 + BOUNDARY_SIZE + sizeof("\r\nContent-Range: bytes=-/\r\nContent-Type: \r\n\r\n") - 1) |
45 | | |
46 | | struct st_h2o_sendfile_generator_t { |
47 | | h2o_generator_t super; |
48 | | struct { |
49 | | h2o_filecache_ref_t *ref; |
50 | | off_t off; |
51 | | } file; |
52 | | size_t bytesleft; |
53 | | h2o_iovec_t content_encoding; |
54 | | unsigned send_vary : 1; |
55 | | unsigned send_etag : 1; |
56 | | unsigned gunzip : 1; |
57 | | struct { |
58 | | char *multirange_buf; /* multi-range mode uses push */ |
59 | | size_t filesize; |
60 | | size_t range_count; |
61 | | size_t *range_infos; /* size_t shows in pair. first is start offset, then length */ |
62 | | h2o_iovec_t boundary; /* boundary used for multipart/byteranges */ |
63 | | h2o_iovec_t mimetype; /* original mimetype for multipart */ |
64 | | size_t current_range; /* range that processing now */ |
65 | | } ranged; |
66 | | struct { |
67 | | char last_modified[H2O_TIMESTR_RFC1123_LEN + 1]; |
68 | | char etag[H2O_FILECACHE_ETAG_MAXLEN + 1]; |
69 | | } header_bufs; |
70 | | #if H2O_USE_IO_URING |
71 | | /** |
72 | | * back pointer to the request which is necessary for splicing async; becomes NULL when the generator is stopped |
73 | | */ |
74 | | h2o_req_t *src_req; |
75 | | int splice_fds[2]; |
76 | | #endif |
77 | | }; |
78 | | |
79 | | struct st_h2o_file_handler_t { |
80 | | h2o_handler_t super; |
81 | | h2o_iovec_t conf_path; /* has "/" appended at last */ |
82 | | h2o_iovec_t real_path; /* has "/" appended at last */ |
83 | | h2o_mimemap_t *mimemap; |
84 | | int flags; |
85 | | size_t max_index_file_len; |
86 | | h2o_iovec_t index_files[1]; |
87 | | }; |
88 | | |
89 | | struct st_h2o_specific_file_handler_t { |
90 | | h2o_handler_t super; |
91 | | h2o_iovec_t real_path; |
92 | | h2o_mimemap_type_t *mime_type; |
93 | | int flags; |
94 | | }; |
95 | | |
96 | | struct st_gzip_decompress_t { |
97 | | h2o_ostream_t super; |
98 | | h2o_compress_context_t *decompressor; |
99 | | }; |
100 | | |
101 | | static const char *default_index_files[] = {"index.html", "index.htm", "index.txt", NULL}; |
102 | | |
103 | | const char **h2o_file_default_index_files = default_index_files; |
104 | | |
105 | | #include "file/templates.c.h" |
106 | | |
107 | | static int tm_is_lessthan(struct tm *x, struct tm *y) |
108 | 0 | { |
109 | 0 | #define CMP(f) \ |
110 | 0 | if (x->f < y->f) \ |
111 | 0 | return 1; \ |
112 | 0 | else if (x->f > y->f) \ |
113 | 0 | return 0; |
114 | 0 | CMP(tm_year); |
115 | 0 | CMP(tm_mon); |
116 | 0 | CMP(tm_mday); |
117 | 0 | CMP(tm_hour); |
118 | 0 | CMP(tm_min); |
119 | 0 | CMP(tm_sec); |
120 | 0 | return 0; |
121 | 0 | #undef CMP |
122 | 0 | } |
123 | | |
124 | | static void close_file(struct st_h2o_sendfile_generator_t *self) |
125 | 0 | { |
126 | 0 | if (self->file.ref != NULL) { |
127 | 0 | h2o_filecache_close_file(self->file.ref); |
128 | 0 | self->file.ref = NULL; |
129 | 0 | } |
130 | | #if H2O_USE_IO_URING |
131 | | if (self->splice_fds[0] != -1) { |
132 | | if (self->src_req != NULL) { |
133 | | h2o_context_return_spare_pipe(self->src_req->conn->ctx, self->splice_fds); |
134 | | } else { |
135 | | /* TODO return pipe upon abrupt close too? maybe that's not need */ |
136 | | close(self->splice_fds[0]); |
137 | | close(self->splice_fds[1]); |
138 | | } |
139 | | self->splice_fds[0] = -1; |
140 | | self->splice_fds[1] = -1; |
141 | | } |
142 | | #endif |
143 | 0 | } |
144 | | |
145 | | static void on_generator_dispose(void *_self) |
146 | 0 | { |
147 | 0 | struct st_h2o_sendfile_generator_t *self = _self; |
148 | 0 | close_file(self); |
149 | 0 | } |
150 | | |
151 | | #if H2O_USE_IO_URING |
152 | | |
153 | | static void do_stop_async_splice(h2o_generator_t *_self, h2o_req_t *req) |
154 | | { |
155 | | struct st_h2o_sendfile_generator_t *self = (void *)_self; |
156 | | self->src_req = NULL; |
157 | | } |
158 | | |
159 | | static void do_proceed_on_splice_complete(h2o_io_uring_cmd_t *cmd) |
160 | | { |
161 | | struct st_h2o_sendfile_generator_t *self = cmd->cb.data; |
162 | | |
163 | | if (self->src_req == NULL) { |
164 | | h2o_mem_release_shared(self); |
165 | | return; |
166 | | } |
167 | | |
168 | | h2o_mem_release_shared(self); |
169 | | |
170 | | if (cmd->result <= 0) { |
171 | | assert(cmd->result != -EINTR); /* could this ever happen? */ |
172 | | h2o_send(self->src_req, NULL, 0, H2O_SEND_STATE_ERROR); |
173 | | return; |
174 | | } |
175 | | |
176 | | self->file.off += cmd->result; |
177 | | self->bytesleft -= cmd->result; |
178 | | |
179 | | h2o_send_from_pipe(self->src_req, self->splice_fds[0], cmd->result, |
180 | | self->bytesleft != 0 ? H2O_SEND_STATE_IN_PROGRESS : H2O_SEND_STATE_FINAL); |
181 | | } |
182 | | |
183 | | #endif |
184 | | |
185 | | static int do_pread(h2o_sendvec_t *src, void *dst, size_t len) |
186 | 0 | { |
187 | 0 | struct st_h2o_sendfile_generator_t *self = (void *)src->cb_arg[0]; |
188 | 0 | uint64_t *file_chunk_at = &src->cb_arg[1]; |
189 | 0 | size_t bytes_read = 0; |
190 | 0 | ssize_t rret; |
191 | | |
192 | | /* read */ |
193 | 0 | while (bytes_read < len) { |
194 | 0 | while ((rret = pread(self->file.ref->fd, dst + bytes_read, len - bytes_read, *file_chunk_at)) == -1 && errno == EINTR) |
195 | 0 | ; |
196 | 0 | if (rret <= 0) |
197 | 0 | return 0; |
198 | 0 | bytes_read += rret; |
199 | 0 | *file_chunk_at += rret; |
200 | 0 | src->len -= rret; |
201 | 0 | } |
202 | | |
203 | 0 | return 1; |
204 | 0 | } |
205 | | |
206 | | #if defined(__linux__) |
207 | | static size_t do_sendfile(int sockfd, int filefd, off_t off, size_t len) |
208 | 0 | { |
209 | 0 | off_t iooff = off; |
210 | 0 | ssize_t ret; |
211 | 0 | while ((ret = sendfile(sockfd, filefd, &iooff, len)) == -1 && errno == EINTR) |
212 | 0 | ; |
213 | 0 | if (ret <= 0) |
214 | 0 | return ret == -1 && errno == EAGAIN ? 0 : SIZE_MAX; |
215 | 0 | return ret; |
216 | 0 | } |
217 | | #elif defined(__APPLE__) |
218 | | static size_t do_sendfile(int sockfd, int filefd, off_t off, size_t len) |
219 | | { |
220 | | off_t iolen = len; |
221 | | int ret; |
222 | | while ((ret = sendfile(filefd, sockfd, off, &iolen, NULL, 0)) != 0 && errno == EINTR) |
223 | | ; |
224 | | if (ret != 0 && errno != EAGAIN) |
225 | | return SIZE_MAX; |
226 | | return iolen; |
227 | | } |
228 | | #elif defined(__FreeBSD__) |
229 | | static size_t do_sendfile(int sockfd, int filefd, off_t off, size_t len) |
230 | | { |
231 | | off_t outlen; |
232 | | int ret; |
233 | | while ((ret = sendfile(filefd, sockfd, off, len, NULL, &outlen, 0)) != 0 && errno == EINTR) |
234 | | ; |
235 | | if (ret != 0 && errno != EAGAIN) |
236 | | return SIZE_MAX; |
237 | | return outlen; |
238 | | } |
239 | | #else |
240 | | #define sendvec_sendfile NULL |
241 | | #endif |
242 | | #if !defined(sendvec_sendfile) |
243 | | static size_t sendvec_sendfile(h2o_sendvec_t *src, int sockfd, size_t len) |
244 | 0 | { |
245 | 0 | struct st_h2o_sendfile_generator_t *self = (void *)src->cb_arg[0]; |
246 | 0 | ssize_t bytes_sent = do_sendfile(sockfd, self->file.ref->fd, (off_t)src->cb_arg[1], len); |
247 | 0 | if (bytes_sent > 0) { |
248 | 0 | src->cb_arg[1] += bytes_sent; |
249 | 0 | src->len -= bytes_sent; |
250 | 0 | } |
251 | 0 | return bytes_sent; |
252 | 0 | } |
253 | | #endif |
254 | | |
255 | | static void do_proceed(h2o_generator_t *_self, h2o_req_t *req) |
256 | 0 | { |
257 | 0 | struct st_h2o_sendfile_generator_t *self = (void *)_self; |
258 | 0 | size_t bytes_to_send = self->bytesleft < H2O_PULL_SENDVEC_MAX_SIZE ? self->bytesleft : H2O_PULL_SENDVEC_MAX_SIZE; |
259 | | |
260 | | /* if io_uring is to be used, addref so that the self would not be released, then call `h2o_io_uring_splice_file` */ |
261 | | #if H2O_USE_IO_URING |
262 | | if (self->splice_fds[0] != -1) { |
263 | | h2o_mem_addref_shared(self); |
264 | | h2o_io_uring_splice(self->src_req->conn->ctx->loop, self->file.ref->fd, self->file.off, self->splice_fds[1], -1, |
265 | | bytes_to_send, 0, do_proceed_on_splice_complete, self); |
266 | | return; |
267 | | } |
268 | | #endif |
269 | |
|
270 | 0 | static const h2o_sendvec_callbacks_t sendvec_callbacks = {.read_ = do_pread, .send_ = sendvec_sendfile}; |
271 | 0 | h2o_sendvec_t vec = { |
272 | 0 | .callbacks = &sendvec_callbacks, .len = bytes_to_send, .cb_arg[0] = (uint64_t)self, .cb_arg[1] = self->file.off}; |
273 | |
|
274 | 0 | self->file.off += vec.len; |
275 | 0 | self->bytesleft -= vec.len; |
276 | |
|
277 | 0 | h2o_sendvec(req, &vec, 1, self->bytesleft != 0 ? H2O_SEND_STATE_IN_PROGRESS : H2O_SEND_STATE_FINAL); |
278 | 0 | } |
279 | | |
280 | | static void do_multirange_proceed(h2o_generator_t *_self, h2o_req_t *req) |
281 | 0 | { |
282 | 0 | struct st_h2o_sendfile_generator_t *self = (void *)_self; |
283 | 0 | size_t rlen, used_buf = 0; |
284 | 0 | ssize_t rret, vecarrsize; |
285 | 0 | h2o_iovec_t vec[2]; |
286 | 0 | h2o_send_state_t send_state; |
287 | |
|
288 | 0 | if (self->bytesleft == 0) { |
289 | 0 | size_t *range_cur = self->ranged.range_infos + 2 * self->ranged.current_range; |
290 | 0 | size_t range_end = *range_cur + *(range_cur + 1) - 1; |
291 | 0 | if (H2O_LIKELY(self->ranged.current_range != 0)) |
292 | 0 | used_buf = |
293 | 0 | sprintf(self->ranged.multirange_buf, "\r\n--%s\r\nContent-Type: %s\r\nContent-Range: bytes %zd-%zd/%zd\r\n\r\n", |
294 | 0 | self->ranged.boundary.base, self->ranged.mimetype.base, *range_cur, range_end, self->ranged.filesize); |
295 | 0 | else |
296 | 0 | used_buf = |
297 | 0 | sprintf(self->ranged.multirange_buf, "--%s\r\nContent-Type: %s\r\nContent-Range: bytes %zd-%zd/%zd\r\n\r\n", |
298 | 0 | self->ranged.boundary.base, self->ranged.mimetype.base, *range_cur, range_end, self->ranged.filesize); |
299 | 0 | self->ranged.current_range++; |
300 | 0 | self->file.off = *range_cur; |
301 | 0 | self->bytesleft = *++range_cur; |
302 | 0 | } |
303 | 0 | rlen = self->bytesleft; |
304 | 0 | if (rlen + used_buf > MAX_BUF_SIZE) |
305 | 0 | rlen = MAX_BUF_SIZE - used_buf; |
306 | 0 | while ((rret = pread(self->file.ref->fd, self->ranged.multirange_buf + used_buf, rlen, self->file.off)) == -1 && errno == EINTR) |
307 | 0 | ; |
308 | 0 | if (rret == -1) |
309 | 0 | goto Error; |
310 | 0 | self->file.off += rret; |
311 | 0 | self->bytesleft -= rret; |
312 | |
|
313 | 0 | vec[0].base = self->ranged.multirange_buf; |
314 | 0 | vec[0].len = rret + used_buf; |
315 | 0 | if (self->ranged.current_range == self->ranged.range_count && self->bytesleft == 0) { |
316 | 0 | vec[1].base = h2o_mem_alloc_pool(&req->pool, char, sizeof("\r\n--") - 1 + BOUNDARY_SIZE + sizeof("--\r\n")); |
317 | 0 | vec[1].len = sprintf(vec[1].base, "\r\n--%s--\r\n", self->ranged.boundary.base); |
318 | 0 | vecarrsize = 2; |
319 | 0 | send_state = H2O_SEND_STATE_FINAL; |
320 | 0 | } else { |
321 | 0 | vecarrsize = 1; |
322 | 0 | send_state = H2O_SEND_STATE_IN_PROGRESS; |
323 | 0 | } |
324 | 0 | h2o_send(req, vec, vecarrsize, send_state); |
325 | 0 | return; |
326 | | |
327 | 0 | Error: |
328 | 0 | h2o_send(req, NULL, 0, H2O_SEND_STATE_ERROR); |
329 | 0 | return; |
330 | 0 | } |
331 | | |
332 | | static struct st_h2o_sendfile_generator_t *create_generator(h2o_req_t *req, const char *path, size_t path_len, int *is_dir, |
333 | | int flags) |
334 | 1.31k | { |
335 | 1.31k | struct st_h2o_sendfile_generator_t *self; |
336 | 1.31k | h2o_filecache_ref_t *fileref; |
337 | 1.31k | h2o_iovec_t content_encoding = (h2o_iovec_t){NULL}; |
338 | 1.31k | unsigned gunzip = 0; |
339 | | |
340 | 1.31k | *is_dir = 0; |
341 | | |
342 | 1.31k | if ((flags & H2O_FILE_FLAG_SEND_COMPRESSED) != 0 && req->version >= 0x101) { |
343 | 0 | int compressible_types = h2o_get_compressible_types(&req->headers); |
344 | 0 | if (compressible_types != 0) { |
345 | 0 | char *variant_path = h2o_mem_alloc_pool(&req->pool, *variant_path, path_len + sizeof(".gz")); |
346 | 0 | memcpy(variant_path, path, path_len); |
347 | 0 | #define TRY_VARIANT(mask, enc, ext) \ |
348 | 0 | if ((compressible_types & mask) != 0) { \ |
349 | 0 | strcpy(variant_path + path_len, ext); \ |
350 | 0 | if ((fileref = h2o_filecache_open_file(req->conn->ctx->filecache, variant_path, O_RDONLY | O_CLOEXEC)) != NULL) { \ |
351 | 0 | content_encoding = h2o_iovec_init(enc, sizeof(enc) - 1); \ |
352 | 0 | goto Opened; \ |
353 | 0 | } \ |
354 | 0 | } |
355 | 0 | TRY_VARIANT(H2O_COMPRESSIBLE_BROTLI, "br", ".br"); |
356 | 0 | TRY_VARIANT(H2O_COMPRESSIBLE_ZSTD, "zstd", ".zst"); |
357 | 0 | TRY_VARIANT(H2O_COMPRESSIBLE_GZIP, "gzip", ".gz"); |
358 | | |
359 | | // Deprecated |
360 | 0 | TRY_VARIANT(H2O_COMPRESSIBLE_ZSTD, "zstd", ".zstd"); |
361 | 0 | #undef TRY_VARIANT |
362 | 0 | } |
363 | 0 | } |
364 | 1.31k | if ((fileref = h2o_filecache_open_file(req->conn->ctx->filecache, path, O_RDONLY | O_CLOEXEC)) != NULL) { |
365 | 0 | goto Opened; |
366 | 0 | } |
367 | 1.31k | if ((flags & H2O_FILE_FLAG_GUNZIP) != 0 && req->version >= 0x101) { |
368 | 0 | char *variant_path = h2o_mem_alloc_pool(&req->pool, *variant_path, path_len + sizeof(".gz")); |
369 | 0 | memcpy(variant_path, path, path_len); |
370 | 0 | strcpy(variant_path + path_len, ".gz"); |
371 | 0 | if ((fileref = h2o_filecache_open_file(req->conn->ctx->filecache, variant_path, O_RDONLY | O_CLOEXEC)) != NULL) { |
372 | 0 | gunzip = 1; |
373 | 0 | goto Opened; |
374 | 0 | } |
375 | 0 | } |
376 | 1.31k | return NULL; |
377 | | |
378 | 0 | Opened: |
379 | 0 | if (S_ISDIR(fileref->st.st_mode)) { |
380 | 0 | h2o_filecache_close_file(fileref); |
381 | 0 | *is_dir = 1; |
382 | 0 | return NULL; |
383 | 0 | } |
384 | | |
385 | 0 | self = h2o_mem_alloc_shared(&req->pool, sizeof(*self), on_generator_dispose); |
386 | 0 | self->super.proceed = do_proceed; |
387 | 0 | self->super.stop = NULL; |
388 | 0 | self->file.ref = fileref; |
389 | 0 | self->file.off = 0; |
390 | 0 | self->bytesleft = self->file.ref->st.st_size; |
391 | 0 | self->ranged.range_count = 0; |
392 | 0 | self->ranged.range_infos = NULL; |
393 | 0 | self->content_encoding = content_encoding; |
394 | 0 | self->send_vary = (flags & H2O_FILE_FLAG_SEND_COMPRESSED) != 0; |
395 | 0 | self->send_etag = (flags & H2O_FILE_FLAG_NO_ETAG) == 0; |
396 | 0 | self->gunzip = gunzip; |
397 | | #if H2O_USE_IO_URING |
398 | | int try_async_splice = (flags & H2O_FILE_FLAG_IO_URING) != 0 && self->bytesleft != 0; |
399 | | if (try_async_splice && h2o_context_new_pipe(req->conn->ctx, self->splice_fds)) { |
400 | | self->super.stop = do_stop_async_splice; |
401 | | self->src_req = req; |
402 | | } else { |
403 | | if (try_async_splice) |
404 | | h2o_req_log_error(req, "lib/handler/file.c", "failed to allocate a pipe for async I/O; falling back to blocking I/O"); |
405 | | self->splice_fds[0] = -1; |
406 | | self->splice_fds[1] = -1; |
407 | | } |
408 | | #endif |
409 | |
|
410 | 0 | return self; |
411 | 0 | } |
412 | | |
413 | | static void add_headers_unconditional(struct st_h2o_sendfile_generator_t *self, h2o_req_t *req) |
414 | 0 | { |
415 | | /* RFC 7232 4.1: The server generating a 304 response MUST generate any of the following header fields that would have been sent |
416 | | * in a 200 (OK) response to the same request: Cache-Control, Content-Location, Date, ETag, Expires, and Vary (snip) a sender |
417 | | * SHOULD NOT generate representation metadata other than the above listed fields unless said metadata exists for the purpose of |
418 | | * guiding cache updates. */ |
419 | 0 | if (self->send_etag) { |
420 | 0 | size_t etag_len = h2o_filecache_get_etag(self->file.ref, self->header_bufs.etag); |
421 | 0 | h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_ETAG, NULL, self->header_bufs.etag, etag_len); |
422 | 0 | } |
423 | 0 | if (self->send_vary) |
424 | 0 | h2o_set_header_token(&req->pool, &req->res.headers, H2O_TOKEN_VARY, H2O_STRLIT("accept-encoding")); |
425 | 0 | } |
426 | | |
427 | | static void send_decompressed(h2o_ostream_t *_self, h2o_req_t *req, h2o_sendvec_t *inbufs, size_t inbufcnt, h2o_send_state_t state) |
428 | 0 | { |
429 | 0 | if (inbufcnt == 0 && h2o_send_state_is_in_progress(state)) { |
430 | 0 | h2o_ostream_send_next(_self, req, inbufs, inbufcnt, state); |
431 | 0 | return; |
432 | 0 | } |
433 | | |
434 | 0 | struct st_gzip_decompress_t *self = (void *)_self; |
435 | 0 | h2o_sendvec_t *outbufs; |
436 | 0 | size_t outbufcnt; |
437 | |
|
438 | 0 | state = h2o_compress_transform(self->decompressor, req, inbufs, inbufcnt, state, &outbufs, &outbufcnt); |
439 | 0 | h2o_ostream_send_next(&self->super, req, outbufs, outbufcnt, state); |
440 | 0 | } |
441 | | |
442 | | static void do_send_file(struct st_h2o_sendfile_generator_t *self, h2o_req_t *req, int status, const char *reason, |
443 | | h2o_iovec_t mime_type, h2o_mime_attributes_t *mime_attr, int is_get) |
444 | 0 | { |
445 | | /* setup response */ |
446 | 0 | req->res.status = status; |
447 | 0 | req->res.reason = reason; |
448 | 0 | req->res.content_length = self->gunzip ? SIZE_MAX : self->bytesleft; |
449 | 0 | req->res.mime_attr = mime_attr; |
450 | |
|
451 | 0 | if (self->ranged.range_count > 1) { |
452 | 0 | mime_type.base = h2o_mem_alloc_pool(&req->pool, char, 52); |
453 | 0 | mime_type.len = sprintf(mime_type.base, "multipart/byteranges; boundary=%s", self->ranged.boundary.base); |
454 | 0 | } |
455 | 0 | h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL, mime_type.base, mime_type.len); |
456 | 0 | h2o_filecache_get_last_modified(self->file.ref, self->header_bufs.last_modified); |
457 | 0 | h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_LAST_MODIFIED, NULL, self->header_bufs.last_modified, |
458 | 0 | H2O_TIMESTR_RFC1123_LEN); |
459 | 0 | add_headers_unconditional(self, req); |
460 | 0 | if (self->content_encoding.base != NULL) |
461 | 0 | h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_ENCODING, NULL, self->content_encoding.base, |
462 | 0 | self->content_encoding.len); |
463 | 0 | if (self->ranged.range_count == 0) |
464 | 0 | h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_ACCEPT_RANGES, NULL, H2O_STRLIT("bytes")); |
465 | 0 | else if (self->ranged.range_count == 1) { |
466 | 0 | h2o_iovec_t content_range; |
467 | 0 | content_range.base = h2o_mem_alloc_pool(&req->pool, char, 128); |
468 | 0 | content_range.len = sprintf(content_range.base, "bytes %zd-%zd/%zd", self->ranged.range_infos[0], |
469 | 0 | self->ranged.range_infos[0] + self->ranged.range_infos[1] - 1, self->ranged.filesize); |
470 | 0 | h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_RANGE, NULL, content_range.base, content_range.len); |
471 | 0 | } |
472 | | |
473 | | /* special path for cases where we do not need to send any data */ |
474 | 0 | if (!is_get || self->bytesleft == 0) { |
475 | 0 | static h2o_generator_t generator = {NULL, NULL}; |
476 | 0 | h2o_start_response(req, &generator); |
477 | 0 | h2o_send(req, NULL, 0, H2O_SEND_STATE_FINAL); |
478 | 0 | return; |
479 | 0 | } |
480 | | |
481 | | /* send data */ |
482 | 0 | h2o_start_response(req, &self->super); |
483 | | |
484 | | /* dynamically setup gzip decompress ostream */ |
485 | 0 | if (self->gunzip) { |
486 | 0 | struct st_gzip_decompress_t *decoder = |
487 | 0 | (void *)h2o_add_ostream(req, H2O_ALIGNOF(*decoder), sizeof(*decoder), &req->_ostr_top); |
488 | 0 | decoder->decompressor = h2o_compress_gunzip_open(&req->pool); |
489 | 0 | decoder->super.do_send = send_decompressed; |
490 | | /* FIXME disable pull mode */ |
491 | 0 | } |
492 | |
|
493 | 0 | if (self->ranged.range_count == 1) |
494 | 0 | self->file.off = self->ranged.range_infos[0]; |
495 | |
|
496 | 0 | if (self->ranged.range_count < 2) |
497 | 0 | do_proceed(&self->super, req); |
498 | 0 | else { |
499 | 0 | self->ranged.multirange_buf = h2o_mem_alloc_pool(&req->pool, char, MAX_BUF_SIZE); |
500 | 0 | self->bytesleft = 0; |
501 | 0 | self->super.proceed = do_multirange_proceed; |
502 | 0 | do_multirange_proceed(&self->super, req); |
503 | 0 | } |
504 | 0 | } |
505 | | |
506 | | int h2o_file_send(h2o_req_t *req, int status, const char *reason, const char *path, h2o_iovec_t mime_type, int flags) |
507 | 0 | { |
508 | 0 | struct st_h2o_sendfile_generator_t *self; |
509 | 0 | int is_dir; |
510 | |
|
511 | 0 | if ((self = create_generator(req, path, strlen(path), &is_dir, flags)) == NULL) |
512 | 0 | return -1; |
513 | | /* note: is_dir is not handled */ |
514 | 0 | do_send_file(self, req, status, reason, mime_type, NULL, 1); |
515 | 0 | return 0; |
516 | 0 | } |
517 | | |
518 | | static int send_dir_listing(h2o_req_t *req, const char *path, size_t path_len, int is_get) |
519 | 0 | { |
520 | 0 | static h2o_generator_t generator = {NULL, NULL}; |
521 | 0 | DIR *dp; |
522 | 0 | h2o_buffer_t *body; |
523 | 0 | h2o_iovec_t bodyvec; |
524 | | |
525 | | /* build html */ |
526 | 0 | if ((dp = opendir(path)) == NULL) |
527 | 0 | return -1; |
528 | 0 | body = build_dir_listing_html(&req->pool, req->path_normalized, dp); |
529 | 0 | closedir(dp); |
530 | |
|
531 | 0 | if (body == NULL) { |
532 | 0 | h2o_send_error_503(req, "Service Unavailable", "please try again later", 0); |
533 | 0 | return 0; |
534 | 0 | } |
535 | | |
536 | 0 | bodyvec = h2o_iovec_init(body->bytes, body->size); |
537 | 0 | h2o_buffer_link_to_pool(body, &req->pool); |
538 | | |
539 | | /* send response */ |
540 | 0 | req->res.status = 200; |
541 | 0 | req->res.reason = "OK"; |
542 | 0 | h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL, H2O_STRLIT("text/html; charset=utf-8")); |
543 | | |
544 | | /* send headers */ |
545 | 0 | if (!is_get) { |
546 | 0 | h2o_send_inline(req, NULL, 0); |
547 | 0 | return 0; |
548 | 0 | } |
549 | | |
550 | | /* send data */ |
551 | 0 | h2o_start_response(req, &generator); |
552 | 0 | h2o_send(req, &bodyvec, 1, H2O_SEND_STATE_FINAL); |
553 | 0 | return 0; |
554 | 0 | } |
555 | | |
556 | | static size_t *process_range(h2o_mem_pool_t *pool, h2o_iovec_t *range_value, size_t file_size, size_t *ret) |
557 | 0 | { |
558 | 0 | #define CHECK_EOF() \ |
559 | 0 | if (buf == buf_end) \ |
560 | 0 | return NULL; |
561 | |
|
562 | 0 | #define CHECK_OVERFLOW(range) \ |
563 | 0 | if (range == SIZE_MAX) \ |
564 | 0 | return NULL; |
565 | |
|
566 | 0 | size_t range_start = SIZE_MAX, range_count = 0; |
567 | 0 | char *buf = range_value->base, *buf_end = buf + range_value->len; |
568 | 0 | int needs_comma = 0; |
569 | 0 | H2O_VECTOR(size_t) ranges = {NULL}; |
570 | |
|
571 | 0 | if (range_value->len < 6 || memcmp(buf, "bytes=", 6) != 0) |
572 | 0 | return NULL; |
573 | | |
574 | 0 | buf += 6; |
575 | 0 | CHECK_EOF(); |
576 | | |
577 | | /* most range requests contain only one range */ |
578 | 0 | do { |
579 | 0 | while (1) { |
580 | 0 | if (*buf != ',') { |
581 | 0 | if (needs_comma) |
582 | 0 | return NULL; |
583 | 0 | break; |
584 | 0 | } |
585 | 0 | needs_comma = 0; |
586 | 0 | buf++; |
587 | 0 | while (H2O_UNLIKELY(*buf == ' ') || H2O_UNLIKELY(*buf == '\t')) { |
588 | 0 | buf++; |
589 | 0 | CHECK_EOF(); |
590 | 0 | } |
591 | 0 | } |
592 | 0 | if (H2O_UNLIKELY(buf == buf_end)) |
593 | 0 | break; |
594 | 0 | if (H2O_LIKELY((range_start = h2o_strtosizefwd(&buf, buf_end - buf)) != SIZE_MAX)) { |
595 | 0 | CHECK_EOF(); |
596 | 0 | if (*buf++ != '-') |
597 | 0 | return NULL; |
598 | 0 | range_count = h2o_strtosizefwd(&buf, buf_end - buf); |
599 | 0 | if (H2O_UNLIKELY(range_start >= file_size)) { |
600 | 0 | range_start = SIZE_MAX; |
601 | 0 | } else if (H2O_LIKELY(range_count != SIZE_MAX)) { |
602 | 0 | if (H2O_UNLIKELY(range_count > file_size - 1)) |
603 | 0 | range_count = file_size - 1; |
604 | 0 | if (H2O_LIKELY(range_start <= range_count)) |
605 | 0 | range_count -= range_start - 1; |
606 | 0 | else |
607 | 0 | range_start = SIZE_MAX; |
608 | 0 | } else { |
609 | 0 | range_count = file_size - range_start; |
610 | 0 | } |
611 | 0 | } else if (H2O_LIKELY(*buf++ == '-')) { |
612 | 0 | CHECK_EOF(); |
613 | 0 | range_count = h2o_strtosizefwd(&buf, buf_end - buf); |
614 | 0 | if (H2O_UNLIKELY(range_count == SIZE_MAX)) |
615 | 0 | return NULL; |
616 | 0 | if (H2O_LIKELY(range_count != 0)) { |
617 | 0 | if (H2O_UNLIKELY(range_count > file_size)) |
618 | 0 | range_count = file_size; |
619 | 0 | range_start = file_size - range_count; |
620 | 0 | } else { |
621 | 0 | range_start = SIZE_MAX; |
622 | 0 | } |
623 | 0 | } else { |
624 | 0 | return NULL; |
625 | 0 | } |
626 | | |
627 | 0 | if (H2O_LIKELY(range_start != SIZE_MAX)) { |
628 | 0 | h2o_vector_reserve(pool, &ranges, ranges.size + 2); |
629 | 0 | ranges.entries[ranges.size++] = range_start; |
630 | 0 | ranges.entries[ranges.size++] = range_count; |
631 | 0 | } |
632 | 0 | if (buf != buf_end) |
633 | 0 | while (H2O_UNLIKELY(*buf == ' ') || H2O_UNLIKELY(*buf == '\t')) { |
634 | 0 | buf++; |
635 | 0 | CHECK_EOF(); |
636 | 0 | } |
637 | 0 | needs_comma = 1; |
638 | 0 | } while (H2O_UNLIKELY(buf < buf_end)); |
639 | 0 | *ret = ranges.size / 2; |
640 | 0 | return ranges.entries; |
641 | 0 | #undef CHECK_EOF |
642 | 0 | #undef CHECK_OVERFLOW |
643 | 0 | } |
644 | | |
645 | | static void gen_rand_string(h2o_iovec_t *s) |
646 | 0 | { |
647 | 0 | int i; |
648 | 0 | static const char alphanum[] = "0123456789" |
649 | 0 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
650 | 0 | "abcdefghijklmnopqrstuvwxyz"; |
651 | |
|
652 | 0 | for (i = 0; i < s->len; ++i) { |
653 | 0 | s->base[i] = alphanum[h2o_rand() % (sizeof(alphanum) - 1)]; |
654 | 0 | } |
655 | |
|
656 | 0 | s->base[s->len] = 0; |
657 | 0 | } |
658 | | |
659 | | static int delegate_dynamic_request(h2o_req_t *req, h2o_iovec_t script_name, h2o_iovec_t path_info, const char *local_path, |
660 | | size_t local_path_len, h2o_mimemap_type_t *mime_type) |
661 | 0 | { |
662 | 0 | h2o_filereq_t *filereq; |
663 | |
|
664 | 0 | assert(mime_type->data.dynamic.pathconf.handlers.size == 1); |
665 | 0 | assert(mime_type->data.dynamic.pathconf._filters.size == 0); |
666 | 0 | assert(mime_type->data.dynamic.pathconf._loggers.size == 0); |
667 | | |
668 | | /* setup CGI attributes (e.g., PATH_INFO) */ |
669 | 0 | filereq = h2o_mem_alloc_pool(&req->pool, *filereq, 1); |
670 | 0 | filereq->script_name = script_name; |
671 | 0 | filereq->path_info = path_info; |
672 | 0 | filereq->local_path = h2o_strdup(&req->pool, local_path, local_path_len); |
673 | 0 | req->filereq = filereq; |
674 | | |
675 | | /* apply environment */ |
676 | 0 | if (mime_type->data.dynamic.pathconf.env != NULL) |
677 | 0 | h2o_req_apply_env(req, mime_type->data.dynamic.pathconf.env); |
678 | | |
679 | | /* call the dynamic handler while retaining current hostconf or pathconf; in other words, filters and loggers of current |
680 | | * path level is applied, rather than of the extension level */ |
681 | 0 | h2o_handler_t *handler = mime_type->data.dynamic.pathconf.handlers.entries[0]; |
682 | 0 | return handler->on_req(handler, req); |
683 | 0 | } |
684 | | |
685 | | static int try_dynamic_request(h2o_file_handler_t *self, h2o_req_t *req, char *rpath, size_t rpath_len) |
686 | 0 | { |
687 | | /* we have full local path in {rpath,rpath_len}, and need to split it into name and path_info */ |
688 | 0 | struct stat st; |
689 | 0 | size_t slash_at = self->real_path.len; |
690 | |
|
691 | 0 | while (1) { |
692 | | /* find the next slash (or return -1 if failed) */ |
693 | 0 | for (++slash_at;; ++slash_at) { |
694 | 0 | if (slash_at >= rpath_len) |
695 | 0 | return -1; |
696 | 0 | if (rpath[slash_at] == '/') |
697 | 0 | break; |
698 | 0 | } |
699 | | /* change the slash to '\0', and check if the file exists */ |
700 | 0 | rpath[slash_at] = '\0'; |
701 | 0 | if (stat(rpath, &st) != 0) |
702 | 0 | return -1; |
703 | 0 | if (!S_ISDIR(st.st_mode)) |
704 | 0 | break; |
705 | | /* restore slash, and continue the search */ |
706 | 0 | rpath[slash_at] = '/'; |
707 | 0 | } |
708 | | |
709 | | /* file found! */ |
710 | 0 | h2o_mimemap_type_t *mime_type = h2o_mimemap_get_type_by_extension(self->mimemap, h2o_get_filext(rpath, slash_at)); |
711 | 0 | switch (mime_type->type) { |
712 | 0 | case H2O_MIMEMAP_TYPE_MIMETYPE: |
713 | 0 | return -1; |
714 | 0 | case H2O_MIMEMAP_TYPE_DYNAMIC: { |
715 | 0 | h2o_iovec_t script_name = h2o_iovec_init(req->path_normalized.base, self->conf_path.len + slash_at - self->real_path.len); |
716 | 0 | h2o_iovec_t path_info = |
717 | 0 | h2o_iovec_init(req->path_normalized.base + script_name.len, req->path_normalized.len - script_name.len); |
718 | 0 | return delegate_dynamic_request(req, script_name, path_info, rpath, slash_at, mime_type); |
719 | 0 | } |
720 | 0 | } |
721 | 0 | h2o_fatal("unknown h2o_miemmap_type_t::type (%d)\n", (int)mime_type->type); |
722 | 0 | } |
723 | | |
724 | | static void send_method_not_allowed(h2o_req_t *req) |
725 | 0 | { |
726 | 0 | h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_ALLOW, NULL, H2O_STRLIT("GET, HEAD")); |
727 | 0 | h2o_send_error_405(req, "Method Not Allowed", "method not allowed", H2O_SEND_ERROR_KEEP_HEADERS); |
728 | 0 | } |
729 | | |
730 | | static int serve_with_generator(struct st_h2o_sendfile_generator_t *generator, h2o_req_t *req, h2o_iovec_t resolved_path, |
731 | | const char *rpath, size_t rpath_len, h2o_mimemap_type_t *mime_type) |
732 | 0 | { |
733 | 0 | enum { METHOD_IS_GET, METHOD_IS_HEAD, METHOD_IS_OTHER } method_type; |
734 | 0 | size_t if_modified_since_header_index, if_none_match_header_index; |
735 | 0 | size_t range_header_index, if_range_header_index; |
736 | | |
737 | | /* determine the method */ |
738 | 0 | if (h2o_memis(req->method.base, req->method.len, H2O_STRLIT("GET"))) { |
739 | 0 | method_type = METHOD_IS_GET; |
740 | 0 | } else if (h2o_memis(req->method.base, req->method.len, H2O_STRLIT("HEAD"))) { |
741 | 0 | method_type = METHOD_IS_HEAD; |
742 | 0 | } else { |
743 | 0 | method_type = METHOD_IS_OTHER; |
744 | 0 | } |
745 | | |
746 | | /* obtain mime type */ |
747 | 0 | if (mime_type->type == H2O_MIMEMAP_TYPE_DYNAMIC) { |
748 | 0 | assert(generator->file.ref != NULL); |
749 | 0 | close_file(generator); |
750 | 0 | return delegate_dynamic_request(req, resolved_path, h2o_iovec_init(NULL, 0), rpath, rpath_len, mime_type); |
751 | 0 | } |
752 | 0 | assert(mime_type->type == H2O_MIMEMAP_TYPE_MIMETYPE); |
753 | | |
754 | | /* if-non-match and if-modified-since */ |
755 | 0 | if ((if_none_match_header_index = h2o_find_header(&req->headers, H2O_TOKEN_IF_NONE_MATCH, -1)) != -1) { |
756 | 0 | h2o_iovec_t *if_none_match = &req->headers.entries[if_none_match_header_index].value; |
757 | 0 | char etag[H2O_FILECACHE_ETAG_MAXLEN + 1]; |
758 | 0 | size_t etag_len = h2o_filecache_get_etag(generator->file.ref, etag); |
759 | 0 | if (h2o_filecache_compare_etag_strong(if_none_match->base, if_none_match->len, etag, etag_len)) |
760 | 0 | goto NotModified; |
761 | 0 | } else if ((if_modified_since_header_index = h2o_find_header(&req->headers, H2O_TOKEN_IF_MODIFIED_SINCE, -1)) != -1) { |
762 | 0 | h2o_iovec_t *ims_vec = &req->headers.entries[if_modified_since_header_index].value; |
763 | 0 | struct tm ims_tm, *last_modified_tm; |
764 | 0 | if (h2o_time_parse_rfc1123(ims_vec->base, ims_vec->len, &ims_tm) == 0) { |
765 | 0 | last_modified_tm = h2o_filecache_get_last_modified(generator->file.ref, NULL); |
766 | 0 | if (!tm_is_lessthan(&ims_tm, last_modified_tm)) |
767 | 0 | goto NotModified; |
768 | 0 | } |
769 | 0 | } |
770 | | |
771 | | /* only allow GET or HEAD for static files */ |
772 | 0 | if (method_type == METHOD_IS_OTHER) { |
773 | 0 | close_file(generator); |
774 | 0 | send_method_not_allowed(req); |
775 | 0 | return 0; |
776 | 0 | } |
777 | | |
778 | | /* range request */ |
779 | 0 | if ((range_header_index = h2o_find_header(&req->headers, H2O_TOKEN_RANGE, -1)) != -1) { |
780 | | /* if range */ |
781 | 0 | if ((if_range_header_index = h2o_find_header(&req->headers, H2O_TOKEN_IF_RANGE, -1)) != -1) { |
782 | 0 | h2o_iovec_t *if_range = &req->headers.entries[if_range_header_index].value; |
783 | | /* first try parse if-range as http-date */ |
784 | 0 | struct tm ir_tm, *last_modified_tm; |
785 | 0 | if (h2o_time_parse_rfc1123(if_range->base, if_range->len, &ir_tm) == 0) { |
786 | 0 | last_modified_tm = h2o_filecache_get_last_modified(generator->file.ref, NULL); |
787 | 0 | if (tm_is_lessthan(&ir_tm, last_modified_tm)) |
788 | 0 | goto EntireFile; |
789 | 0 | } else { /* treat it as an e-tag */ |
790 | 0 | char etag[H2O_FILECACHE_ETAG_MAXLEN + 1]; |
791 | 0 | size_t etag_len = h2o_filecache_get_etag(generator->file.ref, etag); |
792 | 0 | if (!h2o_filecache_compare_etag_strong(if_range->base, if_range->len, etag, etag_len)) |
793 | 0 | goto EntireFile; |
794 | 0 | } |
795 | 0 | } |
796 | 0 | h2o_iovec_t *range = &req->headers.entries[range_header_index].value; |
797 | 0 | size_t *range_infos, range_count; |
798 | 0 | range_infos = process_range(&req->pool, range, generator->bytesleft, &range_count); |
799 | 0 | if (range_infos == NULL) { |
800 | 0 | h2o_iovec_t content_range; |
801 | 0 | content_range.base = h2o_mem_alloc_pool(&req->pool, char, 32); |
802 | 0 | content_range.len = sprintf(content_range.base, "bytes */%zu", generator->bytesleft); |
803 | 0 | h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_RANGE, NULL, content_range.base, content_range.len); |
804 | 0 | h2o_send_error_416(req, "Request Range Not Satisfiable", "requested range not satisfiable", |
805 | 0 | H2O_SEND_ERROR_KEEP_HEADERS); |
806 | 0 | goto Close; |
807 | 0 | } |
808 | 0 | generator->ranged.range_count = range_count; |
809 | 0 | generator->ranged.range_infos = range_infos; |
810 | 0 | generator->ranged.current_range = 0; |
811 | 0 | generator->ranged.filesize = generator->bytesleft; |
812 | | |
813 | | /* set content-length according to range */ |
814 | 0 | if (range_count == 1) |
815 | 0 | generator->bytesleft = range_infos[1]; |
816 | 0 | else { |
817 | 0 | generator->ranged.mimetype = h2o_strdup(&req->pool, mime_type->data.mimetype.base, mime_type->data.mimetype.len); |
818 | 0 | size_t final_content_len = 0, size_tmp = 0, size_fixed_each_part, i; |
819 | 0 | generator->ranged.boundary.base = h2o_mem_alloc_pool(&req->pool, char, BOUNDARY_SIZE + 1); |
820 | 0 | generator->ranged.boundary.len = BOUNDARY_SIZE; |
821 | 0 | gen_rand_string(&generator->ranged.boundary); |
822 | 0 | i = generator->bytesleft; |
823 | 0 | while (i) { |
824 | 0 | i /= 10; |
825 | 0 | size_tmp++; |
826 | 0 | } |
827 | 0 | size_fixed_each_part = FIXED_PART_SIZE + mime_type->data.mimetype.len + size_tmp; |
828 | 0 | for (i = 0; i < range_count; i++) { |
829 | 0 | size_tmp = *range_infos++; |
830 | 0 | if (size_tmp == 0) |
831 | 0 | final_content_len++; |
832 | 0 | while (size_tmp) { |
833 | 0 | size_tmp /= 10; |
834 | 0 | final_content_len++; |
835 | 0 | } |
836 | |
|
837 | 0 | size_tmp = *(range_infos - 1); |
838 | 0 | final_content_len += *range_infos; |
839 | |
|
840 | 0 | size_tmp += *range_infos++ - 1; |
841 | 0 | if (size_tmp == 0) |
842 | 0 | final_content_len++; |
843 | 0 | while (size_tmp) { |
844 | 0 | size_tmp /= 10; |
845 | 0 | final_content_len++; |
846 | 0 | } |
847 | 0 | } |
848 | 0 | final_content_len += sizeof("\r\n--") - 1 + BOUNDARY_SIZE + sizeof("--\r\n") - 1 + size_fixed_each_part * range_count - |
849 | 0 | (sizeof("\r\n") - 1); |
850 | 0 | generator->bytesleft = final_content_len; |
851 | 0 | } |
852 | 0 | do_send_file(generator, req, 206, "Partial Content", mime_type->data.mimetype, &h2o_mime_attributes_as_is, |
853 | 0 | method_type == METHOD_IS_GET); |
854 | 0 | return 0; |
855 | 0 | } |
856 | | |
857 | 0 | EntireFile: |
858 | | /* return file */ |
859 | 0 | do_send_file(generator, req, 200, "OK", mime_type->data.mimetype, &mime_type->data.attr, method_type == METHOD_IS_GET); |
860 | 0 | return 0; |
861 | | |
862 | 0 | NotModified: |
863 | 0 | req->res.status = 304; |
864 | 0 | req->res.reason = "Not Modified"; |
865 | 0 | add_headers_unconditional(generator, req); |
866 | 0 | h2o_send_inline(req, NULL, 0); |
867 | 0 | Close: |
868 | 0 | close_file(generator); |
869 | 0 | return 0; |
870 | 0 | } |
871 | | |
872 | | static int on_req(h2o_handler_t *_self, h2o_req_t *req) |
873 | 625 | { |
874 | 625 | h2o_file_handler_t *self = (void *)_self; |
875 | 625 | char *rpath; |
876 | 625 | size_t rpath_len, req_path_prefix; |
877 | 625 | struct st_h2o_sendfile_generator_t *generator = NULL; |
878 | 625 | int is_dir; |
879 | | |
880 | 625 | if (req->path_normalized.len < self->conf_path.len) { |
881 | 0 | h2o_iovec_t dest = h2o_uri_escape(&req->pool, self->conf_path.base, self->conf_path.len, "/"); |
882 | 0 | if (req->query_at != SIZE_MAX) |
883 | 0 | dest = h2o_concat(&req->pool, dest, h2o_iovec_init(req->path.base + req->query_at, req->path.len - req->query_at)); |
884 | 0 | h2o_send_redirect(req, 301, "Moved Permanently", dest.base, dest.len); |
885 | 0 | return 0; |
886 | 0 | } |
887 | | |
888 | | /* build path (still unterminated at the end of the block) */ |
889 | 625 | req_path_prefix = self->conf_path.len; |
890 | 625 | rpath = alloca(self->real_path.len + (req->path_normalized.len - req_path_prefix) + self->max_index_file_len + 1); |
891 | 625 | rpath_len = 0; |
892 | 625 | memcpy(rpath + rpath_len, self->real_path.base, self->real_path.len); |
893 | 625 | rpath_len += self->real_path.len; |
894 | 625 | memcpy(rpath + rpath_len, req->path_normalized.base + req_path_prefix, req->path_normalized.len - req_path_prefix); |
895 | 625 | rpath_len += req->path_normalized.len - req_path_prefix; |
896 | | |
897 | 625 | h2o_resp_add_date_header(req); |
898 | | |
899 | 625 | h2o_iovec_t resolved_path = req->path_normalized; |
900 | | |
901 | | /* build generator (as well as terminating the rpath and its length upon success) */ |
902 | 625 | if (rpath[rpath_len - 1] == '/') { |
903 | 344 | h2o_iovec_t *index_file; |
904 | 1.37k | for (index_file = self->index_files; index_file->base != NULL; ++index_file) { |
905 | 1.03k | memcpy(rpath + rpath_len, index_file->base, index_file->len); |
906 | 1.03k | rpath[rpath_len + index_file->len] = '\0'; |
907 | 1.03k | if ((generator = create_generator(req, rpath, rpath_len + index_file->len, &is_dir, self->flags)) != NULL) { |
908 | 0 | rpath_len += index_file->len; |
909 | 0 | resolved_path = h2o_concat(&req->pool, req->path_normalized, *index_file); |
910 | 0 | goto Opened; |
911 | 0 | } |
912 | 1.03k | if (is_dir) { |
913 | | /* note: apache redirects "path/" to "path/index.txt/" if index.txt is a dir */ |
914 | 0 | h2o_iovec_t dest = h2o_concat(&req->pool, req->path_normalized, *index_file, h2o_iovec_init(H2O_STRLIT("/"))); |
915 | 0 | dest = h2o_uri_escape(&req->pool, dest.base, dest.len, "/"); |
916 | 0 | if (req->query_at != SIZE_MAX) |
917 | 0 | dest = |
918 | 0 | h2o_concat(&req->pool, dest, h2o_iovec_init(req->path.base + req->query_at, req->path.len - req->query_at)); |
919 | 0 | h2o_send_redirect(req, 301, "Moved Permantently", dest.base, dest.len); |
920 | 0 | return 0; |
921 | 0 | } |
922 | 1.03k | if (errno != ENOENT) |
923 | 1 | break; |
924 | 1.03k | } |
925 | 344 | if (index_file->base == NULL && (self->flags & H2O_FILE_FLAG_DIR_LISTING) != 0) { |
926 | 0 | rpath[rpath_len] = '\0'; |
927 | 0 | int is_get = 0; |
928 | 0 | if (h2o_memis(req->method.base, req->method.len, H2O_STRLIT("GET"))) { |
929 | 0 | is_get = 1; |
930 | 0 | } else if (h2o_memis(req->method.base, req->method.len, H2O_STRLIT("HEAD"))) { |
931 | | /* ok */ |
932 | 0 | } else { |
933 | 0 | send_method_not_allowed(req); |
934 | 0 | return 0; |
935 | 0 | } |
936 | 0 | if (send_dir_listing(req, rpath, rpath_len, is_get) == 0) |
937 | 0 | return 0; |
938 | 0 | } |
939 | 344 | } else { |
940 | 281 | rpath[rpath_len] = '\0'; |
941 | 281 | if ((generator = create_generator(req, rpath, rpath_len, &is_dir, self->flags)) != NULL) |
942 | 0 | goto Opened; |
943 | 281 | if (is_dir) { |
944 | 0 | h2o_iovec_t dest = h2o_concat(&req->pool, req->path_normalized, h2o_iovec_init(H2O_STRLIT("/"))); |
945 | 0 | dest = h2o_uri_escape(&req->pool, dest.base, dest.len, "/"); |
946 | 0 | if (req->query_at != SIZE_MAX) |
947 | 0 | dest = h2o_concat(&req->pool, dest, h2o_iovec_init(req->path.base + req->query_at, req->path.len - req->query_at)); |
948 | 0 | h2o_send_redirect(req, 301, "Moved Permanently", dest.base, dest.len); |
949 | 0 | return 0; |
950 | 0 | } |
951 | 281 | } |
952 | | /* failed to open */ |
953 | | |
954 | 625 | if (errno == ENFILE || errno == EMFILE) { |
955 | 0 | h2o_send_error_503(req, "Service Unavailable", "please try again later", 0); |
956 | 625 | } else { |
957 | 625 | if (h2o_mimemap_has_dynamic_type(self->mimemap) && try_dynamic_request(self, req, rpath, rpath_len) == 0) |
958 | 0 | return 0; |
959 | 625 | if (errno == ENOENT || errno == ENOTDIR) { |
960 | 612 | return -1; |
961 | 612 | } else { |
962 | 13 | h2o_send_error_403(req, "Access Forbidden", "access forbidden", 0); |
963 | 13 | } |
964 | 625 | } |
965 | 13 | return 0; |
966 | | |
967 | 0 | Opened: |
968 | 0 | return serve_with_generator(generator, req, resolved_path, rpath, rpath_len, |
969 | 0 | h2o_mimemap_get_type_by_extension(self->mimemap, h2o_get_filext(rpath, rpath_len))); |
970 | 625 | } |
971 | | |
972 | | static void on_context_init(h2o_handler_t *_self, h2o_context_t *ctx) |
973 | 1 | { |
974 | 1 | h2o_file_handler_t *self = (void *)_self; |
975 | | |
976 | 1 | h2o_mimemap_on_context_init(self->mimemap, ctx); |
977 | 1 | } |
978 | | |
979 | | static void on_context_dispose(h2o_handler_t *_self, h2o_context_t *ctx) |
980 | 0 | { |
981 | 0 | h2o_file_handler_t *self = (void *)_self; |
982 | |
|
983 | 0 | h2o_mimemap_on_context_dispose(self->mimemap, ctx); |
984 | 0 | } |
985 | | |
986 | | static void on_handler_dispose(h2o_handler_t *_self) |
987 | 0 | { |
988 | 0 | h2o_file_handler_t *self = (void *)_self; |
989 | 0 | size_t i; |
990 | |
|
991 | 0 | free(self->conf_path.base); |
992 | 0 | free(self->real_path.base); |
993 | 0 | h2o_mem_release_shared(self->mimemap); |
994 | 0 | for (i = 0; self->index_files[i].base != NULL; ++i) |
995 | 0 | free(self->index_files[i].base); |
996 | 0 | } |
997 | | |
998 | | h2o_file_handler_t *h2o_file_register(h2o_pathconf_t *pathconf, const char *real_path, const char **index_files, |
999 | | h2o_mimemap_t *mimemap, int flags) |
1000 | 1 | { |
1001 | 1 | h2o_file_handler_t *self; |
1002 | 1 | size_t i; |
1003 | | |
1004 | 1 | if (index_files == NULL) |
1005 | 1 | index_files = default_index_files; |
1006 | | |
1007 | | /* allocate memory */ |
1008 | 4 | for (i = 0; index_files[i] != NULL; ++i) |
1009 | 3 | ; |
1010 | 1 | self = |
1011 | 1 | (void *)h2o_create_handler(pathconf, offsetof(h2o_file_handler_t, index_files[0]) + sizeof(self->index_files[0]) * (i + 1)); |
1012 | | |
1013 | | /* setup callbacks */ |
1014 | 1 | self->super.on_context_init = on_context_init; |
1015 | 1 | self->super.on_context_dispose = on_context_dispose; |
1016 | 1 | self->super.dispose = on_handler_dispose; |
1017 | 1 | self->super.on_req = on_req; |
1018 | | |
1019 | | /* setup attributes */ |
1020 | 1 | self->conf_path = h2o_strdup_slashed(NULL, pathconf->path.base, pathconf->path.len); |
1021 | 1 | self->real_path = h2o_strdup_slashed(NULL, real_path, SIZE_MAX); |
1022 | 1 | if (mimemap != NULL) { |
1023 | 0 | h2o_mem_addref_shared(mimemap); |
1024 | 0 | self->mimemap = mimemap; |
1025 | 1 | } else { |
1026 | 1 | self->mimemap = h2o_mimemap_create(); |
1027 | 1 | } |
1028 | 1 | self->flags = flags; |
1029 | 4 | for (i = 0; index_files[i] != NULL; ++i) { |
1030 | 3 | self->index_files[i] = h2o_strdup(NULL, index_files[i], SIZE_MAX); |
1031 | 3 | if (self->max_index_file_len < self->index_files[i].len) |
1032 | 1 | self->max_index_file_len = self->index_files[i].len; |
1033 | 3 | } |
1034 | | |
1035 | 1 | return self; |
1036 | 1 | } |
1037 | | |
1038 | | h2o_mimemap_t *h2o_file_get_mimemap(h2o_file_handler_t *handler) |
1039 | 0 | { |
1040 | 0 | return handler->mimemap; |
1041 | 0 | } |
1042 | | |
1043 | | static void specific_handler_on_context_init(h2o_handler_t *_self, h2o_context_t *ctx) |
1044 | 0 | { |
1045 | 0 | struct st_h2o_specific_file_handler_t *self = (void *)_self; |
1046 | |
|
1047 | 0 | if (self->mime_type->type == H2O_MIMEMAP_TYPE_DYNAMIC) |
1048 | 0 | h2o_context_init_pathconf_context(ctx, &self->mime_type->data.dynamic.pathconf); |
1049 | 0 | } |
1050 | | |
1051 | | static void specific_handler_on_context_dispose(h2o_handler_t *_self, h2o_context_t *ctx) |
1052 | 0 | { |
1053 | 0 | struct st_h2o_specific_file_handler_t *self = (void *)_self; |
1054 | |
|
1055 | 0 | if (self->mime_type->type == H2O_MIMEMAP_TYPE_DYNAMIC) |
1056 | 0 | h2o_context_dispose_pathconf_context(ctx, &self->mime_type->data.dynamic.pathconf); |
1057 | 0 | } |
1058 | | |
1059 | | static void specific_handler_on_dispose(h2o_handler_t *_self) |
1060 | 0 | { |
1061 | 0 | struct st_h2o_specific_file_handler_t *self = (void *)_self; |
1062 | |
|
1063 | 0 | free(self->real_path.base); |
1064 | 0 | h2o_mem_release_shared(self->mime_type); |
1065 | 0 | } |
1066 | | |
1067 | | static int specific_handler_on_req(h2o_handler_t *_self, h2o_req_t *req) |
1068 | 0 | { |
1069 | 0 | struct st_h2o_specific_file_handler_t *self = (void *)_self; |
1070 | 0 | struct st_h2o_sendfile_generator_t *generator; |
1071 | 0 | int is_dir; |
1072 | | |
1073 | | /* open file (or send error or return -1) */ |
1074 | 0 | if ((generator = create_generator(req, self->real_path.base, self->real_path.len, &is_dir, self->flags)) == NULL) { |
1075 | 0 | if (is_dir) { |
1076 | 0 | h2o_send_error_403(req, "Access Forbidden", "access forbidden", 0); |
1077 | 0 | } else if (errno == ENOENT) { |
1078 | 0 | return -1; |
1079 | 0 | } else if (errno == ENFILE || errno == EMFILE) { |
1080 | 0 | h2o_send_error_503(req, "Service Unavailable", "please try again later", 0); |
1081 | 0 | } else { |
1082 | 0 | h2o_send_error_403(req, "Access Forbidden", "access forbidden", 0); |
1083 | 0 | } |
1084 | 0 | return 0; |
1085 | 0 | } |
1086 | | |
1087 | 0 | return serve_with_generator(generator, req, req->path_normalized, self->real_path.base, self->real_path.len, self->mime_type); |
1088 | 0 | } |
1089 | | |
1090 | | h2o_handler_t *h2o_file_register_file(h2o_pathconf_t *pathconf, const char *real_path, h2o_mimemap_type_t *mime_type, int flags) |
1091 | 0 | { |
1092 | 0 | struct st_h2o_specific_file_handler_t *self = (void *)h2o_create_handler(pathconf, sizeof(*self)); |
1093 | |
|
1094 | 0 | self->super.on_context_init = specific_handler_on_context_init; |
1095 | 0 | self->super.on_context_dispose = specific_handler_on_context_dispose; |
1096 | 0 | self->super.dispose = specific_handler_on_dispose; |
1097 | 0 | self->super.on_req = specific_handler_on_req; |
1098 | |
|
1099 | 0 | self->real_path = h2o_strdup(NULL, real_path, SIZE_MAX); |
1100 | 0 | h2o_mem_addref_shared(mime_type); |
1101 | 0 | self->mime_type = mime_type; |
1102 | 0 | self->flags = flags; |
1103 | |
|
1104 | 0 | return &self->super; |
1105 | 0 | } |