varnish-cache/bin/varnishd/http1/cache_http1_proto.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 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
 * HTTP protocol requests
31
 *
32
 * The trouble with the "until magic sequence" design of HTTP protocol messages
33
 * is that either you have to read a single character at a time, which is
34
 * inefficient, or you risk reading too much, and pre-read some of the object,
35
 * or even the next pipelined request, which follows the one you want.
36
 *
37
 * HTC reads a HTTP protocol header into a workspace, subject to limits,
38
 * and stops when we see the magic marker (double [CR]NL), and if we overshoot,
39
 * it keeps track of the "pipelined" data.
40
 *
41
 * We use this both for client and backend connections.
42
 */
43
44
#include "config.h"
45
46
#include "cache/cache_varnishd.h"
47
#include "cache/cache_transport.h"
48
49
#include "cache_http1.h"
50
51
#include "vct.h"
52
53
const int HTTP1_Req[3] = {
54
        HTTP_HDR_METHOD, HTTP_HDR_URL, HTTP_HDR_PROTO
55
};
56
57
const int HTTP1_Resp[3] = {
58
        HTTP_HDR_PROTO, HTTP_HDR_STATUS, HTTP_HDR_REASON
59
};
60
61
/*--------------------------------------------------------------------
62
 * Check if we have a complete HTTP request or response yet
63
 */
64
65
enum htc_status_e v_matchproto_(htc_complete_f)
66 501935
HTTP1_Complete(struct http_conn *htc)
67
{
68
        char *p;
69
        enum htc_status_e retval;
70
71 501935
        CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
72 501935
        AN(WS_Reservation(htc->ws));
73 501935
        assert(pdiff(htc->rxbuf_b, htc->rxbuf_e) <= WS_ReservationSize(htc->ws));
74
75
        /* Skip any leading white space */
76 503415
        for (p = htc->rxbuf_b ; p < htc->rxbuf_e && vct_islws(*p); p++)
77 1480
                continue;
78 501935
        if (p == htc->rxbuf_e)
79 273170
                return (HTC_S_EMPTY);
80
81
        /* Do not return a partial H2 connection preface */
82 228765
        retval = H2_prism_complete(htc);
83 228765
        if (retval != HTC_S_JUNK)
84 10959
                return (retval);
85
86
        /*
87
         * Here we just look for NL[CR]NL to see that reception
88
         * is completed.  More stringent validation happens later.
89
         */
90 786889
        while (1) {
91 786889
                p = memchr(p, '\n', htc->rxbuf_e - p);
92 786889
                if (p == NULL)
93 12027
                        return (HTC_S_MORE);
94 774862
                if (++p == htc->rxbuf_e)
95 885
                        return (HTC_S_MORE);
96 773977
                if (*p == '\r' && ++p == htc->rxbuf_e)
97 80
                        return (HTC_S_MORE);
98 773897
                if (*p == '\n')
99 204814
                        break;
100
        }
101 204814
        return (HTC_S_COMPLETE);
102 501935
}
103
104
/*--------------------------------------------------------------------
105
 * Dissect the headers of the HTTP protocol message.
106
 * Detect conditionals (headers which start with '^[Ii][Ff]-')
107
 */
108
109
static uint16_t
110 204843
http1_dissect_hdrs(struct http *hp, char *p, struct http_conn *htc,
111
    unsigned maxhdr)
