/src/h2o/lib/handler/expires.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2015 DeNA Co., Ltd. |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | | * of this software and associated documentation files (the "Software"), to |
6 | | * deal in the Software without restriction, including without limitation the |
7 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
8 | | * sell copies of the Software, and to permit persons to whom the Software is |
9 | | * furnished to do so, subject to the following conditions: |
10 | | * |
11 | | * The above copyright notice and this permission notice shall be included in |
12 | | * all copies or substantial portions of the Software. |
13 | | * |
14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
19 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
20 | | * IN THE SOFTWARE. |
21 | | */ |
22 | | #include <assert.h> |
23 | | #include <inttypes.h> |
24 | | #include <stdio.h> |
25 | | #include <stdlib.h> |
26 | | #include "h2o.h" |
27 | | |
28 | | struct st_expires_t { |
29 | | h2o_filter_t super; |
30 | | int mode; |
31 | | h2o_iovec_t value; |
32 | | }; |
33 | | |
34 | | static void on_setup_ostream(h2o_filter_t *_self, h2o_req_t *req, h2o_ostream_t **slot) |
35 | 0 | { |
36 | 0 | struct st_expires_t *self = (void *)_self; |
37 | |
|
38 | 0 | switch (req->res.status) { |
39 | 0 | case 200: |
40 | 0 | case 201: |
41 | 0 | case 204: |
42 | 0 | case 206: |
43 | 0 | case 301: |
44 | 0 | case 302: |
45 | 0 | case 303: |
46 | 0 | case 304: |
47 | 0 | case 307: |
48 | 0 | switch (self->mode) { |
49 | 0 | case H2O_EXPIRES_MODE_ABSOLUTE: |
50 | 0 | h2o_set_header(&req->pool, &req->res.headers, H2O_TOKEN_EXPIRES, self->value.base, self->value.len, 0); |
51 | 0 | break; |
52 | 0 | case H2O_EXPIRES_MODE_MAX_AGE: |
53 | 0 | h2o_set_header_token(&req->pool, &req->res.headers, H2O_TOKEN_CACHE_CONTROL, self->value.base, self->value.len); |
54 | 0 | break; |
55 | 0 | default: |
56 | 0 | assert(0); |
57 | 0 | break; |
58 | 0 | } |
59 | 0 | break; |
60 | 0 | default: |
61 | 0 | break; |
62 | 0 | } |
63 | | |
64 | 0 | h2o_setup_next_ostream(req, slot); |
65 | 0 | } |
66 | | |
67 | | void h2o_expires_register(h2o_pathconf_t *pathconf, h2o_expires_args_t *args) |
68 | 0 | { |
69 | 0 | struct st_expires_t *self = (void *)h2o_create_filter(pathconf, sizeof(*self)); |
70 | 0 | self->super.on_setup_ostream = on_setup_ostream; |
71 | 0 | self->mode = args->mode; |
72 | 0 | switch (args->mode) { |
73 | 0 | case H2O_EXPIRES_MODE_ABSOLUTE: |
74 | 0 | self->value = h2o_strdup(NULL, args->data.absolute, SIZE_MAX); |
75 | 0 | break; |
76 | 0 | case H2O_EXPIRES_MODE_MAX_AGE: |
77 | 0 | self->value.base = h2o_mem_alloc(128); |
78 | 0 | self->value.len = sprintf(self->value.base, "max-age=%" PRIu64, args->data.max_age); |
79 | 0 | break; |
80 | 0 | default: |
81 | 0 | assert(0); |
82 | 0 | break; |
83 | 0 | } |
84 | 0 | } |