blob: 24769f077b183850b408475a0ee5257d18a7f3f9 [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>
Amaury Denoyellec00d8142021-07-07 10:49:27 +020019#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020020#include <haproxy/htx.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020021#include <haproxy/tools.h>
Christopher Faulet4f0f88a2019-08-10 11:17:44 +020022
Christopher Faulet4f0f88a2019-08-10 11:17:44 +020023/* Estimate the size of the HTX headers after the parsing, including the EOH. */
24static size_t h1_eval_htx_hdrs_size(const struct http_hdr *hdrs)
25{
26 size_t sz = 0;
27 int i;
28
29 for (i = 0; hdrs[i].n.len; i++)
30 sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len;
31 sz += sizeof(struct htx_blk) + 1;
32 return sz;
33}
34
35/* Estimate the size of the HTX request after the parsing. */
36static size_t h1_eval_htx_size(const struct ist p1, const struct ist p2, const struct ist p3,
37 const struct http_hdr *hdrs)
38{
39 size_t sz;
40
41 /* size of the HTX start-line */
42 sz = sizeof(struct htx_blk) + sizeof(struct htx_sl) + p1.len + p2.len + p3.len;
43 sz += h1_eval_htx_hdrs_size(hdrs);
44 return sz;
45}
46
Christopher Faulet4f0f88a2019-08-10 11:17:44 +020047/* Check the validity of the request version. If the version is valid, it
48 * returns 1. Otherwise, it returns 0.
49 */
50static int h1_process_req_vsn(struct h1m *h1m, union h1_sl *sl)
51{
52 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
53 * exactly one digit "." one digit. This check may be disabled using
54 * option accept-invalid-http-request.
55 */
56 if (h1m->err_pos == -2) { /* PR_O2_REQBUG_OK not set */
57 if (sl->rq.v.len != 8)
58 return 0;
59
Tim Duesterhus47cbeb82020-03-10 00:55:40 +010060 if (!istnmatch(sl->rq.v, ist("HTTP/"), 5) ||
Christopher Faulet4f0f88a2019-08-10 11:17:44 +020061 !isdigit((unsigned char)*(sl->rq.v.ptr + 5)) ||
62 *(sl->rq.v.ptr + 6) != '.' ||
63 !isdigit((unsigned char)*(sl->rq.v.ptr + 7)))
64 return 0;
65 }
66 else if (!sl->rq.v.len) {
67 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
68
69 /* RFC 1945 allows only GET for HTTP/0.9 requests */
70 if (sl->rq.meth != HTTP_METH_GET)
71 return 0;
72
73 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
74 if (!sl->rq.u.len)
75 return 0;
76
77 /* Add HTTP version */
78 sl->rq.v = ist("HTTP/1.0");
79 return 1;
80 }
81
82 if ((sl->rq.v.len == 8) &&
83 ((*(sl->rq.v.ptr + 5) > '1') ||
84 ((*(sl->rq.v.ptr + 5) == '1') && (*(sl->rq.v.ptr + 7) >= '1'))))
85 h1m->flags |= H1_MF_VER_11;
86 return 1;
87}
88
89/* Check the validity of the response version. If the version is valid, it
90 * returns 1. Otherwise, it returns 0.
91 */
92static int h1_process_res_vsn(struct h1m *h1m, union h1_sl *sl)
93{
94 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
95 * exactly one digit "." one digit. This check may be disabled using
96 * option accept-invalid-http-request.
97 */
98 if (h1m->err_pos == -2) { /* PR_O2_REQBUG_OK not set */
99 if (sl->st.v.len != 8)
100 return 0;
101
102 if (*(sl->st.v.ptr + 4) != '/' ||
103 !isdigit((unsigned char)*(sl->st.v.ptr + 5)) ||
104 *(sl->st.v.ptr + 6) != '.' ||
105 !isdigit((unsigned char)*(sl->st.v.ptr + 7)))
106 return 0;
107 }
108
109 if ((sl->st.v.len == 8) &&
110 ((*(sl->st.v.ptr + 5) > '1') ||
111 ((*(sl->st.v.ptr + 5) == '1') && (*(sl->st.v.ptr + 7) >= '1'))))
112 h1m->flags |= H1_MF_VER_11;
113
114 return 1;
115}
116
117/* Convert H1M flags to HTX start-line flags. */
118static unsigned int h1m_htx_sl_flags(struct h1m *h1m)
119{
120 unsigned int flags = HTX_SL_F_NONE;
121
122 if (h1m->flags & H1_MF_RESP)
123 flags |= HTX_SL_F_IS_RESP;
124 if (h1m->flags & H1_MF_VER_11)
125 flags |= HTX_SL_F_VER_11;
126 if (h1m->flags & H1_MF_XFER_ENC)
127 flags |= HTX_SL_F_XFER_ENC;
128 if (h1m->flags & H1_MF_XFER_LEN) {
129 flags |= HTX_SL_F_XFER_LEN;
130 if (h1m->flags & H1_MF_CHNK)
131 flags |= HTX_SL_F_CHNK;
132 else if (h1m->flags & H1_MF_CLEN) {
133 flags |= HTX_SL_F_CLEN;
134 if (h1m->body_len == 0)
135 flags |= HTX_SL_F_BODYLESS;
136 }
137 else
138 flags |= HTX_SL_F_BODYLESS;
139 }
Christopher Faulet576c3582021-01-08 15:53:01 +0100140 if (h1m->flags & H1_MF_CONN_UPG)
141 flags |= HTX_SL_F_CONN_UPG;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200142 return flags;
143}
144
145/* Postprocess the parsed headers for a request and convert them into an htx
146 * message. It returns the number of bytes parsed if > 0, or 0 if it couldn't
147 * proceed. Parsing errors are reported by setting the htx flag
148 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields.
149 */
150static int h1_postparse_req_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *htx,
151 struct http_hdr *hdrs, size_t max)
152{
153 struct htx_sl *sl;
154 struct ist meth, uri, vsn;
155 unsigned int flags;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200156
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;
Christopher Faulet58f21da2021-09-20 07:47:27 +0200168 goto output_full;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200169 }
170
171 /* By default, request have always a known length */
172 h1m->flags |= H1_MF_XFER_LEN;
173
174 if (h1sl->rq.meth == HTTP_METH_CONNECT) {
Christopher Faulet5be651d2021-01-22 15:28:03 +0100175 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
176 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200177 }
178
Christopher Faulet673504a2021-09-09 09:52:51 +0200179
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200180 flags = h1m_htx_sl_flags(h1m);
Christopher Faulet673504a2021-09-09 09:52:51 +0200181 if ((flags & (HTX_SL_F_CONN_UPG|HTX_SL_F_BODYLESS)) == HTX_SL_F_CONN_UPG) {
182 int i;
183
184 for (i = 0; hdrs[i].n.len; i++) {
185 if (isteqi(hdrs[i].n, ist("upgrade")))
186 hdrs[i].v = IST_NULL;
187 }
188 h1m->flags &=~ H1_MF_CONN_UPG;
189 flags &= ~HTX_SL_F_CONN_UPG;
190 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200191 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, uri, vsn);
192 if (!sl || !htx_add_all_headers(htx, hdrs))
193 goto error;
194 sl->info.req.meth = h1sl->rq.meth;
195
Christopher Fauletfe451fb2019-10-08 15:01:34 +0200196 /* Check if the uri contains an authority. Also check if it contains an
197 * explicit scheme and if it is "http" or "https". */
198 if (h1sl->rq.meth == HTTP_METH_CONNECT)
199 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
200 else if (uri.len && uri.ptr[0] != '/' && uri.ptr[0] != '*') {
201 sl->flags |= (HTX_SL_F_HAS_AUTHORITY|HTX_SL_F_HAS_SCHM);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200202 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
203 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
Amaury Denoyellec00d8142021-07-07 10:49:27 +0200204
205 /* absolute-form target URI present, proceed to scheme-based
206 * normalization */
207 http_scheme_based_normalize(htx);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200208 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200209
210 /* If body length cannot be determined, set htx->extra to
211 * ULLONG_MAX. This value is impossible in other cases.
212 */
213 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
214
215 end:
216 return 1;
Christopher Faulet58f21da2021-09-20 07:47:27 +0200217 output_full:
218 h1m_init_req(h1m);
219 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
220 return -2;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200221 error:
222 h1m->err_pos = h1m->next;
223 h1m->err_state = h1m->state;
224 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet58f21da2021-09-20 07:47:27 +0200225 return -1;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200226}
227
228/* Postprocess the parsed headers for a response and convert them into an htx
229 * message. It returns the number of bytes parsed if > 0, or 0 if it couldn't
230 * proceed. Parsing errors are reported by setting the htx flag
231 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields.
232 */
233static int h1_postparse_res_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *htx,
234 struct http_hdr *hdrs, size_t max)
235{
236 struct htx_sl *sl;
237 struct ist vsn, status, reason;
238 unsigned int flags;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200239 uint16_t code = 0;
240
241 if (h1sl) {
242 /* For HTTP responses, the start-line was parsed */
243 code = h1sl->st.status;
244 vsn = h1sl->st.v;
245 status = h1sl->st.c;
246 reason = h1sl->st.r;
247 }
248 else {
249 /* For FCGI responses, there is no start(-line but the "Status"
250 * header must be parsed, if found.
251 */
252 int hdr;
253
254 vsn = ((h1m->flags & H1_MF_VER_11) ? ist("HTTP/1.1") : ist("HTTP/1.0"));
255 for (hdr = 0; hdrs[hdr].n.len; hdr++) {
256 if (isteqi(hdrs[hdr].n, ist("status"))) {
257 code = http_parse_status_val(hdrs[hdr].v, &status, &reason);
258 }
259 else if (isteqi(hdrs[hdr].n, ist("location"))) {
260 code = 302;
261 status = ist("302");
Christopher Faulet79ad9072023-07-20 09:50:56 +0200262 reason = ist("Found");
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200263 }
264 }
265 if (!code) {
266 code = 200;
267 status = ist("200");
268 reason = ist("OK");
269 }
270 /* FIXME: Check the codes 1xx ? */
271 }
272
273 /* Be sure the message, once converted into HTX, will not exceed the max
274 * size allowed.
275 */
276 if (h1_eval_htx_size(vsn, status, reason, hdrs) > max) {
277 if (htx_is_empty(htx))
278 goto error;
Christopher Faulet58f21da2021-09-20 07:47:27 +0200279 goto output_full;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200280 }
281
Christopher Fauletf8c16512023-01-10 18:51:55 +0100282 if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) && code != 101)
283 h1m->flags &= ~(H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET);
284
Christopher Fauletc75668e2020-12-07 18:10:32 +0100285 if (((h1m->flags & H1_MF_METH_CONNECT) && code >= 200 && code < 300) || code == 101) {
Christopher Faulet5be651d2021-01-22 15:28:03 +0100286 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
287 h1m->flags |= H1_MF_XFER_LEN;
288 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200289 }
290 else if ((h1m->flags & H1_MF_METH_HEAD) || (code >= 100 && code < 200) ||
291 (code == 204) || (code == 304)) {
292 /* Responses known to have no body. */
293 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
294 h1m->flags |= H1_MF_XFER_LEN;
295 h1m->curr_len = h1m->body_len = 0;
296 }
297 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
298 /* Responses with a known body length. */
299 h1m->flags |= H1_MF_XFER_LEN;
300 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200301
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200302 flags = h1m_htx_sl_flags(h1m);
303 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, vsn, status, reason);
304 if (!sl || !htx_add_all_headers(htx, hdrs))
305 goto error;
306 sl->info.res.status = code;
307
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200308 /* If body length cannot be determined, set htx->extra to
309 * ULLONG_MAX. This value is impossible in other cases.
310 */
311 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
312
313 end:
314 return 1;
Christopher Faulet58f21da2021-09-20 07:47:27 +0200315 output_full:
316 h1m_init_res(h1m);
317 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
318 return -2;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200319 error:
320 h1m->err_pos = h1m->next;
321 h1m->err_state = h1m->state;
322 htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet58f21da2021-09-20 07:47:27 +0200323 return -1;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200324}
325
Christopher Faulet58f21da2021-09-20 07:47:27 +0200326/* Parse HTTP/1 headers. It returns the number of bytes parsed on success, 0 if
327 * headers are incomplete, -1 if an error occurred or -2 if it needs more space
328 * to proceed while the output buffer is not empty. Parsing errors are reported
329 * by setting the htx flag HTX_FL_PARSING_ERROR and filling h1m->err_pos and
330 * h1m->err_state fields. This functions is responsible to update the parser
331 * state <h1m> and the start-line <h1sl> if not NULL. For the requests, <h1sl>
332 * must always be provided. For responses, <h1sl> may be NULL and <h1m> flags
333 * HTTP_METH_CONNECT of HTTP_METH_HEAD may be set.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200334 */
335int h1_parse_msg_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *dsthtx,
Christopher Faulet58f21da2021-09-20 07:47:27 +0200336 struct buffer *srcbuf, size_t ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200337{
338 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet58f21da2021-09-20 07:47:27 +0200339 int total = 0, ret = 0;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200340
341 if (!max || !b_data(srcbuf))
342 goto end;
343
344 /* Realing input buffer if necessary */
345 if (b_head(srcbuf) + b_data(srcbuf) > b_wrap(srcbuf))
346 b_slow_realign(srcbuf, trash.area, 0);
347
348 if (!h1sl) {
349 /* If there no start-line, be sure to only parse the headers */
350 h1m->flags |= H1_MF_HDRS_ONLY;
351 }
352 ret = h1_headers_to_hdr_list(b_peek(srcbuf, ofs), b_tail(srcbuf),
353 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, h1sl);
354 if (ret <= 0) {
355 /* Incomplete or invalid message. If the input buffer only
356 * contains headers and is full, which is detected by it being
357 * full and the offset to be zero, it's an error because
358 * headers are too large to be handled by the parser. */
359 if (ret < 0 || (!ret && !ofs && !buf_room_for_htx_data(srcbuf)))
360 goto error;
361 goto end;
362 }
Christopher Faulet58f21da2021-09-20 07:47:27 +0200363 total = ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200364
365 /* messages headers fully parsed, do some checks to prepare the body
366 * parsing.
367 */
368
369 if (!(h1m->flags & H1_MF_RESP)) {
370 if (!h1_process_req_vsn(h1m, h1sl)) {
371 h1m->err_pos = h1sl->rq.v.ptr - b_head(srcbuf);
372 h1m->err_state = h1m->state;
373 goto vsn_error;
374 }
Christopher Faulet58f21da2021-09-20 07:47:27 +0200375 ret = h1_postparse_req_hdrs(h1m, h1sl, dsthtx, hdrs, max);
376 if (ret < 0)
377 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200378 }
379 else {
380 if (h1sl && !h1_process_res_vsn(h1m, h1sl)) {
381 h1m->err_pos = h1sl->st.v.ptr - b_head(srcbuf);
382 h1m->err_state = h1m->state;
383 goto vsn_error;
384 }
Christopher Faulet58f21da2021-09-20 07:47:27 +0200385 ret = h1_postparse_res_hdrs(h1m, h1sl, dsthtx, hdrs, max);
386 if (ret < 0)
387 return ret;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200388 }
389
Christopher Faulet76014fd2019-12-10 11:47:22 +0100390 /* Switch messages without any payload to DONE state */
391 if (((h1m->flags & H1_MF_CLEN) && h1m->body_len == 0) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100392 ((h1m->flags & (H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK)) == H1_MF_XFER_LEN)) {
Christopher Faulet76014fd2019-12-10 11:47:22 +0100393 h1m->state = H1_MSG_DONE;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100394 dsthtx->flags |= HTX_FL_EOM;
395 }
Christopher Faulet76014fd2019-12-10 11:47:22 +0100396
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200397 end:
Christopher Faulet58f21da2021-09-20 07:47:27 +0200398 return total;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200399 error:
400 h1m->err_pos = h1m->next;
401 h1m->err_state = h1m->state;
402 vsn_error:
403 dsthtx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet58f21da2021-09-20 07:47:27 +0200404 return -1;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200405
406}
407
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200408/* Copy data from <srbuf> into an DATA block in <dsthtx>. If possible, a
409 * zero-copy is performed. It returns the number of bytes copied.
410 */
Christopher Fauletaf542632019-10-01 21:52:49 +0200411static int h1_copy_msg_data(struct htx **dsthtx, struct buffer *srcbuf, size_t ofs,
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200412 size_t count, struct buffer *htxbuf)
413{
Christopher Fauletaf542632019-10-01 21:52:49 +0200414 struct htx *tmp_htx = *dsthtx;
415
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200416 /* very often with large files we'll face the following
417 * situation :
418 * - htx is empty and points to <htxbuf>
419 * - ret == srcbuf->data
420 * - srcbuf->head == sizeof(struct htx)
421 * => we can swap the buffers and place an htx header into
422 * the target buffer instead
423 */
Christopher Fauletaf542632019-10-01 21:52:49 +0200424 if (unlikely(htx_is_empty(tmp_htx) && count == b_data(srcbuf) &&
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200425 !ofs && b_head_ofs(srcbuf) == sizeof(struct htx))) {
426 void *raw_area = srcbuf->area;
427 void *htx_area = htxbuf->area;
428 struct htx_blk *blk;
429
430 srcbuf->area = htx_area;
431 htxbuf->area = raw_area;
Christopher Fauletaf542632019-10-01 21:52:49 +0200432 tmp_htx = (struct htx *)htxbuf->area;
433 tmp_htx->size = htxbuf->size - sizeof(*tmp_htx);
434 htx_reset(tmp_htx);
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200435 b_set_data(htxbuf, b_size(htxbuf));
436
Christopher Fauletaf542632019-10-01 21:52:49 +0200437 blk = htx_add_blk(tmp_htx, HTX_BLK_DATA, count);
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200438 blk->info += count;
Christopher Fauletaf542632019-10-01 21:52:49 +0200439
440 *dsthtx = tmp_htx;
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200441 /* nothing else to do, the old buffer now contains an
442 * empty pre-initialized HTX header
443 */
444 return count;
445 }
446
Christopher Fauletaf542632019-10-01 21:52:49 +0200447 return htx_add_data(*dsthtx, ist2(b_peek(srcbuf, ofs), count));
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200448}
449
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200450/* Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
451 * couldn't proceed. Parsing errors are reported by setting the htx flags
452 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This
453 * functions is responsible to update the parser state <h1m>.
454 */
Christopher Fauletaf542632019-10-01 21:52:49 +0200455int h1_parse_msg_data(struct h1m *h1m, struct htx **dsthtx,
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200456 struct buffer *srcbuf, size_t ofs, size_t max,
457 struct buffer *htxbuf)
458{
459 size_t total = 0;
460 int32_t ret = 0;
461
462 if (h1m->flags & H1_MF_CLEN) {
463 /* content-length: read only h2m->body_len */
Christopher Fauletaf542632019-10-01 21:52:49 +0200464 ret = htx_get_max_blksz(*dsthtx, max);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200465 if ((uint64_t)ret > h1m->curr_len)
466 ret = h1m->curr_len;
467 if (ret > b_contig_data(srcbuf, ofs))
468 ret = b_contig_data(srcbuf, ofs);
469 if (ret) {
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200470 int32_t try = ret;
471
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200472 ret = h1_copy_msg_data(dsthtx, srcbuf, ofs, try, htxbuf);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200473 h1m->curr_len -= ret;
474 max -= sizeof(struct htx_blk) + ret;
475 ofs += ret;
476 total += ret;
477 if (ret < try)
478 goto end;
479 }
480
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100481 if (!h1m->curr_len) {
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200482 h1m->state = H1_MSG_DONE;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100483 (*dsthtx)->flags |= HTX_FL_EOM;
484 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200485 }
486 else if (h1m->flags & H1_MF_CHNK) {
487 /* te:chunked : parse chunks */
488 new_chunk:
489 if (h1m->state == H1_MSG_CHUNK_CRLF) {
490 ret = h1_skip_chunk_crlf(srcbuf, ofs, b_data(srcbuf));
491 if (ret <= 0)
492 goto end;
493 h1m->state = H1_MSG_CHUNK_SIZE;
494 ofs += ret;
495 total += ret;
496 }
497 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Christopher Faulet405f0542021-01-27 15:17:13 +0100498 uint64_t chksz;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200499
500 ret = h1_parse_chunk_size(srcbuf, ofs, b_data(srcbuf), &chksz);
501 if (ret <= 0)
502 goto end;
503 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
504 h1m->curr_len = chksz;
505 h1m->body_len += chksz;
506 ofs += ret;
507 total += ret;
508 if (!h1m->curr_len)
509 goto end;
510 }
511 if (h1m->state == H1_MSG_DATA) {
Christopher Fauletaf542632019-10-01 21:52:49 +0200512 ret = htx_get_max_blksz(*dsthtx, max);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200513 if ((uint64_t)ret > h1m->curr_len)
514 ret = h1m->curr_len;
515 if (ret > b_contig_data(srcbuf, ofs))
516 ret = b_contig_data(srcbuf, ofs);
517 if (ret) {
518 int32_t try = ret;
519
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200520 ret = h1_copy_msg_data(dsthtx, srcbuf, ofs, try, htxbuf);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200521 h1m->curr_len -= ret;
522 max -= sizeof(struct htx_blk) + ret;
523 ofs += ret;
524 total += ret;
525 if (ret < try)
526 goto end;
527 }
528 if (!h1m->curr_len) {
529 h1m->state = H1_MSG_CHUNK_CRLF;
530 goto new_chunk;
531 }
532 goto end;
533 }
534 }
535 else if (h1m->flags & H1_MF_XFER_LEN) {
536 /* XFER_LEN is set but not CLEN nor CHNK, it means there is no
537 * body. Switch the message in DONE state
538 */
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200539 h1m->state = H1_MSG_DONE;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100540 (*dsthtx)->flags |= HTX_FL_EOM;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200541 }
542 else {
543 /* no content length, read till SHUTW */
Christopher Fauletaf542632019-10-01 21:52:49 +0200544 ret = htx_get_max_blksz(*dsthtx, max);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200545 if (ret > b_contig_data(srcbuf, ofs))
546 ret = b_contig_data(srcbuf, ofs);
547 if (ret)
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200548 total += h1_copy_msg_data(dsthtx, srcbuf, ofs, ret, htxbuf);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200549 }
550
551 end:
552 if (ret < 0) {
Christopher Fauletaf542632019-10-01 21:52:49 +0200553 (*dsthtx)->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200554 h1m->err_state = h1m->state;
555 h1m->err_pos = ofs;
556 total = 0;
557 }
558
559 /* update htx->extra, only when the body length is known */
560 if (h1m->flags & H1_MF_XFER_LEN)
Christopher Fauletaf542632019-10-01 21:52:49 +0200561 (*dsthtx)->extra = h1m->curr_len;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200562 return total;
563}
564
Christopher Faulet58f21da2021-09-20 07:47:27 +0200565/* Parse HTTP/1 trailers. It returns the number of bytes parsed on success, 0 if
566 * trailers are incomplete, -1 if an error occurred or -2 if it needs more space
567 * to proceed while the output buffer is not empty. Parsing errors are reported
568 * by setting the htx flags HTX_FL_PARSING_ERROR and filling h1m->err_pos and
569 * h1m->err_state fields. This functions is responsible to update the parser
570 * state <h1m>.
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200571 */
572int h1_parse_msg_tlrs(struct h1m *h1m, struct htx *dsthtx,
573 struct buffer *srcbuf, size_t ofs, size_t max)
574{
575 struct http_hdr hdrs[global.tune.max_http_hdr];
576 struct h1m tlr_h1m;
577 int ret = 0;
578
Christopher Faulet5d0efb72022-04-13 17:48:54 +0200579 if (b_data(srcbuf) == ofs) {
580 /* Nothing to parse */
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200581 goto end;
Christopher Faulet5d0efb72022-04-13 17:48:54 +0200582 }
583 if (!max) {
584 /* No more room */
585 goto output_full;
586 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200587
588 /* Realing input buffer if necessary */
589 if (b_peek(srcbuf, ofs) > b_tail(srcbuf))
590 b_slow_realign(srcbuf, trash.area, 0);
591
592 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
593 ret = h1_headers_to_hdr_list(b_peek(srcbuf, ofs), b_tail(srcbuf),
594 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
595 if (ret <= 0) {
596 /* Incomplete or invalid trailers. If the input buffer only
597 * contains trailers and is full, which is detected by it being
598 * full and the offset to be zero, it's an error because
599 * trailers are too large to be handled by the parser. */
600 if (ret < 0 || (!ret && !ofs && !buf_room_for_htx_data(srcbuf)))
601 goto error;
602 goto end;
603 }
604
605 /* messages trailers fully parsed. */
606 if (h1_eval_htx_hdrs_size(hdrs) > max) {
607 if (htx_is_empty(dsthtx))
608 goto error;
Christopher Faulet58f21da2021-09-20 07:47:27 +0200609 goto output_full;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200610 }
611
612 if (!htx_add_all_trailers(dsthtx, hdrs))
613 goto error;
614
Christopher Faulet76014fd2019-12-10 11:47:22 +0100615 h1m->state = H1_MSG_DONE;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100616 dsthtx->flags |= HTX_FL_EOM;
Christopher Faulet76014fd2019-12-10 11:47:22 +0100617
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200618 end:
619 return ret;
Christopher Faulet58f21da2021-09-20 07:47:27 +0200620 output_full:
621 return -2;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200622 error:
623 h1m->err_state = h1m->state;
624 h1m->err_pos = h1m->next;
625 dsthtx->flags |= HTX_FL_PARSING_ERROR;
Christopher Faulet58f21da2021-09-20 07:47:27 +0200626 return -1;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200627}
628
Christopher Faulet53a899b2019-10-08 16:38:42 +0200629/* Appends the H1 representation of the request line <sl> to the chunk <chk>. It
630 * returns 1 if data are successfully appended, otherwise it returns 0.
631 */
632int h1_format_htx_reqline(const struct htx_sl *sl, struct buffer *chk)
633{
634 struct ist uri;
635 size_t sz = chk->data;
636
Christopher Fauletfb38c912021-04-26 09:38:55 +0200637 uri = h1_get_uri(sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +0200638 if (!chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)) ||
639 !chunk_memcat(chk, " ", 1) ||
640 !chunk_memcat(chk, uri.ptr, uri.len) ||
641 !chunk_memcat(chk, " ", 1))
642 goto full;
643
644 if (sl->flags & HTX_SL_F_VER_11) {
645 if (!chunk_memcat(chk, "HTTP/1.1", 8))
646 goto full;
647 }
648 else {
649 if (!chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)))
650 goto full;
651 }
652
653 if (!chunk_memcat(chk, "\r\n", 2))
654 goto full;
655
656 return 1;
657
658 full:
659 chk->data = sz;
660 return 0;
661}
662
663/* Appends the H1 representation of the status line <sl> to the chunk <chk>. It
664 * returns 1 if data are successfully appended, otherwise it returns 0.
665 */
666int h1_format_htx_stline(const struct htx_sl *sl, struct buffer *chk)
667{
668 size_t sz = chk->data;
669
670 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
671 return 0;
672
673 if (sl->flags & HTX_SL_F_VER_11) {
674 if (!chunk_memcat(chk, "HTTP/1.1", 8))
675 goto full;
676 }
677 else {
678 if (!chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)))
679 goto full;
680 }
681 if (!chunk_memcat(chk, " ", 1) ||
682 !chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)) ||
683 !chunk_memcat(chk, " ", 1) ||
684 !chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)) ||
685 !chunk_memcat(chk, "\r\n", 2))
686 goto full;
687
688 return 1;
689
690 full:
691 chk->data = sz;
692 return 0;
693}
694
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500695/* Appends the H1 representation of the header <n> with the value <v> to the
Christopher Faulet53a899b2019-10-08 16:38:42 +0200696 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
697 * returns 0.
698 */
699int h1_format_htx_hdr(const struct ist n, const struct ist v, struct buffer *chk)
700{
701 size_t sz = chk->data;
702
703 if (n.len + v.len + 4 > b_room(chk))
704 return 0;
705
706 if (!chunk_memcat(chk, n.ptr, n.len) ||
707 !chunk_memcat(chk, ": ", 2) ||
708 !chunk_memcat(chk, v.ptr, v.len) ||
709 !chunk_memcat(chk, "\r\n", 2))
710 goto full;
711
712 return 1;
713
714 full:
715 chk->data = sz;
716 return 0;
717}
718
719/* Appends the H1 representation of the data <data> to the chunk <chk>. If
720 * <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It returns 1 if
721 * data are successfully appended, otherwise it returns 0.
722 */
723int h1_format_htx_data(const struct ist data, struct buffer *chk, int chunked)
724{
725 size_t sz = chk->data;
726
727 if (chunked) {
728 uint32_t chksz;
729 char tmp[10];
730 char *beg, *end;
731
732 chksz = data.len;
733
734 beg = end = tmp+10;
735 *--beg = '\n';
736 *--beg = '\r';
737 do {
738 *--beg = hextab[chksz & 0xF];
739 } while (chksz >>= 4);
740
741 if (!chunk_memcat(chk, beg, end - beg) ||
742 !chunk_memcat(chk, data.ptr, data.len) ||
743 !chunk_memcat(chk, "\r\n", 2))
744 goto full;
745 }
746 else {
747 if (!chunk_memcat(chk, data.ptr, data.len))
748 return 0;
749 }
750
751 return 1;
752
753 full:
754 chk->data = sz;
755 return 0;
756}
757
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200758/*
759 * Local variables:
760 * c-indent-level: 8
761 * c-basic-offset: 8
762 * End:
763 */