varnish-cache/lib/libvcc/vcc_var.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2011 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 */
30
31
#include "config.h"
32
33
#include <stdlib.h>
34
#include <string.h>
35
36
#include "vcc_compile.h"
37
38
#include "vct.h"
39
40
/*--------------------------------------------------------------------*/
41
42
void
43 788575
vcc_Header_Fh(const struct vcc *tl, const struct symbol *sym)
44
{
45
        const struct symbol *parent;
46
47 788575
        AN(tl);
48 788575
        AN(sym);
49 788575
        AZ(sym->wildcard);
50 788575
        assert(sym->type == HEADER);
51
52 788575
        parent = sym->eval_priv;
53 788575
        AN(parent);
54 788575
        AN(parent->wildcard);
55 788575
        assert(parent->type == HEADER);
56
57
        /* Create the static identifier */
58 788575
        Fh(tl, 0, "static const struct gethdr_s %s =\n", sym->rname + 1);
59 1577150
        Fh(tl, 0, "    { %s, \"\\%03o%s:\"};\n",
60 788575
            parent->rname, (unsigned int)strlen(sym->name) + 1, sym->name);
61 788575
}
62
63
/*--------------------------------------------------------------------*/
64
65
void v_matchproto_(sym_wildcard_t)
66 1372175
vcc_Var_Wildcard(struct vcc *tl, struct symbol *parent, struct symbol *sym)
67
{
68
        struct vsb *vsb;
69
        const char *p;
70
71 1372175
        AN(sym);
72 1372175
        assert(parent->type == HEADER);
73
74 1372175
        if (strlen(sym->name) >= 127) {
75 50
                VSB_printf(tl->sb, "HTTP header (%.20s..) is too long.\n",
76 25
                    sym->name);
77 25
                tl->err = 1;
78 25
                return;
79
        }
80
81 18371025
        for (p = sym->name; *p != '\0'; p++) {
82 16998900
                if (!vct_istchar(*p)) {
83 25
                        VSB_cat(tl->sb, "Invalid character '");
84 25
                        VSB_quote(tl->sb, p, 1, VSB_QUOTE_PLAIN);
85 25
                        VSB_cat(tl->sb, "' in header name.\n");
86 25
                        tl->err = 1;
87 25
                        return;
88
                }
89 16998875
        }
90
91 1372125
        sym->noref = 1;
92 1372125
        sym->kind = SYM_VAR;
93 1372125
        sym->type = parent->type;
94 1372125
        sym->eval = vcc_Eval_Var;
95 1372125
        sym->eval_priv = parent;
96 1372125
        sym->r_methods = parent->r_methods;
97 1372125
        sym->w_methods = parent->w_methods;
98 1372125
        sym->u_methods = parent->u_methods;
99
100
        /* Create a C-name version of the header name */
101 1372125
        vsb = VSB_new_auto();
102 1372125
        AN(vsb);
103 1372125
        VSB_printf(vsb, "&VGC_%s_", parent->rname);
104 1372125
        VCC_PrintCName(vsb, sym->name, NULL);
105 1372125
        AZ(VSB_finish(vsb));
106
107
        /* Create the symbol r/l values */
108 1372125
        sym->rname = TlDup(tl, VSB_data(vsb));
109
110 1372125
        if (sym->w_methods) {
111 1371925
                VSB_clear(vsb);
112 1371925
                VSB_printf(vsb, "VRT_SetHdr(ctx, %s,", sym->rname);
113 1371925
                AZ(VSB_finish(vsb));
114 1371925
                sym->lname = TlDup(tl, VSB_data(vsb));
115 1371925
        }
116
117 1372125
        if (sym->u_methods) {
118 1371925
                VSB_clear(vsb);
119 1371925
                VSB_printf(vsb, "VRT_UnsetHdr(ctx, %s)", sym->rname);
120 1371925
                AZ(VSB_finish(vsb));
121 1371925
                sym->uname = TlDup(tl, VSB_data(vsb));
122 1371925
        }
123 1372125
        VSB_destroy(&vsb);
124
125 1372125
        if (sym->lorev > 0)
126 788325
                vcc_Header_Fh(tl, sym);
127 1372175
}