varnish-cache/lib/libvmod_std/vmod_std.c
1
/*-
2
 * Copyright (c) 2010-2017 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@FreeBSD.org>
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 */
28
29
#include "config.h"
30
31
#include <sys/stat.h>
32
33
#include <netinet/in.h>
34
35
#include <ctype.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#include <syslog.h>
39
#include <sys/socket.h>
40
41
#include "cache/cache.h"
42
43
#include "vrnd.h"
44
#include "vtcp.h"
45
#include "vsa.h"
46
#include "vtim.h"
47
#include "vcl.h"
48
49
#include "cache/cache_director.h"
50
51
#include "vcc_if.h"
52
53
VCL_VOID v_matchproto_(td_std_set_ip_tos)
54 1
vmod_set_ip_tos(VRT_CTX, VCL_INT tos)
55
{
56 1
        int itos = tos;
57
58 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
59 1
        VTCP_Assert(setsockopt(ctx->req->sp->fd,
60
            IPPROTO_IP, IP_TOS, &itos, sizeof(itos)));
61 1
}
62
63
static const char *
64 10
vmod_updown(VRT_CTX, int up, const char *s, va_list ap)
65
{
66
        unsigned u;
67
        char *b, *e;
68
        const char *p;
69
70 10
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
71 10
        u = WS_Reserve(ctx->ws, 0);
72 10
        e = b = ctx->ws->f;
73 10
        e += u;
74 10
        p = s;
75 30
        while (p != vrt_magic_string_end && b < e) {
76 10
                if (p != NULL) {
77 2186
                        for (; b < e && *p != '\0'; p++)
78 2176
                                if (up)
79 1082
                                        *b++ = (char)toupper(*p);
80
                                else
81 1094
                                        *b++ = (char)tolower(*p);
82
                }
83 10
                p = va_arg(ap, const char *);
84
        }
85 10
        if (b < e)
86 10
                *b = '\0';
87 10
        b++;
88 10
        if (b > e) {
89 0
                WS_MarkOverflow(ctx->ws);
90 0
                WS_Release(ctx->ws, 0);
91 0
                return (NULL);
92
        } else {
93 10
                e = b;
94 10
                b = ctx->ws->f;
95 10
                WS_Release(ctx->ws, e - b);
96 10
                return (b);
97
        }
98
}
99
100
VCL_STRING v_matchproto_(td_std_toupper)
101 4
vmod_toupper(VRT_CTX, const char *s, ...)
102
{
103
        const char *p;
104
        va_list ap;
105
106 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
107 4
        va_start(ap, s);
108 4
        p = vmod_updown(ctx, 1, s, ap);
109 4
        va_end(ap);
110 4
        return (p);
111
}
112
113
VCL_STRING v_matchproto_(td_std_tolower)
114 6
vmod_tolower(VRT_CTX, const char *s, ...)
115
{
116
        const char *p;
117
        va_list ap;
118
119 6
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
120 6
        va_start(ap, s);
121 6
        p = vmod_updown(ctx, 0, s, ap);
122 6
        va_end(ap);
123 6
        return (p);
124
}
125
126
VCL_REAL v_matchproto_(td_std_random)
127 76
vmod_random(VRT_CTX, VCL_REAL lo, VCL_REAL hi)
128
{
129
        double a;
130
131 76
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
132 76
        a = VRND_RandomTestableDouble();
133 76
        a *= hi - lo;
134 76
        a += lo;
135 76
        return (a);
136
}
137
138
VCL_VOID v_matchproto_(td_std_log)
139 55
vmod_log(VRT_CTX, const char *fmt, ...)
140
{
141
        const char *p;
142
        va_list ap;
143
        uintptr_t sn;
144
145 55
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
146 55
        sn = WS_Snapshot(ctx->ws);
147 55
        va_start(ap, fmt);
148 55
        p = VRT_String(ctx->ws, NULL, fmt, ap);
149 55
        va_end(ap);
150
151 55
        if (p == NULL) {
152 0
                WS_MarkOverflow(ctx->ws);
153 0
                WS_Reset(ctx->ws, sn);
154 55
                return;
155
        }
156
157 55
        AN(p);
158 55
        if (ctx->vsl != NULL)
159 28
                VSLb(ctx->vsl, SLT_VCL_Log, "%s", p);
160
        else
161 27
                VSL(SLT_VCL_Log, 0, "%s", p);
162 55
        WS_Reset(ctx->ws, sn);
163
}
164
165
/* XXX use vsyslog() ? */
166
VCL_VOID v_matchproto_(td_std_syslog)
167 3
vmod_syslog(VRT_CTX, VCL_INT fac, const char *fmt, ...)
168
{
169
        const char *p;
170
        va_list ap;
171
        uintptr_t sn;
172
173 3
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
174 3
        sn = WS_Snapshot(ctx->ws);
175 3
        va_start(ap, fmt);
176 3
        p = VRT_String(ctx->ws, NULL, fmt, ap);
177 3
        va_end(ap);
178 3
        if (p != NULL)
179 3
                syslog((int)fac, "%s", p);
180 3
        WS_Reset(ctx->ws, sn);
181 3
}
182
183
VCL_BOOL v_matchproto_(td_std_file_exists)
184 2
vmod_file_exists(VRT_CTX, VCL_STRING file_name)
185
{
186
        struct stat st;
187
188 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
189 2
        return (stat(file_name, &st) == 0);
190
}
191
192
VCL_VOID v_matchproto_(td_std_collect)
193 10
vmod_collect(VRT_CTX, VCL_HEADER hdr, VCL_STRING sep)
194
{
195
        struct http *hp;
196
197 10
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
198 10
        hp = VRT_selecthttp(ctx, hdr->where);
199 10
        http_CollectHdrSep(hp, hdr->what, sep);
200 10
}
201
202
VCL_BOOL v_matchproto_(td_std_healthy)
203 8
vmod_healthy(VRT_CTX, VCL_BACKEND be)
204
{
205 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
206 8
        CHECK_OBJ_ORNULL(be, DIRECTOR_MAGIC);
207 8
        return (VRT_Healthy(ctx, be));
208
}
209
210
VCL_INT v_matchproto_(td_std_port)
211 58
vmod_port(VRT_CTX, VCL_IP ip)
212
{
213 58
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
214 58
        if (ip == NULL)
215 0
                return (0);
216 58
        return (VSA_Port(ip));
217
}
218
219
VCL_VOID v_matchproto_(td_std_rollback)
220 4
vmod_rollback(VRT_CTX, VCL_HTTP hp)
221
{
222 4
        VRT_Rollback(ctx, hp);
223 4
}
224
225
VCL_VOID v_matchproto_(td_std_timestamp)
226 3
vmod_timestamp(VRT_CTX, VCL_STRING label)
227
{
228
229 3
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
230 3
        if (label == NULL)
231 0
                return;
232 3
        if (*label == '\0')
233 0
                return;
234 3
        if (ctx->bo != NULL && ctx->req == NULL) {
235
                /* Called from backend vcl methods */
236 1
                CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
237 1
                VSLb_ts_busyobj(ctx->bo, label, VTIM_real());
238 2
        } else if (ctx->req != NULL) {
239
                /* Called from request vcl methods */
240 1
                CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
241 1
                VSLb_ts_req(ctx->req, label, VTIM_real());
242
        }
243
}
244
245
VCL_BOOL v_matchproto_(td_std_cache_req_body)
246 10
vmod_cache_req_body(VRT_CTX, VCL_BYTES size)
247
{
248 10
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
249 10
        if (size < 0)
250 0
                size = 0;
251 10
        if (VRT_CacheReqBody(ctx, (size_t)size) < 0)
252 3
                return (0);
253 7
        return (1);
254
}
255
256
VCL_STRING v_matchproto_(td_std_strstr)
257 2
vmod_strstr(VRT_CTX, VCL_STRING s1, VCL_STRING s2)
258
{
259 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
260 2
        if (s1 == NULL || s2 == NULL)
261 0
                return (NULL);
262 2
        return (strstr(s1, s2));
263
}
264
265
VCL_STRING v_matchproto_(td_std_getenv)
266 4
vmod_getenv(VRT_CTX, VCL_STRING name)
267
{
268 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
269 4
        if (name == NULL || *name == '\0')
270 0
                return (NULL);
271 4
        return (getenv(name));
272
}
273
274
VCL_VOID v_matchproto_(td_std_late_100_continue)
275 5
vmod_late_100_continue(VRT_CTX, VCL_BOOL late)
276
{
277 5
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
278 5
        if (ctx->method != VCL_MET_RECV) {
279 0
                VSLb(ctx->vsl, SLT_VCL_Error,
280
                    "std.late_100_continue() only valid in vcl_recv{}");
281 5
                return;
282
        }
283
284 5
        CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
285 5
        if (ctx->req->want100cont)
286 5
                ctx->req->late100cont = late;
287
}