1 /*
2  * Copyright (c) 2015 DeNA Co., Ltd., Kazuho Oku
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 "h2o.h"
23 #include "h2o/configurator.h"
24 
25 #define DEFAULT_GZIP_QUALITY 1
26 #define DEFAULT_BROTLI_QUALITY 1
27 
28 struct compress_configurator_t {
29     h2o_configurator_t super;
30     h2o_compress_args_t *vars, _vars_stack[H2O_CONFIGURATOR_NUM_LEVELS + 1];
31 };
32 
33 static const h2o_compress_args_t all_off = {0, {-1}, {-1}}, all_on = {100, {DEFAULT_GZIP_QUALITY}, {DEFAULT_BROTLI_QUALITY}};
34 
on_config_gzip(h2o_configurator_command_t * cmd,h2o_configurator_context_t * ctx,yoml_t * node)35 static int on_config_gzip(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
36 {
37     struct compress_configurator_t *self = (void *)cmd->configurator;
38     int mode;
39 
40     if ((mode = (int)h2o_configurator_get_one_of(cmd, node, "OFF,ON")) == -1)
41         return -1;
42 
43     *self->vars = all_off;
44     if (mode != 0)
45         self->vars->gzip.quality = DEFAULT_GZIP_QUALITY;
46 
47     return 0;
48 }
49 
obtain_quality(yoml_t * node,int min_quality,int max_quality,int default_quality,int * slot)50 static int obtain_quality(yoml_t *node, int min_quality, int max_quality, int default_quality, int *slot)
51 {
52     int tmp;
53     if (node->type != YOML_TYPE_SCALAR)
54         return -1;
55     if (strcasecmp(node->data.scalar, "OFF") == 0) {
56         *slot = -1;
57         return 0;
58     }
59     if (strcasecmp(node->data.scalar, "ON") == 0) {
60         *slot = default_quality;
61         return 0;
62     }
63     if (sscanf(node->data.scalar, "%d", &tmp) == 1 && (min_quality <= tmp && tmp <= max_quality)) {
64         *slot = tmp;
65         return 0;
66     }
67     return -1;
68 }
69 
on_config_compress_min_size(h2o_configurator_command_t * cmd,h2o_configurator_context_t * ctx,yoml_t * node)70 static int on_config_compress_min_size(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
71 {
72     struct compress_configurator_t *self = (void *)cmd->configurator;
73     return h2o_configurator_scanf(cmd, node, "%zu", &self->vars->min_size);
74 }
75 
on_config_compress(h2o_configurator_command_t * cmd,h2o_configurator_context_t * ctx,yoml_t * node)76 static int on_config_compress(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
77 {
78     struct compress_configurator_t *self = (void *)cmd->configurator;
79     size_t i;
80 
81     switch (node->type) {
82     case YOML_TYPE_SCALAR:
83         if (strcasecmp(node->data.scalar, "OFF") == 0) {
84             *self->vars = all_off;
85         } else if (strcasecmp(node->data.scalar, "ON") == 0) {
86             *self->vars = all_on;
87         } else {
88             h2o_configurator_errprintf(cmd, node, "scalar argument must be either of: `OFF`, `ON`");
89             return -1;
90         }
91         break;
92     case YOML_TYPE_SEQUENCE:
93         *self->vars = all_off;
94         for (i = 0; i != node->data.sequence.size; ++i) {
95             yoml_t *element = node->data.sequence.elements[i];
96             if (element->type == YOML_TYPE_SCALAR && strcasecmp(element->data.scalar, "gzip") == 0) {
97                 self->vars->gzip.quality = DEFAULT_GZIP_QUALITY;
98             } else if (element->type == YOML_TYPE_SCALAR && strcasecmp(element->data.scalar, "br") == 0) {
99                 self->vars->brotli.quality = DEFAULT_BROTLI_QUALITY;
100             } else {
101                 h2o_configurator_errprintf(cmd, element, "element of the sequence must be either of: `gzip`, `br`");
102                 return -1;
103             }
104         }
105         break;
106     case YOML_TYPE_MAPPING: {
107         yoml_t **gzip_node, **br_node;
108         *self->vars = all_off;
109         if (h2o_configurator_parse_mapping(cmd, node, NULL, "gzip:*,br:*", &gzip_node, &br_node) != 0)
110             return -1;
111         if (gzip_node != NULL && obtain_quality(*gzip_node, 1, 9, DEFAULT_GZIP_QUALITY, &self->vars->gzip.quality) != 0) {
112             h2o_configurator_errprintf(cmd, *gzip_node,
113                                        "value of gzip attribute must be either of `OFF`, `ON` or an integer value between 1 and 9");
114             return -1;
115         }
116         if (br_node != NULL && obtain_quality(*br_node, 0, 11, DEFAULT_BROTLI_QUALITY, &self->vars->brotli.quality) != 0) {
117             h2o_configurator_errprintf(cmd, *br_node,
118                                        "value of br attribute must be either of `OFF`, `ON` or an integer between 0 and 11");
119             return -1;
120         }
121     } break;
122     default:
123         h2o_fatal("unexpected node type");
124         break;
125     }
126 
127     return 0;
128 }
129 
on_config_enter(h2o_configurator_t * configurator,h2o_configurator_context_t * ctx,yoml_t * node)130 static int on_config_enter(h2o_configurator_t *configurator, h2o_configurator_context_t *ctx, yoml_t *node)
131 {
132     struct compress_configurator_t *self = (void *)configurator;
133 
134     ++self->vars;
135     self->vars[0] = self->vars[-1];
136     return 0;
137 }
138 
on_config_exit(h2o_configurator_t * configurator,h2o_configurator_context_t * ctx,yoml_t * node)139 static int on_config_exit(h2o_configurator_t *configurator, h2o_configurator_context_t *ctx, yoml_t *node)
140 {
141     struct compress_configurator_t *self = (void *)configurator;
142 
143     if (ctx->pathconf != NULL && (self->vars->gzip.quality != -1 || self->vars->brotli.quality != -1))
144         h2o_compress_register(ctx->pathconf, self->vars);
145 
146     --self->vars;
147     return 0;
148 }
149 
h2o_compress_register_configurator(h2o_globalconf_t * conf)150 void h2o_compress_register_configurator(h2o_globalconf_t *conf)
151 {
152     struct compress_configurator_t *c = (void *)h2o_configurator_create(conf, sizeof(*c));
153 
154     c->super.enter = on_config_enter;
155     c->super.exit = on_config_exit;
156     h2o_configurator_define_command(&c->super, "compress", H2O_CONFIGURATOR_FLAG_ALL_LEVELS, on_config_compress);
157     h2o_configurator_define_command(&c->super, "compress-minimum-size",
158                                     H2O_CONFIGURATOR_FLAG_ALL_LEVELS | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR,
159                                     on_config_compress_min_size);
160     h2o_configurator_define_command(&c->super, "gzip", H2O_CONFIGURATOR_FLAG_ALL_LEVELS | H2O_CONFIGURATOR_FLAG_EXPECT_SCALAR,
161                                     on_config_gzip);
162     c->vars = c->_vars_stack;
163     c->vars->gzip.quality = -1;
164     c->vars->brotli.quality = -1;
165 }
166