112
{
113
        char *q, *r, *s;
114
        int i;
115
116 204843
        assert(p > htc->rxbuf_b);
117 204843
        assert(p <= htc->rxbuf_e);
118 204843
        hp->nhd = HTTP_HDR_FIRST;
119 204843
        r = NULL;               /* For FlexeLint */
120 764712
        for (; p < htc->rxbuf_e; p = r) {
121
122
                /* Find end of next header */
123 764710
                q = r = p;
124 764710
                if (vct_iscrlf(p, htc->rxbuf_e))
125 203879
                        break;
126 11907280
                while (r < htc->rxbuf_e) {
127 11907242
                        if (vct_ishdrval(*r)) {
128 11346311
                                r++;
129 11346311
                                continue;
130
                        }
131 560931
                        i = vct_iscrlf(r, htc->rxbuf_e);
132 560931
                        if (i == 0) {
133 320
                                VSLb(hp->vsl, SLT_BogoHeader,
134 160
                                    "Header has ctrl char 0x%02x", *r);
135 160
                                return (400);
136
                        }
137 560771
                        q = r;
138 560771
                        r += i;
139 560771
                        assert(r <= htc->rxbuf_e);
140 560771
                        if (r == htc->rxbuf_e)
141 0
                                break;
142 560771
                        if (vct_iscrlf(r, htc->rxbuf_e))
143 203580
                                break;
144
                        /* If line does not continue: got it. */
145 357191
                        if (!vct_issp(*r))
146 356831
                                break;
147
148
                        /* Clear line continuation LWS to spaces */
149 720
                        while (q < r)
150 360
                                *q++ = ' ';
151 760
                        while (q < htc->rxbuf_e && vct_issp(*q))
152 400
                                *q++ = ' ';
153
                }
154
155
                /* Empty header = end of headers */
156 560449
                if (p == q)
157 0
                        break;
158
159 560419
                if (q - p > maxhdr) {
160 320
                        VSLb(hp->vsl, SLT_BogoHeader, "Header too long: %.*s",
161 160
                            (int)(q - p > 20 ? 20 : q - p), p);
162 160
                        return (400);
163
                }
164
165 560259
                if (vct_islws(*p)) {
166 160
                        VSLb(hp->vsl, SLT_BogoHeader,
167
                            "1st header has white space: %.*s",
168 80
                            (int)(q - p > 20 ? 20 : q - p), p);
169 80
                        return (400);
170
                }
171
172 560179
                if (*p == ':') {
173 80
                        VSLb(hp->vsl, SLT_BogoHeader,
174
                            "Missing header name: %.*s",
175 40
                            (int)(q - p > 20 ? 20 : q - p), p);
176 40
                        return (400);
177
                }
178
179 560859
                while (q > p && vct_issp(q[-1]))
180 720
                        q--;
181 560109
                *q = '\0';
182
183 4867975
                for (s = p; *s != ':' && s < q; s++) {
184 4308066
                        if (!vct_istchar(*s)) {
185 400
                                VSLb(hp->vsl, SLT_BogoHeader,
186 200
                                    "Illegal char 0x%02x in header name", *s);
187 200
                                return (400);
188
                        }
189 4307866
                }
190 559909
                if (*s != ':') {
191 80
                        VSLb(hp->vsl, SLT_BogoHeader, "Header without ':' %.*s",
192 40
                            (int)(q - p > 20 ? 20 : q - p), p);
193 40
                        return (400);
194
                }
195
196 559869
                if (hp->nhd < hp->shd) {
197 559869
                        hp->hdf[hp->nhd] = 0;
198 559869
                        hp->hd[hp->nhd].b = p;
199 559869
                        hp->hd[hp->nhd].e = q;
200 559869
                        hp->nhd++;
201 559869
                } else {
202 0
                        VSLb(hp->vsl, SLT_BogoHeader, "Too many headers: %.*s",
203 0
                            (int)(q - p > 20 ? 20 : q - p), p);
204 0
                        return (400);
205
                }
206 559869
        }
207 203881
        i = vct_iscrlf(p, htc->rxbuf_e);
208 203881
        assert(i > 0);          /* HTTP1_Complete guarantees this */
209 203879
        p += i;
210 203879
        HTC_RxPipeline(htc, p);
211 203879
        htc->rxbuf_e = p;
212 203879
        return (0);
213 204559
}
214
215
/*--------------------------------------------------------------------
216
 * Deal with first line of HTTP protocol message.
217
 */
218
219
static uint16_t
220 204795
http1_splitline(struct http *hp, struct http_conn *htc, const int *hf,
221
    unsigned maxhdr)
