blob: e55420c4073fe8edb265801a17c781f696720362 [file] [log] [blame]
Christopher Faulet47596d32018-10-22 09:17:28 +02001/*
2 * Functions to manipulate HTTP messages using the internal representation.
3 *
4 * Copyright (C) 2018 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
13#include <common/config.h>
Christopher Fauleta7b677c2018-11-29 16:48:49 +010014#include <common/cfgparse.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020016#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010017#include <common/htx.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020018
19#include <proto/http_htx.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020020
Christopher Fauleta7b677c2018-11-29 16:48:49 +010021struct buffer htx_err_chunks[HTTP_ERR_SIZE];
22
Christopher Faulet297fbb42019-05-13 14:41:27 +020023/* Returns the next unporocessed start line in the HTX message. It returns NULL
24 * is the start-line is undefined (sl_pos == 1). Otherwise, it returns the
25 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020026 */
Christopher Faulet297fbb42019-05-13 14:41:27 +020027struct htx_sl *http_get_stline(struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020028{
Christopher Faulet297fbb42019-05-13 14:41:27 +020029 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010030
Christopher Faulet297fbb42019-05-13 14:41:27 +020031 if (htx->sl_pos == -1)
32 return NULL;
Christopher Faulet47596d32018-10-22 09:17:28 +020033
Christopher Faulet297fbb42019-05-13 14:41:27 +020034 blk = htx_get_blk(htx, htx->sl_pos);
35 if (!blk)
36 return NULL;
37 return htx_get_blk_ptr(htx, blk);
Christopher Faulet47596d32018-10-22 09:17:28 +020038}
39
40/* Finds the first or next occurrence of header <name> in the HTX message <htx>
41 * using the context <ctx>. This structure holds everything necessary to use the
42 * header and find next occurrence. If its <blk> member is NULL, the header is
43 * searched from the beginning. Otherwise, the next occurrence is returned. The
44 * function returns 1 when it finds a value, and 0 when there is no more. It is
45 * designed to work with headers defined as comma-separated lists. If <full> is
46 * set, it works on full-line headers in whose comma is not a delimiter but is
47 * part of the syntax. A special case, if ctx->value is NULL when searching for
48 * a new values of a header, the current header is rescanned. This allows
49 * rescanning after a header deletion.
50 */
51int http_find_header(const struct htx *htx, const struct ist name,
52 struct http_hdr_ctx *ctx, int full)
53{
54 struct htx_blk *blk = ctx->blk;
55 struct ist n, v;
56 enum htx_blk_type type;
Christopher Faulet47596d32018-10-22 09:17:28 +020057
58 if (blk) {
59 char *p;
60
Christopher Faulet47596d32018-10-22 09:17:28 +020061 if (!ctx->value.ptr)
62 goto rescan_hdr;
63 if (full)
64 goto next_blk;
65 v = htx_get_blk_value(htx, blk);
66 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
67 v.len -= (p - v.ptr);
68 v.ptr = p;
69 if (!v.len)
70 goto next_blk;
71 /* Skip comma */
72 if (*(v.ptr) == ',') {
73 v.ptr++;
74 v.len--;
75 }
76
77 goto return_hdr;
78 }
79
80 if (!htx->used)
81 return 0;
82
Christopher Faulet28f29c72019-04-30 17:55:45 +020083 for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulet47596d32018-10-22 09:17:28 +020084 rescan_hdr:
Christopher Faulet47596d32018-10-22 09:17:28 +020085 type = htx_get_blk_type(blk);
Christopher Faulet573fe732018-11-28 16:55:12 +010086 if (type == HTX_BLK_EOH || type == HTX_BLK_EOM)
87 break;
Christopher Faulet47596d32018-10-22 09:17:28 +020088 if (type != HTX_BLK_HDR)
Christopher Faulet28f29c72019-04-30 17:55:45 +020089 continue;
Christopher Faulet47596d32018-10-22 09:17:28 +020090 if (name.len) {
91 /* If no name was passed, we want any header. So skip the comparison */
92 n = htx_get_blk_name(htx, blk);
93 if (!isteqi(n, name))
94 goto next_blk;
95 }
96 v = htx_get_blk_value(htx, blk);
97
98 return_hdr:
99 ctx->lws_before = 0;
100 ctx->lws_after = 0;
101 while (v.len && HTTP_IS_LWS(*v.ptr)) {
102 v.ptr++;
103 v.len--;
104 ctx->lws_before++;
105 }
106 if (!full)
107 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
108 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
109 v.len--;
110 ctx->lws_after++;
111 }
112 if (!v.len)
Christopher Faulet28f29c72019-04-30 17:55:45 +0200113 continue;
Christopher Faulet47596d32018-10-22 09:17:28 +0200114 ctx->blk = blk;
115 ctx->value = v;
116 return 1;
117
118 next_blk:
Christopher Faulet28f29c72019-04-30 17:55:45 +0200119 ;
Christopher Faulet47596d32018-10-22 09:17:28 +0200120 }
121
122 ctx->blk = NULL;
123 ctx->value = ist("");
124 ctx->lws_before = ctx->lws_after = 0;
125 return 0;
126}
127
128/* Adds a header block int the HTX message <htx>, just before the EOH block. It
129 * returns 1 on success, otherwise it returns 0.
130 */
131int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
132{
133 struct htx_blk *blk;
134 enum htx_blk_type type = htx_get_tail_type(htx);
135 int32_t prev;
136
137 blk = htx_add_header(htx, n, v);
138 if (!blk)
139 return 0;
140
141 if (unlikely(type < HTX_BLK_EOH))
142 return 1;
143
144 /* <blk> is the head, swap it iteratively with its predecessor to place
145 * it just before the end-of-header block. So blocks remains ordered. */
146 for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) {
147 struct htx_blk *pblk = htx_get_blk(htx, prev);
148 enum htx_blk_type type = htx_get_blk_type(pblk);
149
150 /* Swap .addr and .info fields */
151 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
152 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
153
154 if (blk->addr == pblk->addr)
155 blk->addr += htx_get_blksz(pblk);
156 htx->front = prev;
157
158 /* Stop when end-of-header is reached */
159 if (type == HTX_BLK_EOH)
160 break;
161
162 blk = pblk;
163 }
Christopher Faulet05aab642019-04-11 13:43:57 +0200164
165 if (htx_get_blk_pos(htx, blk) != htx->front)
166 htx_defrag(htx, NULL);
167
Christopher Faulet47596d32018-10-22 09:17:28 +0200168 return 1;
169}
170
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100171/* Replaces parts of the start-line of the HTX message <htx>. It returns 1 on
172 * success, otherwise it returns 0. The right block is search in the HTX
Christopher Faulet47596d32018-10-22 09:17:28 +0200173 * message.
174 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100175int http_replace_stline(struct htx *htx, const struct ist p1, const struct ist p2, const struct ist p3)
Christopher Faulet47596d32018-10-22 09:17:28 +0200176{
177 int32_t pos;
178
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100179 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100180 struct htx_blk *blk = htx_get_blk(htx, pos);
Christopher Fauletaa75b3d2018-12-05 16:20:40 +0100181 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet47596d32018-10-22 09:17:28 +0200182
Christopher Faulet9c66b982019-04-30 18:08:26 +0200183 if (htx->sl_pos == pos) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100184 if (!htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet47596d32018-10-22 09:17:28 +0200185 return 0;
186 return 1;
187 }
Christopher Faulet47596d32018-10-22 09:17:28 +0200188
Christopher Faulet47596d32018-10-22 09:17:28 +0200189 if (type == HTX_BLK_EOM)
190 break;
191 }
192
193 return 0;
194}
195
Christopher Faulete010c802018-10-24 10:36:45 +0200196/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
197 * on success, otherwise 0.
198 */
199int http_replace_req_meth(struct htx *htx, const struct ist meth)
200{
201 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200202 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100203 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200204
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100205 if (!sl)
206 return 0;
207
Christopher Faulete010c802018-10-24 10:36:45 +0200208 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100209 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
210 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200211
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100212 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
213 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200214
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100215 /* create the new start line */
216 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
217 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200218}
219
220/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
221 * success, otherwise 0.
222 */
223int http_replace_req_uri(struct htx *htx, const struct ist uri)
224{
225 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200226 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100227 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200228
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100229 if (!sl)
230 return 0;
231
Christopher Faulete010c802018-10-24 10:36:45 +0200232 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100233 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
234 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200235
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100236 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
237 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200238
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100239 /* create the new start line */
240 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200241}
242
243/* Replace the request path in the HTX message <htx> by <path>. The host part
244 * and the query string are preserved. It returns 1 on success, otherwise 0.
245 */
246int http_replace_req_path(struct htx *htx, const struct ist path)
247{
248 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200249 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100250 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200251 size_t plen = 0;
252
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100253 if (!sl)
254 return 0;
255
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100256 uri = htx_sl_req_uri(sl);
257 p = http_get_path(uri);
Christopher Faulete010c802018-10-24 10:36:45 +0200258 if (!p.ptr)
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100259 p = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200260 while (plen < p.len && *(p.ptr + plen) != '?')
261 plen++;
262
263 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100264 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
265 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200266
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100267 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
268 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
269
270 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200271 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
272 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100273 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200274
275 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100276 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200277}
278
279/* Replace the request query-string in the HTX message <htx> by <query>. The
280 * host part and the path are preserved. It returns 1 on success, otherwise
281 * 0.
282 */
283int http_replace_req_query(struct htx *htx, const struct ist query)
284{
285 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200286 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100287 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200288 int offset = 1;
289
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100290 if (!sl)
291 return 0;
292
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100293 uri = htx_sl_req_uri(sl);
294 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200295 while (q.len > 0 && *(q.ptr) != '?') {
296 q.ptr++;
297 q.len--;
298 }
299
300 /* skip the question mark or indicate that we must insert it
301 * (but only if the format string is not empty then).
302 */
303 if (q.len) {
304 q.ptr++;
305 q.len--;
306 }
307 else if (query.len > 1)
308 offset = 0;
309
310 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100311 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
312 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200313
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100314 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
315 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200316
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100317 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
318 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
319 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200320
321 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100322 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200323}
324
325/* Replace the response status in the HTX message <htx> by <status>. It returns
326 * 1 on success, otherwise 0.
327*/
328int http_replace_res_status(struct htx *htx, const struct ist status)
329{
330 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200331 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100332 struct ist vsn, reason;
Christopher Faulete010c802018-10-24 10:36:45 +0200333
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100334 if (!sl)
335 return 0;
336
Christopher Faulete010c802018-10-24 10:36:45 +0200337 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100338 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
339 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200340
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100341 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
342 reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200343
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100344 /* create the new start line */
345 sl->info.res.status = strl2ui(status.ptr, status.len);
346 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200347}
348
349/* Replace the response reason in the HTX message <htx> by <reason>. It returns
350 * 1 on success, otherwise 0.
351*/
352int http_replace_res_reason(struct htx *htx, const struct ist reason)
353{
354 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200355 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100356 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200357
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100358 if (!sl)
359 return 0;
360
Christopher Faulete010c802018-10-24 10:36:45 +0200361 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100362 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
363 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200364
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100365 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
366 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200367
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100368 /* create the new start line */
369 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200370}
371
Christopher Faulet47596d32018-10-22 09:17:28 +0200372/* Replaces a part of a header value referenced in the context <ctx> by
373 * <data>. It returns 1 on success, otherwise it returns 0. The context is
374 * updated if necessary.
375 */
376int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
377{
378 struct htx_blk *blk = ctx->blk;
379 char *start;
380 struct ist v;
381 uint32_t len, off;
382
383 if (!blk)
384 return 0;
385
386 v = htx_get_blk_value(htx, blk);
387 start = ctx->value.ptr - ctx->lws_before;
388 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
389 off = start - v.ptr;
390
391 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
392 if (!blk)
393 return 0;
394
395 v = htx_get_blk_value(htx, blk);
396 ctx->blk = blk;
397 ctx->value.ptr = v.ptr + off;
398 ctx->value.len = data.len;
399 ctx->lws_before = ctx->lws_after = 0;
400
401 return 1;
402}
403
404/* Fully replaces a header referenced in the context <ctx> by the name <name>
405 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
406 * context is updated if necessary.
407 */
408int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
409 const struct ist name, const struct ist value)
410{
411 struct htx_blk *blk = ctx->blk;
412
413 if (!blk)
414 return 0;
415
416 blk = htx_replace_header(htx, blk, name, value);
417 if (!blk)
418 return 0;
419
420 ctx->blk = blk;
421 ctx->value = ist(NULL);
422 ctx->lws_before = ctx->lws_after = 0;
423
424 return 1;
425}
426
427/* Remove one value of a header. This only works on a <ctx> returned by
428 * http_find_header function. The value is removed, as well as surrounding commas
429 * if any. If the removed value was alone, the whole header is removed. The
430 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
431 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
432 * form that can be handled by http_find_header() to find next occurrence.
433 */
434int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
435{
436 struct htx_blk *blk = ctx->blk;
437 char *start;
438 struct ist v;
439 uint32_t len;
440
441 if (!blk)
442 return 0;
443
444 start = ctx->value.ptr - ctx->lws_before;
445 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
446
447 v = htx_get_blk_value(htx, blk);
448 if (len == v.len) {
449 blk = htx_remove_blk(htx, blk);
450 if (blk || !htx->used) {
451 ctx->blk = blk;
452 ctx->value = ist2(NULL, 0);
453 ctx->lws_before = ctx->lws_after = 0;
454 }
455 else {
456 ctx->blk = htx_get_blk(htx, htx->tail);
457 ctx->value = htx_get_blk_value(htx, ctx->blk);
458 ctx->lws_before = ctx->lws_after = 0;
459 }
460 return 1;
461 }
462
463 /* This was not the only value of this header. We have to remove the
464 * part pointed by ctx->value. If it is the last entry of the list, we
465 * remove the last separator.
466 */
467 if (start == v.ptr) {
468 /* It's the first header part but not the only one. So remove
469 * the comma after it. */
470 len++;
471 }
472 else {
473 /* There is at least one header part before the removed one. So
474 * remove the comma between them. */
475 start--;
476 len++;
477 }
478 /* Update the block content and its len */
479 memmove(start, start+len, v.len-len);
480 htx_set_blk_value_len(blk, v.len-len);
481
482 /* Update HTX msg */
483 htx->data -= len;
484
485 /* Finally update the ctx */
486 ctx->value.ptr = start;
487 ctx->value.len = 0;
488 ctx->lws_before = ctx->lws_after = 0;
489
490 return 1;
491}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200492
493
494/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
495 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
496 * performed over the whole headers. Otherwise it must contain a valid header
497 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
498 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
499 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
500 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
501 * -1. The value fetch stops at commas, so this function is suited for use with
502 * list headers.
503 * The return value is 0 if nothing was found, or non-zero otherwise.
504 */
505unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
506 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
507{
508 struct http_hdr_ctx local_ctx;
509 struct ist val_hist[MAX_HDR_HISTORY];
510 unsigned int hist_idx;
511 int found;
512
513 if (!ctx) {
514 local_ctx.blk = NULL;
515 ctx = &local_ctx;
516 }
517
518 if (occ >= 0) {
519 /* search from the beginning */
520 while (http_find_header(htx, hdr, ctx, 0)) {
521 occ--;
522 if (occ <= 0) {
523 *vptr = ctx->value.ptr;
524 *vlen = ctx->value.len;
525 return 1;
526 }
527 }
528 return 0;
529 }
530
531 /* negative occurrence, we scan all the list then walk back */
532 if (-occ > MAX_HDR_HISTORY)
533 return 0;
534
535 found = hist_idx = 0;
536 while (http_find_header(htx, hdr, ctx, 0)) {
537 val_hist[hist_idx] = ctx->value;
538 if (++hist_idx >= MAX_HDR_HISTORY)
539 hist_idx = 0;
540 found++;
541 }
542 if (-occ > found)
543 return 0;
544
545 /* OK now we have the last occurrence in [hist_idx-1], and we need to
546 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
547 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
548 * to remain in the 0..9 range.
549 */
550 hist_idx += occ + MAX_HDR_HISTORY;
551 if (hist_idx >= MAX_HDR_HISTORY)
552 hist_idx -= MAX_HDR_HISTORY;
553 *vptr = val_hist[hist_idx].ptr;
554 *vlen = val_hist[hist_idx].len;
555 return 1;
556}
557
558/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
559 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
560 * performed over the whole headers. Otherwise it must contain a valid header
561 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
562 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
563 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
564 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
565 * -1. This function differs from http_get_hdr() in that it only returns full
566 * line header values and does not stop at commas.
567 * The return value is 0 if nothing was found, or non-zero otherwise.
568 */
569unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
570 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
571{
572 struct http_hdr_ctx local_ctx;
573 struct ist val_hist[MAX_HDR_HISTORY];
574 unsigned int hist_idx;
575 int found;
576
577 if (!ctx) {
578 local_ctx.blk = NULL;
579 ctx = &local_ctx;
580 }
581
582 if (occ >= 0) {
583 /* search from the beginning */
584 while (http_find_header(htx, hdr, ctx, 1)) {
585 occ--;
586 if (occ <= 0) {
587 *vptr = ctx->value.ptr;
588 *vlen = ctx->value.len;
589 return 1;
590 }
591 }
592 return 0;
593 }
594
595 /* negative occurrence, we scan all the list then walk back */
596 if (-occ > MAX_HDR_HISTORY)
597 return 0;
598
599 found = hist_idx = 0;
600 while (http_find_header(htx, hdr, ctx, 1)) {
601 val_hist[hist_idx] = ctx->value;
602 if (++hist_idx >= MAX_HDR_HISTORY)
603 hist_idx = 0;
604 found++;
605 }
606 if (-occ > found)
607 return 0;
608
609 /* OK now we have the last occurrence in [hist_idx-1], and we need to
610 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
611 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
612 * to remain in the 0..9 range.
613 */
614 hist_idx += occ + MAX_HDR_HISTORY;
615 if (hist_idx >= MAX_HDR_HISTORY)
616 hist_idx -= MAX_HDR_HISTORY;
617 *vptr = val_hist[hist_idx].ptr;
618 *vlen = val_hist[hist_idx].len;
619 return 1;
620}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100621
622static struct htx *http_str_to_htx(struct buffer *buf, struct ist raw)
623{
624 struct htx *htx;
625 struct htx_sl *sl;
626 struct h1m h1m;
627 struct http_hdr hdrs[MAX_HTTP_HDR];
628 union h1_sl h1sl;
629 unsigned int flags = HTX_SL_F_IS_RESP;
630 int ret = 0;
631
632 buf->size = global.tune.bufsize;
633 buf->area = (char *)malloc(buf->size);
634 if (!buf->area)
635 goto error;
636 b_reset(buf);
637
638 h1m_init_res(&h1m);
639 h1m.flags |= H1_MF_NO_PHDR;
640 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
641 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
642 if (ret <= 0)
643 goto error;
644
645 if (unlikely(h1sl.st.v.len != 8))
646 goto error;
647 if ((*(h1sl.st.v.ptr + 5) > '1') ||
648 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
649 h1m.flags |= H1_MF_VER_11;
650
651 if (h1m.flags & H1_MF_VER_11)
652 flags |= HTX_SL_F_VER_11;
653 if (h1m.flags & H1_MF_XFER_ENC)
654 flags |= HTX_SL_F_XFER_ENC;
655 if (h1m.flags & H1_MF_XFER_LEN) {
656 flags |= HTX_SL_F_XFER_LEN;
657 if (h1m.flags & H1_MF_CHNK)
658 goto error; /* Unsupported because there is no body parsing */
659 else if (h1m.flags & H1_MF_CLEN) {
660 flags |= HTX_SL_F_CLEN;
661 if (h1m.body_len == 0)
662 flags |= HTX_SL_F_BODYLESS;
663 }
664 }
665
666 htx = htx_from_buf(buf);
667 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
668 if (!sl || !htx_add_all_headers(htx, hdrs))
669 goto error;
670 sl->info.res.status = h1sl.st.status;
671
672 if (raw.len > ret) {
673 if (!htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret)))
674 goto error;
675 }
676 if (!htx_add_endof(htx, HTX_BLK_EOM))
677 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100678 return htx;
679
680error:
681 if (buf->size)
682 free(buf->area);
683 return NULL;
684}
685
686static int http_htx_init(void)
687{
688 struct proxy *px;
689 struct buffer chk;
690 struct ist raw;
691 int rc;
692 int err_code = 0;
693
694 for (px = proxies_list; px; px = px->next) {
Christopher Faulet49040582019-04-24 15:25:00 +0200695 if (px->mode != PR_MODE_HTTP || !(px->options2 & PR_O2_USE_HTX))
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100696 continue;
697
698 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
699 if (!b_data(&px->errmsg[rc]))
700 continue;
701
702 raw = ist2(b_head(&px->errmsg[rc]), b_data(&px->errmsg[rc]));
703 if (!http_str_to_htx(&chk, raw)) {
704 ha_alert("config: %s '%s': Unable to convert message in HTX for HTTP return code %d.\n",
705 proxy_type_str(px), px->id, http_err_codes[rc]);
706 err_code |= ERR_ALERT | ERR_FATAL;
707 }
708 chunk_destroy(&px->errmsg[rc]);
709 px->errmsg[rc] = chk;
710 }
711 }
712
713 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
714 if (!http_err_msgs[rc]) {
715 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
716 err_code |= ERR_ALERT | ERR_FATAL;
717 continue;
718 }
719
720 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
721 if (!http_str_to_htx(&chk, raw)) {
722 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
723 http_err_codes[rc]);
724 err_code |= ERR_ALERT | ERR_FATAL;
725 }
726 htx_err_chunks[rc] = chk;
727 }
728end:
729 return err_code;
730}
731
732REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);