blob: 9892fd32ac05aaa356be5402e6c50234cd59c6c2 [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 Faulet29f17582019-05-23 11:03:26 +020014#include <common/debug.h>
Christopher Fauleta7b677c2018-11-29 16:48:49 +010015#include <common/cfgparse.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010016#include <common/h1.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020017#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010018#include <common/htx.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020019
20#include <proto/http_htx.h>
Christopher Faulet47596d32018-10-22 09:17:28 +020021
Christopher Fauleta7b677c2018-11-29 16:48:49 +010022struct buffer htx_err_chunks[HTTP_ERR_SIZE];
23
Christopher Faulet297fbb42019-05-13 14:41:27 +020024/* Returns the next unporocessed start line in the HTX message. It returns NULL
Christopher Faulet29f17582019-05-23 11:03:26 +020025 * if the start-line is undefined (first == -1). Otherwise, it returns the
Christopher Faulet297fbb42019-05-13 14:41:27 +020026 * pointer on the htx_sl structure.
Christopher Faulet47596d32018-10-22 09:17:28 +020027 */
Christopher Faulet297fbb42019-05-13 14:41:27 +020028struct htx_sl *http_get_stline(struct htx *htx)
Christopher Faulet47596d32018-10-22 09:17:28 +020029{
Christopher Faulet297fbb42019-05-13 14:41:27 +020030 struct htx_blk *blk;
Christopher Faulet573fe732018-11-28 16:55:12 +010031
Christopher Faulet29f17582019-05-23 11:03:26 +020032 BUG_ON(htx->first == -1);
33 blk = htx_get_first_blk(htx);
Christopher Faulet297fbb42019-05-13 14:41:27 +020034 if (!blk)
35 return NULL;
Christopher Faulet29f17582019-05-23 11:03:26 +020036 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL && htx_get_blk_type(blk) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +020037 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 Fauleta3f15502019-05-13 15:27:23 +020083 for (blk = htx_get_first_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. */
Christopher Faulet29f17582019-05-23 11:03:26 +0200146 for (prev = htx_get_prev(htx, htx->tail); prev != htx->first; prev = htx_get_prev(htx, prev)) {
Christopher Faulet47596d32018-10-22 09:17:28 +0200147 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
Christopher Faulet29f17582019-05-23 11:03:26 +0200172 * success, otherwise it returns 0.
Christopher Faulet47596d32018-10-22 09:17:28 +0200173 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100174int 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 +0200175{
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200176 struct htx_blk *blk;
Christopher Faulet47596d32018-10-22 09:17:28 +0200177
Christopher Faulet29f17582019-05-23 11:03:26 +0200178 blk = htx_get_first_blk(htx);
179 if (!blk || !htx_replace_stline(htx, blk, p1, p2, p3))
Christopher Faulet7b7d5072019-05-13 15:22:59 +0200180 return 0;
181 return 1;
Christopher Faulet47596d32018-10-22 09:17:28 +0200182}
183
Christopher Faulete010c802018-10-24 10:36:45 +0200184/* Replace the request method in the HTX message <htx> by <meth>. It returns 1
185 * on success, otherwise 0.
186 */
187int http_replace_req_meth(struct htx *htx, const struct ist meth)
188{
189 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200190 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100191 struct ist uri, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200192
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100193 if (!sl)
194 return 0;
195
Christopher Faulete010c802018-10-24 10:36:45 +0200196 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100197 chunk_memcat(temp, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); /* uri */
198 uri = ist2(temp->area, HTX_SL_REQ_ULEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200199
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100200 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
201 vsn = ist2(temp->area + uri.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200202
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100203 /* create the new start line */
204 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
205 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200206}
207
208/* Replace the request uri in the HTX message <htx> by <uri>. It returns 1 on
209 * success, otherwise 0.
210 */
211int http_replace_req_uri(struct htx *htx, const struct ist uri)
212{
213 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200214 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100215 struct ist meth, vsn;
Christopher Faulete010c802018-10-24 10:36:45 +0200216
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100217 if (!sl)
218 return 0;
219
Christopher Faulete010c802018-10-24 10:36:45 +0200220 /* Start by copying old method and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100221 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
222 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200223
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100224 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
225 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200226
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100227 /* create the new start line */
228 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200229}
230
231/* Replace the request path in the HTX message <htx> by <path>. The host part
232 * and the query string are preserved. It returns 1 on success, otherwise 0.
233 */
234int http_replace_req_path(struct htx *htx, const struct ist path)
235{
236 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200237 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100238 struct ist meth, uri, vsn, p;
Christopher Faulete010c802018-10-24 10:36:45 +0200239 size_t plen = 0;
240
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100241 if (!sl)
242 return 0;
243
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100244 uri = htx_sl_req_uri(sl);
245 p = http_get_path(uri);
Christopher Faulete010c802018-10-24 10:36:45 +0200246 if (!p.ptr)
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100247 p = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200248 while (plen < p.len && *(p.ptr + plen) != '?')
249 plen++;
250
251 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100252 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
253 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200254
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100255 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
256 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
257
258 chunk_memcat(temp, uri.ptr, p.ptr - uri.ptr); /* uri: host part */
Christopher Faulete010c802018-10-24 10:36:45 +0200259 chunk_memcat(temp, path.ptr, path.len); /* uri: new path */
260 chunk_memcat(temp, p.ptr + plen, p.len - plen); /* uri: QS part */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100261 uri = ist2(temp->area + meth.len + vsn.len, uri.len - plen + path.len);
Christopher Faulete010c802018-10-24 10:36:45 +0200262
263 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100264 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200265}
266
267/* Replace the request query-string in the HTX message <htx> by <query>. The
268 * host part and the path are preserved. It returns 1 on success, otherwise
269 * 0.
270 */
271int http_replace_req_query(struct htx *htx, const struct ist query)
272{
273 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200274 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100275 struct ist meth, uri, vsn, q;
Christopher Faulete010c802018-10-24 10:36:45 +0200276 int offset = 1;
277
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100278 if (!sl)
279 return 0;
280
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100281 uri = htx_sl_req_uri(sl);
282 q = uri;
Christopher Faulete010c802018-10-24 10:36:45 +0200283 while (q.len > 0 && *(q.ptr) != '?') {
284 q.ptr++;
285 q.len--;
286 }
287
288 /* skip the question mark or indicate that we must insert it
289 * (but only if the format string is not empty then).
290 */
291 if (q.len) {
292 q.ptr++;
293 q.len--;
294 }
295 else if (query.len > 1)
296 offset = 0;
297
298 /* Start by copying old method and version and create the new uri */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100299 chunk_memcat(temp, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); /* meth */
300 meth = ist2(temp->area, HTX_SL_REQ_MLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200301
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100302 chunk_memcat(temp, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); /* vsn */
303 vsn = ist2(temp->area + meth.len, HTX_SL_REQ_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200304
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100305 chunk_memcat(temp, uri.ptr, q.ptr - uri.ptr); /* uri: host + path part */
306 chunk_memcat(temp, query.ptr + offset, query.len - offset); /* uri: new QS */
307 uri = ist2(temp->area + meth.len + vsn.len, uri.len - q.len + query.len - offset);
Christopher Faulete010c802018-10-24 10:36:45 +0200308
309 /* create the new start line */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100310 return http_replace_stline(htx, meth, uri, vsn);
Christopher Faulete010c802018-10-24 10:36:45 +0200311}
312
313/* Replace the response status in the HTX message <htx> by <status>. It returns
314 * 1 on success, otherwise 0.
315*/
316int http_replace_res_status(struct htx *htx, const struct ist status)
317{
318 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200319 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100320 struct ist vsn, reason;
Christopher Faulete010c802018-10-24 10:36:45 +0200321
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100322 if (!sl)
323 return 0;
324
Christopher Faulete010c802018-10-24 10:36:45 +0200325 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100326 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
327 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200328
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100329 chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
330 reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200331
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100332 /* create the new start line */
333 sl->info.res.status = strl2ui(status.ptr, status.len);
334 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200335}
336
337/* Replace the response reason in the HTX message <htx> by <reason>. It returns
338 * 1 on success, otherwise 0.
339*/
340int http_replace_res_reason(struct htx *htx, const struct ist reason)
341{
342 struct buffer *temp = get_trash_chunk();
Christopher Faulet297fbb42019-05-13 14:41:27 +0200343 struct htx_sl *sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100344 struct ist vsn, status;
Christopher Faulete010c802018-10-24 10:36:45 +0200345
Willy Tarreaucdce54c2019-02-12 12:02:27 +0100346 if (!sl)
347 return 0;
348
Christopher Faulete010c802018-10-24 10:36:45 +0200349 /* Start by copying old uri and version */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100350 chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
351 vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200352
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100353 chunk_memcat(temp, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); /* code */
354 status = ist2(temp->area + vsn.len, HTX_SL_RES_CLEN(sl));
Christopher Faulete010c802018-10-24 10:36:45 +0200355
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100356 /* create the new start line */
357 return http_replace_stline(htx, vsn, status, reason);
Christopher Faulete010c802018-10-24 10:36:45 +0200358}
359
Christopher Faulet47596d32018-10-22 09:17:28 +0200360/* Replaces a part of a header value referenced in the context <ctx> by
361 * <data>. It returns 1 on success, otherwise it returns 0. The context is
362 * updated if necessary.
363 */
364int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
365{
366 struct htx_blk *blk = ctx->blk;
367 char *start;
368 struct ist v;
369 uint32_t len, off;
370
371 if (!blk)
372 return 0;
373
374 v = htx_get_blk_value(htx, blk);
375 start = ctx->value.ptr - ctx->lws_before;
376 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
377 off = start - v.ptr;
378
379 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
380 if (!blk)
381 return 0;
382
383 v = htx_get_blk_value(htx, blk);
384 ctx->blk = blk;
385 ctx->value.ptr = v.ptr + off;
386 ctx->value.len = data.len;
387 ctx->lws_before = ctx->lws_after = 0;
388
389 return 1;
390}
391
392/* Fully replaces a header referenced in the context <ctx> by the name <name>
393 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
394 * context is updated if necessary.
395 */
396int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
397 const struct ist name, const struct ist value)
398{
399 struct htx_blk *blk = ctx->blk;
400
401 if (!blk)
402 return 0;
403
404 blk = htx_replace_header(htx, blk, name, value);
405 if (!blk)
406 return 0;
407
408 ctx->blk = blk;
409 ctx->value = ist(NULL);
410 ctx->lws_before = ctx->lws_after = 0;
411
412 return 1;
413}
414
415/* Remove one value of a header. This only works on a <ctx> returned by
416 * http_find_header function. The value is removed, as well as surrounding commas
417 * if any. If the removed value was alone, the whole header is removed. The
418 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
419 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
420 * form that can be handled by http_find_header() to find next occurrence.
421 */
422int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
423{
424 struct htx_blk *blk = ctx->blk;
425 char *start;
426 struct ist v;
427 uint32_t len;
428
429 if (!blk)
430 return 0;
431
432 start = ctx->value.ptr - ctx->lws_before;
433 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
434
435 v = htx_get_blk_value(htx, blk);
436 if (len == v.len) {
437 blk = htx_remove_blk(htx, blk);
438 if (blk || !htx->used) {
439 ctx->blk = blk;
440 ctx->value = ist2(NULL, 0);
441 ctx->lws_before = ctx->lws_after = 0;
442 }
443 else {
444 ctx->blk = htx_get_blk(htx, htx->tail);
445 ctx->value = htx_get_blk_value(htx, ctx->blk);
446 ctx->lws_before = ctx->lws_after = 0;
447 }
448 return 1;
449 }
450
451 /* This was not the only value of this header. We have to remove the
452 * part pointed by ctx->value. If it is the last entry of the list, we
453 * remove the last separator.
454 */
455 if (start == v.ptr) {
456 /* It's the first header part but not the only one. So remove
457 * the comma after it. */
458 len++;
459 }
460 else {
461 /* There is at least one header part before the removed one. So
462 * remove the comma between them. */
463 start--;
464 len++;
465 }
466 /* Update the block content and its len */
467 memmove(start, start+len, v.len-len);
468 htx_set_blk_value_len(blk, v.len-len);
469
470 /* Update HTX msg */
471 htx->data -= len;
472
473 /* Finally update the ctx */
474 ctx->value.ptr = start;
475 ctx->value.len = 0;
476 ctx->lws_before = ctx->lws_after = 0;
477
478 return 1;
479}
Christopher Faulet7ff1cea2018-10-24 10:39:35 +0200480
481
482/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
483 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
484 * performed over the whole headers. Otherwise it must contain a valid header
485 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
486 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
487 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
488 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
489 * -1. The value fetch stops at commas, so this function is suited for use with
490 * list headers.
491 * The return value is 0 if nothing was found, or non-zero otherwise.
492 */
493unsigned int http_get_htx_hdr(const struct htx *htx, const struct ist hdr,
494 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
495{
496 struct http_hdr_ctx local_ctx;
497 struct ist val_hist[MAX_HDR_HISTORY];
498 unsigned int hist_idx;
499 int found;
500
501 if (!ctx) {
502 local_ctx.blk = NULL;
503 ctx = &local_ctx;
504 }
505
506 if (occ >= 0) {
507 /* search from the beginning */
508 while (http_find_header(htx, hdr, ctx, 0)) {
509 occ--;
510 if (occ <= 0) {
511 *vptr = ctx->value.ptr;
512 *vlen = ctx->value.len;
513 return 1;
514 }
515 }
516 return 0;
517 }
518
519 /* negative occurrence, we scan all the list then walk back */
520 if (-occ > MAX_HDR_HISTORY)
521 return 0;
522
523 found = hist_idx = 0;
524 while (http_find_header(htx, hdr, ctx, 0)) {
525 val_hist[hist_idx] = ctx->value;
526 if (++hist_idx >= MAX_HDR_HISTORY)
527 hist_idx = 0;
528 found++;
529 }
530 if (-occ > found)
531 return 0;
532
533 /* OK now we have the last occurrence in [hist_idx-1], and we need to
534 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
535 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
536 * to remain in the 0..9 range.
537 */
538 hist_idx += occ + MAX_HDR_HISTORY;
539 if (hist_idx >= MAX_HDR_HISTORY)
540 hist_idx -= MAX_HDR_HISTORY;
541 *vptr = val_hist[hist_idx].ptr;
542 *vlen = val_hist[hist_idx].len;
543 return 1;
544}
545
546/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
547 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
548 * performed over the whole headers. Otherwise it must contain a valid header
549 * context, initialised with ctx->blk=NULL for the first lookup in a series. If
550 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
551 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
552 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
553 * -1. This function differs from http_get_hdr() in that it only returns full
554 * line header values and does not stop at commas.
555 * The return value is 0 if nothing was found, or non-zero otherwise.
556 */
557unsigned int http_get_htx_fhdr(const struct htx *htx, const struct ist hdr,
558 int occ, struct http_hdr_ctx *ctx, char **vptr, size_t *vlen)
559{
560 struct http_hdr_ctx local_ctx;
561 struct ist val_hist[MAX_HDR_HISTORY];
562 unsigned int hist_idx;
563 int found;
564
565 if (!ctx) {
566 local_ctx.blk = NULL;
567 ctx = &local_ctx;
568 }
569
570 if (occ >= 0) {
571 /* search from the beginning */
572 while (http_find_header(htx, hdr, ctx, 1)) {
573 occ--;
574 if (occ <= 0) {
575 *vptr = ctx->value.ptr;
576 *vlen = ctx->value.len;
577 return 1;
578 }
579 }
580 return 0;
581 }
582
583 /* negative occurrence, we scan all the list then walk back */
584 if (-occ > MAX_HDR_HISTORY)
585 return 0;
586
587 found = hist_idx = 0;
588 while (http_find_header(htx, hdr, ctx, 1)) {
589 val_hist[hist_idx] = ctx->value;
590 if (++hist_idx >= MAX_HDR_HISTORY)
591 hist_idx = 0;
592 found++;
593 }
594 if (-occ > found)
595 return 0;
596
597 /* OK now we have the last occurrence in [hist_idx-1], and we need to
598 * find occurrence -occ. 0 <= hist_idx < MAX_HDR_HISTORY, and we have
599 * -10 <= occ <= -1. So we have to check [hist_idx%MAX_HDR_HISTORY+occ]
600 * to remain in the 0..9 range.
601 */
602 hist_idx += occ + MAX_HDR_HISTORY;
603 if (hist_idx >= MAX_HDR_HISTORY)
604 hist_idx -= MAX_HDR_HISTORY;
605 *vptr = val_hist[hist_idx].ptr;
606 *vlen = val_hist[hist_idx].len;
607 return 1;
608}
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100609
610static struct htx *http_str_to_htx(struct buffer *buf, struct ist raw)
611{
612 struct htx *htx;
613 struct htx_sl *sl;
614 struct h1m h1m;
615 struct http_hdr hdrs[MAX_HTTP_HDR];
616 union h1_sl h1sl;
617 unsigned int flags = HTX_SL_F_IS_RESP;
618 int ret = 0;
619
620 buf->size = global.tune.bufsize;
621 buf->area = (char *)malloc(buf->size);
622 if (!buf->area)
623 goto error;
624 b_reset(buf);
625
626 h1m_init_res(&h1m);
627 h1m.flags |= H1_MF_NO_PHDR;
628 ret = h1_headers_to_hdr_list(raw.ptr, raw.ptr + raw.len,
629 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
630 if (ret <= 0)
631 goto error;
632
633 if (unlikely(h1sl.st.v.len != 8))
634 goto error;
635 if ((*(h1sl.st.v.ptr + 5) > '1') ||
636 ((*(h1sl.st.v.ptr + 5) == '1') && (*(h1sl.st.v.ptr + 7) >= '1')))
637 h1m.flags |= H1_MF_VER_11;
638
639 if (h1m.flags & H1_MF_VER_11)
640 flags |= HTX_SL_F_VER_11;
641 if (h1m.flags & H1_MF_XFER_ENC)
642 flags |= HTX_SL_F_XFER_ENC;
643 if (h1m.flags & H1_MF_XFER_LEN) {
644 flags |= HTX_SL_F_XFER_LEN;
645 if (h1m.flags & H1_MF_CHNK)
646 goto error; /* Unsupported because there is no body parsing */
647 else if (h1m.flags & H1_MF_CLEN) {
648 flags |= HTX_SL_F_CLEN;
649 if (h1m.body_len == 0)
650 flags |= HTX_SL_F_BODYLESS;
651 }
652 }
653
654 htx = htx_from_buf(buf);
655 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r);
656 if (!sl || !htx_add_all_headers(htx, hdrs))
657 goto error;
658 sl->info.res.status = h1sl.st.status;
659
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200660 while (raw.len > ret) {
661 int sent = htx_add_data(htx, ist2(raw.ptr + ret, raw.len - ret));
662 if (!sent)
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100663 goto error;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200664 ret += sent;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100665 }
666 if (!htx_add_endof(htx, HTX_BLK_EOM))
667 goto error;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100668 return htx;
669
670error:
671 if (buf->size)
672 free(buf->area);
673 return NULL;
674}
675
676static int http_htx_init(void)
677{
678 struct proxy *px;
679 struct buffer chk;
680 struct ist raw;
681 int rc;
682 int err_code = 0;
683
684 for (px = proxies_list; px; px = px->next) {
Christopher Faulet49040582019-04-24 15:25:00 +0200685 if (px->mode != PR_MODE_HTTP || !(px->options2 & PR_O2_USE_HTX))
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100686 continue;
687
688 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
689 if (!b_data(&px->errmsg[rc]))
690 continue;
691
692 raw = ist2(b_head(&px->errmsg[rc]), b_data(&px->errmsg[rc]));
693 if (!http_str_to_htx(&chk, raw)) {
694 ha_alert("config: %s '%s': Unable to convert message in HTX for HTTP return code %d.\n",
695 proxy_type_str(px), px->id, http_err_codes[rc]);
696 err_code |= ERR_ALERT | ERR_FATAL;
697 }
698 chunk_destroy(&px->errmsg[rc]);
699 px->errmsg[rc] = chk;
700 }
701 }
702
703 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
704 if (!http_err_msgs[rc]) {
705 ha_alert("Internal error: no message defined for HTTP return code %d", rc);
706 err_code |= ERR_ALERT | ERR_FATAL;
707 continue;
708 }
709
710 raw = ist2(http_err_msgs[rc], strlen(http_err_msgs[rc]));
711 if (!http_str_to_htx(&chk, raw)) {
712 ha_alert("Internal error: Unable to convert message in HTX for HTTP return code %d.\n",
713 http_err_codes[rc]);
714 err_code |= ERR_ALERT | ERR_FATAL;
715 }
716 htx_err_chunks[rc] = chk;
717 }
718end:
719 return err_code;
720}
721
722REGISTER_CONFIG_POSTPARSER("http_htx", http_htx_init);