summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/lib/handler/configurator/errordoc.c
blob: ab24923ee2c4a58858063fab44f043114db2f7be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Copyright (c) 2015-2016 DeNA Co., Ltd., Kazuho Oku
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */
#include "h2o.h"
#include "h2o/configurator.h"

struct errordoc_configurator_t {
    h2o_configurator_t super;
    h2o_mem_pool_t pool;
    H2O_VECTOR(h2o_errordoc_t) * vars, _vars_stack[H2O_CONFIGURATOR_NUM_LEVELS + 1];
};

static int scan_and_check_status(h2o_configurator_command_t *cmd, yoml_t *value, int *slot)
{
    if (value->type != YOML_TYPE_SCALAR) {
        h2o_configurator_errprintf(cmd, value, "status must be must be either of: scalar, sequence of scalar");
        return -1;
    }
    if (h2o_configurator_scanf(cmd, value, "%d", slot) != 0)
        return -1;
    if (!(400 <= *slot && *slot <= 599)) {
        h2o_configurator_errprintf(cmd, value, "status must be within range of 400 to 599");
        return -1;
    }
    return 0;
}

static int register_errordoc(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *hash)
{
    struct errordoc_configurator_t *self = (void *)cmd->configurator;
    int status[200];
    size_t status_len = 0;
    int parsed;
    const char *url = NULL;
    size_t i, j, k;
    yoml_t *key, *value;

    for (i = 0; i != hash->data.mapping.size; ++i) {
        key = hash->data.mapping.elements[i].key;
        value = hash->data.mapping.elements[i].value;
        if (key->type != YOML_TYPE_SCALAR)
            goto UnknownKeyError;
        if (strcmp(key->data.scalar, "status") == 0) {
            if (status_len != 0)
                goto KeyAlreadyDefinedError;

            if (value->type == YOML_TYPE_SEQUENCE) {
                if (value->data.sequence.size == 0) {
                    h2o_configurator_errprintf(cmd, value, "status sequence must not be empty");
                    return -1;
                }
                for (j = 0; j != value->data.sequence.size; ++j) {
                    if (scan_and_check_status(cmd, value->data.sequence.elements[j], &parsed) != 0)
                        return -1;
                    /* check the scanned status hasn't already appeared */
                    for (k = 0; k != status_len; ++k) {
                        if (status[k] == parsed) {
                            h2o_configurator_errprintf(cmd, value, "status %d appears multiple times", status[k]);
                            return -1;
                        }
                    }
                    status[status_len++] = parsed;
                }
            } else {
                if (scan_and_check_status(cmd, value, &parsed) != 0)
                    return -1;
                status[status_len++] = parsed;
            }

        } else if (strcmp(key->data.scalar, "url") == 0) {
            if (url != NULL)
                goto KeyAlreadyDefinedError;
            if (value->type != YOML_TYPE_SCALAR) {
                h2o_configurator_errprintf(cmd, value, "URL must be a scalar");
                return -1;
            }
            url = value->data.scalar;
        } else {
            goto UnknownKeyError;
        }
    }

    if (status_len == 0) {
        h2o_configurator_errprintf(cmd, hash, "mandatory key `status` is not defined");
        return -1;
    }
    if (url == NULL) {
        h2o_configurator_errprintf(cmd, hash, "mandatory key `url` is not defined");
        return -1;
    }

    h2o_iovec_t _url = h2o_strdup(&self->pool, url, SIZE_MAX);
    for (i = 0; i != status_len; ++i){
        /* register */
        h2o_vector_reserve(&self->pool, self->vars, self->vars->size + 1);
        h2o_errordoc_t *errordoc = self->vars->entries + self->vars->size++;
        errordoc->status = status[i];
        errordoc->url = _url;
    }

    return 0;

UnknownKeyError:
    h2o_configurator_errprintf(cmd, key, "key must be either of: `status`, `url`");
    return -1;
KeyAlreadyDefinedError:
    h2o_configurator_errprintf(cmd, key, "the key cannot be defined more than once");
    return -1;
}

static int on_config_errordoc(h2o_configurator_command_t *cmd, h2o_configurator_context_t *ctx, yoml_t *node)
{
    switch (node->type) {
    case YOML_TYPE_SEQUENCE: {
        size_t i;
        for (i = 0; i != node->data.sequence.size; ++i) {
            yoml_t *e = node->data.sequence.elements[i];
            if (e->type != YOML_TYPE_MAPPING) {
                h2o_configurator_errprintf(cmd, e, "element must be a mapping");
                return -1;
            }
            if (register_errordoc(cmd, ctx, e) != 0)
                return -1;
        }
        return 0;
    }
    case YOML_TYPE_MAPPING:
        return register_errordoc(cmd, ctx, node);
    default:
        break;
    }

    h2o_configurator_errprintf(cmd, node, "argument must be either of: sequence, mapping");
    return -1;
}

static int on_config_enter(h2o_configurator_t *_self, h2o_configurator_context_t *ctx, yoml_t *node)
{
    struct errordoc_configurator_t *self = (void *)_self;

    if (self->vars == self->_vars_stack) {
        /* entering global level */
        h2o_mem_init_pool(&self->pool);
    }

    /* copy vars */
    memset(&self->vars[1], 0, sizeof(self->vars[1]));
    h2o_vector_reserve(&self->pool, &self->vars[1], self->vars[0].size);
    h2o_memcpy(self->vars[1].entries, self->vars[0].entries, sizeof(self->vars[0].entries[0]) * self->vars[0].size);
    self->vars[1].size = self->vars[0].size;

    ++self->vars;
    return 0;
}

static int on_config_exit(h2o_configurator_t *_self, h2o_configurator_context_t *ctx, yoml_t *node)
{
    struct errordoc_configurator_t *self = (void *)_self;

    if (ctx->pathconf != NULL && self->vars->size != 0)
        h2o_errordoc_register(ctx->pathconf, self->vars->entries, self->vars->size);

    --self->vars;
    if (self->vars == self->_vars_stack) {
        /* exitting global level */
        h2o_mem_clear_pool(&self->pool);
    }

    return 0;
}

void h2o_errordoc_register_configurator(h2o_globalconf_t *conf)
{
    struct errordoc_configurator_t *c = (void *)h2o_configurator_create(conf, sizeof(*c));

    /* set default vars */
    c->vars = c->_vars_stack;

    /* setup handlers */
    c->super.enter = on_config_enter;
    c->super.exit = on_config_exit;

    /* reproxy: ON | OFF */
    h2o_configurator_define_command(&c->super, "error-doc", H2O_CONFIGURATOR_FLAG_ALL_LEVELS, on_config_errordoc);
}