blob: 47e36c85607161a93678d9f527e853753214b238 [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>
14#include <common/http.h>
15
16#include <proto/http_htx.h>
17#include <proto/htx.h>
18
19/* Finds the start line in the HTX message stopping at the first
20 * end-of-message. It returns an empty start line when not found, otherwise, it
21 * returns the corresponding <struct h1_sl>.
22 */
23union h1_sl http_find_stline(const struct htx *htx)
24{
25 union htx_sl *htx_sl;
26 union h1_sl sl;
27 int32_t pos;
28
29 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
30 struct htx_blk *blk = htx_get_blk(htx, pos);
31 enum htx_blk_type type = htx_get_blk_type(blk);
32
33 if (type == HTX_BLK_REQ_SL) {
34 htx_sl = htx_get_blk_ptr(htx, blk);
35 sl.rq.meth = htx_sl->rq.meth;
36 sl.rq.m = ist2(htx_sl->rq.l, htx_sl->rq.m_len);
37 sl.rq.u = ist2(htx_sl->rq.l + htx_sl->rq.m_len, htx_sl->rq.u_len);
38 sl.rq.v = ist2(htx_sl->rq.l + htx_sl->rq.m_len + htx_sl->rq.u_len, htx_sl->rq.v_len);
39 return sl;
40 }
41
42 if (type == HTX_BLK_RES_SL) {
43 htx_sl = htx_get_blk_ptr(htx, blk);
44 sl.st.status = htx_sl->st.status;
45 sl.st.v = ist2(htx_sl->st.l, htx_sl->st.v_len);
46 sl.st.c = ist2(htx_sl->st.l + htx_sl->st.v_len, htx_sl->st.c_len);
47 sl.st.r = ist2(htx_sl->st.l + htx_sl->st.v_len + htx_sl->st.c_len, htx_sl->st.r_len);
48 return sl;
49 }
50 if (type == HTX_BLK_EOM)
51 break;
52 }
53
54 sl.rq.m = ist("");
55 sl.rq.u = ist("");
56 sl.rq.v = ist("");
57 return sl;
58}
59
60/* Finds the first or next occurrence of header <name> in the HTX message <htx>
61 * using the context <ctx>. This structure holds everything necessary to use the
62 * header and find next occurrence. If its <blk> member is NULL, the header is
63 * searched from the beginning. Otherwise, the next occurrence is returned. The
64 * function returns 1 when it finds a value, and 0 when there is no more. It is
65 * designed to work with headers defined as comma-separated lists. If <full> is
66 * set, it works on full-line headers in whose comma is not a delimiter but is
67 * part of the syntax. A special case, if ctx->value is NULL when searching for
68 * a new values of a header, the current header is rescanned. This allows
69 * rescanning after a header deletion.
70 */
71int http_find_header(const struct htx *htx, const struct ist name,
72 struct http_hdr_ctx *ctx, int full)
73{
74 struct htx_blk *blk = ctx->blk;
75 struct ist n, v;
76 enum htx_blk_type type;
77 uint32_t pos;
78
79 if (blk) {
80 char *p;
81
82 pos = htx_get_blk_pos(htx, blk);
83 if (!ctx->value.ptr)
84 goto rescan_hdr;
85 if (full)
86 goto next_blk;
87 v = htx_get_blk_value(htx, blk);
88 p = ctx->value.ptr + ctx->value.len + ctx->lws_after;
89 v.len -= (p - v.ptr);
90 v.ptr = p;
91 if (!v.len)
92 goto next_blk;
93 /* Skip comma */
94 if (*(v.ptr) == ',') {
95 v.ptr++;
96 v.len--;
97 }
98
99 goto return_hdr;
100 }
101
102 if (!htx->used)
103 return 0;
104
105 pos = htx_get_head(htx);
106 while (1) {
107 rescan_hdr:
108 blk = htx_get_blk(htx, pos);
109 type = htx_get_blk_type(blk);
110 if (type != HTX_BLK_HDR)
111 goto next_blk;
112 if (name.len) {
113 /* If no name was passed, we want any header. So skip the comparison */
114 n = htx_get_blk_name(htx, blk);
115 if (!isteqi(n, name))
116 goto next_blk;
117 }
118 v = htx_get_blk_value(htx, blk);
119
120 return_hdr:
121 ctx->lws_before = 0;
122 ctx->lws_after = 0;
123 while (v.len && HTTP_IS_LWS(*v.ptr)) {
124 v.ptr++;
125 v.len--;
126 ctx->lws_before++;
127 }
128 if (!full)
129 v.len = http_find_hdr_value_end(v.ptr, v.ptr + v.len) - v.ptr;
130 while (v.len && HTTP_IS_LWS(*(v.ptr + v.len - 1))) {
131 v.len--;
132 ctx->lws_after++;
133 }
134 if (!v.len)
135 goto next_blk;
136 ctx->blk = blk;
137 ctx->value = v;
138 return 1;
139
140 next_blk:
141 if (pos == htx->tail)
142 break;
143 pos++;
144 if (pos >= htx->wrap)
145 pos = 0;
146 }
147
148 ctx->blk = NULL;
149 ctx->value = ist("");
150 ctx->lws_before = ctx->lws_after = 0;
151 return 0;
152}
153
154/* Adds a header block int the HTX message <htx>, just before the EOH block. It
155 * returns 1 on success, otherwise it returns 0.
156 */
157int http_add_header(struct htx *htx, const struct ist n, const struct ist v)
158{
159 struct htx_blk *blk;
160 enum htx_blk_type type = htx_get_tail_type(htx);
161 int32_t prev;
162
163 blk = htx_add_header(htx, n, v);
164 if (!blk)
165 return 0;
166
167 if (unlikely(type < HTX_BLK_EOH))
168 return 1;
169
170 /* <blk> is the head, swap it iteratively with its predecessor to place
171 * it just before the end-of-header block. So blocks remains ordered. */
172 for (prev = htx_get_prev(htx, htx->tail); prev != -1; prev = htx_get_prev(htx, prev)) {
173 struct htx_blk *pblk = htx_get_blk(htx, prev);
174 enum htx_blk_type type = htx_get_blk_type(pblk);
175
176 /* Swap .addr and .info fields */
177 blk->addr ^= pblk->addr; pblk->addr ^= blk->addr; blk->addr ^= pblk->addr;
178 blk->info ^= pblk->info; pblk->info ^= blk->info; blk->info ^= pblk->info;
179
180 if (blk->addr == pblk->addr)
181 blk->addr += htx_get_blksz(pblk);
182 htx->front = prev;
183
184 /* Stop when end-of-header is reached */
185 if (type == HTX_BLK_EOH)
186 break;
187
188 blk = pblk;
189 }
190 return 1;
191}
192
193/* Replaces the request start line of the HTX message <htx> by <sl>. It returns
194 * 1 on success, otherwise it returns 0. The start line must be found in the
195 * message.
196 */
197int http_replace_reqline(struct htx *htx, const union h1_sl sl)
198{
199 int32_t pos;
200
201 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
202 struct htx_blk *blk = htx_get_blk(htx, pos);
203 enum htx_blk_type type = htx_get_blk_type(blk);
204
205 if (type == HTX_BLK_REQ_SL) {
206 blk = htx_replace_reqline(htx, blk, sl);
207 if (!blk)
208 return 0;
209 return 1;
210 }
211 if (type == HTX_BLK_EOM)
212 break;
213 }
214
215 return 0;
216}
217
218
219/* Replaces the response start line of the HTX message <htx> by <sl>. It returns
220 * 1 on success, otherwise it returns 0. The start line must be found in the
221 * message.
222 */
223int http_replace_resline(struct htx *htx, const union h1_sl sl)
224{
225 int32_t pos;
226
227 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
228 struct htx_blk *blk = htx_get_blk(htx, pos);
229 enum htx_blk_type type = htx_get_blk_type(blk);
230
231 if (type == HTX_BLK_RES_SL) {
232 blk = htx_replace_resline(htx, blk, sl);
233 if (!blk)
234 return 0;
235 return 1;
236 }
237 if (type == HTX_BLK_EOM)
238 break;
239 }
240
241 return 0;
242}
243
244/* Replaces a part of a header value referenced in the context <ctx> by
245 * <data>. It returns 1 on success, otherwise it returns 0. The context is
246 * updated if necessary.
247 */
248int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data)
249{
250 struct htx_blk *blk = ctx->blk;
251 char *start;
252 struct ist v;
253 uint32_t len, off;
254
255 if (!blk)
256 return 0;
257
258 v = htx_get_blk_value(htx, blk);
259 start = ctx->value.ptr - ctx->lws_before;
260 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
261 off = start - v.ptr;
262
263 blk = htx_replace_blk_value(htx, blk, ist2(start, len), data);
264 if (!blk)
265 return 0;
266
267 v = htx_get_blk_value(htx, blk);
268 ctx->blk = blk;
269 ctx->value.ptr = v.ptr + off;
270 ctx->value.len = data.len;
271 ctx->lws_before = ctx->lws_after = 0;
272
273 return 1;
274}
275
276/* Fully replaces a header referenced in the context <ctx> by the name <name>
277 * with the value <value>. It returns 1 on success, otherwise it returns 0. The
278 * context is updated if necessary.
279 */
280int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx,
281 const struct ist name, const struct ist value)
282{
283 struct htx_blk *blk = ctx->blk;
284
285 if (!blk)
286 return 0;
287
288 blk = htx_replace_header(htx, blk, name, value);
289 if (!blk)
290 return 0;
291
292 ctx->blk = blk;
293 ctx->value = ist(NULL);
294 ctx->lws_before = ctx->lws_after = 0;
295
296 return 1;
297}
298
299/* Remove one value of a header. This only works on a <ctx> returned by
300 * http_find_header function. The value is removed, as well as surrounding commas
301 * if any. If the removed value was alone, the whole header is removed. The
302 * <ctx> is always updated accordingly, as well as the HTX message <htx>. It
303 * returns 1 on success. Otherwise, it returns 0. The <ctx> is always left in a
304 * form that can be handled by http_find_header() to find next occurrence.
305 */
306int http_remove_header(struct htx *htx, struct http_hdr_ctx *ctx)
307{
308 struct htx_blk *blk = ctx->blk;
309 char *start;
310 struct ist v;
311 uint32_t len;
312
313 if (!blk)
314 return 0;
315
316 start = ctx->value.ptr - ctx->lws_before;
317 len = ctx->lws_before + ctx->value.len + ctx->lws_after;
318
319 v = htx_get_blk_value(htx, blk);
320 if (len == v.len) {
321 blk = htx_remove_blk(htx, blk);
322 if (blk || !htx->used) {
323 ctx->blk = blk;
324 ctx->value = ist2(NULL, 0);
325 ctx->lws_before = ctx->lws_after = 0;
326 }
327 else {
328 ctx->blk = htx_get_blk(htx, htx->tail);
329 ctx->value = htx_get_blk_value(htx, ctx->blk);
330 ctx->lws_before = ctx->lws_after = 0;
331 }
332 return 1;
333 }
334
335 /* This was not the only value of this header. We have to remove the
336 * part pointed by ctx->value. If it is the last entry of the list, we
337 * remove the last separator.
338 */
339 if (start == v.ptr) {
340 /* It's the first header part but not the only one. So remove
341 * the comma after it. */
342 len++;
343 }
344 else {
345 /* There is at least one header part before the removed one. So
346 * remove the comma between them. */
347 start--;
348 len++;
349 }
350 /* Update the block content and its len */
351 memmove(start, start+len, v.len-len);
352 htx_set_blk_value_len(blk, v.len-len);
353
354 /* Update HTX msg */
355 htx->data -= len;
356
357 /* Finally update the ctx */
358 ctx->value.ptr = start;
359 ctx->value.len = 0;
360 ctx->lws_before = ctx->lws_after = 0;
361
362 return 1;
363}