blob: 6ea19d265f2d2f3f7b8f0f8ce2d062741a3656cf [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 Denoyelle852d78c2021-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
60 if (*(sl->rq.v.ptr + 4) != '/' ||
61 !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;
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
Christopher Faulet52a5ec22021-09-09 09:52:51 +0200181
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200182 flags = h1m_htx_sl_flags(h1m);
Christopher Faulet52a5ec22021-09-09 09:52:51 +0200183 if ((flags & (HTX_SL_F_CONN_UPG|HTX_SL_F_BODYLESS)) == HTX_SL_F_CONN_UPG) {
184 int i;
185
186 for (i = 0; hdrs[i].n.len; i++) {
187 if (isteqi(hdrs[i].n, ist("upgrade")))
188 hdrs[i].v = IST_NULL;
189 }
190 h1m->flags &=~ H1_MF_CONN_UPG;
191 flags &= ~HTX_SL_F_CONN_UPG;
192 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200193 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth, uri, vsn);
194 if (!sl || !htx_add_all_headers(htx, hdrs))
195 goto error;
196 sl->info.req.meth = h1sl->rq.meth;
197
Christopher Fauletfe451fb2019-10-08 15:01:34 +0200198 /* Check if the uri contains an authority. Also check if it contains an
199 * explicit scheme and if it is "http" or "https". */
200 if (h1sl->rq.meth == HTTP_METH_CONNECT)
201 sl->flags |= HTX_SL_F_HAS_AUTHORITY;
202 else if (uri.len && uri.ptr[0] != '/' && uri.ptr[0] != '*') {
203 sl->flags |= (HTX_SL_F_HAS_AUTHORITY|HTX_SL_F_HAS_SCHM);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200204 if (uri.len > 4 && (uri.ptr[0] | 0x20) == 'h')
205 sl->flags |= ((uri.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
Amaury Denoyelle852d78c2021-07-07 10:49:27 +0200206
207 /* absolute-form target URI present, proceed to scheme-based
208 * normalization */
209 http_scheme_based_normalize(htx);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200210 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200211
212 /* If body length cannot be determined, set htx->extra to
213 * ULLONG_MAX. This value is impossible in other cases.
214 */
215 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
216
217 end:
218 return 1;
219 error:
220 h1m->err_pos = h1m->next;
221 h1m->err_state = h1m->state;
222 htx->flags |= HTX_FL_PARSING_ERROR;
223 return 0;
224}
225
226/* Postprocess the parsed headers for a response and convert them into an htx
227 * message. It returns the number of bytes parsed if > 0, or 0 if it couldn't
228 * proceed. Parsing errors are reported by setting the htx flag
229 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields.
230 */
231static int h1_postparse_res_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *htx,
232 struct http_hdr *hdrs, size_t max)
233{
234 struct htx_sl *sl;
235 struct ist vsn, status, reason;
236 unsigned int flags;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200237 uint16_t code = 0;
238
239 if (h1sl) {
240 /* For HTTP responses, the start-line was parsed */
241 code = h1sl->st.status;
242 vsn = h1sl->st.v;
243 status = h1sl->st.c;
244 reason = h1sl->st.r;
245 }
246 else {
247 /* For FCGI responses, there is no start(-line but the "Status"
248 * header must be parsed, if found.
249 */
250 int hdr;
251
252 vsn = ((h1m->flags & H1_MF_VER_11) ? ist("HTTP/1.1") : ist("HTTP/1.0"));
253 for (hdr = 0; hdrs[hdr].n.len; hdr++) {
254 if (isteqi(hdrs[hdr].n, ist("status"))) {
255 code = http_parse_status_val(hdrs[hdr].v, &status, &reason);
256 }
257 else if (isteqi(hdrs[hdr].n, ist("location"))) {
258 code = 302;
259 status = ist("302");
260 reason = ist("Moved Temporarily");
261 }
262 }
263 if (!code) {
264 code = 200;
265 status = ist("200");
266 reason = ist("OK");
267 }
268 /* FIXME: Check the codes 1xx ? */
269 }
270
271 /* Be sure the message, once converted into HTX, will not exceed the max
272 * size allowed.
273 */
274 if (h1_eval_htx_size(vsn, status, reason, hdrs) > max) {
275 if (htx_is_empty(htx))
276 goto error;
277 h1m_init_res(h1m);
278 h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
279 return 0;
280 }
281
Christopher Fauletc75668e2020-12-07 18:10:32 +0100282 if (((h1m->flags & H1_MF_METH_CONNECT) && code >= 200 && code < 300) || code == 101) {
Christopher Faulet5be651d2021-01-22 15:28:03 +0100283 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
284 h1m->flags |= H1_MF_XFER_LEN;
285 h1m->curr_len = h1m->body_len = 0;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200286 }
287 else if ((h1m->flags & H1_MF_METH_HEAD) || (code >= 100 && code < 200) ||
288 (code == 204) || (code == 304)) {
289 /* Responses known to have no body. */
290 h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK);
291 h1m->flags |= H1_MF_XFER_LEN;
292 h1m->curr_len = h1m->body_len = 0;
293 }
294 else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
295 /* Responses with a known body length. */
296 h1m->flags |= H1_MF_XFER_LEN;
297 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200298
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200299 flags = h1m_htx_sl_flags(h1m);
300 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, vsn, status, reason);
301 if (!sl || !htx_add_all_headers(htx, hdrs))
302 goto error;
303 sl->info.res.status = code;
304
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200305 /* If body length cannot be determined, set htx->extra to
306 * ULLONG_MAX. This value is impossible in other cases.
307 */
308 htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX);
309
310 end:
311 return 1;
312 error:
313 h1m->err_pos = h1m->next;
314 h1m->err_state = h1m->state;
315 htx->flags |= HTX_FL_PARSING_ERROR;
316 return 0;
317}
318
319/* Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if
320 * it couldn't proceed. Parsing errors are reported by setting the htx flag
321 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This
322 * functions is responsible to update the parser state <h1m> and the start-line
323 * <h1sl> if not NULL.
324 * For the requests, <h1sl> must always be provided. For responses, <h1sl> may
325 * be NULL and <h1m> flags HTTP_METH_CONNECT of HTTP_METH_HEAD may be set.
326 */
Christopher Fauletde471a42021-02-01 16:37:28 +0100327size_t h1_parse_msg_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx *dsthtx,
328 struct buffer *srcbuf, size_t ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200329{
330 struct http_hdr hdrs[global.tune.max_http_hdr];
331 int ret = 0;
332
333 if (!max || !b_data(srcbuf))
334 goto end;
335
336 /* Realing input buffer if necessary */
337 if (b_head(srcbuf) + b_data(srcbuf) > b_wrap(srcbuf))
Christopher Faulet00d7cde2021-02-04 11:01:51 +0100338 b_slow_realign_ofs(srcbuf, trash.area, 0);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200339
340 if (!h1sl) {
341 /* If there no start-line, be sure to only parse the headers */
342 h1m->flags |= H1_MF_HDRS_ONLY;
343 }
344 ret = h1_headers_to_hdr_list(b_peek(srcbuf, ofs), b_tail(srcbuf),
345 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, h1sl);
346 if (ret <= 0) {
347 /* Incomplete or invalid message. If the input buffer only
348 * contains headers and is full, which is detected by it being
349 * full and the offset to be zero, it's an error because
350 * headers are too large to be handled by the parser. */
351 if (ret < 0 || (!ret && !ofs && !buf_room_for_htx_data(srcbuf)))
352 goto error;
353 goto end;
354 }
355
356 /* messages headers fully parsed, do some checks to prepare the body
357 * parsing.
358 */
359
360 if (!(h1m->flags & H1_MF_RESP)) {
361 if (!h1_process_req_vsn(h1m, h1sl)) {
362 h1m->err_pos = h1sl->rq.v.ptr - b_head(srcbuf);
363 h1m->err_state = h1m->state;
364 goto vsn_error;
365 }
366 if (!h1_postparse_req_hdrs(h1m, h1sl, dsthtx, hdrs, max))
367 ret = 0;
368 }
369 else {
370 if (h1sl && !h1_process_res_vsn(h1m, h1sl)) {
371 h1m->err_pos = h1sl->st.v.ptr - b_head(srcbuf);
372 h1m->err_state = h1m->state;
373 goto vsn_error;
374 }
375 if (!h1_postparse_res_hdrs(h1m, h1sl, dsthtx, hdrs, max))
376 ret = 0;
377 }
378
Christopher Faulet76014fd2019-12-10 11:47:22 +0100379 /* Switch messages without any payload to DONE state */
380 if (((h1m->flags & H1_MF_CLEN) && h1m->body_len == 0) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100381 ((h1m->flags & (H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK)) == H1_MF_XFER_LEN)) {
Christopher Faulet76014fd2019-12-10 11:47:22 +0100382 h1m->state = H1_MSG_DONE;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100383 dsthtx->flags |= HTX_FL_EOM;
384 }
Christopher Faulet76014fd2019-12-10 11:47:22 +0100385
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200386 end:
387 return ret;
388 error:
389 h1m->err_pos = h1m->next;
390 h1m->err_state = h1m->state;
391 vsn_error:
392 dsthtx->flags |= HTX_FL_PARSING_ERROR;
393 return 0;
394
395}
396
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200397/* Copy data from <srbuf> into an DATA block in <dsthtx>. If possible, a
398 * zero-copy is performed. It returns the number of bytes copied.
399 */
Christopher Fauletde471a42021-02-01 16:37:28 +0100400static size_t h1_copy_msg_data(struct htx **dsthtx, struct buffer *srcbuf, size_t ofs,
Christopher Fauletf7c20442021-02-02 19:40:07 +0100401 size_t count, size_t max, struct buffer *htxbuf)
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200402{
Christopher Fauletaf542632019-10-01 21:52:49 +0200403 struct htx *tmp_htx = *dsthtx;
Christopher Fauletf7c20442021-02-02 19:40:07 +0100404 size_t block1, block2, ret = 0;
405
406 /* Be prepared to create at least one HTX block by reserving its size
407 * and adjust <count> accordingly.
408 */
409 max -= sizeof(struct htx_blk);
410 if (count > max)
411 count = max;
Christopher Fauletaf542632019-10-01 21:52:49 +0200412
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200413 /* very often with large files we'll face the following
414 * situation :
415 * - htx is empty and points to <htxbuf>
Christopher Fauletf7c20442021-02-02 19:40:07 +0100416 * - count == srcbuf->data
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200417 * - srcbuf->head == sizeof(struct htx)
418 * => we can swap the buffers and place an htx header into
419 * the target buffer instead
420 */
Christopher Fauletaf542632019-10-01 21:52:49 +0200421 if (unlikely(htx_is_empty(tmp_htx) && count == b_data(srcbuf) &&
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200422 !ofs && b_head_ofs(srcbuf) == sizeof(struct htx))) {
423 void *raw_area = srcbuf->area;
424 void *htx_area = htxbuf->area;
425 struct htx_blk *blk;
426
427 srcbuf->area = htx_area;
428 htxbuf->area = raw_area;
Christopher Fauletaf542632019-10-01 21:52:49 +0200429 tmp_htx = (struct htx *)htxbuf->area;
430 tmp_htx->size = htxbuf->size - sizeof(*tmp_htx);
431 htx_reset(tmp_htx);
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200432 b_set_data(htxbuf, b_size(htxbuf));
433
Christopher Fauletaf542632019-10-01 21:52:49 +0200434 blk = htx_add_blk(tmp_htx, HTX_BLK_DATA, count);
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200435 blk->info += count;
Christopher Fauletaf542632019-10-01 21:52:49 +0200436
437 *dsthtx = tmp_htx;
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200438 /* nothing else to do, the old buffer now contains an
439 * empty pre-initialized HTX header
440 */
441 return count;
442 }
443
Christopher Fauletf7c20442021-02-02 19:40:07 +0100444 /* * First block is the copy of contiguous data starting at offset <ofs>
445 * with <count> as max. <max> is updated accordingly
446 *
447 * * Second block is the remaining (count - block1) if <max> is large
448 * enough. Another HTX block is reserved.
449 */
450 block1 = b_contig_data(srcbuf, ofs);
451 block2 = 0;
452 if (block1 > count)
453 block1 = count;
454 max -= block1;
455
456 if (max > sizeof(struct htx_blk)) {
457 block2 = count - block1;
458 max -= sizeof(struct htx_blk);
459 if (block2 > max)
460 block2 = max;
461 }
462
463 ret = htx_add_data(tmp_htx, ist2(b_peek(srcbuf, ofs), block1));
464 if (ret == block1 && block2)
465 ret += htx_add_data(tmp_htx, ist2(b_orig(srcbuf), block2));
466 end:
467 return ret;
Christopher Fauletcc3124c2019-08-12 22:42:21 +0200468}
469
Christopher Faulet7a835f32021-05-21 11:31:35 +0200470static const char hextable[] = {
471 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
472 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
473 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
474 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
475 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
476 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
477 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
478 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
479};
480
Christopher Faulet0d4c9242021-05-21 10:56:24 +0200481/* Generic function to parse the current HTTP chunk. It may be used to parsed
Ilya Shipitsin213bb992021-06-12 15:55:27 +0500482 * any kind of chunks, including incomplete HTTP chunks or split chunks
Christopher Faulet0d4c9242021-05-21 10:56:24 +0200483 * because the buffer wraps. This version tries to performed zero-copy on large
484 * chunks if possible.
Christopher Faulet140691b2021-02-03 11:51:24 +0100485 */
Christopher Faulet0d4c9242021-05-21 10:56:24 +0200486static size_t h1_parse_chunk(struct h1m *h1m, struct htx **dsthtx,
487 struct buffer *srcbuf, size_t ofs, size_t *max,
488 struct buffer *htxbuf)
Christopher Faulet140691b2021-02-03 11:51:24 +0100489{
490 uint64_t chksz;
Christopher Faulet0d4c9242021-05-21 10:56:24 +0200491 size_t sz, used, lmax, total = 0;
Christopher Faulet140691b2021-02-03 11:51:24 +0100492 int ret = 0;
493
Christopher Faulet0d4c9242021-05-21 10:56:24 +0200494 lmax = *max;
Christopher Faulet140691b2021-02-03 11:51:24 +0100495 switch (h1m->state) {
496 case H1_MSG_DATA:
497 new_chunk:
498 used = htx_used_space(*dsthtx);
499
Christopher Faulet0d4c9242021-05-21 10:56:24 +0200500 if (b_data(srcbuf) == ofs || !lmax)
Christopher Faulet140691b2021-02-03 11:51:24 +0100501 break;
502
503 sz = b_data(srcbuf) - ofs;
504 if (unlikely(sz > h1m->curr_len))
505 sz = h1m->curr_len;
Christopher Faulet0d4c9242021-05-21 10:56:24 +0200506 sz = h1_copy_msg_data(dsthtx, srcbuf, ofs, sz, lmax, htxbuf);
507 lmax -= htx_used_space(*dsthtx) - used;
Christopher Faulet140691b2021-02-03 11:51:24 +0100508 ofs += sz;
509 total += sz;
510 h1m->curr_len -= sz;
511 if (h1m->curr_len)
512 break;
513
514 h1m->state = H1_MSG_CHUNK_CRLF;
515 /*fall through */
516
517 case H1_MSG_CHUNK_CRLF:
518 ret = h1_skip_chunk_crlf(srcbuf, ofs, b_data(srcbuf));
519 if (ret <= 0)
520 break;
521 ofs += ret;
522 total += ret;
Christopher Faulet0d4c9242021-05-21 10:56:24 +0200523
524 /* Don't parse next chunk to try to handle contiguous chunks if possible */
Christopher Faulet140691b2021-02-03 11:51:24 +0100525 h1m->state = H1_MSG_CHUNK_SIZE;
Christopher Faulet0d4c9242021-05-21 10:56:24 +0200526 break;
Christopher Faulet140691b2021-02-03 11:51:24 +0100527
528 case H1_MSG_CHUNK_SIZE:
529 ret = h1_parse_chunk_size(srcbuf, ofs, b_data(srcbuf), &chksz);
530 if (ret <= 0)
531 break;
532 h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA);
533 h1m->curr_len = chksz;
534 h1m->body_len += chksz;
535 ofs += ret;
536 total += ret;
537
538 if (h1m->curr_len) {
539 h1m->state = H1_MSG_DATA;
540 goto new_chunk;
541 }
542 h1m->state = H1_MSG_TRAILERS;
543 break;
544
545 default:
546 /* unexpected */
547 ret = -1;
548 break;
549 }
550
551 if (ret < 0) {
552 (*dsthtx)->flags |= HTX_FL_PARSING_ERROR;
553 h1m->err_state = h1m->state;
554 h1m->err_pos = ofs;
555 total = 0;
556 }
557
558 /* Don't forget to update htx->extra */
559 (*dsthtx)->extra = h1m->curr_len;
Christopher Faulet0d4c9242021-05-21 10:56:24 +0200560 *max = lmax;
561 return total;
562}
563
Christopher Fauletbdcefe52021-05-21 11:05:12 +0200564/* Parses full contiguous HTTP chunks. This version is optimized for small
565 * chunks and does not performed zero-copy. It must be called in
Ilya Shipitsin213bb992021-06-12 15:55:27 +0500566 * H1_MSG_CHUNK_SIZE state. Be careful if you change something in this
Christopher Fauletbdcefe52021-05-21 11:05:12 +0200567 * function. It is really sensitive, any change may have an impact on
568 * performance.
569 */
570static size_t h1_parse_full_contig_chunks(struct h1m *h1m, struct htx **dsthtx,
571 struct buffer *srcbuf, size_t ofs, size_t *max,
572 struct buffer *htxbuf)
573{
574 char *start, *end, *dptr;
575 ssize_t dpos, ridx, save;
576 size_t lmax, total = 0;
577 uint64_t chksz;
578 struct htx_ret htxret;
579
580 /* source info :
581 * start : pointer at <ofs> position
582 * end : pointer marking the end of data to parse
583 * ridx : the reverse index (negative) marking the parser position (end[ridx])
584 */
585 ridx = -b_contig_data(srcbuf, ofs);
586 if (!ridx)
587 goto out;
588 start = b_peek(srcbuf, ofs);
589 end = start - ridx;
590
591 /* Reserve the maximum possible size for the data */
592 htxret = htx_reserve_max_data(*dsthtx);
593 if (!htxret.blk)
594 goto out;
595
596 /* destination info :
597 * dptr : pointer on the beginning of the data
598 * dpos : current position where to copy data
599 */
600 dptr = htx_get_blk_ptr(*dsthtx, htxret.blk);
601 dpos = htxret.ret;
602
603 /* Empty DATA block is not possible, thus if <dpos> is the beginning of
604 * the block, it means it is a new block. We can remove the block size
605 * from <max>. Then we must adjust it if it exceeds the free size in the
606 * block.
607 */
608 lmax = *max;
609 if (!dpos)
610 lmax -= sizeof(struct htx_blk);
611 if (lmax > htx_get_blksz(htxret.blk) - dpos)
612 lmax = htx_get_blksz(htxret.blk) - dpos;
613
614 while (1) {
615 /* The chunk size is in the following form, though we are only
616 * interested in the size and CRLF :
617 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
618 */
619 chksz = 0;
620 save = ridx; /* Save the parser position to rewind if necessary */
621 while (1) {
622 int c;
623
624 if (!ridx)
625 goto end_parsing;
626
627 /* Convert current character */
Christopher Faulet7a835f32021-05-21 11:31:35 +0200628 c = hextable[(unsigned char)end[ridx]];
Christopher Fauletbdcefe52021-05-21 11:05:12 +0200629
630 /* not a hex digit anymore */
Christopher Fauletbf76df12021-06-11 13:39:06 +0200631 if (c & 0xF0)
Christopher Fauletbdcefe52021-05-21 11:05:12 +0200632 break;
633
634 /* Update current chunk size */
635 chksz = (chksz << 4) + c;
636
637 if (unlikely(chksz & 0xF0000000000000)) {
638 /* Don't get more than 13 hexa-digit (2^52 - 1)
639 * to never fed possibly bogus values from
640 * languages that use floats for their integers
641 */
642 goto parsing_error;
643 }
644 ++ridx;
645 }
646
647 if (unlikely(chksz > lmax))
648 goto end_parsing;
649
650 if (unlikely(ridx == save)) {
651 /* empty size not allowed */
652 goto parsing_error;
653 }
654
655 /* Skip spaces */
656 while (HTTP_IS_SPHT(end[ridx])) {
657 if (!++ridx)
658 goto end_parsing;
659 }
660
661 /* Up to there, we know that at least one byte is present. Check
662 * for the end of chunk size.
663 */
664 while (1) {
665 if (likely(end[ridx] == '\r')) {
666 /* Parse CRLF */
667 if (!++ridx)
668 goto end_parsing;
669 if (unlikely(end[ridx] != '\n')) {
670 /* CR must be followed by LF */
671 goto parsing_error;
672 }
673
674 /* done */
675 ++ridx;
676 break;
677 }
678 else if (end[ridx] == '\n') {
679 /* Parse LF only, nothing more to do */
680 ++ridx;
681 break;
682 }
683 else if (likely(end[ridx] == ';')) {
684 /* chunk extension, ends at next CRLF */
685 if (!++ridx)
686 goto end_parsing;
687 while (!HTTP_IS_CRLF(end[ridx])) {
688 if (!++ridx)
689 goto end_parsing;
690 }
691 /* we have a CRLF now, loop above */
692 continue;
693 }
694 else {
695 /* all other characters are unexpected */
696 goto parsing_error;
697 }
698 }
699
700 /* Exit if it is the last chunk */
701 if (unlikely(!chksz)) {
702 h1m->state = H1_MSG_TRAILERS;
703 save = ridx;
704 goto end_parsing;
705 }
706
707 /* Now check if the whole chunk is here (including the CRLF at
Ilya Shipitsin213bb992021-06-12 15:55:27 +0500708 * the end), otherwise we switch in H1_MSG_DATA state.
Christopher Fauletbdcefe52021-05-21 11:05:12 +0200709 */
710 if (chksz + 2 > -ridx) {
711 h1m->curr_len = chksz;
712 h1m->body_len += chksz;
713 h1m->state = H1_MSG_DATA;
714 (*dsthtx)->extra = h1m->curr_len;
715 save = ridx;
716 goto end_parsing;
717 }
718
719 memcpy(dptr + dpos, end + ridx, chksz);
720 h1m->body_len += chksz;
721 lmax -= chksz;
722 dpos += chksz;
723 ridx += chksz;
724
725 /* Parse CRLF or LF (always present) */
726 if (likely(end[ridx] == '\r'))
727 ++ridx;
728 if (end[ridx] != '\n') {
729 h1m->state = H1_MSG_CHUNK_CRLF;
730 goto parsing_error;
731 }
732 ++ridx;
733 }
734
735 end_parsing:
736 ridx = save;
737
738 /* Adjust the HTX block size or remove the block if nothing was copied
739 * (Empty HTX data block are not supported).
740 */
741 if (!dpos)
742 htx_remove_blk(*dsthtx, htxret.blk);
743 else
744 htx_change_blk_value_len(*dsthtx, htxret.blk, dpos);
745 total = end + ridx - start;
746 *max = lmax;
747
748 out:
749 return total;
750
751 parsing_error:
752 (*dsthtx)->flags |= HTX_FL_PARSING_ERROR;
753 h1m->err_state = h1m->state;
754 h1m->err_pos = ofs + end + ridx - start;
755 return 0;
756}
757
758/* Parse HTTP chunks. This function relies on an optimized function to parse
759 * contiguous chunks if possible. Otherwise, when a chunk is incomplete or when
760 * the underlying buffer is wrapping, a generic function is used.
761 */
Christopher Faulet0d4c9242021-05-21 10:56:24 +0200762static size_t h1_parse_msg_chunks(struct h1m *h1m, struct htx **dsthtx,
763 struct buffer *srcbuf, size_t ofs, size_t max,
764 struct buffer *htxbuf)
765{
766 size_t ret, total = 0;
767
768 while (ofs < b_data(srcbuf)) {
769 ret = 0;
770
Christopher Fauletbdcefe52021-05-21 11:05:12 +0200771 /* First parse full contiguous chunks. It is only possible if we
772 * are waiting for the next chunk size.
773 */
774 if (h1m->state == H1_MSG_CHUNK_SIZE) {
775 ret = h1_parse_full_contig_chunks(h1m, dsthtx, srcbuf, ofs, &max, htxbuf);
776 /* exit on error */
777 if (!ret && (*dsthtx)->flags & HTX_FL_PARSING_ERROR) {
778 total = 0;
779 break;
780 }
781 /* or let a chance to parse remaining data */
782 total += ret;
783 ofs += ret;
784 ret = 0;
785 }
786
787 /* If some data remains, try to parse it using the generic
Ilya Shipitsin213bb992021-06-12 15:55:27 +0500788 * function handling incomplete chunks and split chunks
Christopher Fauletbdcefe52021-05-21 11:05:12 +0200789 * because of a wrapping buffer.
790 */
791 if (h1m->state < H1_MSG_TRAILERS && ofs < b_data(srcbuf)) {
Christopher Faulet0d4c9242021-05-21 10:56:24 +0200792 ret = h1_parse_chunk(h1m, dsthtx, srcbuf, ofs, &max, htxbuf);
793 total += ret;
794 ofs += ret;
795 }
796
Christopher Fauletbdcefe52021-05-21 11:05:12 +0200797 /* nothing more was parsed or parsing was stopped on incomplete
798 * chunk, we can exit, handling parsing error if necessary.
Christopher Faulet0d4c9242021-05-21 10:56:24 +0200799 */
Christopher Fauletbdcefe52021-05-21 11:05:12 +0200800 if (!ret || h1m->state != H1_MSG_CHUNK_SIZE) {
Christopher Faulet0d4c9242021-05-21 10:56:24 +0200801 if ((*dsthtx)->flags & HTX_FL_PARSING_ERROR)
802 total = 0;
803 break;
804 }
805 }
806
Christopher Faulet140691b2021-02-03 11:51:24 +0100807 return total;
808}
809
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200810/* Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it
811 * couldn't proceed. Parsing errors are reported by setting the htx flags
812 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This
813 * functions is responsible to update the parser state <h1m>.
814 */
Christopher Fauletde471a42021-02-01 16:37:28 +0100815size_t h1_parse_msg_data(struct h1m *h1m, struct htx **dsthtx,
816 struct buffer *srcbuf, size_t ofs, size_t max,
817 struct buffer *htxbuf)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200818{
Christopher Fauletde471a42021-02-01 16:37:28 +0100819 size_t sz, total = 0;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200820
Christopher Fauletf7c20442021-02-02 19:40:07 +0100821 if (b_data(srcbuf) == ofs || !max)
Christopher Faulet140691b2021-02-03 11:51:24 +0100822 return 0;
Christopher Fauletf7c20442021-02-02 19:40:07 +0100823
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200824 if (h1m->flags & H1_MF_CLEN) {
825 /* content-length: read only h2m->body_len */
Christopher Fauletf7c20442021-02-02 19:40:07 +0100826 sz = b_data(srcbuf) - ofs;
827 if (unlikely(sz > h1m->curr_len))
Christopher Fauletde471a42021-02-01 16:37:28 +0100828 sz = h1m->curr_len;
Christopher Fauletf7c20442021-02-02 19:40:07 +0100829 sz = h1_copy_msg_data(dsthtx, srcbuf, ofs, sz, max, htxbuf);
830 h1m->curr_len -= sz;
831 (*dsthtx)->extra = h1m->curr_len;
832 total += sz;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100833 if (!h1m->curr_len) {
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200834 h1m->state = H1_MSG_DONE;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100835 (*dsthtx)->flags |= HTX_FL_EOM;
836 }
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200837 }
838 else if (h1m->flags & H1_MF_CHNK) {
839 /* te:chunked : parse chunks */
Christopher Faulet140691b2021-02-03 11:51:24 +0100840 total += h1_parse_msg_chunks(h1m, dsthtx, srcbuf, ofs, max, htxbuf);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200841 }
842 else if (h1m->flags & H1_MF_XFER_LEN) {
843 /* XFER_LEN is set but not CLEN nor CHNK, it means there is no
844 * body. Switch the message in DONE state
845 */
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200846 h1m->state = H1_MSG_DONE;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100847 (*dsthtx)->flags |= HTX_FL_EOM;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200848 }
849 else {
850 /* no content length, read till SHUTW */
Christopher Fauletf7c20442021-02-02 19:40:07 +0100851 sz = b_data(srcbuf) - ofs;
852 sz = h1_copy_msg_data(dsthtx, srcbuf, ofs, sz, max, htxbuf);
853 total += sz;
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200854 }
855
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200856 return total;
857}
858
859/* Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if
860 * it couldn't proceed. Parsing errors are reported by setting the htx flags
861 * HTX_FL_PARSING_ERROR and filling h1m->err_pos and h1m->err_state fields. This
862 * functions is responsible to update the parser state <h1m>.
863 */
Christopher Fauletde471a42021-02-01 16:37:28 +0100864size_t h1_parse_msg_tlrs(struct h1m *h1m, struct htx *dsthtx,
865 struct buffer *srcbuf, size_t ofs, size_t max)
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200866{
867 struct http_hdr hdrs[global.tune.max_http_hdr];
868 struct h1m tlr_h1m;
869 int ret = 0;
870
871 if (!max || !b_data(srcbuf))
872 goto end;
873
874 /* Realing input buffer if necessary */
875 if (b_peek(srcbuf, ofs) > b_tail(srcbuf))
Christopher Faulet00d7cde2021-02-04 11:01:51 +0100876 b_slow_realign_ofs(srcbuf, trash.area, 0);
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200877
878 tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY);
879 ret = h1_headers_to_hdr_list(b_peek(srcbuf, ofs), b_tail(srcbuf),
880 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL);
881 if (ret <= 0) {
882 /* Incomplete or invalid trailers. If the input buffer only
883 * contains trailers and is full, which is detected by it being
884 * full and the offset to be zero, it's an error because
885 * trailers are too large to be handled by the parser. */
886 if (ret < 0 || (!ret && !ofs && !buf_room_for_htx_data(srcbuf)))
887 goto error;
888 goto end;
889 }
890
891 /* messages trailers fully parsed. */
892 if (h1_eval_htx_hdrs_size(hdrs) > max) {
893 if (htx_is_empty(dsthtx))
894 goto error;
895 ret = 0;
896 goto end;
897 }
898
899 if (!htx_add_all_trailers(dsthtx, hdrs))
900 goto error;
901
Christopher Faulet76014fd2019-12-10 11:47:22 +0100902 h1m->state = H1_MSG_DONE;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100903 dsthtx->flags |= HTX_FL_EOM;
Christopher Faulet76014fd2019-12-10 11:47:22 +0100904
Christopher Faulet4f0f88a2019-08-10 11:17:44 +0200905 end:
906 return ret;
907 error:
908 h1m->err_state = h1m->state;
909 h1m->err_pos = h1m->next;
910 dsthtx->flags |= HTX_FL_PARSING_ERROR;
911 return 0;
912}
913
Christopher Faulet53a899b2019-10-08 16:38:42 +0200914/* Appends the H1 representation of the request line <sl> to the chunk <chk>. It
915 * returns 1 if data are successfully appended, otherwise it returns 0.
916 */
917int h1_format_htx_reqline(const struct htx_sl *sl, struct buffer *chk)
918{
919 struct ist uri;
920 size_t sz = chk->data;
921
Christopher Fauletfb38c912021-04-26 09:38:55 +0200922 uri = h1_get_uri(sl);
Christopher Faulet53a899b2019-10-08 16:38:42 +0200923 if (!chunk_memcat(chk, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)) ||
924 !chunk_memcat(chk, " ", 1) ||
925 !chunk_memcat(chk, uri.ptr, uri.len) ||
926 !chunk_memcat(chk, " ", 1))
927 goto full;
928
929 if (sl->flags & HTX_SL_F_VER_11) {
930 if (!chunk_memcat(chk, "HTTP/1.1", 8))
931 goto full;
932 }
933 else {
934 if (!chunk_memcat(chk, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)))
935 goto full;
936 }
937
938 if (!chunk_memcat(chk, "\r\n", 2))
939 goto full;
940
941 return 1;
942
943 full:
944 chk->data = sz;
945 return 0;
946}
947
948/* Appends the H1 representation of the status line <sl> to the chunk <chk>. It
949 * returns 1 if data are successfully appended, otherwise it returns 0.
950 */
951int h1_format_htx_stline(const struct htx_sl *sl, struct buffer *chk)
952{
953 size_t sz = chk->data;
954
955 if (HTX_SL_LEN(sl) + 4 > b_room(chk))
956 return 0;
957
958 if (sl->flags & HTX_SL_F_VER_11) {
959 if (!chunk_memcat(chk, "HTTP/1.1", 8))
960 goto full;
961 }
962 else {
963 if (!chunk_memcat(chk, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)))
964 goto full;
965 }
966 if (!chunk_memcat(chk, " ", 1) ||
967 !chunk_memcat(chk, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)) ||
968 !chunk_memcat(chk, " ", 1) ||
969 !chunk_memcat(chk, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)) ||
970 !chunk_memcat(chk, "\r\n", 2))
971 goto full;
972
973 return 1;
974
975 full:
976 chk->data = sz;
977 return 0;
978}
979
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500980/* Appends the H1 representation of the header <n> with the value <v> to the
Christopher Faulet53a899b2019-10-08 16:38:42 +0200981 * chunk <chk>. It returns 1 if data are successfully appended, otherwise it
982 * returns 0.
983 */
984int h1_format_htx_hdr(const struct ist n, const struct ist v, struct buffer *chk)
985{
986 size_t sz = chk->data;
987
988 if (n.len + v.len + 4 > b_room(chk))
989 return 0;
990
991 if (!chunk_memcat(chk, n.ptr, n.len) ||
992 !chunk_memcat(chk, ": ", 2) ||
993 !chunk_memcat(chk, v.ptr, v.len) ||
994 !chunk_memcat(chk, "\r\n", 2))
995 goto full;
996
997 return 1;
998
999 full:
1000 chk->data = sz;
1001 return 0;
1002}
1003
1004/* Appends the H1 representation of the data <data> to the chunk <chk>. If
1005 * <chunked> is non-zero, it emits HTTP/1 chunk-encoded data. It returns 1 if
1006 * data are successfully appended, otherwise it returns 0.
1007 */
1008int h1_format_htx_data(const struct ist data, struct buffer *chk, int chunked)
1009{
1010 size_t sz = chk->data;
1011
1012 if (chunked) {
1013 uint32_t chksz;
1014 char tmp[10];
1015 char *beg, *end;
1016
1017 chksz = data.len;
1018
1019 beg = end = tmp+10;
1020 *--beg = '\n';
1021 *--beg = '\r';
1022 do {
1023 *--beg = hextab[chksz & 0xF];
1024 } while (chksz >>= 4);
1025
1026 if (!chunk_memcat(chk, beg, end - beg) ||
1027 !chunk_memcat(chk, data.ptr, data.len) ||
1028 !chunk_memcat(chk, "\r\n", 2))
1029 goto full;
1030 }
1031 else {
1032 if (!chunk_memcat(chk, data.ptr, data.len))
1033 return 0;
1034 }
1035
1036 return 1;
1037
1038 full:
1039 chk->data = sz;
1040 return 0;
1041}
1042
Christopher Faulet4f0f88a2019-08-10 11:17:44 +02001043/*
1044 * Local variables:
1045 * c-indent-level: 8
1046 * c-basic-offset: 8
1047 * End:
1048 */