1 /*
2  * Copyright (c) 2014 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 <inttypes.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include "h2o.h"
26 #include "h2o/configurator.h"
27 
28 struct expires_configurator_t {
29     h2o_configurator_t super;
30     h2o_expires_args_t **args;
31     h2o_expires_args_t *_args_stack[H2O_CONFIGURATOR_NUM_LEVELS + 1];
32 };
33 
on_config_expires(h2o_configurator_command_t * cmd,h2o_configurator_context_t * ctx,yoml_t * node)34 static int on_config_expires(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
35 {
36     struct expires_configurator_t *self = (void *)cmd->configurator;
37     uint64_t value;
38     char unit[32];
39 
40     if (strcasecmp(node->data.scalar, "OFF") == 0) {
41         free(*self->args);
42         *self->args = NULL;
43     } else if (sscanf(node->data.scalar, "%" SCNu64 " %31s", &value, unit) == 2) {
44         /* convert value to seconds depending on the unit */
45         if (strncasecmp(unit, H2O_STRLIT("second")) == 0) {
46             /* ok */
47         } else if (strncasecmp(unit, H2O_STRLIT("minute")) == 0) {
48             value *= 60;
49         } else if (strncasecmp(unit, H2O_STRLIT("hour")) == 0) {
50             value *= 60 * 60;
51         } else if (strncasecmp(unit, H2O_STRLIT("day")) == 0) {
52             value *= 24 * 60 * 60;
53         } else if (strncasecmp(unit, H2O_STRLIT("month")) == 0) {
54             value *= 30 * 24 * 60 * 60;
55         } else if (strncasecmp(unit, H2O_STRLIT("year")) == 0) {
56             value *= 365 * 30 * 24 * 60 * 60;
57         } else {
58             /* TODO add support for H2O_EXPIRES_MODE_MAX_ABSOLUTE that sets the Expires header? */
59             h2o_configurator_errprintf(cmd, node, "unknown unit:`%s` (see --help)", unit);
60             return -1;
61         }
62         /* save the value */
63         if (*self->args == NULL)
64             *self->args = h2o_mem_alloc(sizeof(**self->args));
65         (*self->args)->mode = H2O_EXPIRES_MODE_MAX_AGE;
66         (*self->args)->data.max_age = value;
67     } else {
68         h2o_configurator_errprintf(cmd, node,
69                                    "failed to parse the value, should be in form of: `<number> <unit>` or `OFF` (see --help)");
70         return -1;
71     }
72 
73     return 0;
74 }
75 
on_config_enter(h2o_configurator_t * _self,h2o_configurator_context_t * ctx,yoml_t * node)76 static int on_config_enter(h2o_configurator_t *_self, h2o_configurator_context_t *ctx, yoml_t *node)
77 {
78     struct expires_configurator_t *self = (void *)_self;
79 
80     if (self->args[0] != NULL) {
81         /* duplicate */
82         assert(self->args[0]->mode == H2O_EXPIRES_MODE_MAX_AGE);
83         self->args[1] = h2o_mem_alloc(sizeof(**self->args));
84         *self->args[1] = *self->args[0];
85     } else {
86         self->args[1] = NULL;
87     }
88     ++self->args;
89     return 0;
90 }
91 
on_config_exit(h2o_configurator_t * _self,h2o_configurator_context_t * ctx,yoml_t * node)92 static int on_config_exit(h2o_configurator_t *_self, h2o_configurator_context_t *ctx, yoml_t *node)
93 {
94     struct expires_configurator_t *self = (void *)_self;
95 
96     if (*self->args != NULL) {
97         /* setup */
98         if (ctx->pathconf != NULL) {
99             h2o_expires_register(ctx->pathconf, *self->args);
100         }
101         /* destruct */
102         assert((*self->args)->mode == H2O_EXPIRES_MODE_MAX_AGE);
103         free(*self->args);
104         *self->args = NULL;
105     }
106 
107     --self->args;
108     return 0;
109 }
110 
h2o_expires_register_configurator(h2o_globalconf_t * conf)111 void h2o_expires_register_configurator(h2o_globalconf_t *conf)
112 {
113     struct expires_configurator_t *c = (void *)h2o_configurator_create(conf, sizeof(*c));
114 
115     /* set default vars */
116     c->args = c->_args_stack;
117 
118     /* setup handlers */
119     c->super.enter = on_config_enter;
120     c->super.exit = on_config_exit;
121     h2o_configurator_define_command(&c->super, "expires", H2O_CONFIGURATOR_FLAG_ALL_LEVELS | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR,
122                                     on_config_expires);
123 }
124