blob: 9ce95e00693f45866a2c350a458e6cbfdf348104 [file] [log] [blame]
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001/*
2 * Functions to manipulate H1 messages using the internal representation.
3 *
4 * Copyright (C) 2019 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020013#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020014#include <haproxy/cfgparse.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020015#include <haproxy/global.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020016#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020017#include <haproxy/h1_htx.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020018#include <haproxy/http.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020019#include <haproxy/htx.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020020#include <haproxy/tools.h>
Christopher Faulet4f0f88a2019-08-10 11:17:44 +020021
Christopher Faulet4f0f88a2019-08-10 11:17:44 +020022/* Estimate the size of the HTX headers after the parsing, including the EOH. */
23static size_t h1_eval_htx_hdrs_size(const struct http_hdr *hdrs)
24{
25 size_t sz = 0;
26 int i;
27
28 for (i = 0; hdrs[i].n.len; i++)
29 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
30 sz += sizeof(struct htx_blk) + 1;
31 return sz;
32}
33
34/* Estimate the size of the HTX request after the parsing. */
35static size_t h1_eval_htx_size(const struct ist p1, const struct ist p2, const struct ist p3,
36 const struct http_hdr *hdrs)
37{
38 size_t sz;
39
40 /* size of the HTX start-line */
41 sz = sizeof(struct htx_blk) + sizeof(struct htx_sl) + p1.len + p2.len + p3.len;
42 sz += h1_eval_htx_hdrs_size(hdrs);
43 return sz;
44}
45
Christopher Faulet4f0f88a2019-08-10 11:17:44 +020046/* Check the validity of the request version. If the version is valid, it
47 * returns 1. Otherwise, it returns 0.
48 */
49static int h1_process_req_vsn(struct h1m *h1m, union h1_sl *sl)
50{
51 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
52 * exactly one digit "." one digit. This check may be disabled using
53 * option accept-invalid-http-request.
54 */
55 if (h1m->err_pos == -2) { /* PR_O2_REQBUG_OK not set */
56 if (sl->rq.v.len != 8)
57 return 0;
58
59 if (*(sl->rq.v.ptr + 4) != '/' ||
60 !isdigit((unsigned char)*(sl->rq.v.ptr + 5)) ||
61 *(sl->rq.v.ptr + 6) != '.' ||
62 !isdigit((unsigned char)*(sl->rq.v.ptr + 7)))
63 return 0;
64 }
65 else if (!sl->rq.v.len) {
66 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
67
68 /* RFC 1945 allows only GET for HTTP/0.9 requests */
69 if (sl->rq.meth != HTTP_METH_GET)
70 return 0;
71
72 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
73 if (!sl->rq.u.len)
74 return 0;
75
76 /* Add HTTP version */
77 sl->rq.v = ist("HTTP/1.0");
78 return 1;
79 }
80
81 if ((sl->rq.v.len == 8) &&
82 ((*(sl->rq.v.ptr + 5) > '1') ||
83 ((*(sl->rq.v.ptr + 5) == '1') && (*(sl->rq.v.ptr + 7) >= '1'))))
84 h1m->flags |= H1_MF_VER_11;
85 return 1;
86}
87
88/* Check the validity of the response version. If the version is valid, it
89 * returns 1. Otherwise, it returns 0.
90 */
91static int h1_process_res_vsn(struct h1m *h1m, union h1_sl *sl)
92{
93 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
94 * exactly one digit "." one digit. This check may be disabled using
95 * option accept-invalid-http-request.
96 */
97 if (h1m->err_pos == -2) { /* PR_O2_REQBUG_OK not set */
98 if (sl->st.v.len != 8)
99 return 0;
100
101 if (*(sl->st.v.ptr + 4) != '/' ||
102 !isdigit((unsigned char)*(sl->st.v.ptr + 5)) ||
103 *(sl->st.v.ptr + 6) != '.' ||
104 !isdigit((unsigned char)*(sl->st.v.ptr + 7)))
105 return 0;
106 }
107
108 if ((sl->st.v.len == 8) &&
109 ((*(sl->st.v.ptr + 5) > '1') ||
110 ((*(sl->st.v.ptr + 5) == '1') && (*(sl->st.v.ptr + 7) >= '1'))))
111 h1m->flags |= H1_MF_VER_11;
112
113 return 1;
114}
115
116/* Convert H1M flags to HTX start-line flags. */
117static unsigned int h1m_htx_sl_flags(struct h1m *h1m)
118{
119 unsigned int flags = HTX_SL_F_NONE;
120
121 if (h1m->flags & H1_MF_RESP)
122 flags |= HTX_SL_F_IS_RESP;
123 if (h1m->flags & H1_MF_VER_11)
124 flags |= HTX_SL_F_VER_11;
125 if (h1m->flags & H1_MF_XFER_ENC)
126 flags |= HTX_SL_F_XFER_ENC;
127 if (h1m->flags & H1_MF_XFER_LEN) {
128 flags |= HTX_SL_F_XFER_LEN;
129 if (h1m->flags & H1_MF_CHNK)
130 flags |= HTX_SL_F_CHNK;
131 else if (h1m->flags & H1_MF_CLEN) {
132 flags |= HTX_SL_F_CLEN;
133 if (h1m->body_len == 0)
134 flags |= HTX_SL_F_BODYLESS;
135 }
136 else
137 flags |= HTX_SL_F_BODYLESS;
138 }
Christopher Faulet576c3582021-01-08 15:53:01 +0100139 if (h1m->flags & H1_MF_CONN_UPG)
140 flags |= HTX_SL_F_CONN_UPG;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200141 return flags;
142}
143
144/* Postprocess the parsed headers for a request and convert them into an htx
145 * message. It returns the number of bytes parsed if > 0, or 0 if it couldn't
146 * proceed. Parsing errors are reported by setting the htx flag
147 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields.
148 */
149static int h1_postparse_req_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *htx,
150 struct http_hdr *hdrs, size_t max)
151{
152 struct htx_sl *sl;
153 struct ist meth, uri, vsn;
154 unsigned int flags;
155 size_t used;
156
157 /* <h1sl> is always defined for a request */
158 meth = h1sl->rq.m;
159 uri = h1sl->rq.u;
160 vsn = h1sl->rq.v;
161
162 /* Be sure the message, once converted into HTX, will not exceed the max
163 * size allowed.
164 */
165 if (h1_eval_htx_size(meth, uri, vsn, hdrs) > max) {
166 if (htx_is_empty(htx))
167 goto error;
168 h1m_init_res(h1m);
169 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
170 return 0;
171 }
172
173 /* By default, request have always a known length */
174 h1m->flags |= H1_MF_XFER_LEN;
175
176 if (h1sl->rq.meth == HTTP_METH_CONNECT) {
Christopher Faulet5be651d2021-01-22 15:28:03 +0100177 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
178 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200179 }
180
181 used = htx_used_space(htx);
182 flags = h1m_htx_sl_flags(h1m);
183 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, uri, vsn);
184 if (!sl || !htx_add_all_headers(htx, hdrs))
185 goto error;
186 sl->info.req.meth = h1sl->rq.meth;
187
Christopher Fauletfe451fb2019-10-08 15:01:34 +0200188 /* Check if the uri contains an authority. Also check if it contains an
189 * explicit scheme and if it is "http" or "https". */
190 if (h1sl->rq.meth == HTTP_METH_CONNECT)
191 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
192 else if (uri.len && uri.ptr[0] != '/' && uri.ptr[0] != '*') {
193 sl->flags |= (HTX_SL_F_HAS_AUTHORITY|HTX_SL_F_HAS_SCHM);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200194 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
195 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
196 }
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500197 /* Set bytes used in the HTX message for the headers now */
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200198 sl->hdrs_bytes = htx_used_space(htx) - used;
199
200 /* If body length cannot be determined, set htx->extra to
201 * ULLONG_MAX. This value is impossible in other cases.
202 */
203 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
204
205 end:
206 return 1;
207 error:
208 h1m->err_pos = h1m->next;
209 h1m->err_state = h1m->state;
210 htx->flags |= HTX_FL_PARSING_ERROR;
211 return 0;
212}
213
214/* Postprocess the parsed headers for a response and convert them into an htx
215 * message. It returns the number of bytes parsed if > 0, or 0 if it couldn't
216 * proceed. Parsing errors are reported by setting the htx flag
217 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields.
218 */
219static int h1_postparse_res_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *htx,
220 struct http_hdr *hdrs, size_t max)
221{
222 struct htx_sl *sl;
223 struct ist vsn, status, reason;
224 unsigned int flags;
225 size_t used;
226 uint16_t code = 0;
227
228 if (h1sl) {
229 /* For HTTP responses, the start-line was parsed */
230 code = h1sl->st.status;
231 vsn = h1sl->st.v;
232 status = h1sl->st.c;
233 reason = h1sl->st.r;
234 }
235 else {
236 /* For FCGI responses, there is no start(-line but the "Status"
237 * header must be parsed, if found.
238 */
239 int hdr;
240
241 vsn = ((h1m->flags & H1_MF_VER_11) ? ist("HTTP/1.1") : ist("HTTP/1.0"));
242 for (hdr = 0; hdrs[hdr].n.len; hdr++) {
243 if (isteqi(hdrs[hdr].n, ist("status"))) {
244 code = http_parse_status_val(hdrs[hdr].v, &status, &reason);
245 }
246 else if (isteqi(hdrs[hdr].n, ist("location"))) {
247 code = 302;
248 status = ist("302");
249 reason = ist("Moved Temporarily");
250 }
251 }
252 if (!code) {
253 code = 200;
254 status = ist("200");
255 reason = ist("OK");
256 }
257 /* FIXME: Check the codes 1xx ? */
258 }
259
260 /* Be sure the message, once converted into HTX, will not exceed the max
261 * size allowed.
262 */
263 if (h1_eval_htx_size(vsn, status, reason, hdrs) > max) {
264 if (htx_is_empty(htx))
265 goto error;
266 h1m_init_res(h1m);
267 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
268 return 0;
269 }
270
Christopher Fauletc75668e2020-12-07 18:10:32 +0100271 if (((h1m->flags & H1_MF_METH_CONNECT) && code >= 200 && code < 300) || code == 101) {
Christopher Faulet5be651d2021-01-22 15:28:03 +0100272 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
273 h1m->flags |= H1_MF_XFER_LEN;
274 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200275 }
276 else if ((h1m->flags & H1_MF_METH_HEAD) || (code >= 100 && code < 200) ||
277 (code == 204) || (code == 304)) {
278 /* Responses known to have no body. */
279 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
280 h1m->flags |= H1_MF_XFER_LEN;
281 h1m->curr_len = h1m->body_len = 0;
282 }
283 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
284 /* Responses with a known body length. */
285 h1m->flags |= H1_MF_XFER_LEN;
286 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200287
288 used = htx_used_space(htx);
289 flags = h1m_htx_sl_flags(h1m);
290 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, vsn, status, reason);
291 if (!sl || !htx_add_all_headers(htx, hdrs))
292 goto error;
293 sl->info.res.status = code;
294
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500295 /* Set bytes used in the HTX message for the headers now */
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200296 sl->hdrs_bytes = htx_used_space(htx) - used;
297
298 /* If body length cannot be determined, set htx->extra to
299 * ULLONG_MAX. This value is impossible in other cases.
300 */
301 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
302
303 end:
304 return 1;
305 error:
306 h1m->err_pos = h1m->next;
307 h1m->err_state = h1m->state;
308 htx->flags |= HTX_FL_PARSING_ERROR;
309 return 0;
310}
311
312/* Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
313 * it couldn't proceed. Parsing errors are reported by setting the htx flag
314 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This
315 * functions is responsible to update the parser state <h1m> and the start-line
316 * <h1sl> if not NULL.
317 * For the requests, <h1sl> must always be provided. For responses, <h1sl> may
318 * be NULL and <h1m> flags HTTP_METH_CONNECT of HTTP_METH_HEAD may be set.
319 */
320int h1_parse_msg_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *dsthtx,
321 struct buffer *srcbuf, size_t ofs, size_t max)
322{
323 struct http_hdr hdrs[global.tune.max_http_hdr];
324 int ret = 0;
325
326 if (!max || !b_data(srcbuf))
327 goto end;
328
329 /* Realing input buffer if necessary */
330 if (b_head(srcbuf) + b_data(srcbuf) > b_wrap(srcbuf))
331 b_slow_realign(srcbuf, trash.area, 0);
332
333 if (!h1sl) {
334 /* If there no start-line, be sure to only parse the headers */
335 h1m->flags |= H1_MF_HDRS_ONLY;
336 }
337 ret = h1_headers_to_hdr_list(b_peek(srcbuf, ofs), b_tail(srcbuf),
338 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, h1sl);
339 if (ret <= 0) {
340 /* Incomplete or invalid message. If the input buffer only
341 * contains headers and is full, which is detected by it being
342 * full and the offset to be zero, it's an error because
343 * headers are too large to be handled by the parser. */
344 if (ret < 0 || (!ret && !ofs && !buf_room_for_htx_data(srcbuf)))
345 goto error;
346 goto end;
347 }
348
349 /* messages headers fully parsed, do some checks to prepare the body
350 * parsing.
351 */
352
353 if (!(h1m->flags & H1_MF_RESP)) {
354 if (!h1_process_req_vsn(h1m, h1sl)) {
355 h1m->err_pos = h1sl->rq.v.ptr - b_head(srcbuf);
356 h1m->err_state = h1m->state;
357 goto vsn_error;
358 }
359 if (!h1_postparse_req_hdrs(h1m, h1sl, dsthtx, hdrs, max))
360 ret = 0;
361 }
362 else {
363 if (h1sl && !h1_process_res_vsn(h1m, h1sl)) {
364 h1m->err_pos = h1sl->st.v.ptr - b_head(srcbuf);
365 h1m->err_state = h1m->state;
366 goto vsn_error;
367 }
368 if (!h1_postparse_res_hdrs(h1m, h1sl, dsthtx, hdrs, max))
369 ret = 0;
370 }
371
Christopher Faulet76014fd2019-12-10 11:47:22 +0100372 /* Switch messages without any payload to DONE state */
373 if (((h1m->flags & H1_MF_CLEN) && h1m->body_len == 0) ||
374 ((h1m->flags & (H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK)) == H1_MF_XFER_LEN))
375 h1m->state = H1_MSG_DONE;
376
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200377 end:
378 return ret;
379 error:
380 h1m->err_pos = h1m->next;
381 h1m->err_state = h1m->state;
382 vsn_error:
383 dsthtx->flags |= HTX_FL_PARSING_ERROR;
384 return 0;
385
386}
387
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200388/* Copy data from <srbuf> into an DATA block in <dsthtx>. If possible, a
389 * zero-copy is performed. It returns the number of bytes copied.
390 */
Christopher Fauletaf542632019-10-01 21:52:49 +0200391static int h1_copy_msg_data(struct htx **dsthtx, struct buffer *srcbuf, size_t ofs,
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200392 size_t count, struct buffer *htxbuf)
393{
Christopher Fauletaf542632019-10-01 21:52:49 +0200394 struct htx *tmp_htx = *dsthtx;
395
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200396 /* very often with large files we'll face the following
397 * situation :
398 * - htx is empty and points to <htxbuf>
399 * - ret == srcbuf->data
400 * - srcbuf->head == sizeof(struct htx)
401 * => we can swap the buffers and place an htx header into
402 * the target buffer instead
403 */
Christopher Fauletaf542632019-10-01 21:52:49 +0200404 if (unlikely(htx_is_empty(tmp_htx) && count == b_data(srcbuf) &&
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200405 !ofs && b_head_ofs(srcbuf) == sizeof(struct htx))) {
406 void *raw_area = srcbuf->area;
407 void *htx_area = htxbuf->area;
408 struct htx_blk *blk;
409
410 srcbuf->area = htx_area;
411 htxbuf->area = raw_area;
Christopher Fauletaf542632019-10-01 21:52:49 +0200412 tmp_htx = (struct htx *)htxbuf->area;
413 tmp_htx->size = htxbuf->size - sizeof(*tmp_htx);
414 htx_reset(tmp_htx);
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200415 b_set_data(htxbuf, b_size(htxbuf));
416
Christopher Fauletaf542632019-10-01 21:52:49 +0200417 blk = htx_add_blk(tmp_htx, HTX_BLK_DATA, count);
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200418 blk->info += count;
Christopher Fauletaf542632019-10-01 21:52:49 +0200419
420 *dsthtx = tmp_htx;
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200421 /* nothing else to do, the old buffer now contains an
422 * empty pre-initialized HTX header
423 */
424 return count;
425 }
426
Christopher Fauletaf542632019-10-01 21:52:49 +0200427 return htx_add_data(*dsthtx, ist2(b_peek(srcbuf, ofs), count));
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200428}
429
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200430/* Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
431 * couldn't proceed. Parsing errors are reported by setting the htx flags
432 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This
433 * functions is responsible to update the parser state <h1m>.
434 */
Christopher Fauletaf542632019-10-01 21:52:49 +0200435int h1_parse_msg_data(struct h1m *h1m, struct htx **dsthtx,
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200436 struct buffer *srcbuf, size_t ofs, size_t max,
437 struct buffer *htxbuf)
438{
439 size_t total = 0;
440 int32_t ret = 0;
441
442 if (h1m->flags & H1_MF_CLEN) {
443 /* content-length: read only h2m->body_len */
Christopher Fauletaf542632019-10-01 21:52:49 +0200444 ret = htx_get_max_blksz(*dsthtx, max);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200445 if ((uint64_t)ret > h1m->curr_len)
446 ret = h1m->curr_len;
447 if (ret > b_contig_data(srcbuf, ofs))
448 ret = b_contig_data(srcbuf, ofs);
449 if (ret) {
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200450 int32_t try = ret;
451
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200452 ret = h1_copy_msg_data(dsthtx, srcbuf, ofs, try, htxbuf);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200453 h1m->curr_len -= ret;
454 max -= sizeof(struct htx_blk) + ret;
455 ofs += ret;
456 total += ret;
457 if (ret < try)
458 goto end;
459 }
460
Christopher Faulet76014fd2019-12-10 11:47:22 +0100461 if (!h1m->curr_len)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200462 h1m->state = H1_MSG_DONE;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200463 }
464 else if (h1m->flags & H1_MF_CHNK) {
465 /* te:chunked : parse chunks */
466 new_chunk:
467 if (h1m->state == H1_MSG_CHUNK_CRLF) {
468 ret = h1_skip_chunk_crlf(srcbuf, ofs, b_data(srcbuf));
469 if (ret <= 0)
470 goto end;
471 h1m->state = H1_MSG_CHUNK_SIZE;
472 ofs += ret;
473 total += ret;
474 }
475 if (h1m->state == H1_MSG_CHUNK_SIZE) {
476 unsigned int chksz;
477
478 ret = h1_parse_chunk_size(srcbuf, ofs, b_data(srcbuf), &chksz);
479 if (ret <= 0)
480 goto end;
481 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
482 h1m->curr_len = chksz;
483 h1m->body_len += chksz;
484 ofs += ret;
485 total += ret;
486 if (!h1m->curr_len)
487 goto end;
488 }
489 if (h1m->state == H1_MSG_DATA) {
Christopher Fauletaf542632019-10-01 21:52:49 +0200490 ret = htx_get_max_blksz(*dsthtx, max);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200491 if ((uint64_t)ret > h1m->curr_len)
492 ret = h1m->curr_len;
493 if (ret > b_contig_data(srcbuf, ofs))
494 ret = b_contig_data(srcbuf, ofs);
495 if (ret) {
496 int32_t try = ret;
497
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200498 ret = h1_copy_msg_data(dsthtx, srcbuf, ofs, try, htxbuf);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200499 h1m->curr_len -= ret;
500 max -= sizeof(struct htx_blk) + ret;
501 ofs += ret;
502 total += ret;
503 if (ret < try)
504 goto end;
505 }
506 if (!h1m->curr_len) {
507 h1m->state = H1_MSG_CHUNK_CRLF;
508 goto new_chunk;
509 }
510 goto end;
511 }
512 }
513 else if (h1m->flags & H1_MF_XFER_LEN) {
514 /* XFER_LEN is set but not CLEN nor CHNK, it means there is no
515 * body. Switch the message in DONE state
516 */
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200517 h1m->state = H1_MSG_DONE;
518 }
519 else {
520 /* no content length, read till SHUTW */
Christopher Fauletaf542632019-10-01 21:52:49 +0200521 ret = htx_get_max_blksz(*dsthtx, max);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200522 if (ret > b_contig_data(srcbuf, ofs))
523 ret = b_contig_data(srcbuf, ofs);
524 if (ret)
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200525 total += h1_copy_msg_data(dsthtx, srcbuf, ofs, ret, htxbuf);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200526 }
527
528 end:
529 if (ret < 0) {
Christopher Fauletaf542632019-10-01 21:52:49 +0200530 (*dsthtx)->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200531 h1m->err_state = h1m->state;
532 h1m->err_pos = ofs;
533 total = 0;
534 }
535
536 /* update htx->extra, only when the body length is known */
537 if (h1m->flags & H1_MF_XFER_LEN)
Christopher Fauletaf542632019-10-01 21:52:49 +0200538 (*dsthtx)->extra = h1m->curr_len;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200539 return total;
540}
541
542/* Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
543 * it couldn't proceed. Parsing errors are reported by setting the htx flags
544 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This
545 * functions is responsible to update the parser state <h1m>.
546 */
547int h1_parse_msg_tlrs(struct h1m *h1m, struct htx *dsthtx,
548 struct buffer *srcbuf, size_t ofs, size_t max)
549{
550 struct http_hdr hdrs[global.tune.max_http_hdr];
551 struct h1m tlr_h1m;
552 int ret = 0;
553
554 if (!max || !b_data(srcbuf))
555 goto end;
556
557 /* Realing input buffer if necessary */
558 if (b_peek(srcbuf, ofs) > b_tail(srcbuf))
559 b_slow_realign(srcbuf, trash.area, 0);
560
561 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
562 ret = h1_headers_to_hdr_list(b_peek(srcbuf, ofs), b_tail(srcbuf),
563 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
564 if (ret <= 0) {
565 /* Incomplete or invalid trailers. If the input buffer only
566 * contains trailers and is full, which is detected by it being
567 * full and the offset to be zero, it's an error because
568 * trailers are too large to be handled by the parser. */
569 if (ret < 0 || (!ret && !ofs && !buf_room_for_htx_data(srcbuf)))
570 goto error;
571 goto end;
572 }
573
574 /* messages trailers fully parsed. */
575 if (h1_eval_htx_hdrs_size(hdrs) > max) {
576 if (htx_is_empty(dsthtx))
577 goto error;
578 ret = 0;
579 goto end;
580 }
581
582 if (!htx_add_all_trailers(dsthtx, hdrs))
583 goto error;
584
Christopher Faulet76014fd2019-12-10 11:47:22 +0100585 h1m->state = H1_MSG_DONE;
586
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200587 end:
588 return ret;
589 error:
590 h1m->err_state = h1m->state;
591 h1m->err_pos = h1m->next;
592 dsthtx->flags |= HTX_FL_PARSING_ERROR;
593 return 0;
594}
595
Christopher Faulet76014fd2019-12-10 11:47:22 +0100596/* Finish HTTP/1 parsing by adding the HTX EOM block. It returns 1 on success or
597 * 0 if it couldn't proceed. There is no parsing at this stage, but a parsing
598 * error is reported if the message state is not H1_MSG_DONE. */
599int h1_parse_msg_eom(struct h1m *h1m, struct htx *dsthtx, size_t max)
600{
601 if (h1m->state != H1_MSG_DONE) {
602 h1m->err_state = h1m->state;
603 h1m->err_pos = h1m->next;
604 dsthtx->flags |= HTX_FL_PARSING_ERROR;
605 return 0;
606 }
Christopher Faulet810df062020-07-22 16:20:34 +0200607
Christopher Faulet42432f32020-11-20 17:43:16 +0100608 dsthtx->flags |= HTX_FL_EOM; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet76014fd2019-12-10 11:47:22 +0100609 if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(dsthtx, HTX_BLK_EOM))
610 return 0;
611
612 return 1;
613}
614
Christopher Faulet53a899b2019-10-08 16:38:42 +0200615
616/* Appends the H1 representation of the request line <sl> to the chunk <chk>. It
617 * returns 1 if data are successfully appended, otherwise it returns 0.
618 */
619int h1_format_htx_reqline(const struct htx_sl *sl, struct buffer *chk)
620{
621 struct ist uri;
622 size_t sz = chk->data;
623
624 uri = htx_sl_req_uri(sl);
625 if (sl->flags & HTX_SL_F_NORMALIZED_URI) {
626 uri = http_get_path(uri);
627 if (unlikely(!uri.len)) {
628 if (sl->info.req.meth == HTTP_METH_OPTIONS)
629 uri = ist("*");
630 else
631 uri = ist("/");
632 }
633 }
634
635 if (!chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)) ||
636 !chunk_memcat(chk, " ", 1) ||
637 !chunk_memcat(chk, uri.ptr, uri.len) ||
638 !chunk_memcat(chk, " ", 1))
639 goto full;
640
641 if (sl->flags & HTX_SL_F_VER_11) {
642 if (!chunk_memcat(chk, "HTTP/1.1", 8))
643 goto full;
644 }
645 else {
646 if (!chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)))
647 goto full;
648 }
649
650 if (!chunk_memcat(chk, "\r\n", 2))
651 goto full;
652
653 return 1;
654
655 full:
656 chk->data = sz;
657 return 0;
658}
659
660/* Appends the H1 representation of the status line <sl> to the chunk <chk>. It
661 * returns 1 if data are successfully appended, otherwise it returns 0.
662 */
663int h1_format_htx_stline(const struct htx_sl *sl, struct buffer *chk)
664{
665 size_t sz = chk->data;
666
667 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
668 return 0;
669
670 if (sl->flags & HTX_SL_F_VER_11) {
671 if (!chunk_memcat(chk, "HTTP/1.1", 8))
672 goto full;
673 }
674 else {
675 if (!chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)))
676 goto full;
677 }
678 if (!chunk_memcat(chk, " ", 1) ||
679 !chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)) ||
680 !chunk_memcat(chk, " ", 1) ||
681 !chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)) ||
682 !chunk_memcat(chk, "\r\n", 2))
683 goto full;
684
685 return 1;
686
687 full:
688 chk->data = sz;
689 return 0;
690}
691
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500692/* Appends the H1 representation of the header <n> with the value <v> to the
Christopher Faulet53a899b2019-10-08 16:38:42 +0200693 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
694 * returns 0.
695 */
696int h1_format_htx_hdr(const struct ist n, const struct ist v, struct buffer *chk)
697{
698 size_t sz = chk->data;
699
700 if (n.len + v.len + 4 > b_room(chk))
701 return 0;
702
703 if (!chunk_memcat(chk, n.ptr, n.len) ||
704 !chunk_memcat(chk, ": ", 2) ||
705 !chunk_memcat(chk, v.ptr, v.len) ||
706 !chunk_memcat(chk, "\r\n", 2))
707 goto full;
708
709 return 1;
710
711 full:
712 chk->data = sz;
713 return 0;
714}
715
716/* Appends the H1 representation of the data <data> to the chunk <chk>. If
717 * <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It returns 1 if
718 * data are successfully appended, otherwise it returns 0.
719 */
720int h1_format_htx_data(const struct ist data, struct buffer *chk, int chunked)
721{
722 size_t sz = chk->data;
723
724 if (chunked) {
725 uint32_t chksz;
726 char tmp[10];
727 char *beg, *end;
728
729 chksz = data.len;
730
731 beg = end = tmp+10;
732 *--beg = '\n';
733 *--beg = '\r';
734 do {
735 *--beg = hextab[chksz & 0xF];
736 } while (chksz >>= 4);
737
738 if (!chunk_memcat(chk, beg, end - beg) ||
739 !chunk_memcat(chk, data.ptr, data.len) ||
740 !chunk_memcat(chk, "\r\n", 2))
741 goto full;
742 }
743 else {
744 if (!chunk_memcat(chk, data.ptr, data.len))
745 return 0;
746 }
747
748 return 1;
749
750 full:
751 chk->data = sz;
752 return 0;
753}
754
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200755/*
756 * Local variables:
757 * c-indent-level: 8
758 * c-basic-offset: 8
759 * End:
760 */