222
{
223
        char *p, *q;
224
        int i;
225
226 204795
        assert(hf == HTTP1_Req || hf == HTTP1_Resp);
227 204795
        CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
228 204795
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
229 204795
        assert(htc->rxbuf_e >= htc->rxbuf_b);
230
231 204795
        AZ(hp->hd[hf[0]].b);
232 204795
        AZ(hp->hd[hf[1]].b);
233 204795
        AZ(hp->hd[hf[2]].b);
234
235
        /* Skip leading LWS */
236 204995
        for (p = htc->rxbuf_b ; vct_islws(*p); p++)
237 200
                continue;
238 204795
        hp->hd[hf[0]].b = p;
239
240
        /* First field cannot contain SP or CTL */
241 1228480
        for (; !vct_issp(*p); p++) {
242 1023845
                if (vct_isctl(*p))
243 160
                        return (400);
244 1023685
        }
245 204635
        hp->hd[hf[0]].e = p;
246 204635
        assert(Tlen(hp->hd[hf[0]]));
247 204635
        *p++ = '\0';
248
249
        /* Skip SP */
250 204875
        for (; vct_issp(*p); p++) {
251 240
                if (vct_isctl(*p))
252 0
                        return (400);
253 240
        }
254 204635
        hp->hd[hf[1]].b = p;
255
256
        /* Second field cannot contain LWS or CTL */
257 1482507
        for (; !vct_islws(*p); p++) {
258 1277872
                if (vct_isctl(*p))
259 0
                        return (400);
260 1277872
        }
261 204635
        hp->hd[hf[1]].e = p;
262 204635
        if (!Tlen(hp->hd[hf[1]]))
263 40
                return (400);
264
265
        /* Skip SP */
266 204595
        q = p;
267 408905
        for (; vct_issp(*p); p++) {
268 204310
                if (vct_isctl(*p))
269 0
                        return (400);
270 204310
        }
271 204595
        if (q < p)
272 204271
                *q = '\0';      /* Nul guard for the 2nd field. If q == p
273
                                 * (the third optional field is not
274
                                 * present), the last nul guard will
275
                                 * cover this field. */
276
277
        /* Third field is optional and cannot contain CTL except TAB */
278 204595
        q = p;
279 1362204
        for (; p < htc->rxbuf_e && !vct_iscrlf(p, htc->rxbuf_e); p++) {
280 1157649
                if (vct_isctl(*p) && !vct_issp(*p))
281 40
                        return (400);
282 1157609
        }
283 204545
        if (p > q) {
284 204109
                hp->hd[hf[2]].b = q;
285 204109
                hp->hd[hf[2]].e = p;
286 204109
        }
287
288
        /* Skip CRLF */
289 204545
        i = vct_iscrlf(p, htc->rxbuf_e);
290 204545
        if (!i)
291 0
                return (400);
292 204545
        *p = '\0';
293 204545
        p += i;
294
295 204545
        http_Proto(hp);
296
297 204545
        return (http1_dissect_hdrs(hp, p, htc, maxhdr));
298 204785
}
299
300
/*--------------------------------------------------------------------*/
301
302
static body_status_t
303 203683
http1_body_status(const struct http *hp, struct http_conn *htc, int request)
304
{
305
        ssize_t cl;
306
        const char *b;
307
308 203683
        CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
309 203683
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
310
311 203683
        htc->content_length = -1;
312
313 203683
        cl = http_GetContentLength(hp);
314 203683
        if (cl == -2)
315 160
                return (BS_ERROR);
316 203523
        if (http_GetHdr(hp, H_Transfer_Encoding, &b)) {
317 6240
                if (!http_coding_eq(b, chunked))
318 80
                        return (BS_ERROR);
319 6160
                if (cl != -1) {
320
                        /*
321
                         * RFC7230 3.3.3 allows more lenient handling
322
                         * but we're going to be strict.
323
                         */
324 80
                        return (BS_ERROR);
325
                }
326 6080
                return (BS_CHUNKED);
327
        }
328 197283
        if (cl >= 0) {
329 75435
                htc->content_length = cl;
330 75435
                return (cl == 0 ? BS_NONE : BS_LENGTH);
331
        }
332
333 121848
        if (hp->protover == 11 && request)
334 119568
                return (BS_NONE);
335
336 2280
        if (http_HdrIs(hp, H_Connection, "keep-alive")) {
337
                /*
338
                 * Keep alive with neither TE=Chunked or C-Len is impossible.
339
                 * We assume a zero length body.
340
                 */
341 80
                return (BS_NONE);
342
        }
343
344
        /*
345
         * Fall back to EOF transfer.
346
         */
347 2200
        return (BS_EOF);
348 203683
}
349
350
/*--------------------------------------------------------------------*/
351
352
uint16_t
353 125006
HTTP1_DissectRequest(struct http_conn *htc, struct http *hp)
354
{
355
        uint16_t retval;
356
        const char *p;
357 125006
        const char *b = NULL, *e;
358
359 125006
        CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
360 125006
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
361
362 250012
        retval = http1_splitline(hp, htc,
363 125006
            HTTP1_Req, cache_param->http_req_hdr_len);
364 125006
        if (retval != 0)
365 600
                return (retval);
366
367 124406
        if (hp->protover < 10 || hp->protover > 11)
368 480
                return (400);
369
370
        /* RFC2616, section 5.2, point 1 */
371 123926
        if (http_scheme_at(hp->hd[HTTP_HDR_URL].b, http))
372 120
                b = hp->hd[HTTP_HDR_URL].b + 7;
373 123806
        else if (FEATURE(FEATURE_HTTPS_SCHEME) &&
374 40
            http_scheme_at(hp->hd[HTTP_HDR_URL].b, https))
375 40
                b = hp->hd[HTTP_HDR_URL].b + 8;
376 123926
        if (b) {
377 160
                e = strchr(b, '/');
378 160
                if (e) {
379 160
                        http_Unset(hp, H_Host);
380 160
                        http_PrintfHeader(hp, "Host: %.*s", (int)(e - b), b);
381 160
                        hp->hd[HTTP_HDR_URL].b = e;
382 160
                }
383 160
        }
384
385 123926
        htc->body_status = http1_body_status(hp, htc, 1);
386 123926
        if (htc->body_status == BS_ERROR)
387 160
                return (400);
388
389 123766
        p = http_GetMethod(hp);
390 123766
        AN(p);
391
392 123766
        if (htc->body_status == BS_EOF) {
393 560
                assert(hp->protover == 10);
394
                /* RFC1945 8.3 p32 and D.1.1 p58 */
395 560
                if (http_method_eq(p, POST) || http_method_eq(p, PUT))
396 80
                        return (400);
397 480
                htc->body_status = BS_NONE;
398 480
        }
399
400
        /* HEAD with a body is a hard error */
401 123686
        if (htc->body_status != BS_NONE && http_method_eq(p, HEAD))
402 0
                return (400);
403
404 123686
        return (retval);
405 125006
}
406
407
/*--------------------------------------------------------------------*/
408
409
uint16_t
410 79799
HTTP1_DissectResponse(struct http_conn *htc, struct http *hp,
411
    const struct http *rhttp)
