-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
709 lines (615 loc) · 25 KB
/
file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
* Copyright (c) 2014 DeNA Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include "h2o.h"
#define MAX_BUF_SIZE 65536
struct st_h2o_sendfile_generator_t {
h2o_generator_t super;
int fd;
h2o_req_t *req;
size_t bytesleft;
char last_modified_buf[H2O_TIMESTR_RFC1123_LEN + 1];
char etag_buf[sizeof("\"deadbeef-deadbeefdeadbeef\"")];
size_t etag_len;
char buf[1];
};
struct st_h2o_file_handler_t {
h2o_handler_t super;
h2o_buf_t virtual_path; /* has "/" appended at last */
h2o_buf_t real_path; /* has "/" appended at last */
h2o_mimemap_t *mimemap;
size_t max_index_file_len;
h2o_buf_t index_files[1];
};
struct st_h2o_file_config_vars_t {
const char **index_files;
h2o_mimemap_t *mimemap;
};
struct st_h2o_file_configurator_t {
h2o_configurator_t super;
struct st_h2o_file_config_vars_t *vars;
struct st_h2o_file_config_vars_t _vars_stack[H2O_CONFIGURATOR_NUM_LEVELS + 1];
};
static const char *default_index_files[] = {
"index.html",
"index.htm",
"index.txt",
NULL
};
static void do_close(h2o_generator_t *_self, h2o_req_t *req)
{
struct st_h2o_sendfile_generator_t *self = (void*)_self;
close(self->fd);
}
static void do_proceed(h2o_generator_t *_self, h2o_req_t *req)
{
struct st_h2o_sendfile_generator_t *self = (void*)_self;
size_t rlen;
ssize_t rret;
h2o_buf_t vec;
int is_final;
/* read the file */
rlen = self->bytesleft;
if (rlen > MAX_BUF_SIZE)
rlen = MAX_BUF_SIZE;
while ((rret = read(self->fd, self->buf, rlen)) == -1 && errno == EINTR)
;
if (rret == -1) {
is_final = 1;
req->http1_is_persistent = 0; /* FIXME need a better interface to dispose an errored response w. content-length */
h2o_send(req, NULL, 0, 1);
do_close(&self->super, req);
return;
}
self->bytesleft -= rret;
is_final = self->bytesleft == 0;
/* send (and close if done) */
vec.base = self->buf;
vec.len = rret;
h2o_send(req, &vec, 1, is_final);
if (is_final)
do_close(&self->super, req);
}
static struct st_h2o_sendfile_generator_t *create_generator(h2o_mempool_t *pool, const char *path, int *is_dir)
{
struct st_h2o_sendfile_generator_t *self;
int fd;
struct stat st;
size_t bufsz;
*is_dir = 0;
if ((fd = open(path, O_RDONLY)) == -1)
return NULL;
if (fstat(fd, &st) != 0) {
perror("fstat");
close(fd);
return NULL;
}
if (S_ISDIR(st.st_mode)) {
close(fd);
*is_dir = 1;
return NULL;
}
bufsz = MAX_BUF_SIZE;
if (st.st_size < bufsz)
bufsz = st.st_size;
self = h2o_mempool_alloc(pool, offsetof(struct st_h2o_sendfile_generator_t, buf) + bufsz);
self->super.proceed = do_proceed;
self->super.stop = do_close;
self->fd = fd;
self->req = NULL;
self->bytesleft = st.st_size;
h2o_time2str_rfc1123(self->last_modified_buf, st.st_mtime);
self->etag_len = sprintf(self->etag_buf, "\"%08x-%zx\"", (unsigned)st.st_mtime, (size_t)st.st_size);
return self;
}
static void do_send_file(struct st_h2o_sendfile_generator_t *self, h2o_req_t *req, int status, const char *reason, h2o_buf_t mime_type)
{
/* link the request */
self->req = req;
/* setup response */
req->res.status = status;
req->res.reason = reason;
req->res.content_length = self->bytesleft;
h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, mime_type.base, mime_type.len);
h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_LAST_MODIFIED, self->last_modified_buf, H2O_TIMESTR_RFC1123_LEN);
h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_ETAG, self->etag_buf, self->etag_len);
/* send data */
h2o_start_response(req, &self->super);
do_proceed(&self->super, req);
}
int h2o_file_send(h2o_req_t *req, int status, const char *reason, const char *path, h2o_buf_t mime_type)
{
struct st_h2o_sendfile_generator_t *self;
int is_dir;
if ((self = create_generator(&req->pool, path, &is_dir)) == NULL)
return -1;
/* note: is_dir is not handled */
do_send_file(self, req, status, reason, mime_type);
return 0;
}
static int redirect_to_dir(h2o_req_t *req, const char *path, size_t path_len)
{
static h2o_generator_t generator = { NULL, NULL };
static const h2o_buf_t body_prefix = {
H2O_STRLIT("<!DOCTYPE html><TITLE>301 Moved Permanently</TITLE><P>The document has moved <A HREF=\"")
};
static const h2o_buf_t body_suffix = {
H2O_STRLIT("\">here</A>")
};
h2o_buf_t url;
size_t alloc_size;
h2o_buf_t bufs[3];
/* determine the size of the memory needed */
alloc_size = sizeof(":///") + req->scheme.len + req->authority.len + path_len;
/* allocate and build url */
url.base = h2o_mempool_alloc(&req->pool, alloc_size);
url.len = sprintf(url.base, "%.*s://%.*s%.*s/", (int)req->scheme.len, req->scheme.base, (int)req->authority.len, req->authority.base, (int)path_len, path);
assert(url.len + 1 == alloc_size);
/* build response header */
req->res.status = 301;
req->res.reason = "Moved Permanently";
memset(&req->res.headers, 0, sizeof(req->res.headers));
h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_LOCATION, url.base, url.len);
h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, H2O_STRLIT("text/html; charset=utf-8"));
/* build response */
bufs[0] = body_prefix;
bufs[1] = h2o_htmlescape(&req->pool, url.base, url.len);
bufs[2] = body_suffix;
/* send */
h2o_start_response(req, &generator);
h2o_send(req, bufs, 3, 1);
return 0;
}
static int on_req(h2o_handler_t *_self, h2o_req_t *req)
{
h2o_file_handler_t *self = (void*)_self;
h2o_buf_t vpath, mime_type;
char *rpath;
size_t rpath_len;
struct st_h2o_sendfile_generator_t *generator = NULL;
size_t if_modified_since_header_index, if_none_match_header_index;
int is_dir;
/* only accept GET (TODO accept HEAD as well) */
if (! h2o_memis(req->method.base, req->method.len, H2O_STRLIT("GET")))
return -1;
/* prefix match */
if (req->path.len < self->virtual_path.len
|| memcmp(req->path.base, self->virtual_path.base, self->virtual_path.len) != 0)
return -1;
/* normalize path */
vpath = h2o_normalize_path(&req->pool, req->path.base + self->virtual_path.len - 1, req->path.len - self->virtual_path.len + 1);
if (vpath.len > PATH_MAX)
return -1;
/* build path (still unterminated at the end of the block) */
rpath = alloca(
self->real_path.len
+ (vpath.len - 1) /* exclude "/" at the head */
+ self->max_index_file_len
+ 1);
rpath_len = 0;
memcpy(rpath + rpath_len, self->real_path.base, self->real_path.len);
rpath_len += self->real_path.len;
memcpy(rpath + rpath_len, vpath.base + 1, vpath.len - 1);
rpath_len += vpath.len - 1;
/* build generator (as well as terminating the rpath and its length upon success) */
if (rpath[rpath_len - 1] == '/') {
h2o_buf_t *index_file;
for (index_file = self->index_files; index_file->base != NULL; ++index_file) {
memcpy(rpath + rpath_len, index_file->base, index_file->len);
rpath[rpath_len + index_file->len] = '\0';
if ((generator = create_generator(&req->pool, rpath, &is_dir)) != NULL) {
rpath_len += index_file->len;
break;
}
if (is_dir) {
/* note: apache redirects "path/" to "path/index.txt/" if index.txt is a dir */
char *path = alloca(req->path.len + index_file->len + 1);
size_t path_len = sprintf(path, "%.*s%.*s", (int)req->path.len, req->path.base, (int)index_file->len, index_file->base);
return redirect_to_dir(req, path, path_len);
}
if (errno != ENOENT)
break;
}
} else {
rpath[rpath_len] = '\0';
generator = create_generator(&req->pool, rpath, &is_dir);
if (generator == NULL && is_dir)
return redirect_to_dir(req, req->path.base, req->path.len);
}
/* return error if failed */
if (generator == NULL) {
if (errno == ENOENT) {
h2o_send_error(req, 404, "File Not Found", "file not found");
} else {
h2o_send_error(req, 403, "Access Forbidden", "access forbidden");
}
return 0;
}
if ((if_none_match_header_index = h2o_find_header(&req->headers, H2O_TOKEN_IF_NONE_MATCH, SIZE_MAX)) != -1) {
h2o_buf_t *if_none_match = &req->headers.entries[if_none_match_header_index].value;
if (h2o_memis(if_none_match->base, if_none_match->len, generator->etag_buf, generator->etag_len))
goto NotModified;
} else if ((if_modified_since_header_index = h2o_find_header(&req->headers, H2O_TOKEN_IF_MODIFIED_SINCE, SIZE_MAX)) != -1) {
h2o_buf_t *if_modified_since = &req->headers.entries[if_modified_since_header_index].value;
if (h2o_memis(if_modified_since->base, if_modified_since->len, generator->last_modified_buf, H2O_TIMESTR_RFC1123_LEN))
goto NotModified;
}
/* obtain mime type */
mime_type = h2o_mimemap_get_type(self->mimemap, h2o_get_filext(rpath, rpath_len));
/* return file */
do_send_file(generator, req, 200, "OK", mime_type);
return 0;
NotModified:
req->res.status = 304;
req->res.reason = "Not Modified";
h2o_send_inline(req, NULL, 0);
do_close(&generator->super, req);
return 0;
}
static void on_dispose(h2o_handler_t *_self)
{
h2o_file_handler_t *self = (void*)_self;
size_t i;
free(self->virtual_path.base);
free(self->real_path.base);
h2o_mempool_release_shared(self->mimemap);
for (i = 0; self->index_files[i].base != NULL; ++i)
free(self->index_files[i].base);
}
static h2o_buf_t append_slash_and_dup(const char *path)
{
char *buf;
size_t path_len = strlen(path);
int needs_slash = 0;
if (path_len == 0 || path[path_len - 1] != '/')
needs_slash = 1;
buf = h2o_malloc(path_len + 1 + needs_slash);
memcpy(buf, path, path_len);
if (needs_slash)
buf[path_len++] = '/';
buf[path_len] = '\0';
return h2o_buf_init(buf, path_len);
}
h2o_file_handler_t *h2o_file_register(h2o_hostconf_t *host_config, const char *virtual_path, const char *real_path, const char **index_files, h2o_mimemap_t *mimemap)
{
h2o_file_handler_t *self;
size_t i;
if (index_files == NULL)
index_files = default_index_files;
/* allocate memory */
for (i = 0; index_files[i] != NULL; ++i)
;
self = (void*)h2o_create_handler(host_config, offsetof(h2o_file_handler_t, index_files[0]) + sizeof(self->index_files[0]) * (i + 1));
/* setup callbacks */
self->super.dispose = on_dispose;
self->super.on_req = on_req;
/* setup attributes */
self->virtual_path = append_slash_and_dup(virtual_path);
self->real_path = append_slash_and_dup(real_path);
if (mimemap != NULL) {
h2o_mempool_addref_shared(mimemap);
self->mimemap = mimemap;
} else {
self->mimemap = h2o_mimemap_create();
}
for (i = 0; index_files[i] != NULL; ++i) {
self->index_files[i] = h2o_strdup(NULL, index_files[i], SIZE_MAX);
if (self->max_index_file_len < self->index_files[i].len)
self->max_index_file_len = self->index_files[i].len;
}
return self;
}
h2o_mimemap_t *h2o_file_get_mimemap(h2o_file_handler_t *handler)
{
return handler->mimemap;
}
static int on_config_dir(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, const char *file, yoml_t *node)
{
struct st_h2o_file_configurator_t *self = (void*)cmd->configurator;
h2o_file_register(ctx->hostconf, ctx->path->base, node->data.scalar, self->vars->index_files, self->vars->mimemap);
return 0;
}
static int on_config_index(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, const char *file, yoml_t *node)
{
struct st_h2o_file_configurator_t *self = (void*)cmd->configurator;
size_t i;
free(self->vars->index_files);
self->vars->index_files = h2o_malloc(sizeof(self->vars->index_files[0]) * (node->data.sequence.size + 1));
for (i = 0; i != node->data.sequence.size; ++i) {
yoml_t *element = node->data.sequence.elements[i];
if (element->type != YOML_TYPE_SCALAR) {
h2o_config_print_error(cmd, file, element, "argument must be a sequence of scalars");
return -1;
}
self->vars->index_files[i] = element->data.scalar;
}
self->vars->index_files[i] = NULL;
return 0;
}
static int assert_is_mimetype(h2o_configurator_command_t *cmd, const char *file, yoml_t *node)
{
if (node->type != YOML_TYPE_SCALAR) {
h2o_config_print_error(cmd, file, node, "expected a scalar (mime-type)");
return -1;
}
if (strchr(node->data.scalar, '/') == NULL) {
h2o_config_print_error(cmd, file, node, "the string \"%s\" does not look like a mime-type", node->data.scalar);
return -1;
}
return 0;
}
static int assert_is_extension(h2o_configurator_command_t *cmd, const char *file, yoml_t *node)
{
if (node->type != YOML_TYPE_SCALAR) {
h2o_config_print_error(cmd, file, node, "expected a scalar (extension)");
return -1;
}
if (node->data.scalar[0] != '.') {
h2o_config_print_error(cmd, file, node, "given extension \"%s\" does not start with a \".\"", node->data.scalar);
return -1;
}
return 0;
}
static int set_mimetypes(h2o_configurator_command_t *cmd, h2o_mimemap_t *mimemap, const char *file, yoml_t *node)
{
size_t i, j;
assert(node->type == YOML_TYPE_MAPPING);
for (i = 0; i != node->data.mapping.size; ++i) {
yoml_t *key = node->data.mapping.elements[i].key;
yoml_t *value = node->data.mapping.elements[i].value;
if (assert_is_mimetype(cmd, file, key) != 0)
return -1;
switch (value->type) {
case YOML_TYPE_SCALAR:
if (assert_is_extension(cmd, file, value) != 0)
return -1;
h2o_mimemap_set_type(mimemap, value->data.scalar + 1, key->data.scalar);
break;
case YOML_TYPE_SEQUENCE:
for (j = 0; j != value->data.sequence.size; ++j) {
yoml_t *ext_node = value->data.sequence.elements[j];
if (assert_is_extension(cmd, file, ext_node) != 0)
return -1;
h2o_mimemap_set_type(mimemap, ext_node->data.scalar + 1, key->data.scalar);
}
break;
default:
h2o_config_print_error(cmd, file, value, "only scalar or sequence of scalar is permitted at the value part of the argument");
return -1;
}
}
return 0;
}
static int on_config_mime_settypes(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, const char *file, yoml_t *node)
{
struct st_h2o_file_configurator_t *self = (void*)cmd->configurator;
h2o_mimemap_t *newmap = h2o_mimemap_create();
h2o_mimemap_set_default_type(newmap, h2o_mimemap_get_default_type(self->vars->mimemap).base);
if (set_mimetypes(cmd, newmap, file, node) != 0) {
h2o_mempool_release_shared(newmap);
return -1;
}
h2o_mempool_release_shared(self->vars->mimemap);
self->vars->mimemap = newmap;
return 0;
}
static void clone_mimemap_if_clean(struct st_h2o_file_configurator_t *self)
{
if (self->vars->mimemap != self->vars[-1].mimemap)
return;
h2o_mempool_release_shared(self->vars->mimemap);
self->vars->mimemap = h2o_mimemap_clone(self->vars->mimemap);
}
static int on_config_mime_addtypes(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, const char *file, yoml_t *node)
{
struct st_h2o_file_configurator_t *self = (void*)cmd->configurator;
clone_mimemap_if_clean(self);
return set_mimetypes(cmd, self->vars->mimemap, file, node);
}
static int on_config_mime_removetypes(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, const char *file, yoml_t *node)
{
struct st_h2o_file_configurator_t *self = (void*)cmd->configurator;
size_t i;
clone_mimemap_if_clean(self);
for (i = 0; i != node->data.sequence.size; ++i) {
yoml_t *ext_node = node->data.sequence.elements[i];
if (assert_is_extension(cmd, file, ext_node) != 0)
return -1;
h2o_mimemap_remove_type(self->vars->mimemap, ext_node->data.scalar + 1);
}
return 0;
}
static int on_config_mime_setdefaulttype(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, const char *file, yoml_t *node)
{
struct st_h2o_file_configurator_t *self = (void*)cmd->configurator;
if (assert_is_mimetype(cmd, file, node) != 0)
return -1;
clone_mimemap_if_clean(self);
h2o_mimemap_set_default_type(self->vars->mimemap, node->data.scalar);
return 0;
}
static const char **dup_strlist(const char **s)
{
size_t i;
const char **ret;
for (i = 0; s[i] != NULL; ++i)
;
ret = h2o_malloc(sizeof(*ret) * (i + 1));
for (i = 0; s[i] != NULL; ++i)
ret[i] = s[i];
ret[i] = NULL;
return ret;
}
static int on_config_enter(h2o_configurator_t *_self, h2o_configurator_context_t *ctx)
{
struct st_h2o_file_configurator_t *self = (void*)_self;
++self->vars;
self->vars[0].index_files = dup_strlist(self->vars[-1].index_files);
self->vars[0].mimemap = self->vars[-1].mimemap;
h2o_mempool_addref_shared(self->vars[0].mimemap);
return 0;
}
static int on_config_exit(h2o_configurator_t *_self, h2o_configurator_context_t *ctx)
{
struct st_h2o_file_configurator_t *self = (void*)_self;
free(self->vars->index_files);
h2o_mempool_release_shared(self->vars->mimemap);
--self->vars;
return 0;
}
void h2o_file_register_configurator(h2o_globalconf_t *globalconf)
{
struct st_h2o_file_configurator_t *self = (void*)h2o_config_create_configurator(globalconf, sizeof(*self));
self->super.enter = on_config_enter;
self->super.exit = on_config_exit;
self->vars = self->_vars_stack;
self->vars->mimemap = h2o_mimemap_create();
self->vars->index_files = default_index_files;
h2o_config_define_command(
&self->super, "file.dir",
H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR | H2O_CONFIGURATOR_FLAG_DEFERRED,
on_config_dir,
"directory under which to serve the target path");
h2o_config_define_command(
&self->super, "file.index",
H2O_CONFIGURATOR_FLAG_GLOBAL | H2O_CONFIGURATOR_FLAG_HOST | H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_SEQUENCE,
on_config_index,
"sequence of index file names (default: index.html index.htm index.txt)");
h2o_config_define_command(
&self->super, "file.mime.settypes",
H2O_CONFIGURATOR_FLAG_GLOBAL | H2O_CONFIGURATOR_FLAG_HOST | H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_MAPPING,
on_config_mime_settypes,
"map of mime-type -> (extension | sequence-of-extensions)");
h2o_config_define_command(
&self->super, "file.mime.addtypes",
H2O_CONFIGURATOR_FLAG_GLOBAL | H2O_CONFIGURATOR_FLAG_HOST | H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_MAPPING,
on_config_mime_addtypes,
"map of mime-type -> (extension | sequence-of-extensions)");
h2o_config_define_command(
&self->super, "file.mime.removetypes",
H2O_CONFIGURATOR_FLAG_GLOBAL | H2O_CONFIGURATOR_FLAG_HOST | H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_SEQUENCE,
on_config_mime_removetypes,
"sequence of extensions");
h2o_config_define_command(
&self->super, "file.mime.setdefaulttype",
H2O_CONFIGURATOR_FLAG_GLOBAL | H2O_CONFIGURATOR_FLAG_HOST | H2O_CONFIGURATOR_FLAG_PATH | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR,
on_config_mime_setdefaulttype,
"default mime-type");
}
#ifdef H2O_UNITTEST
#include "t/test.h"
static int check_header(h2o_res_t *res, const h2o_token_t *header_name, const char *expected)
{
size_t index = h2o_find_header(&res->headers, header_name, SIZE_MAX);
if (index == SIZE_MAX)
return 0;
return h2o_lcstris(res->headers.entries[index].value.base, res->headers.entries[index].value.len, expected, strlen(expected));
}
void test_lib__file_c()
{
h2o_globalconf_t globalconf;
h2o_hostconf_t *hostconf;
h2o_context_t ctx;
h2o_config_init(&globalconf);
hostconf = h2o_config_register_host(&globalconf, "default");
h2o_file_register(hostconf, "/", "t/00unit/file", NULL, NULL);
h2o_context_init(&ctx, test_loop, &globalconf);
{
h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
conn->req.path = h2o_buf_init(H2O_STRLIT("/"));
h2o_loopback_run_loop(conn);
ok(conn->req.res.status == 200);
ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/html"));
ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("hello html\n")));
h2o_loopback_destroy(conn);
}
{
h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
conn->req.path = h2o_buf_init(H2O_STRLIT("/index.html"));
h2o_loopback_run_loop(conn);
ok(conn->req.res.status == 200);
ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/html"));
ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("hello html\n")));
h2o_loopback_destroy(conn);
}
{
h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
conn->req.path = h2o_buf_init(H2O_STRLIT("/1000.txt"));
h2o_loopback_run_loop(conn);
ok(conn->req.res.status == 200);
ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain"));
ok(conn->body->size == 1000);
ok(strcmp(sha1sum(conn->body->bytes, conn->body->size), "dfd3ae1f5c475555fad62efe42e07309fa45f2ed") == 0);
h2o_loopback_destroy(conn);
}
{
h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
conn->req.path = h2o_buf_init(H2O_STRLIT("/1000000.txt"));
h2o_loopback_run_loop(conn);
ok(conn->req.res.status == 200);
ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain"));
ok(conn->body->size == 1000000);
ok(strcmp(sha1sum(conn->body->bytes, conn->body->size), "00c8ab71d0914dce6a1ec2eaa0fda0df7044b2a2") == 0);
h2o_loopback_destroy(conn);
}
{
h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
conn->req.path = h2o_buf_init(H2O_STRLIT("/index_txt/"));
h2o_loopback_run_loop(conn);
ok(conn->req.res.status == 200);
ok(check_header(&conn->req.res, H2O_TOKEN_CONTENT_TYPE, "text/plain"));
ok(h2o_memis(conn->body->bytes, conn->body->size, H2O_STRLIT("hello text\n")));
h2o_loopback_destroy(conn);
}
{
h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
conn->req.path = h2o_buf_init(H2O_STRLIT("/index_txt"));
h2o_loopback_run_loop(conn);
ok(conn->req.res.status == 301);
ok(check_header(&conn->req.res, H2O_TOKEN_LOCATION, "http://default/index_txt/"));
h2o_loopback_destroy(conn);
}
{
h2o_loopback_conn_t *conn = h2o_loopback_create(&ctx);
conn->req.method = h2o_buf_init(H2O_STRLIT("GET"));
conn->req.path = h2o_buf_init(H2O_STRLIT("/index_txt_as_dir/"));
h2o_loopback_run_loop(conn);
ok(conn->req.res.status == 301);
ok(check_header(&conn->req.res, H2O_TOKEN_LOCATION, "http://default/index_txt_as_dir/index.txt/"));
h2o_loopback_destroy(conn);
}
h2o_context_dispose(&ctx);
h2o_config_dispose(&globalconf);
}
#endif