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