412
{
413 79799
        uint16_t retval = 0;
414
        const char *p;
415
416 79799
        CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
417 79799
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
418 79799
        CHECK_OBJ_NOTNULL(rhttp, HTTP_MAGIC);
419
420 159598
        if (http1_splitline(hp, htc,
421 79799
            HTTP1_Resp, cache_param->http_resp_hdr_len))
422 320
                retval = 503;
423
424 79799
        if (retval == 0 && hp->protover < 10)
425 0
                retval = 503;
426
427 79799
        if (retval == 0 && hp->protover > rhttp->protover)
428 40
                http_SetH(hp, HTTP_HDR_PROTO, rhttp->hd[HTTP_HDR_PROTO].b);
429
430 79799
        if (retval == 0 && Tlen(hp->hd[HTTP_HDR_STATUS]) != 3)
431 120
                retval = 503;
432
433 79799
        if (retval == 0) {
434 79356
                p = hp->hd[HTTP_HDR_STATUS].b;
435
436 158472
                if (p[0] >= '1' && p[0] <= '9' &&
437 79236
                    p[1] >= '0' && p[1] <= '9' &&
438 79156
                    p[2] >= '0' && p[2] <= '9')
439 79076
                        hp->status =
440 79076
                            100 * (p[0] - '0') + 10 * (p[1] - '0') + p[2] - '0';
441
                else
442 280
                        retval = 503;
443 79356
        }
444
445 79799
        if (retval != 0) {
446 1440
                VSLb(hp->vsl, SLT_HttpGarbage, "%.*s",
447 720
                    (int)(htc->rxbuf_e - htc->rxbuf_b), htc->rxbuf_b);
448 720
                assert(retval >= 100 && retval <= 999);
449 720
                assert(retval == 503);
450 720
                http_SetStatus(hp, 503, NULL);
451 720
        }
452
453 79799
        if (hp->hd[HTTP_HDR_REASON].b == NULL ||
454 79716
            !Tlen(hp->hd[HTTP_HDR_REASON])) {
455 172
                http_SetH(hp, HTTP_HDR_REASON,
456 86
                    http_Status2Reason(hp->status, NULL));
457 86
        }
458
459 79793
        htc->body_status = http1_body_status(hp, htc, 0);
460
461 79793
        return (retval);
462
}
463
464
/*--------------------------------------------------------------------*/
465
466
static unsigned
467 2090088
http1_WrTxt(const struct worker *wrk, const txt *hh, const char *suf)
468
{
469
        unsigned u;
470
471 2090088
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
472 2090088
        AN(wrk);
473 2090088
        AN(hh);
474 2090088
        AN(hh->b);
475 2090088
        AN(hh->e);
476 2090088
        u = V1L_Write(wrk, hh->b, hh->e - hh->b);
477 2090088
        if (suf != NULL)
478 2089813
                u += V1L_Write(wrk, suf, -1);
479 2090090
        return (u);
480
}
481
482
unsigned
483 197786
HTTP1_Write(const struct worker *w, const struct http *hp, const int *hf)
484
{
485
        unsigned u, l;
486
487 197786
        assert(hf == HTTP1_Req || hf == HTTP1_Resp);
488 197786
        AN(hp->hd[hf[0]].b);
489 197786
        AN(hp->hd[hf[1]].b);
490 197786
        AN(hp->hd[hf[2]].b);
491 197786
        l = http1_WrTxt(w, &hp->hd[hf[0]], " ");
492 197786
        l += http1_WrTxt(w, &hp->hd[hf[1]], " ");
493 197786
        l += http1_WrTxt(w, &hp->hd[hf[2]], "\r\n");
494
495 1694809
        for (u = HTTP_HDR_FIRST; u < hp->nhd; u++)
496 1497023
                l += http1_WrTxt(w, &hp->hd[u], "\r\n");
497 197786
        l += V1L_Write(w, "\r\n", -1);
498 197786
        return (l);
499
}