Coverage Report

Created: 2026-09-04 06:56

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