blob: dfc6b89c9bacd4c6760e301ff7ab29785997e557 [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;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200155
156 /* <h1sl> is always defined for a request */
157 meth = h1sl->rq.m;
158 uri = h1sl->rq.u;
159 vsn = h1sl->rq.v;
160
161 /* Be sure the message, once converted into HTX, will not exceed the max
162 * size allowed.
163 */
164 if (h1_eval_htx_size(meth, uri, vsn, hdrs) > max) {
165 if (htx_is_empty(htx))
166 goto error;
167 h1m_init_res(h1m);
168 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
169 return 0;
170 }
171
172 /* By default, request have always a known length */
173 h1m->flags |= H1_MF_XFER_LEN;
174
175 if (h1sl->rq.meth == HTTP_METH_CONNECT) {
Christopher Faulet5be651d2021-01-22 15:28:03 +0100176 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
177 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200178 }
179
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200180 flags = h1m_htx_sl_flags(h1m);
181 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, uri, vsn);
182 if (!sl || !htx_add_all_headers(htx, hdrs))
183 goto error;
184 sl->info.req.meth = h1sl->rq.meth;
185
Christopher Fauletfe451fb2019-10-08 15:01:34 +0200186 /* Check if the uri contains an authority. Also check if it contains an
187 * explicit scheme and if it is "http" or "https". */
188 if (h1sl->rq.meth == HTTP_METH_CONNECT)
189 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
190 else if (uri.len && uri.ptr[0] != '/' && uri.ptr[0] != '*') {
191 sl->flags |= (HTX_SL_F_HAS_AUTHORITY|HTX_SL_F_HAS_SCHM);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200192 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
193 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
194 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200195
196 /* If body length cannot be determined, set htx->extra to
197 * ULLONG_MAX. This value is impossible in other cases.
198 */
199 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
200
201 end:
202 return 1;
203 error:
204 h1m->err_pos = h1m->next;
205 h1m->err_state = h1m->state;
206 htx->flags |= HTX_FL_PARSING_ERROR;
207 return 0;
208}
209
210/* Postprocess the parsed headers for a response and convert them into an htx
211 * message. It returns the number of bytes parsed if > 0, or 0 if it couldn't
212 * proceed. Parsing errors are reported by setting the htx flag
213 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields.
214 */
215static int h1_postparse_res_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *htx,
216 struct http_hdr *hdrs, size_t max)
217{
218 struct htx_sl *sl;
219 struct ist vsn, status, reason;
220 unsigned int flags;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200221 uint16_t code = 0;
222
223 if (h1sl) {
224 /* For HTTP responses, the start-line was parsed */
225 code = h1sl->st.status;
226 vsn = h1sl->st.v;
227 status = h1sl->st.c;
228 reason = h1sl->st.r;
229 }
230 else {
231 /* For FCGI responses, there is no start(-line but the "Status"
232 * header must be parsed, if found.
233 */
234 int hdr;
235
236 vsn = ((h1m->flags & H1_MF_VER_11) ? ist("HTTP/1.1") : ist("HTTP/1.0"));
237 for (hdr = 0; hdrs[hdr].n.len; hdr++) {
238 if (isteqi(hdrs[hdr].n, ist("status"))) {
239 code = http_parse_status_val(hdrs[hdr].v, &status, &reason);
240 }
241 else if (isteqi(hdrs[hdr].n, ist("location"))) {
242 code = 302;
243 status = ist("302");
244 reason = ist("Moved Temporarily");
245 }
246 }
247 if (!code) {
248 code = 200;
249 status = ist("200");
250 reason = ist("OK");
251 }
252 /* FIXME: Check the codes 1xx ? */
253 }
254
255 /* Be sure the message, once converted into HTX, will not exceed the max
256 * size allowed.
257 */
258 if (h1_eval_htx_size(vsn, status, reason, hdrs) > max) {
259 if (htx_is_empty(htx))
260 goto error;
261 h1m_init_res(h1m);
262 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
263 return 0;
264 }
265
Christopher Fauletc75668e2020-12-07 18:10:32 +0100266 if (((h1m->flags & H1_MF_METH_CONNECT) && code >= 200 && code < 300) || code == 101) {
Christopher Faulet5be651d2021-01-22 15:28:03 +0100267 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
268 h1m->flags |= H1_MF_XFER_LEN;
269 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200270 }
271 else if ((h1m->flags & H1_MF_METH_HEAD) || (code >= 100 && code < 200) ||
272 (code == 204) || (code == 304)) {
273 /* Responses known to have no body. */
274 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
275 h1m->flags |= H1_MF_XFER_LEN;
276 h1m->curr_len = h1m->body_len = 0;
277 }
278 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
279 /* Responses with a known body length. */
280 h1m->flags |= H1_MF_XFER_LEN;
281 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200282
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200283 flags = h1m_htx_sl_flags(h1m);
284 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, vsn, status, reason);
285 if (!sl || !htx_add_all_headers(htx, hdrs))
286 goto error;
287 sl->info.res.status = code;
288
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200289 /* If body length cannot be determined, set htx->extra to
290 * ULLONG_MAX. This value is impossible in other cases.
291 */
292 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
293
294 end:
295 return 1;
296 error:
297 h1m->err_pos = h1m->next;
298 h1m->err_state = h1m->state;
299 htx->flags |= HTX_FL_PARSING_ERROR;
300 return 0;
301}
302
303/* Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
304 * it couldn't proceed. Parsing errors are reported by setting the htx flag
305 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This
306 * functions is responsible to update the parser state <h1m> and the start-line
307 * <h1sl> if not NULL.
308 * For the requests, <h1sl> must always be provided. For responses, <h1sl> may
309 * be NULL and <h1m> flags HTTP_METH_CONNECT of HTTP_METH_HEAD may be set.
310 */
311int h1_parse_msg_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *dsthtx,
312 struct buffer *srcbuf, size_t ofs, size_t max)
313{
314 struct http_hdr hdrs[global.tune.max_http_hdr];
315 int ret = 0;
316
317 if (!max || !b_data(srcbuf))
318 goto end;
319
320 /* Realing input buffer if necessary */
321 if (b_head(srcbuf) + b_data(srcbuf) > b_wrap(srcbuf))
322 b_slow_realign(srcbuf, trash.area, 0);
323
324 if (!h1sl) {
325 /* If there no start-line, be sure to only parse the headers */
326 h1m->flags |= H1_MF_HDRS_ONLY;
327 }
328 ret = h1_headers_to_hdr_list(b_peek(srcbuf, ofs), b_tail(srcbuf),
329 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, h1sl);
330 if (ret <= 0) {
331 /* Incomplete or invalid message. If the input buffer only
332 * contains headers and is full, which is detected by it being
333 * full and the offset to be zero, it's an error because
334 * headers are too large to be handled by the parser. */
335 if (ret < 0 || (!ret && !ofs && !buf_room_for_htx_data(srcbuf)))
336 goto error;
337 goto end;
338 }
339
340 /* messages headers fully parsed, do some checks to prepare the body
341 * parsing.
342 */
343
344 if (!(h1m->flags & H1_MF_RESP)) {
345 if (!h1_process_req_vsn(h1m, h1sl)) {
346 h1m->err_pos = h1sl->rq.v.ptr - b_head(srcbuf);
347 h1m->err_state = h1m->state;
348 goto vsn_error;
349 }
350 if (!h1_postparse_req_hdrs(h1m, h1sl, dsthtx, hdrs, max))
351 ret = 0;
352 }
353 else {
354 if (h1sl && !h1_process_res_vsn(h1m, h1sl)) {
355 h1m->err_pos = h1sl->st.v.ptr - b_head(srcbuf);
356 h1m->err_state = h1m->state;
357 goto vsn_error;
358 }
359 if (!h1_postparse_res_hdrs(h1m, h1sl, dsthtx, hdrs, max))
360 ret = 0;
361 }
362
Christopher Faulet76014fd2019-12-10 11:47:22 +0100363 /* Switch messages without any payload to DONE state */
364 if (((h1m->flags & H1_MF_CLEN) && h1m->body_len == 0) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100365 ((h1m->flags & (H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK)) == H1_MF_XFER_LEN)) {
Christopher Faulet76014fd2019-12-10 11:47:22 +0100366 h1m->state = H1_MSG_DONE;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100367 dsthtx->flags |= HTX_FL_EOM;
368 }
Christopher Faulet76014fd2019-12-10 11:47:22 +0100369
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200370 end:
371 return ret;
372 error:
373 h1m->err_pos = h1m->next;
374 h1m->err_state = h1m->state;
375 vsn_error:
376 dsthtx->flags |= HTX_FL_PARSING_ERROR;
377 return 0;
378
379}
380
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200381/* Copy data from <srbuf> into an DATA block in <dsthtx>. If possible, a
382 * zero-copy is performed. It returns the number of bytes copied.
383 */
Christopher Fauletaf542632019-10-01 21:52:49 +0200384static int h1_copy_msg_data(struct htx **dsthtx, struct buffer *srcbuf, size_t ofs,
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200385 size_t count, struct buffer *htxbuf)
386{
Christopher Fauletaf542632019-10-01 21:52:49 +0200387 struct htx *tmp_htx = *dsthtx;
388
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200389 /* very often with large files we'll face the following
390 * situation :
391 * - htx is empty and points to <htxbuf>
392 * - ret == srcbuf->data
393 * - srcbuf->head == sizeof(struct htx)
394 * => we can swap the buffers and place an htx header into
395 * the target buffer instead
396 */
Christopher Fauletaf542632019-10-01 21:52:49 +0200397 if (unlikely(htx_is_empty(tmp_htx) && count == b_data(srcbuf) &&
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200398 !ofs && b_head_ofs(srcbuf) == sizeof(struct htx))) {
399 void *raw_area = srcbuf->area;
400 void *htx_area = htxbuf->area;
401 struct htx_blk *blk;
402
403 srcbuf->area = htx_area;
404 htxbuf->area = raw_area;
Christopher Fauletaf542632019-10-01 21:52:49 +0200405 tmp_htx = (struct htx *)htxbuf->area;
406 tmp_htx->size = htxbuf->size - sizeof(*tmp_htx);
407 htx_reset(tmp_htx);
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200408 b_set_data(htxbuf, b_size(htxbuf));
409
Christopher Fauletaf542632019-10-01 21:52:49 +0200410 blk = htx_add_blk(tmp_htx, HTX_BLK_DATA, count);
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200411 blk->info += count;
Christopher Fauletaf542632019-10-01 21:52:49 +0200412
413 *dsthtx = tmp_htx;
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200414 /* nothing else to do, the old buffer now contains an
415 * empty pre-initialized HTX header
416 */
417 return count;
418 }
419
Christopher Fauletaf542632019-10-01 21:52:49 +0200420 return htx_add_data(*dsthtx, ist2(b_peek(srcbuf, ofs), count));
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200421}
422
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200423/* Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
424 * couldn't proceed. Parsing errors are reported by setting the htx flags
425 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This
426 * functions is responsible to update the parser state <h1m>.
427 */
Christopher Fauletaf542632019-10-01 21:52:49 +0200428int h1_parse_msg_data(struct h1m *h1m, struct htx **dsthtx,
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200429 struct buffer *srcbuf, size_t ofs, size_t max,
430 struct buffer *htxbuf)
431{
432 size_t total = 0;
433 int32_t ret = 0;
434
435 if (h1m->flags & H1_MF_CLEN) {
436 /* content-length: read only h2m->body_len */
Christopher Fauletaf542632019-10-01 21:52:49 +0200437 ret = htx_get_max_blksz(*dsthtx, max);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200438 if ((uint64_t)ret > h1m->curr_len)
439 ret = h1m->curr_len;
440 if (ret > b_contig_data(srcbuf, ofs))
441 ret = b_contig_data(srcbuf, ofs);
442 if (ret) {
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200443 int32_t try = ret;
444
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200445 ret = h1_copy_msg_data(dsthtx, srcbuf, ofs, try, htxbuf);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200446 h1m->curr_len -= ret;
447 max -= sizeof(struct htx_blk) + ret;
448 ofs += ret;
449 total += ret;
450 if (ret < try)
451 goto end;
452 }
453
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100454 if (!h1m->curr_len) {
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200455 h1m->state = H1_MSG_DONE;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100456 (*dsthtx)->flags |= HTX_FL_EOM;
457 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200458 }
459 else if (h1m->flags & H1_MF_CHNK) {
460 /* te:chunked : parse chunks */
461 new_chunk:
462 if (h1m->state == H1_MSG_CHUNK_CRLF) {
463 ret = h1_skip_chunk_crlf(srcbuf, ofs, b_data(srcbuf));
464 if (ret <= 0)
465 goto end;
466 h1m->state = H1_MSG_CHUNK_SIZE;
467 ofs += ret;
468 total += ret;
469 }
470 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Christopher Faulet405f0542021-01-27 15:17:13 +0100471 uint64_t chksz;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200472
473 ret = h1_parse_chunk_size(srcbuf, ofs, b_data(srcbuf), &chksz);
474 if (ret <= 0)
475 goto end;
476 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
477 h1m->curr_len = chksz;
478 h1m->body_len += chksz;
479 ofs += ret;
480 total += ret;
481 if (!h1m->curr_len)
482 goto end;
483 }
484 if (h1m->state == H1_MSG_DATA) {
Christopher Fauletaf542632019-10-01 21:52:49 +0200485 ret = htx_get_max_blksz(*dsthtx, max);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200486 if ((uint64_t)ret > h1m->curr_len)
487 ret = h1m->curr_len;
488 if (ret > b_contig_data(srcbuf, ofs))
489 ret = b_contig_data(srcbuf, ofs);
490 if (ret) {
491 int32_t try = ret;
492
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200493 ret = h1_copy_msg_data(dsthtx, srcbuf, ofs, try, htxbuf);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200494 h1m->curr_len -= ret;
495 max -= sizeof(struct htx_blk) + ret;
496 ofs += ret;
497 total += ret;
498 if (ret < try)
499 goto end;
500 }
501 if (!h1m->curr_len) {
502 h1m->state = H1_MSG_CHUNK_CRLF;
503 goto new_chunk;
504 }
505 goto end;
506 }
507 }
508 else if (h1m->flags & H1_MF_XFER_LEN) {
509 /* XFER_LEN is set but not CLEN nor CHNK, it means there is no
510 * body. Switch the message in DONE state
511 */
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200512 h1m->state = H1_MSG_DONE;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100513 (*dsthtx)->flags |= HTX_FL_EOM;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200514 }
515 else {
516 /* no content length, read till SHUTW */
Christopher Fauletaf542632019-10-01 21:52:49 +0200517 ret = htx_get_max_blksz(*dsthtx, max);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200518 if (ret > b_contig_data(srcbuf, ofs))
519 ret = b_contig_data(srcbuf, ofs);
520 if (ret)
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200521 total += h1_copy_msg_data(dsthtx, srcbuf, ofs, ret, htxbuf);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200522 }
523
524 end:
525 if (ret < 0) {
Christopher Fauletaf542632019-10-01 21:52:49 +0200526 (*dsthtx)->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200527 h1m->err_state = h1m->state;
528 h1m->err_pos = ofs;
529 total = 0;
530 }
531
532 /* update htx->extra, only when the body length is known */
533 if (h1m->flags & H1_MF_XFER_LEN)
Christopher Fauletaf542632019-10-01 21:52:49 +0200534 (*dsthtx)->extra = h1m->curr_len;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200535 return total;
536}
537
538/* Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
539 * it couldn't proceed. Parsing errors are reported by setting the htx flags
540 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This
541 * functions is responsible to update the parser state <h1m>.
542 */
543int h1_parse_msg_tlrs(struct h1m *h1m, struct htx *dsthtx,
544 struct buffer *srcbuf, size_t ofs, size_t max)
545{
546 struct http_hdr hdrs[global.tune.max_http_hdr];
547 struct h1m tlr_h1m;
548 int ret = 0;
549
550 if (!max || !b_data(srcbuf))
551 goto end;
552
553 /* Realing input buffer if necessary */
554 if (b_peek(srcbuf, ofs) > b_tail(srcbuf))
555 b_slow_realign(srcbuf, trash.area, 0);
556
557 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
558 ret = h1_headers_to_hdr_list(b_peek(srcbuf, ofs), b_tail(srcbuf),
559 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
560 if (ret <= 0) {
561 /* Incomplete or invalid trailers. If the input buffer only
562 * contains trailers and is full, which is detected by it being
563 * full and the offset to be zero, it's an error because
564 * trailers are too large to be handled by the parser. */
565 if (ret < 0 || (!ret && !ofs && !buf_room_for_htx_data(srcbuf)))
566 goto error;
567 goto end;
568 }
569
570 /* messages trailers fully parsed. */
571 if (h1_eval_htx_hdrs_size(hdrs) > max) {
572 if (htx_is_empty(dsthtx))
573 goto error;
574 ret = 0;
575 goto end;
576 }
577
578 if (!htx_add_all_trailers(dsthtx, hdrs))
579 goto error;
580
Christopher Faulet76014fd2019-12-10 11:47:22 +0100581 h1m->state = H1_MSG_DONE;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100582 dsthtx->flags |= HTX_FL_EOM;
Christopher Faulet76014fd2019-12-10 11:47:22 +0100583
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200584 end:
585 return ret;
586 error:
587 h1m->err_state = h1m->state;
588 h1m->err_pos = h1m->next;
589 dsthtx->flags |= HTX_FL_PARSING_ERROR;
590 return 0;
591}
592
Christopher Faulet53a899b2019-10-08 16:38:42 +0200593/* Appends the H1 representation of the request line <sl> to the chunk <chk>. It
594 * returns 1 if data are successfully appended, otherwise it returns 0.
595 */
596int h1_format_htx_reqline(const struct htx_sl *sl, struct buffer *chk)
597{
598 struct ist uri;
599 size_t sz = chk->data;
600
Christopher Fauletfb38c912021-04-26 09:38:55 +0200601 uri = h1_get_uri(sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +0200602 if (!chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)) ||
603 !chunk_memcat(chk, " ", 1) ||
604 !chunk_memcat(chk, uri.ptr, uri.len) ||
605 !chunk_memcat(chk, " ", 1))
606 goto full;
607
608 if (sl->flags & HTX_SL_F_VER_11) {
609 if (!chunk_memcat(chk, "HTTP/1.1", 8))
610 goto full;
611 }
612 else {
613 if (!chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)))
614 goto full;
615 }
616
617 if (!chunk_memcat(chk, "\r\n", 2))
618 goto full;
619
620 return 1;
621
622 full:
623 chk->data = sz;
624 return 0;
625}
626
627/* Appends the H1 representation of the status line <sl> to the chunk <chk>. It
628 * returns 1 if data are successfully appended, otherwise it returns 0.
629 */
630int h1_format_htx_stline(const struct htx_sl *sl, struct buffer *chk)
631{
632 size_t sz = chk->data;
633
634 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
635 return 0;
636
637 if (sl->flags & HTX_SL_F_VER_11) {
638 if (!chunk_memcat(chk, "HTTP/1.1", 8))
639 goto full;
640 }
641 else {
642 if (!chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)))
643 goto full;
644 }
645 if (!chunk_memcat(chk, " ", 1) ||
646 !chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)) ||
647 !chunk_memcat(chk, " ", 1) ||
648 !chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)) ||
649 !chunk_memcat(chk, "\r\n", 2))
650 goto full;
651
652 return 1;
653
654 full:
655 chk->data = sz;
656 return 0;
657}
658
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500659/* Appends the H1 representation of the header <n> with the value <v> to the
Christopher Faulet53a899b2019-10-08 16:38:42 +0200660 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
661 * returns 0.
662 */
663int h1_format_htx_hdr(const struct ist n, const struct ist v, struct buffer *chk)
664{
665 size_t sz = chk->data;
666
667 if (n.len + v.len + 4 > b_room(chk))
668 return 0;
669
670 if (!chunk_memcat(chk, n.ptr, n.len) ||
671 !chunk_memcat(chk, ": ", 2) ||
672 !chunk_memcat(chk, v.ptr, v.len) ||
673 !chunk_memcat(chk, "\r\n", 2))
674 goto full;
675
676 return 1;
677
678 full:
679 chk->data = sz;
680 return 0;
681}
682
683/* Appends the H1 representation of the data <data> to the chunk <chk>. If
684 * <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It returns 1 if
685 * data are successfully appended, otherwise it returns 0.
686 */
687int h1_format_htx_data(const struct ist data, struct buffer *chk, int chunked)
688{
689 size_t sz = chk->data;
690
691 if (chunked) {
692 uint32_t chksz;
693 char tmp[10];
694 char *beg, *end;
695
696 chksz = data.len;
697
698 beg = end = tmp+10;
699 *--beg = '\n';
700 *--beg = '\r';
701 do {
702 *--beg = hextab[chksz & 0xF];
703 } while (chksz >>= 4);
704
705 if (!chunk_memcat(chk, beg, end - beg) ||
706 !chunk_memcat(chk, data.ptr, data.len) ||
707 !chunk_memcat(chk, "\r\n", 2))
708 goto full;
709 }
710 else {
711 if (!chunk_memcat(chk, data.ptr, data.len))
712 return 0;
713 }
714
715 return 1;
716
717 full:
718 chk->data = sz;
719 return 0;
720}
721
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200722/*
723 * Local variables:
724 * c-indent-level: 8
725 * c-basic-offset: 8
726 * End:
727 */