blob: 123d371c4dcf4a837dcdccf5563cd0681c4b7d31 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau8797c062007-05-07 00:55:35 +020044#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020045#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010046#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020048#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010049#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020050#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020053#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010055#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010056#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020057#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010059#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020061#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010062#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020063#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010064#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020065#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020066#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020067#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau522d6c02009-12-06 18:49:18 +010069const char HTTP_100[] =
70 "HTTP/1.1 100 Continue\r\n\r\n";
71
72const struct chunk http_100_chunk = {
73 .str = (char *)&HTTP_100,
74 .len = sizeof(HTTP_100)-1
75};
76
Willy Tarreaua9679ac2010-01-03 17:32:57 +010077/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040096
97/* same as 302 except that the browser MUST retry with the same method */
98const char *HTTP_307 =
99 "HTTP/1.1 307 Temporary Redirect\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Content-length: 0\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
104/* same as 301 except that the browser MUST retry with the same method */
105const char *HTTP_308 =
106 "HTTP/1.1 308 Permanent Redirect\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau844a7e72010-01-31 21:46:18 +0100120const char *HTTP_407_fmt =
121 "HTTP/1.0 407 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
124 "Content-Type: text/html\r\n"
125 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800127 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400134 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400136 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100137 [HTTP_ERR_500] = 500,
138 [HTTP_ERR_502] = 502,
139 [HTTP_ERR_503] = 503,
140 [HTTP_ERR_504] = 504,
141};
142
Willy Tarreau80587432006-12-24 17:47:20 +0100143static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200144 [HTTP_ERR_200] =
145 "HTTP/1.0 200 OK\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
151
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100153 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
159
160 [HTTP_ERR_403] =
161 "HTTP/1.0 403 Forbidden\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
167
CJ Ess108b1dd2015-04-07 12:03:37 -0400168 [HTTP_ERR_405] =
169 "HTTP/1.0 405 Method Not Allowed\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
175
Willy Tarreau0f772532006-12-23 20:51:41 +0100176 [HTTP_ERR_408] =
177 "HTTP/1.0 408 Request Time-out\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
183
CJ Ess108b1dd2015-04-07 12:03:37 -0400184 [HTTP_ERR_429] =
185 "HTTP/1.0 429 Too Many Requests\r\n"
186 "Cache-Control: no-cache\r\n"
187 "Connection: close\r\n"
188 "Content-Type: text/html\r\n"
189 "\r\n"
190 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
191
Willy Tarreau0f772532006-12-23 20:51:41 +0100192 [HTTP_ERR_500] =
193 "HTTP/1.0 500 Server Error\r\n"
194 "Cache-Control: no-cache\r\n"
195 "Connection: close\r\n"
196 "Content-Type: text/html\r\n"
197 "\r\n"
198 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
199
200 [HTTP_ERR_502] =
201 "HTTP/1.0 502 Bad Gateway\r\n"
202 "Cache-Control: no-cache\r\n"
203 "Connection: close\r\n"
204 "Content-Type: text/html\r\n"
205 "\r\n"
206 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
207
208 [HTTP_ERR_503] =
209 "HTTP/1.0 503 Service Unavailable\r\n"
210 "Cache-Control: no-cache\r\n"
211 "Connection: close\r\n"
212 "Content-Type: text/html\r\n"
213 "\r\n"
214 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
215
216 [HTTP_ERR_504] =
217 "HTTP/1.0 504 Gateway Time-out\r\n"
218 "Cache-Control: no-cache\r\n"
219 "Connection: close\r\n"
220 "Content-Type: text/html\r\n"
221 "\r\n"
222 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
223
224};
225
Cyril Bonté19979e12012-04-04 12:57:21 +0200226/* status codes available for the stats admin page (strictly 4 chars length) */
227const char *stat_status_codes[STAT_STATUS_SIZE] = {
228 [STAT_STATUS_DENY] = "DENY",
229 [STAT_STATUS_DONE] = "DONE",
230 [STAT_STATUS_ERRP] = "ERRP",
231 [STAT_STATUS_EXCD] = "EXCD",
232 [STAT_STATUS_NONE] = "NONE",
233 [STAT_STATUS_PART] = "PART",
234 [STAT_STATUS_UNKN] = "UNKN",
235};
236
237
William Lallemand73025dd2014-04-24 14:38:37 +0200238/* List head of all known action keywords for "http-request" */
239struct http_req_action_kw_list http_req_keywords = {
240 .list = LIST_HEAD_INIT(http_req_keywords.list)
241};
242
243/* List head of all known action keywords for "http-response" */
244struct http_res_action_kw_list http_res_keywords = {
245 .list = LIST_HEAD_INIT(http_res_keywords.list)
246};
247
Willy Tarreau80587432006-12-24 17:47:20 +0100248/* We must put the messages here since GCC cannot initialize consts depending
249 * on strlen().
250 */
251struct chunk http_err_chunks[HTTP_ERR_SIZE];
252
Willy Tarreaua890d072013-04-02 12:01:06 +0200253/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
254static struct hdr_ctx static_hdr_ctx;
255
Willy Tarreau42250582007-04-01 01:30:43 +0200256#define FD_SETS_ARE_BITFIELDS
257#ifdef FD_SETS_ARE_BITFIELDS
258/*
259 * This map is used with all the FD_* macros to check whether a particular bit
260 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
261 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
262 * byte should be encoded. Be careful to always pass bytes from 0 to 255
263 * exclusively to the macros.
264 */
265fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
266fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100267fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200268
269#else
270#error "Check if your OS uses bitfields for fd_sets"
271#endif
272
Willy Tarreau87b09662015-04-03 00:22:06 +0200273static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200274
Willy Tarreau80587432006-12-24 17:47:20 +0100275void init_proto_http()
276{
Willy Tarreau42250582007-04-01 01:30:43 +0200277 int i;
278 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100279 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200280
Willy Tarreau80587432006-12-24 17:47:20 +0100281 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
282 if (!http_err_msgs[msg]) {
283 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
284 abort();
285 }
286
287 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
288 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
289 }
Willy Tarreau42250582007-04-01 01:30:43 +0200290
291 /* initialize the log header encoding map : '{|}"#' should be encoded with
292 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
293 * URL encoding only requires '"', '#' to be encoded as well as non-
294 * printable characters above.
295 */
296 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
297 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100298 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200299 for (i = 0; i < 32; i++) {
300 FD_SET(i, hdr_encode_map);
301 FD_SET(i, url_encode_map);
302 }
303 for (i = 127; i < 256; i++) {
304 FD_SET(i, hdr_encode_map);
305 FD_SET(i, url_encode_map);
306 }
307
308 tmp = "\"#{|}";
309 while (*tmp) {
310 FD_SET(*tmp, hdr_encode_map);
311 tmp++;
312 }
313
314 tmp = "\"#";
315 while (*tmp) {
316 FD_SET(*tmp, url_encode_map);
317 tmp++;
318 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200319
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100320 /* initialize the http header encoding map. The draft httpbis define the
321 * header content as:
322 *
323 * HTTP-message = start-line
324 * *( header-field CRLF )
325 * CRLF
326 * [ message-body ]
327 * header-field = field-name ":" OWS field-value OWS
328 * field-value = *( field-content / obs-fold )
329 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
330 * obs-fold = CRLF 1*( SP / HTAB )
331 * field-vchar = VCHAR / obs-text
332 * VCHAR = %x21-7E
333 * obs-text = %x80-FF
334 *
335 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
336 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
337 * "obs-fold" is volontary forgotten because haproxy remove this.
338 */
339 memset(http_encode_map, 0, sizeof(http_encode_map));
340 for (i = 0x00; i <= 0x08; i++)
341 FD_SET(i, http_encode_map);
342 for (i = 0x0a; i <= 0x1f; i++)
343 FD_SET(i, http_encode_map);
344 FD_SET(0x7f, http_encode_map);
345
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200346 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200347 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200348 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100349 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100350}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351
Willy Tarreau53b6c742006-12-17 13:37:46 +0100352/*
353 * We have 26 list of methods (1 per first letter), each of which can have
354 * up to 3 entries (2 valid, 1 null).
355 */
356struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100357 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100358 int len;
359 const char text[8];
360};
361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100362const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100363 ['C' - 'A'] = {
364 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
365 },
366 ['D' - 'A'] = {
367 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
368 },
369 ['G' - 'A'] = {
370 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
371 },
372 ['H' - 'A'] = {
373 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
374 },
375 ['P' - 'A'] = {
376 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
377 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
378 },
379 ['T' - 'A'] = {
380 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
381 },
382 /* rest is empty like this :
383 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
384 */
385};
386
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100387const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
388 [HTTP_METH_NONE] = { "", 0 },
389 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
390 [HTTP_METH_GET] = { "GET", 3 },
391 [HTTP_METH_HEAD] = { "HEAD", 4 },
392 [HTTP_METH_POST] = { "POST", 4 },
393 [HTTP_METH_PUT] = { "PUT", 3 },
394 [HTTP_METH_DELETE] = { "DELETE", 6 },
395 [HTTP_METH_TRACE] = { "TRACE", 5 },
396 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
397};
398
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100399/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200400 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100401 * RFC2616 for those chars.
402 */
403
404const char http_is_spht[256] = {
405 [' '] = 1, ['\t'] = 1,
406};
407
408const char http_is_crlf[256] = {
409 ['\r'] = 1, ['\n'] = 1,
410};
411
412const char http_is_lws[256] = {
413 [' '] = 1, ['\t'] = 1,
414 ['\r'] = 1, ['\n'] = 1,
415};
416
417const char http_is_sep[256] = {
418 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
419 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
420 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
421 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
422 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
423};
424
425const char http_is_ctl[256] = {
426 [0 ... 31] = 1,
427 [127] = 1,
428};
429
430/*
431 * A token is any ASCII char that is neither a separator nor a CTL char.
432 * Do not overwrite values in assignment since gcc-2.95 will not handle
433 * them correctly. Instead, define every non-CTL char's status.
434 */
435const char http_is_token[256] = {
436 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
437 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
438 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
439 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
440 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
441 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
442 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
443 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
444 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
445 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
446 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
447 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
448 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
449 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
450 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
451 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
452 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
453 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
454 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
455 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
456 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
457 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
458 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
459 ['|'] = 1, ['}'] = 0, ['~'] = 1,
460};
461
462
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100463/*
464 * An http ver_token is any ASCII which can be found in an HTTP version,
465 * which includes 'H', 'T', 'P', '/', '.' and any digit.
466 */
467const char http_is_ver_token[256] = {
468 ['.'] = 1, ['/'] = 1,
469 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
470 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100471 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100472};
473
474
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100475/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100476 * Adds a header and its CRLF at the tail of the message's buffer, just before
477 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100478 * The header is also automatically added to the index <hdr_idx>, and the end
479 * of headers is automatically adjusted. The number of bytes added is returned
480 * on success, otherwise <0 is returned indicating an error.
481 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100482int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100483{
484 int bytes, len;
485
486 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200487 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100488 if (!bytes)
489 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100490 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100491 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
492}
493
494/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100495 * Adds a header and its CRLF at the tail of the message's buffer, just before
496 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100497 * the buffer is only opened and the space reserved, but nothing is copied.
498 * The header is also automatically added to the index <hdr_idx>, and the end
499 * of headers is automatically adjusted. The number of bytes added is returned
500 * on success, otherwise <0 is returned indicating an error.
501 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100502int http_header_add_tail2(struct http_msg *msg,
503 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100504{
505 int bytes;
506
Willy Tarreau9b28e032012-10-12 23:49:43 +0200507 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100508 if (!bytes)
509 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100510 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100511 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
512}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200513
514/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100515 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
516 * If so, returns the position of the first non-space character relative to
517 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
518 * to return a pointer to the place after the first space. Returns 0 if the
519 * header name does not match. Checks are case-insensitive.
520 */
521int http_header_match2(const char *hdr, const char *end,
522 const char *name, int len)
523{
524 const char *val;
525
526 if (hdr + len >= end)
527 return 0;
528 if (hdr[len] != ':')
529 return 0;
530 if (strncasecmp(hdr, name, len) != 0)
531 return 0;
532 val = hdr + len + 1;
533 while (val < end && HTTP_IS_SPHT(*val))
534 val++;
535 if ((val >= end) && (len + 2 <= end - hdr))
536 return len + 2; /* we may replace starting from second space */
537 return val - hdr;
538}
539
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200540/* Find the first or next occurrence of header <name> in message buffer <sol>
541 * using headers index <idx>, and return it in the <ctx> structure. This
542 * structure holds everything necessary to use the header and find next
543 * occurrence. If its <idx> member is 0, the header is searched from the
544 * beginning. Otherwise, the next occurrence is returned. The function returns
545 * 1 when it finds a value, and 0 when there is no more. It is very similar to
546 * http_find_header2() except that it is designed to work with full-line headers
547 * whose comma is not a delimiter but is part of the syntax. As a special case,
548 * if ctx->val is NULL when searching for a new values of a header, the current
549 * header is rescanned. This allows rescanning after a header deletion.
550 */
551int http_find_full_header2(const char *name, int len,
552 char *sol, struct hdr_idx *idx,
553 struct hdr_ctx *ctx)
554{
555 char *eol, *sov;
556 int cur_idx, old_idx;
557
558 cur_idx = ctx->idx;
559 if (cur_idx) {
560 /* We have previously returned a header, let's search another one */
561 sol = ctx->line;
562 eol = sol + idx->v[cur_idx].len;
563 goto next_hdr;
564 }
565
566 /* first request for this header */
567 sol += hdr_idx_first_pos(idx);
568 old_idx = 0;
569 cur_idx = hdr_idx_first_idx(idx);
570 while (cur_idx) {
571 eol = sol + idx->v[cur_idx].len;
572
573 if (len == 0) {
574 /* No argument was passed, we want any header.
575 * To achieve this, we simply build a fake request. */
576 while (sol + len < eol && sol[len] != ':')
577 len++;
578 name = sol;
579 }
580
581 if ((len < eol - sol) &&
582 (sol[len] == ':') &&
583 (strncasecmp(sol, name, len) == 0)) {
584 ctx->del = len;
585 sov = sol + len + 1;
586 while (sov < eol && http_is_lws[(unsigned char)*sov])
587 sov++;
588
589 ctx->line = sol;
590 ctx->prev = old_idx;
591 ctx->idx = cur_idx;
592 ctx->val = sov - sol;
593 ctx->tws = 0;
594 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
595 eol--;
596 ctx->tws++;
597 }
598 ctx->vlen = eol - sov;
599 return 1;
600 }
601 next_hdr:
602 sol = eol + idx->v[cur_idx].cr + 1;
603 old_idx = cur_idx;
604 cur_idx = idx->v[cur_idx].next;
605 }
606 return 0;
607}
608
Willy Tarreauc90dc232015-02-20 13:51:36 +0100609/* Find the first or next header field in message buffer <sol> using headers
610 * index <idx>, and return it in the <ctx> structure. This structure holds
611 * everything necessary to use the header and find next occurrence. If its
612 * <idx> member is 0, the first header is retrieved. Otherwise, the next
613 * occurrence is returned. The function returns 1 when it finds a value, and
614 * 0 when there is no more. It is equivalent to http_find_full_header2() with
615 * no header name.
616 */
617int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
618{
619 char *eol, *sov;
620 int cur_idx, old_idx;
621 int len;
622
623 cur_idx = ctx->idx;
624 if (cur_idx) {
625 /* We have previously returned a header, let's search another one */
626 sol = ctx->line;
627 eol = sol + idx->v[cur_idx].len;
628 goto next_hdr;
629 }
630
631 /* first request for this header */
632 sol += hdr_idx_first_pos(idx);
633 old_idx = 0;
634 cur_idx = hdr_idx_first_idx(idx);
635 while (cur_idx) {
636 eol = sol + idx->v[cur_idx].len;
637
638 len = 0;
639 while (1) {
640 if (len >= eol - sol)
641 goto next_hdr;
642 if (sol[len] == ':')
643 break;
644 len++;
645 }
646
647 ctx->del = len;
648 sov = sol + len + 1;
649 while (sov < eol && http_is_lws[(unsigned char)*sov])
650 sov++;
651
652 ctx->line = sol;
653 ctx->prev = old_idx;
654 ctx->idx = cur_idx;
655 ctx->val = sov - sol;
656 ctx->tws = 0;
657
658 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
659 eol--;
660 ctx->tws++;
661 }
662 ctx->vlen = eol - sov;
663 return 1;
664
665 next_hdr:
666 sol = eol + idx->v[cur_idx].cr + 1;
667 old_idx = cur_idx;
668 cur_idx = idx->v[cur_idx].next;
669 }
670 return 0;
671}
672
Willy Tarreau68085d82010-01-18 14:54:04 +0100673/* Find the end of the header value contained between <s> and <e>. See RFC2616,
674 * par 2.2 for more information. Note that it requires a valid header to return
675 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200676 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100677char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200678{
679 int quoted, qdpair;
680
681 quoted = qdpair = 0;
682 for (; s < e; s++) {
683 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200684 else if (quoted) {
685 if (*s == '\\') qdpair = 1;
686 else if (*s == '"') quoted = 0;
687 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200688 else if (*s == '"') quoted = 1;
689 else if (*s == ',') return s;
690 }
691 return s;
692}
693
694/* Find the first or next occurrence of header <name> in message buffer <sol>
695 * using headers index <idx>, and return it in the <ctx> structure. This
696 * structure holds everything necessary to use the header and find next
697 * occurrence. If its <idx> member is 0, the header is searched from the
698 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100699 * 1 when it finds a value, and 0 when there is no more. It is designed to work
700 * with headers defined as comma-separated lists. As a special case, if ctx->val
701 * is NULL when searching for a new values of a header, the current header is
702 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200703 */
704int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100705 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200706 struct hdr_ctx *ctx)
707{
Willy Tarreau68085d82010-01-18 14:54:04 +0100708 char *eol, *sov;
709 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200710
Willy Tarreau68085d82010-01-18 14:54:04 +0100711 cur_idx = ctx->idx;
712 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200713 /* We have previously returned a value, let's search
714 * another one on the same line.
715 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200716 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200717 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100718 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200719 eol = sol + idx->v[cur_idx].len;
720
721 if (sov >= eol)
722 /* no more values in this header */
723 goto next_hdr;
724
Willy Tarreau68085d82010-01-18 14:54:04 +0100725 /* values remaining for this header, skip the comma but save it
726 * for later use (eg: for header deletion).
727 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200728 sov++;
729 while (sov < eol && http_is_lws[(unsigned char)*sov])
730 sov++;
731
732 goto return_hdr;
733 }
734
735 /* first request for this header */
736 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100737 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200738 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200739 while (cur_idx) {
740 eol = sol + idx->v[cur_idx].len;
741
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200742 if (len == 0) {
743 /* No argument was passed, we want any header.
744 * To achieve this, we simply build a fake request. */
745 while (sol + len < eol && sol[len] != ':')
746 len++;
747 name = sol;
748 }
749
Willy Tarreau33a7e692007-06-10 19:45:56 +0200750 if ((len < eol - sol) &&
751 (sol[len] == ':') &&
752 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100753 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200754 sov = sol + len + 1;
755 while (sov < eol && http_is_lws[(unsigned char)*sov])
756 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100757
Willy Tarreau33a7e692007-06-10 19:45:56 +0200758 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100759 ctx->prev = old_idx;
760 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200761 ctx->idx = cur_idx;
762 ctx->val = sov - sol;
763
764 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200765 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200766 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200767 eol--;
768 ctx->tws++;
769 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200770 ctx->vlen = eol - sov;
771 return 1;
772 }
773 next_hdr:
774 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100775 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200776 cur_idx = idx->v[cur_idx].next;
777 }
778 return 0;
779}
780
781int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100782 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200783 struct hdr_ctx *ctx)
784{
785 return http_find_header2(name, strlen(name), sol, idx, ctx);
786}
787
Willy Tarreau68085d82010-01-18 14:54:04 +0100788/* Remove one value of a header. This only works on a <ctx> returned by one of
789 * the http_find_header functions. The value is removed, as well as surrounding
790 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100791 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100792 * message <msg>. The new index is returned. If it is zero, it means there is
793 * no more header, so any processing may stop. The ctx is always left in a form
794 * that can be handled by http_find_header2() to find next occurrence.
795 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100796int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100797{
798 int cur_idx = ctx->idx;
799 char *sol = ctx->line;
800 struct hdr_idx_elem *hdr;
801 int delta, skip_comma;
802
803 if (!cur_idx)
804 return 0;
805
806 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200807 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100808 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200809 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100810 http_msg_move_end(msg, delta);
811 idx->used--;
812 hdr->len = 0; /* unused entry */
813 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100814 if (idx->tail == ctx->idx)
815 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100816 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100817 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100818 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200819 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100820 return ctx->idx;
821 }
822
823 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200824 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
825 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100826 */
827
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200828 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200829 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200830 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100831 NULL, 0);
832 hdr->len += delta;
833 http_msg_move_end(msg, delta);
834 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200835 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100836 return ctx->idx;
837}
838
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100839/* This function handles a server error at the stream interface level. The
840 * stream interface is assumed to be already in a closed state. An optional
841 * message is copied into the input buffer, and an HTTP status code stored.
842 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100843 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200845static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100846 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100848 channel_auto_read(si_oc(si));
849 channel_abort(si_oc(si));
850 channel_auto_close(si_oc(si));
851 channel_erase(si_oc(si));
852 channel_auto_close(si_ic(si));
853 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100854 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200855 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100856 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200857 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200858 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200859 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200860 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200861 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200862}
863
Willy Tarreau87b09662015-04-03 00:22:06 +0200864/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100865 * and message.
866 */
867
Willy Tarreau87b09662015-04-03 00:22:06 +0200868struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100869{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200870 if (s->be->errmsg[msgnum].str)
871 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200872 else if (strm_fe(s)->errmsg[msgnum].str)
873 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100874 else
875 return &http_err_chunks[msgnum];
876}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200877
Willy Tarreau53b6c742006-12-17 13:37:46 +0100878/*
879 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
880 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
881 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100882enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100883{
884 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100885 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100886
887 m = ((unsigned)*str - 'A');
888
889 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100890 for (h = http_methods[m]; h->len > 0; h++) {
891 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100892 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100893 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100894 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100895 };
896 return HTTP_METH_OTHER;
897 }
898 return HTTP_METH_NONE;
899
900}
901
Willy Tarreau21d2af32008-02-14 20:25:24 +0100902/* Parse the URI from the given transaction (which is assumed to be in request
903 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
904 * It is returned otherwise.
905 */
906static char *
907http_get_path(struct http_txn *txn)
908{
909 char *ptr, *end;
910
Willy Tarreau9b28e032012-10-12 23:49:43 +0200911 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100912 end = ptr + txn->req.sl.rq.u_l;
913
914 if (ptr >= end)
915 return NULL;
916
917 /* RFC2616, par. 5.1.2 :
918 * Request-URI = "*" | absuri | abspath | authority
919 */
920
921 if (*ptr == '*')
922 return NULL;
923
924 if (isalpha((unsigned char)*ptr)) {
925 /* this is a scheme as described by RFC3986, par. 3.1 */
926 ptr++;
927 while (ptr < end &&
928 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
929 ptr++;
930 /* skip '://' */
931 if (ptr == end || *ptr++ != ':')
932 return NULL;
933 if (ptr == end || *ptr++ != '/')
934 return NULL;
935 if (ptr == end || *ptr++ != '/')
936 return NULL;
937 }
938 /* skip [user[:passwd]@]host[:[port]] */
939
940 while (ptr < end && *ptr != '/')
941 ptr++;
942
943 if (ptr == end)
944 return NULL;
945
946 /* OK, we got the '/' ! */
947 return ptr;
948}
949
William Lallemand65ad6e12014-01-31 15:08:02 +0100950/* Parse the URI from the given string and look for the "/" beginning the PATH.
951 * If not found, return NULL. It is returned otherwise.
952 */
953static char *
954http_get_path_from_string(char *str)
955{
956 char *ptr = str;
957
958 /* RFC2616, par. 5.1.2 :
959 * Request-URI = "*" | absuri | abspath | authority
960 */
961
962 if (*ptr == '*')
963 return NULL;
964
965 if (isalpha((unsigned char)*ptr)) {
966 /* this is a scheme as described by RFC3986, par. 3.1 */
967 ptr++;
968 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
969 ptr++;
970 /* skip '://' */
971 if (*ptr == '\0' || *ptr++ != ':')
972 return NULL;
973 if (*ptr == '\0' || *ptr++ != '/')
974 return NULL;
975 if (*ptr == '\0' || *ptr++ != '/')
976 return NULL;
977 }
978 /* skip [user[:passwd]@]host[:[port]] */
979
980 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
981 ptr++;
982
983 if (*ptr == '\0' || *ptr == ' ')
984 return NULL;
985
986 /* OK, we got the '/' ! */
987 return ptr;
988}
989
Willy Tarreau71241ab2012-12-27 11:30:54 +0100990/* Returns a 302 for a redirectable request that reaches a server working in
991 * in redirect mode. This may only be called just after the stream interface
992 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
993 * follow normal proxy processing. NOTE: this function is designed to support
994 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100995 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200996void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100997{
998 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100999 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001000 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001001 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001002
1003 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001004 trash.len = strlen(HTTP_302);
1005 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001007 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001008
Willy Tarreauefb453c2008-10-26 20:49:47 +01001009 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001010 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001011 return;
1012
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001013 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001014 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001015 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1016 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001017 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001018
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001019 /* 3: add the request URI. Since it was already forwarded, we need
1020 * to temporarily rewind the buffer.
1021 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001022 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001023 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001024
Willy Tarreauefb453c2008-10-26 20:49:47 +01001025 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001026 len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001027
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001028 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001029
Willy Tarreauefb453c2008-10-26 20:49:47 +01001030 if (!path)
1031 return;
1032
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001033 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001034 return;
1035
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001036 memcpy(trash.str + trash.len, path, len);
1037 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001038
1039 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001040 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1041 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001042 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001043 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1044 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001045 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001046
1047 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001048 si_shutr(si);
1049 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001050 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001051 si->state = SI_ST_CLO;
1052
1053 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001054 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001055
1056 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001057 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001058 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001059}
1060
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001061/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001062 * that the server side is closed. Note that err_type is actually a
1063 * bitmask, where almost only aborts may be cumulated with other
1064 * values. We consider that aborted operations are more important
1065 * than timeouts or errors due to the fact that nobody else in the
1066 * logs might explain incomplete retries. All others should avoid
1067 * being cumulated. It should normally not be possible to have multiple
1068 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001069 * Note that connection errors appearing on the second request of a keep-alive
1070 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001071 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001072void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001073{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001074 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001075
1076 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001077 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001078 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001079 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001080 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001081 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001082 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001083 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001084 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001085 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001087 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001088 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001089 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001090 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001091 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001092 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001093 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001094 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1095 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001096 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001097 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001098 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001099 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001100 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001101 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001102 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001103 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001104}
1105
Willy Tarreau42250582007-04-01 01:30:43 +02001106extern const char sess_term_cond[8];
1107extern const char sess_fin_state[8];
1108extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001109struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001110struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001111struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001112struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113
Willy Tarreau117f59e2007-03-04 18:17:17 +01001114/*
1115 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001116 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001117 */
1118void capture_headers(char *som, struct hdr_idx *idx,
1119 char **cap, struct cap_hdr *cap_hdr)
1120{
1121 char *eol, *sol, *col, *sov;
1122 int cur_idx;
1123 struct cap_hdr *h;
1124 int len;
1125
1126 sol = som + hdr_idx_first_pos(idx);
1127 cur_idx = hdr_idx_first_idx(idx);
1128
1129 while (cur_idx) {
1130 eol = sol + idx->v[cur_idx].len;
1131
1132 col = sol;
1133 while (col < eol && *col != ':')
1134 col++;
1135
1136 sov = col + 1;
1137 while (sov < eol && http_is_lws[(unsigned char)*sov])
1138 sov++;
1139
1140 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001141 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001142 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1143 if (cap[h->index] == NULL)
1144 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001145 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001146
1147 if (cap[h->index] == NULL) {
1148 Alert("HTTP capture : out of memory.\n");
1149 continue;
1150 }
1151
1152 len = eol - sov;
1153 if (len > h->len)
1154 len = h->len;
1155
1156 memcpy(cap[h->index], sov, len);
1157 cap[h->index][len]=0;
1158 }
1159 }
1160 sol = eol + idx->v[cur_idx].cr + 1;
1161 cur_idx = idx->v[cur_idx].next;
1162 }
1163}
1164
1165
Willy Tarreau42250582007-04-01 01:30:43 +02001166/* either we find an LF at <ptr> or we jump to <bad>.
1167 */
1168#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1169
1170/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1171 * otherwise to <http_msg_ood> with <state> set to <st>.
1172 */
1173#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1174 ptr++; \
1175 if (likely(ptr < end)) \
1176 goto good; \
1177 else { \
1178 state = (st); \
1179 goto http_msg_ood; \
1180 } \
1181 } while (0)
1182
1183
Willy Tarreaubaaee002006-06-26 02:48:02 +02001184/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001185 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001186 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1187 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1188 * will give undefined results.
1189 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1190 * and that msg->sol points to the beginning of the response.
1191 * If a complete line is found (which implies that at least one CR or LF is
1192 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1193 * returned indicating an incomplete line (which does not mean that parts have
1194 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1195 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1196 * upon next call.
1197 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001198 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001199 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1200 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001201 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001202 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001203const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001204 enum ht_state state, const char *ptr, const char *end,
1205 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001206{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001207 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001208
Willy Tarreau8973c702007-01-21 23:58:29 +01001209 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001210 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001211 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001212 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001213 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1214
1215 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001216 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001217 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1218 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001219 state = HTTP_MSG_ERROR;
1220 break;
1221
Willy Tarreau8973c702007-01-21 23:58:29 +01001222 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001223 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001224 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001225 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001226 goto http_msg_rpcode;
1227 }
1228 if (likely(HTTP_IS_SPHT(*ptr)))
1229 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1230 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001231 state = HTTP_MSG_ERROR;
1232 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001233
Willy Tarreau8973c702007-01-21 23:58:29 +01001234 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001235 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001236 if (likely(!HTTP_IS_LWS(*ptr)))
1237 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1238
1239 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001240 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001241 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1242 }
1243
1244 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001245 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001246 http_msg_rsp_reason:
1247 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001248 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001249 msg->sl.st.r_l = 0;
1250 goto http_msg_rpline_eol;
1251
Willy Tarreau8973c702007-01-21 23:58:29 +01001252 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001253 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001254 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001255 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001256 goto http_msg_rpreason;
1257 }
1258 if (likely(HTTP_IS_SPHT(*ptr)))
1259 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1260 /* so it's a CR/LF, so there is no reason phrase */
1261 goto http_msg_rsp_reason;
1262
Willy Tarreau8973c702007-01-21 23:58:29 +01001263 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001264 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001265 if (likely(!HTTP_IS_CRLF(*ptr)))
1266 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001267 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 http_msg_rpline_eol:
1269 /* We have seen the end of line. Note that we do not
1270 * necessarily have the \n yet, but at least we know that we
1271 * have EITHER \r OR \n, otherwise the response would not be
1272 * complete. We can then record the response length and return
1273 * to the caller which will be able to register it.
1274 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001275 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 return ptr;
1277
Willy Tarreau8973c702007-01-21 23:58:29 +01001278 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001279#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001280 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1281 exit(1);
1282#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001283 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 }
1285
1286 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001287 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001288 if (ret_state)
1289 *ret_state = state;
1290 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001291 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001292 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001293}
1294
Willy Tarreau8973c702007-01-21 23:58:29 +01001295/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001296 * This function parses a request line between <ptr> and <end>, starting with
1297 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1298 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1299 * will give undefined results.
1300 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1301 * and that msg->sol points to the beginning of the request.
1302 * If a complete line is found (which implies that at least one CR or LF is
1303 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1304 * returned indicating an incomplete line (which does not mean that parts have
1305 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1306 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1307 * upon next call.
1308 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001309 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1311 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001312 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001313 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001314const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001315 enum ht_state state, const char *ptr, const char *end,
1316 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001317{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001318 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001322 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001323 if (likely(HTTP_IS_TOKEN(*ptr)))
1324 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001327 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1329 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001330
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 if (likely(HTTP_IS_CRLF(*ptr))) {
1332 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001333 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001334 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001335 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001336 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001337 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001338 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001339 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 msg->sl.rq.v_l = 0;
1341 goto http_msg_rqline_eol;
1342 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001343 state = HTTP_MSG_ERROR;
1344 break;
1345
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001346 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001347 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001348 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001349 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 goto http_msg_rquri;
1351 }
1352 if (likely(HTTP_IS_SPHT(*ptr)))
1353 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1354 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1355 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001357 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001358 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001359 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001363 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1365 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001366
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001367 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001368 /* non-ASCII chars are forbidden unless option
1369 * accept-invalid-http-request is enabled in the frontend.
1370 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001371 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001372 if (msg->err_pos < -1)
1373 goto invalid_char;
1374 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001375 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001376 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1377 }
1378
1379 if (likely(HTTP_IS_CRLF(*ptr))) {
1380 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1381 goto http_msg_req09_uri_e;
1382 }
1383
1384 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001385 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001386 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001387 state = HTTP_MSG_ERROR;
1388 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001391 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001393 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 goto http_msg_rqver;
1395 }
1396 if (likely(HTTP_IS_SPHT(*ptr)))
1397 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1398 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1399 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001402 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001403 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001405
1406 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001407 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001408 http_msg_rqline_eol:
1409 /* We have seen the end of line. Note that we do not
1410 * necessarily have the \n yet, but at least we know that we
1411 * have EITHER \r OR \n, otherwise the request would not be
1412 * complete. We can then record the request length and return
1413 * to the caller which will be able to register it.
1414 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001415 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001416 return ptr;
1417 }
1418
1419 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001420 state = HTTP_MSG_ERROR;
1421 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001424#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1426 exit(1);
1427#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001428 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001430
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001432 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 if (ret_state)
1434 *ret_state = state;
1435 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001436 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001439
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001440/*
1441 * Returns the data from Authorization header. Function may be called more
1442 * than once so data is stored in txn->auth_data. When no header is found
1443 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001444 * searching again for something we are unable to find anyway. However, if
1445 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001446 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001447 */
1448
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001449/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1450 * set according to global.tune.bufsize.
1451 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001452char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001453
1454int
Willy Tarreau87b09662015-04-03 00:22:06 +02001455get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001456{
1457
Willy Tarreaueee5b512015-04-03 23:46:31 +02001458 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001459 struct chunk auth_method;
1460 struct hdr_ctx ctx;
1461 char *h, *p;
1462 int len;
1463
1464#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001465 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001466#endif
1467
1468 if (txn->auth.method == HTTP_AUTH_WRONG)
1469 return 0;
1470
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001471 txn->auth.method = HTTP_AUTH_WRONG;
1472
1473 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001474
1475 if (txn->flags & TX_USE_PX_CONN) {
1476 h = "Proxy-Authorization";
1477 len = strlen(h);
1478 } else {
1479 h = "Authorization";
1480 len = strlen(h);
1481 }
1482
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001483 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001484 return 0;
1485
1486 h = ctx.line + ctx.val;
1487
1488 p = memchr(h, ' ', ctx.vlen);
1489 if (!p || p == h)
1490 return 0;
1491
1492 chunk_initlen(&auth_method, h, 0, p-h);
1493 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1494
1495 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1496
1497 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001498 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001499
1500 if (len < 0)
1501 return 0;
1502
1503
1504 get_http_auth_buff[len] = '\0';
1505
1506 p = strchr(get_http_auth_buff, ':');
1507
1508 if (!p)
1509 return 0;
1510
1511 txn->auth.user = get_http_auth_buff;
1512 *p = '\0';
1513 txn->auth.pass = p+1;
1514
1515 txn->auth.method = HTTP_AUTH_BASIC;
1516 return 1;
1517 }
1518
1519 return 0;
1520}
1521
Willy Tarreau58f10d72006-12-04 02:26:12 +01001522
Willy Tarreau8973c702007-01-21 23:58:29 +01001523/*
1524 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001525 * depending on the initial msg->msg_state. The caller is responsible for
1526 * ensuring that the message does not wrap. The function can be preempted
1527 * everywhere when data are missing and recalled at the exact same location
1528 * with no information loss. The message may even be realigned between two
1529 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001530 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001531 * fields. Note that msg->sol will be initialized after completing the first
1532 * state, so that none of the msg pointers has to be initialized prior to the
1533 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001534 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001535void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536{
Willy Tarreau3770f232013-12-07 00:01:53 +01001537 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001539 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001540
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001541 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001542 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001543 ptr = buf->p + msg->next;
1544 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 if (unlikely(ptr >= end))
1547 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001548
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001550 /*
1551 * First, states that are specific to the response only.
1552 * We check them first so that request and headers are
1553 * closer to each other (accessed more often).
1554 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001555 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001556 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001557 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001558 /* we have a start of message, but we have to check
1559 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001560 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001561 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001562 if (unlikely(ptr != buf->p)) {
1563 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001564 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001565 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001566 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001567 }
Willy Tarreau26927362012-05-18 23:22:52 +02001568 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001569 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001570 hdr_idx_init(idx);
1571 state = HTTP_MSG_RPVER;
1572 goto http_msg_rpver;
1573 }
1574
1575 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1576 goto http_msg_invalid;
1577
1578 if (unlikely(*ptr == '\n'))
1579 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1580 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1581 /* stop here */
1582
Willy Tarreau8973c702007-01-21 23:58:29 +01001583 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001584 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001585 EXPECT_LF_HERE(ptr, http_msg_invalid);
1586 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1587 /* stop here */
1588
Willy Tarreau8973c702007-01-21 23:58:29 +01001589 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001590 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001591 case HTTP_MSG_RPVER_SP:
1592 case HTTP_MSG_RPCODE:
1593 case HTTP_MSG_RPCODE_SP:
1594 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001595 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001596 state, ptr, end,
1597 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001598 if (unlikely(!ptr))
1599 return;
1600
1601 /* we have a full response and we know that we have either a CR
1602 * or an LF at <ptr>.
1603 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001604 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1605
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001606 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001607 if (likely(*ptr == '\r'))
1608 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1609 goto http_msg_rpline_end;
1610
Willy Tarreau8973c702007-01-21 23:58:29 +01001611 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001612 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001613 /* msg->sol must point to the first of CR or LF. */
1614 EXPECT_LF_HERE(ptr, http_msg_invalid);
1615 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1616 /* stop here */
1617
1618 /*
1619 * Second, states that are specific to the request only
1620 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001622 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001624 /* we have a start of message, but we have to check
1625 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001626 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001627 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001628 if (likely(ptr != buf->p)) {
1629 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001630 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001631 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001632 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001633 }
Willy Tarreau26927362012-05-18 23:22:52 +02001634 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001635 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001636 state = HTTP_MSG_RQMETH;
1637 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1641 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 if (unlikely(*ptr == '\n'))
1644 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1645 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001646 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001648 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001649 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001650 EXPECT_LF_HERE(ptr, http_msg_invalid);
1651 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001652 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001655 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 case HTTP_MSG_RQMETH_SP:
1657 case HTTP_MSG_RQURI:
1658 case HTTP_MSG_RQURI_SP:
1659 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001660 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001661 state, ptr, end,
1662 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 if (unlikely(!ptr))
1664 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 /* we have a full request and we know that we have either a CR
1667 * or an LF at <ptr>.
1668 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001670
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001671 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001672 if (likely(*ptr == '\r'))
1673 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001675
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001677 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001678 /* check for HTTP/0.9 request : no version information available.
1679 * msg->sol must point to the first of CR or LF.
1680 */
1681 if (unlikely(msg->sl.rq.v_l == 0))
1682 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001683
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 EXPECT_LF_HERE(ptr, http_msg_invalid);
1685 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001686 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001687
Willy Tarreau8973c702007-01-21 23:58:29 +01001688 /*
1689 * Common states below
1690 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001691 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001692 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001693 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 if (likely(!HTTP_IS_CRLF(*ptr))) {
1695 goto http_msg_hdr_name;
1696 }
1697
1698 if (likely(*ptr == '\r'))
1699 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1700 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001701
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001702 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001703 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 /* assumes msg->sol points to the first char */
1705 if (likely(HTTP_IS_TOKEN(*ptr)))
1706 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001707
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001708 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001710
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001711 if (likely(msg->err_pos < -1) || *ptr == '\n')
1712 goto http_msg_invalid;
1713
1714 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001715 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001716
1717 /* and we still accept this non-token character */
1718 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001719
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001721 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001722 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 if (likely(HTTP_IS_SPHT(*ptr)))
1724 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001725
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001727 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001728
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 if (likely(!HTTP_IS_CRLF(*ptr))) {
1730 goto http_msg_hdr_val;
1731 }
1732
1733 if (likely(*ptr == '\r'))
1734 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1735 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001736
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001737 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001738 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 EXPECT_LF_HERE(ptr, http_msg_invalid);
1740 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001741
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001743 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 if (likely(HTTP_IS_SPHT(*ptr))) {
1745 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001746 for (; buf->p + msg->sov < ptr; msg->sov++)
1747 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 goto http_msg_hdr_l1_sp;
1749 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001750 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001751 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 goto http_msg_complete_header;
1753
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001755 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001756 /* assumes msg->sol points to the first char, and msg->sov
1757 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 */
1759 if (likely(!HTTP_IS_CRLF(*ptr)))
1760 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001761
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001762 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 /* Note: we could also copy eol into ->eoh so that we have the
1764 * real header end in case it ends with lots of LWS, but is this
1765 * really needed ?
1766 */
1767 if (likely(*ptr == '\r'))
1768 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1769 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001770
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001772 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 EXPECT_LF_HERE(ptr, http_msg_invalid);
1774 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001775
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001777 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1779 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001780 for (; buf->p + msg->eol < ptr; msg->eol++)
1781 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001782 goto http_msg_hdr_val;
1783 }
1784 http_msg_complete_header:
1785 /*
1786 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001787 * Assumes msg->sol points to the first char, msg->sov points
1788 * to the first character of the value and msg->eol to the
1789 * first CR or LF so we know how the line ends. We insert last
1790 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001792 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 idx, idx->tail) < 0))
1794 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001795
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001796 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 if (likely(!HTTP_IS_CRLF(*ptr))) {
1798 goto http_msg_hdr_name;
1799 }
1800
1801 if (likely(*ptr == '\r'))
1802 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1803 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001806 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001807 /* Assumes msg->sol points to the first of either CR or LF.
1808 * Sets ->sov and ->next to the total header length, ->eoh to
1809 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1810 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811 EXPECT_LF_HERE(ptr, http_msg_invalid);
1812 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001813 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001814 msg->eoh = msg->sol;
1815 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001816 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001817 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001818 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001819
1820 case HTTP_MSG_ERROR:
1821 /* this may only happen if we call http_msg_analyser() twice with an error */
1822 break;
1823
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001825#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1827 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001828#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001829 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 }
1831 http_msg_ood:
1832 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001833 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001834 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001836
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001837 http_msg_invalid:
1838 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001839 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001840 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001841 return;
1842}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001843
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001844/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1845 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1846 * nothing is done and 1 is returned.
1847 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001848static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001849{
1850 int delta;
1851 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001852 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001853
1854 if (msg->sl.rq.v_l != 0)
1855 return 1;
1856
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001857 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1858 if (txn->meth != HTTP_METH_GET)
1859 return 0;
1860
Willy Tarreau9b28e032012-10-12 23:49:43 +02001861 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001862 delta = 0;
1863
1864 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001865 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1866 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001867 }
1868 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001869 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001870 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001871 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001872 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001873 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001874 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001875 NULL, NULL);
1876 if (unlikely(!cur_end))
1877 return 0;
1878
1879 /* we have a full HTTP/1.0 request now and we know that
1880 * we have either a CR or an LF at <ptr>.
1881 */
1882 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1883 return 1;
1884}
1885
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001886/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001887 * and "keep-alive" values. If we already know that some headers may safely
1888 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001889 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1890 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001891 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001892 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1893 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1894 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001895 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001896 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001897void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001898{
Willy Tarreau5b154472009-12-21 20:11:07 +01001899 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001900 const char *hdr_val = "Connection";
1901 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001902
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001903 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001904 return;
1905
Willy Tarreau88d349d2010-01-25 12:15:43 +01001906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection";
1908 hdr_len = 16;
1909 }
1910
Willy Tarreau5b154472009-12-21 20:11:07 +01001911 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001912 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001913 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001914 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1915 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001916 if (to_del & 2)
1917 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001918 else
1919 txn->flags |= TX_CON_KAL_SET;
1920 }
1921 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1922 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001923 if (to_del & 1)
1924 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001925 else
1926 txn->flags |= TX_CON_CLO_SET;
1927 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001928 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1929 txn->flags |= TX_HDR_CONN_UPG;
1930 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001931 }
1932
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001933 txn->flags |= TX_HDR_CONN_PRS;
1934 return;
1935}
Willy Tarreau5b154472009-12-21 20:11:07 +01001936
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001937/* Apply desired changes on the Connection: header. Values may be removed and/or
1938 * added depending on the <wanted> flags, which are exclusively composed of
1939 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1940 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1941 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001942void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001943{
1944 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001945 const char *hdr_val = "Connection";
1946 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001947
1948 ctx.idx = 0;
1949
Willy Tarreau88d349d2010-01-25 12:15:43 +01001950
1951 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1952 hdr_val = "Proxy-Connection";
1953 hdr_len = 16;
1954 }
1955
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001956 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001957 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001958 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1959 if (wanted & TX_CON_KAL_SET)
1960 txn->flags |= TX_CON_KAL_SET;
1961 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001962 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001963 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001964 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1965 if (wanted & TX_CON_CLO_SET)
1966 txn->flags |= TX_CON_CLO_SET;
1967 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001968 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001969 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001970 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001971
1972 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1973 return;
1974
1975 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1976 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001977 hdr_val = "Connection: close";
1978 hdr_len = 17;
1979 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1980 hdr_val = "Proxy-Connection: close";
1981 hdr_len = 23;
1982 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001983 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001984 }
1985
1986 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1987 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001988 hdr_val = "Connection: keep-alive";
1989 hdr_len = 22;
1990 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1991 hdr_val = "Proxy-Connection: keep-alive";
1992 hdr_len = 28;
1993 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001994 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001995 }
1996 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001997}
1998
Willy Tarreauc24715e2014-04-17 15:21:20 +02001999/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2000 * the first byte of data after the chunk size, so that we know we can forward
2001 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2002 * the chunk size. That way it is always possible to differentiate between the
2003 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002004 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002005 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002006 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002007static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002008{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002009 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002010 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002011 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002012 const char *end = buf->data + buf->size;
2013 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002014 unsigned int chunk = 0;
2015
2016 /* The chunk size is in the following form, though we are only
2017 * interested in the size and CRLF :
2018 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2019 */
2020 while (1) {
2021 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002022 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002023 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002024 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002025 if (c < 0) /* not a hex digit anymore */
2026 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002027 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002028 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002029 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002030 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002031 chunk = (chunk << 4) + c;
2032 }
2033
Willy Tarreaud98cf932009-12-27 22:54:55 +01002034 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002035 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002036 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037
2038 while (http_is_spht[(unsigned char)*ptr]) {
2039 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002040 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002041 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002042 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002043 }
2044
Willy Tarreaud98cf932009-12-27 22:54:55 +01002045 /* Up to there, we know that at least one byte is present at *ptr. Check
2046 * for the end of chunk size.
2047 */
2048 while (1) {
2049 if (likely(HTTP_IS_CRLF(*ptr))) {
2050 /* we now have a CR or an LF at ptr */
2051 if (likely(*ptr == '\r')) {
2052 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002053 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002054 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002055 return 0;
2056 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002057
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002059 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002061 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002062 /* done */
2063 break;
2064 }
2065 else if (*ptr == ';') {
2066 /* chunk extension, ends at next CRLF */
2067 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002068 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002069 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002070 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002071
2072 while (!HTTP_IS_CRLF(*ptr)) {
2073 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002074 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002075 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002076 return 0;
2077 }
2078 /* we have a CRLF now, loop above */
2079 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002080 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002081 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002082 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002083 }
2084
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002086 * which may or may not be present. We save that into ->next,
2087 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002088 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002089 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002090 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002091 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002092 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002093 msg->chunk_len = chunk;
2094 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002096 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002097 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002098 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002099 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002100}
2101
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002102/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002103 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002104 * the trailers is found, it is automatically scheduled to be forwarded,
2105 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2106 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002107 * except maybe msg->next if it could parse some lines, and returns zero.
2108 * If a parse error is encountered, the function returns < 0 and does not
2109 * change anything except maybe msg->next. Note that the message must
2110 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002111 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002112 * forwarding, and that msg->next exactly matches the length of trailers
2113 * already parsed and not forwarded. It is also important to note that this
2114 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002115 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002116static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002117{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002118 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002119
Willy Tarreaua458b672012-03-05 11:17:50 +01002120 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002121 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002122 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002123 const char *ptr = b_ptr(buf, msg->next);
2124 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002125 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002126
2127 /* scan current line and stop at LF or CRLF */
2128 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002129 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002130 return 0;
2131
2132 if (*ptr == '\n') {
2133 if (!p1)
2134 p1 = ptr;
2135 p2 = ptr;
2136 break;
2137 }
2138
2139 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002140 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002141 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002142 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002143 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002144 p1 = ptr;
2145 }
2146
2147 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002148 if (ptr >= buf->data + buf->size)
2149 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002150 }
2151
2152 /* after LF; point to beginning of next line */
2153 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002154 if (p2 >= buf->data + buf->size)
2155 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002156
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002157 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002158 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002159 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002160
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002161 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002162 /* LF/CRLF at beginning of line => end of trailers at p2.
2163 * Everything was scheduled for forwarding, there's nothing
2164 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002165 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002166 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002167 msg->msg_state = HTTP_MSG_DONE;
2168 return 1;
2169 }
2170 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002171 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002172 }
2173}
2174
Willy Tarreauc24715e2014-04-17 15:21:20 +02002175/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2176 * or a possible LF alone at the end of a chunk. It automatically adjusts
2177 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002178 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002179 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2180 * not enough data are available, the function does not change anything and
2181 * returns zero. If a parse error is encountered, the function returns < 0 and
2182 * does not change anything. Note: this function is designed to parse wrapped
2183 * CRLF at the end of the buffer.
2184 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002185static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002186{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002187 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002188 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002189 int bytes;
2190
2191 /* NB: we'll check data availabilty at the end. It's not a
2192 * problem because whatever we match first will be checked
2193 * against the correct length.
2194 */
2195 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002196 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 if (*ptr == '\r') {
2198 bytes++;
2199 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002200 if (ptr >= buf->data + buf->size)
2201 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002202 }
2203
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002204 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002205 return 0;
2206
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002207 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002208 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002209 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002210 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002211
2212 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002213 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002214 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002215 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002216 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002217 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2218 return 1;
2219}
Willy Tarreau5b154472009-12-21 20:11:07 +01002220
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002221/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2222 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2223 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2224 * Unparsable qvalues return 1000 as "q=1.000".
2225 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002226int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002227{
2228 int q = 1000;
2229
Willy Tarreau506c69a2014-07-08 00:59:48 +02002230 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002231 goto out;
2232 q = (*qvalue++ - '0') * 1000;
2233
2234 if (*qvalue++ != '.')
2235 goto out;
2236
Willy Tarreau506c69a2014-07-08 00:59:48 +02002237 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002238 goto out;
2239 q += (*qvalue++ - '0') * 100;
2240
Willy Tarreau506c69a2014-07-08 00:59:48 +02002241 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002242 goto out;
2243 q += (*qvalue++ - '0') * 10;
2244
Willy Tarreau506c69a2014-07-08 00:59:48 +02002245 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002246 goto out;
2247 q += (*qvalue++ - '0') * 1;
2248 out:
2249 if (q > 1000)
2250 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002251 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002252 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002253 return q;
2254}
William Lallemand82fe75c2012-10-23 10:25:10 +02002255
2256/*
2257 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002258 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002259int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002260{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002261 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002262 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002263 struct hdr_ctx ctx;
2264 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002265 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002266
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002267 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2268 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002269 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2270 */
2271 ctx.idx = 0;
2272 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2273 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002274 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2275 (ctx.vlen < 31 ||
2276 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2277 ctx.line[ctx.val + 30] < '6' ||
2278 (ctx.line[ctx.val + 30] == '6' &&
2279 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2280 s->comp_algo = NULL;
2281 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002282 }
2283
William Lallemand82fe75c2012-10-23 10:25:10 +02002284 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002285 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002286 int best_q = 0;
2287
William Lallemand82fe75c2012-10-23 10:25:10 +02002288 ctx.idx = 0;
2289 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002290 const char *qval;
2291 int q;
2292 int toklen;
2293
2294 /* try to isolate the token from the optional q-value */
2295 toklen = 0;
2296 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2297 toklen++;
2298
2299 qval = ctx.line + ctx.val + toklen;
2300 while (1) {
2301 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2302 qval++;
2303
2304 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2305 qval = NULL;
2306 break;
2307 }
2308 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002309
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002310 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2311 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002312
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002313 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2314 qval = NULL;
2315 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002316 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002317 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2318 break;
2319
2320 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2321 qval++;
2322 }
2323
2324 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002325 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002326
2327 if (q <= best_q)
2328 continue;
2329
2330 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2331 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002332 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002333 s->comp_algo = comp_algo;
2334 best_q = q;
2335 break;
2336 }
2337 }
2338 }
2339 }
2340
2341 /* remove all occurrences of the header when "compression offload" is set */
2342 if (s->comp_algo) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002343 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002344 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2345 ctx.idx = 0;
2346 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2347 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002348 }
2349 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002350 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002351 }
2352
2353 /* identity is implicit does not require headers */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002354 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002355 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002356 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002357 s->comp_algo = comp_algo;
2358 return 1;
2359 }
2360 }
2361 }
2362
2363 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002364 return 0;
2365}
2366
2367/*
2368 * Selects a comression algorithm depending of the server response.
2369 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002370int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002371{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002372 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002373 struct http_msg *msg = &txn->rsp;
2374 struct hdr_ctx ctx;
2375 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002376
2377 /* no common compression algorithm was found in request header */
2378 if (s->comp_algo == NULL)
2379 goto fail;
2380
2381 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002382 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002383 goto fail;
2384
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002385 /* compress 200,201,202,203 responses only */
2386 if ((txn->status != 200) &&
2387 (txn->status != 201) &&
2388 (txn->status != 202) &&
2389 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002390 goto fail;
2391
William Lallemand82fe75c2012-10-23 10:25:10 +02002392 /* Content-Length is null */
2393 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2394 goto fail;
2395
2396 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002397 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002398 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2399 goto fail;
2400
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002401 /* no compression when Cache-Control: no-transform is present in the message */
2402 ctx.idx = 0;
2403 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2404 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2405 goto fail;
2406 }
2407
William Lallemand82fe75c2012-10-23 10:25:10 +02002408 comp_type = NULL;
2409
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002410 /* we don't want to compress multipart content-types, nor content-types that are
2411 * not listed in the "compression type" directive if any. If no content-type was
2412 * found but configuration requires one, we don't compress either. Backend has
2413 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002414 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002415 ctx.idx = 0;
2416 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2417 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2418 goto fail;
2419
2420 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002421 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002422 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002423 if (ctx.vlen >= comp_type->name_len &&
2424 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002425 /* this Content-Type should be compressed */
2426 break;
2427 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002428 /* this Content-Type should not be compressed */
2429 if (comp_type == NULL)
2430 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002431 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002432 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002433 else { /* no content-type header */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002434 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002435 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002436 }
2437
William Lallemandd85f9172012-11-09 17:05:39 +01002438 /* limit compression rate */
2439 if (global.comp_rate_lim > 0)
2440 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2441 goto fail;
2442
William Lallemand072a2bf2012-11-20 17:01:01 +01002443 /* limit cpu usage */
2444 if (idle_pct < compress_min_idle)
2445 goto fail;
2446
William Lallemand4c49fae2012-11-07 15:00:23 +01002447 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002448 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002449 goto fail;
2450
Willy Tarreaue7dff022015-04-03 01:14:29 +02002451 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002452
William Lallemand82fe75c2012-10-23 10:25:10 +02002453 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002454 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002455 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2456 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2457
2458 /* add Transfer-Encoding header */
2459 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2460 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2461
2462 /*
2463 * Add Content-Encoding header when it's not identity encoding.
2464 * RFC 2616 : Identity encoding: This content-coding is used only in the
2465 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2466 * header.
2467 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002468 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002469 trash.len = 18;
2470 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002471 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2472 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002473 trash.str[trash.len] = '\0';
2474 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002475 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002476 return 1;
2477
2478fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002479 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002480 return 0;
2481}
2482
Willy Tarreau87b09662015-04-03 00:22:06 +02002483void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002484{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002485 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002486 int tmp = TX_CON_WANT_KAL;
2487
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002488 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2489 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002490 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2491 tmp = TX_CON_WANT_TUN;
2492
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002493 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002494 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2495 tmp = TX_CON_WANT_TUN;
2496 }
2497
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002498 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002499 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2500 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002501 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002502 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2503 tmp = TX_CON_WANT_CLO;
2504 else
2505 tmp = TX_CON_WANT_SCL;
2506 }
2507
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002508 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002509 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2510 tmp = TX_CON_WANT_CLO;
2511
2512 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2513 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2514
2515 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2516 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2517 /* parse the Connection header and possibly clean it */
2518 int to_del = 0;
2519 if ((msg->flags & HTTP_MSGF_VER_11) ||
2520 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002521 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002522 to_del |= 2; /* remove "keep-alive" */
2523 if (!(msg->flags & HTTP_MSGF_VER_11))
2524 to_del |= 1; /* remove "close" */
2525 http_parse_connection_header(txn, msg, to_del);
2526 }
2527
2528 /* check if client or config asks for explicit close in KAL/SCL */
2529 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2530 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2531 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2532 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2533 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002534 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002535 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2536}
William Lallemand82fe75c2012-10-23 10:25:10 +02002537
Willy Tarreaud787e662009-07-07 10:14:51 +02002538/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2539 * processing can continue on next analysers, or zero if it either needs more
2540 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002541 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002542 * when it has nothing left to do, and may remove any analyser when it wants to
2543 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002544 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002545int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002546{
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 /*
2548 * We will parse the partial (or complete) lines.
2549 * We will check the request syntax, and also join multi-line
2550 * headers. An index of all the lines will be elaborated while
2551 * parsing.
2552 *
2553 * For the parsing, we use a 28 states FSM.
2554 *
2555 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002556 * req->buf->p = beginning of request
2557 * req->buf->p + msg->eoh = end of processed headers / start of current one
2558 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002559 * msg->eol = end of current header or line (LF or CRLF)
2560 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002561 *
2562 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002563 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002564 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2565 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002566 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002567
Willy Tarreau59234e92008-11-30 23:51:27 +01002568 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002569 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002570 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002571 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002572 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002573
Willy Tarreau87b09662015-04-03 00:22:06 +02002574 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002575 now_ms, __FUNCTION__,
2576 s,
2577 req,
2578 req->rex, req->wex,
2579 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002580 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002581 req->analysers);
2582
Willy Tarreau52a0c602009-08-16 22:45:38 +02002583 /* we're speaking HTTP here, so let's speak HTTP to the client */
2584 s->srv_error = http_return_srv_error;
2585
Willy Tarreau83e3af02009-12-28 17:39:57 +01002586 /* There's a protected area at the end of the buffer for rewriting
2587 * purposes. We don't want to start to parse the request if the
2588 * protected area is affected, because we may have to move processed
2589 * data later, which is much more complicated.
2590 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002591 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002592 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002593 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002594 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002595 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002596 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002597 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002598 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002599 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002600 return 0;
2601 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002602 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2603 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2604 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002605 }
2606
Willy Tarreau065e8332010-01-08 00:30:20 +01002607 /* Note that we have the same problem with the response ; we
2608 * may want to send a redirect, error or anything which requires
2609 * some spare space. So we'll ensure that we have at least
2610 * maxrewrite bytes available in the response buffer before
2611 * processing that one. This will only affect pipelined
2612 * keep-alive requests.
2613 */
2614 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002615 unlikely(!channel_is_rewritable(&s->res) ||
2616 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2617 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2618 if (s->res.buf->o) {
2619 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002620 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002621 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002622 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002623 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2624 s->res.flags |= CF_WAKE_WRITE;
2625 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002626 return 0;
2627 }
2628 }
2629
Willy Tarreau9b28e032012-10-12 23:49:43 +02002630 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002631 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002632 }
2633
Willy Tarreau59234e92008-11-30 23:51:27 +01002634 /* 1: we might have to print this header in debug mode */
2635 if (unlikely((global.mode & MODE_DEBUG) &&
2636 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002637 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002639
Willy Tarreau9b28e032012-10-12 23:49:43 +02002640 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002641 /* this is a bit complex : in case of error on the request line,
2642 * we know that rq.l is still zero, so we display only the part
2643 * up to the end of the line (truncated by debug_hdr).
2644 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002645 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002646 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002647
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 sol += hdr_idx_first_pos(&txn->hdr_idx);
2649 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002650
Willy Tarreau59234e92008-11-30 23:51:27 +01002651 while (cur_idx) {
2652 eol = sol + txn->hdr_idx.v[cur_idx].len;
2653 debug_hdr("clihdr", s, sol, eol);
2654 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2655 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002656 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 }
2658
Willy Tarreau58f10d72006-12-04 02:26:12 +01002659
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 /*
2661 * Now we quickly check if we have found a full valid request.
2662 * If not so, we check the FD and buffer states before leaving.
2663 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002664 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002665 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002666 * on a keep-alive stream, if we encounter and error, close, t/o,
2667 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002668 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002669 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002670 * Last, we may increase some tracked counters' http request errors on
2671 * the cases that are deliberately the client's fault. For instance,
2672 * a timeout or connection reset is not counted as an error. However
2673 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002674 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002675
Willy Tarreau655dce92009-11-08 13:10:58 +01002676 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002677 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002678 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002679 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002680 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002681 stream_inc_http_req_ctr(s);
2682 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002683 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002685 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002686
Willy Tarreau59234e92008-11-30 23:51:27 +01002687 /* 1: Since we are in header mode, if there's no space
2688 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002689 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 * must terminate it now.
2691 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002692 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 /* FIXME: check if URI is set and return Status
2694 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002695 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002696 stream_inc_http_req_ctr(s);
2697 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002698 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002699 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002700 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002701 goto return_bad_req;
2702 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002703
Willy Tarreau59234e92008-11-30 23:51:27 +01002704 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002705 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002706 if (!(s->flags & SF_ERR_MASK))
2707 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002708
Willy Tarreaufcffa692010-01-10 14:21:19 +01002709 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002710 goto failed_keep_alive;
2711
Willy Tarreau0f228a02015-05-01 15:37:53 +02002712 if (sess->fe->options & PR_O_IGNORE_PRB)
2713 goto failed_keep_alive;
2714
Willy Tarreau59234e92008-11-30 23:51:27 +01002715 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002716 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002717 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002718 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002719 }
2720
Willy Tarreaudc979f22012-12-04 10:39:01 +01002721 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002722 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 msg->msg_state = HTTP_MSG_ERROR;
2724 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002725
Willy Tarreau87b09662015-04-03 00:22:06 +02002726 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002727 proxy_inc_fe_req_ctr(sess->fe);
2728 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002729 if (sess->listener->counters)
2730 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002731
Willy Tarreaue7dff022015-04-03 01:14:29 +02002732 if (!(s->flags & SF_FINST_MASK))
2733 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 return 0;
2735 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002736
Willy Tarreau59234e92008-11-30 23:51:27 +01002737 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002738 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002739 if (!(s->flags & SF_ERR_MASK))
2740 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002741
Willy Tarreaufcffa692010-01-10 14:21:19 +01002742 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002743 goto failed_keep_alive;
2744
Willy Tarreau0f228a02015-05-01 15:37:53 +02002745 if (sess->fe->options & PR_O_IGNORE_PRB)
2746 goto failed_keep_alive;
2747
Willy Tarreau59234e92008-11-30 23:51:27 +01002748 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002749 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002750 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002751 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002752 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002753 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002754 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002755 msg->msg_state = HTTP_MSG_ERROR;
2756 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002757
Willy Tarreau87b09662015-04-03 00:22:06 +02002758 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002759 proxy_inc_fe_req_ctr(sess->fe);
2760 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002761 if (sess->listener->counters)
2762 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002763
Willy Tarreaue7dff022015-04-03 01:14:29 +02002764 if (!(s->flags & SF_FINST_MASK))
2765 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002766 return 0;
2767 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002768
Willy Tarreau59234e92008-11-30 23:51:27 +01002769 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002770 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002771 if (!(s->flags & SF_ERR_MASK))
2772 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002773
Willy Tarreaufcffa692010-01-10 14:21:19 +01002774 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002775 goto failed_keep_alive;
2776
Willy Tarreau0f228a02015-05-01 15:37:53 +02002777 if (sess->fe->options & PR_O_IGNORE_PRB)
2778 goto failed_keep_alive;
2779
Willy Tarreau4076a152009-04-02 15:18:36 +02002780 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002781 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002782 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002783 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002784 msg->msg_state = HTTP_MSG_ERROR;
2785 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002786
Willy Tarreau87b09662015-04-03 00:22:06 +02002787 stream_inc_http_err_ctr(s);
2788 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002789 proxy_inc_fe_req_ctr(sess->fe);
2790 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002791 if (sess->listener->counters)
2792 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002793
Willy Tarreaue7dff022015-04-03 01:14:29 +02002794 if (!(s->flags & SF_FINST_MASK))
2795 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002796 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002797 }
2798
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002799 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002800 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002801 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002802#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002803 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2804 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002805 /* We need more data, we have to re-enable quick-ack in case we
2806 * previously disabled it, otherwise we might cause the client
2807 * to delay next data.
2808 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002809 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002810 }
2811#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002812
Willy Tarreaufcffa692010-01-10 14:21:19 +01002813 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2814 /* If the client starts to talk, let's fall back to
2815 * request timeout processing.
2816 */
2817 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002818 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002819 }
2820
Willy Tarreau59234e92008-11-30 23:51:27 +01002821 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002822 if (!tick_isset(req->analyse_exp)) {
2823 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2824 (txn->flags & TX_WAIT_NEXT_RQ) &&
2825 tick_isset(s->be->timeout.httpka))
2826 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2827 else
2828 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2829 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002830
Willy Tarreau59234e92008-11-30 23:51:27 +01002831 /* we're not ready yet */
2832 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002833
2834 failed_keep_alive:
2835 /* Here we process low-level errors for keep-alive requests. In
2836 * short, if the request is not the first one and it experiences
2837 * a timeout, read error or shutdown, we just silently close so
2838 * that the client can try again.
2839 */
2840 txn->status = 0;
2841 msg->msg_state = HTTP_MSG_RQBEFORE;
2842 req->analysers = 0;
2843 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002844 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002845 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002846 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002847 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002848 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002849
Willy Tarreaud787e662009-07-07 10:14:51 +02002850 /* OK now we have a complete HTTP request with indexed headers. Let's
2851 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002852 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002853 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002854 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002855 * byte after the last LF. msg->sov points to the first byte of data.
2856 * msg->eol cannot be trusted because it may have been left uninitialized
2857 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002858 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002859
Willy Tarreau87b09662015-04-03 00:22:06 +02002860 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002861 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002862
Willy Tarreaub16a5742010-01-10 14:46:16 +01002863 if (txn->flags & TX_WAIT_NEXT_RQ) {
2864 /* kill the pending keep-alive timeout */
2865 txn->flags &= ~TX_WAIT_NEXT_RQ;
2866 req->analyse_exp = TICK_ETERNITY;
2867 }
2868
2869
Willy Tarreaud787e662009-07-07 10:14:51 +02002870 /* Maybe we found in invalid header name while we were configured not
2871 * to block on that, so we have to capture it now.
2872 */
2873 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002874 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002875
Willy Tarreau59234e92008-11-30 23:51:27 +01002876 /*
2877 * 1: identify the method
2878 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002879 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002880
2881 /* we can make use of server redirect on GET and HEAD */
2882 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002883 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002884
Willy Tarreau59234e92008-11-30 23:51:27 +01002885 /*
2886 * 2: check if the URI matches the monitor_uri.
2887 * We have to do this for every request which gets in, because
2888 * the monitor-uri is defined by the frontend.
2889 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002890 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2891 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002892 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002893 sess->fe->monitor_uri,
2894 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002895 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002896 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002897 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002898 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002899
Willy Tarreaue7dff022015-04-03 01:14:29 +02002900 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002901 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002902
Willy Tarreau59234e92008-11-30 23:51:27 +01002903 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002904 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002905 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002906
Willy Tarreau59234e92008-11-30 23:51:27 +01002907 ret = acl_pass(ret);
2908 if (cond->pol == ACL_COND_UNLESS)
2909 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002910
Willy Tarreau59234e92008-11-30 23:51:27 +01002911 if (ret) {
2912 /* we fail this request, let's return 503 service unavail */
2913 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002914 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002915 if (!(s->flags & SF_ERR_MASK))
2916 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002917 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002918 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002919 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002920
Willy Tarreau59234e92008-11-30 23:51:27 +01002921 /* nothing to fail, let's reply normaly */
2922 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01002923 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002924 if (!(s->flags & SF_ERR_MASK))
2925 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002926 goto return_prx_cond;
2927 }
2928
2929 /*
2930 * 3: Maybe we have to copy the original REQURI for the logs ?
2931 * Note: we cannot log anymore if the request has been
2932 * classified as invalid.
2933 */
2934 if (unlikely(s->logs.logwait & LW_REQ)) {
2935 /* we have a complete HTTP request that we must log */
2936 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2937 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002938
Willy Tarreau59234e92008-11-30 23:51:27 +01002939 if (urilen >= REQURI_LEN)
2940 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002941 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002942 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002943
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002944 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002945 s->do_log(s);
2946 } else {
2947 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002948 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002949 }
Willy Tarreau06619262006-12-17 08:37:22 +01002950
Willy Tarreau91852eb2015-05-01 13:26:00 +02002951 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2952 * exactly one digit "." one digit. This check may be disabled using
2953 * option accept-invalid-http-request.
2954 */
2955 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2956 if (msg->sl.rq.v_l != 8) {
2957 msg->err_pos = msg->sl.rq.v;
2958 goto return_bad_req;
2959 }
2960
2961 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2962 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2963 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2964 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2965 msg->err_pos = msg->sl.rq.v + 4;
2966 goto return_bad_req;
2967 }
2968 }
Willy Tarreau13317662015-05-01 13:47:08 +02002969 else {
2970 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2971 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2972 goto return_bad_req;
2973 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002974
Willy Tarreau5b154472009-12-21 20:11:07 +01002975 /* ... and check if the request is HTTP/1.1 or above */
2976 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002977 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2978 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2979 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002980 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002981
2982 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002983 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002984
Willy Tarreau88d349d2010-01-25 12:15:43 +01002985 /* if the frontend has "option http-use-proxy-header", we'll check if
2986 * we have what looks like a proxied connection instead of a connection,
2987 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2988 * Note that this is *not* RFC-compliant, however browsers and proxies
2989 * happen to do that despite being non-standard :-(
2990 * We consider that a request not beginning with either '/' or '*' is
2991 * a proxied connection, which covers both "scheme://location" and
2992 * CONNECT ip:port.
2993 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002994 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002995 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002996 txn->flags |= TX_USE_PX_CONN;
2997
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002998 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002999 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003000
Willy Tarreau59234e92008-11-30 23:51:27 +01003001 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003002 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003003 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003004 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003005
Willy Tarreau557f1992015-05-01 10:05:17 +02003006 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3007 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003008 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003009 * The length of a message body is determined by one of the following
3010 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003011 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003012 * 1. Any response to a HEAD request and any response with a 1xx
3013 * (Informational), 204 (No Content), or 304 (Not Modified) status
3014 * code is always terminated by the first empty line after the
3015 * header fields, regardless of the header fields present in the
3016 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003017 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003018 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3019 * the connection will become a tunnel immediately after the empty
3020 * line that concludes the header fields. A client MUST ignore any
3021 * Content-Length or Transfer-Encoding header fields received in
3022 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003023 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003024 * 3. If a Transfer-Encoding header field is present and the chunked
3025 * transfer coding (Section 4.1) is the final encoding, the message
3026 * body length is determined by reading and decoding the chunked
3027 * data until the transfer coding indicates the data is complete.
3028 *
3029 * If a Transfer-Encoding header field is present in a response and
3030 * the chunked transfer coding is not the final encoding, the
3031 * message body length is determined by reading the connection until
3032 * it is closed by the server. If a Transfer-Encoding header field
3033 * is present in a request and the chunked transfer coding is not
3034 * the final encoding, the message body length cannot be determined
3035 * reliably; the server MUST respond with the 400 (Bad Request)
3036 * status code and then close the connection.
3037 *
3038 * If a message is received with both a Transfer-Encoding and a
3039 * Content-Length header field, the Transfer-Encoding overrides the
3040 * Content-Length. Such a message might indicate an attempt to
3041 * perform request smuggling (Section 9.5) or response splitting
3042 * (Section 9.4) and ought to be handled as an error. A sender MUST
3043 * remove the received Content-Length field prior to forwarding such
3044 * a message downstream.
3045 *
3046 * 4. If a message is received without Transfer-Encoding and with
3047 * either multiple Content-Length header fields having differing
3048 * field-values or a single Content-Length header field having an
3049 * invalid value, then the message framing is invalid and the
3050 * recipient MUST treat it as an unrecoverable error. If this is a
3051 * request message, the server MUST respond with a 400 (Bad Request)
3052 * status code and then close the connection. If this is a response
3053 * message received by a proxy, the proxy MUST close the connection
3054 * to the server, discard the received response, and send a 502 (Bad
3055 * Gateway) response to the client. If this is a response message
3056 * received by a user agent, the user agent MUST close the
3057 * connection to the server and discard the received response.
3058 *
3059 * 5. If a valid Content-Length header field is present without
3060 * Transfer-Encoding, its decimal value defines the expected message
3061 * body length in octets. If the sender closes the connection or
3062 * the recipient times out before the indicated number of octets are
3063 * received, the recipient MUST consider the message to be
3064 * incomplete and close the connection.
3065 *
3066 * 6. If this is a request message and none of the above are true, then
3067 * the message body length is zero (no message body is present).
3068 *
3069 * 7. Otherwise, this is a response message without a declared message
3070 * body length, so the message body length is determined by the
3071 * number of octets received prior to the server closing the
3072 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003073 */
3074
Willy Tarreau32b47f42009-10-18 20:55:02 +02003075 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003076 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003077 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003078 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003079 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3080 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003081 /* chunked not last, return badreq */
3082 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003083 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003084 }
3085
Willy Tarreau1c913912015-04-30 10:57:51 +02003086 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003087 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003088 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3089 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3090 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3091 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003092 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003093 signed long long cl;
3094
Willy Tarreauad14f752011-09-02 20:33:27 +02003095 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003096 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003097 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003098 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003099
Willy Tarreauad14f752011-09-02 20:33:27 +02003100 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003101 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003102 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003103 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003104
Willy Tarreauad14f752011-09-02 20:33:27 +02003105 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003106 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003107 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003108 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003109
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003110 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003111 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003112 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003113 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003114
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003115 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003116 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003117 }
3118
Willy Tarreau34dfc602015-05-01 10:09:49 +02003119 /* even bodyless requests have a known length */
3120 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003121
Willy Tarreau179085c2014-04-28 16:48:56 +02003122 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3123 * only change if both the request and the config reference something else.
3124 * Option httpclose by itself sets tunnel mode where headers are mangled.
3125 * However, if another mode is set, it will affect it (eg: server-close/
3126 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3127 * if FE and BE have the same settings (common). The method consists in
3128 * checking if options changed between the two calls (implying that either
3129 * one is non-null, or one of them is non-null and we are there for the first
3130 * time.
3131 */
3132 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003133 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003134 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003135
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003136 /* we may have to wait for the request's body */
3137 if ((s->be->options & PR_O_WREQ_BODY) &&
3138 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3139 req->analysers |= AN_REQ_HTTP_BODY;
3140
Willy Tarreaud787e662009-07-07 10:14:51 +02003141 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003142 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003143 req->analyse_exp = TICK_ETERNITY;
3144 return 1;
3145
3146 return_bad_req:
3147 /* We centralize bad requests processing here */
3148 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3149 /* we detected a parsing error. We want to archive this request
3150 * in the dedicated proxy area for later troubleshooting.
3151 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003152 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003153 }
3154
3155 txn->req.msg_state = HTTP_MSG_ERROR;
3156 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003157 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003158
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003159 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003160 if (sess->listener->counters)
3161 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003162
3163 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003164 if (!(s->flags & SF_ERR_MASK))
3165 s->flags |= SF_ERR_PRXCOND;
3166 if (!(s->flags & SF_FINST_MASK))
3167 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003168
3169 req->analysers = 0;
3170 req->analyse_exp = TICK_ETERNITY;
3171 return 0;
3172}
3173
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003174
Willy Tarreau347a35d2013-11-22 17:51:09 +01003175/* This function prepares an applet to handle the stats. It can deal with the
3176 * "100-continue" expectation, check that admin rules are met for POST requests,
3177 * and program a response message if something was unexpected. It cannot fail
3178 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003179 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003180 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003181 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003182 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003183int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003184{
3185 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003186 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003187 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003188 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003189 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003190 struct uri_auth *uri_auth = s->be->uri_auth;
3191 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003192 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003193
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003194 appctx = si_appctx(si);
3195 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3196 appctx->st1 = appctx->st2 = 0;
3197 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3198 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003199 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003200 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003201
3202 uri = msg->chn->buf->p + msg->sl.rq.u;
3203 lookup = uri + uri_auth->uri_len;
3204
3205 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3206 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003207 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003208 break;
3209 }
3210 }
3211
3212 if (uri_auth->refresh) {
3213 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3214 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003215 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003216 break;
3217 }
3218 }
3219 }
3220
3221 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3222 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003223 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003224 break;
3225 }
3226 }
3227
3228 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3229 if (memcmp(h, ";st=", 4) == 0) {
3230 int i;
3231 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003232 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003233 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3234 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003235 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003236 break;
3237 }
3238 }
3239 break;
3240 }
3241 }
3242
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003243 appctx->ctx.stats.scope_str = 0;
3244 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003245 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3246 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3247 int itx = 0;
3248 const char *h2;
3249 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3250 const char *err;
3251
3252 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3253 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003254 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003255 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3256 itx++;
3257 h++;
3258 }
3259
3260 if (itx > STAT_SCOPE_TXT_MAXLEN)
3261 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003262 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003263
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003264 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003265 memcpy(scope_txt, h2, itx);
3266 scope_txt[itx] = '\0';
3267 err = invalid_char(scope_txt);
3268 if (err) {
3269 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003270 appctx->ctx.stats.scope_str = 0;
3271 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003272 }
3273 break;
3274 }
3275 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003276
3277 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003278 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003279 int ret = 1;
3280
3281 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003282 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003283 ret = acl_pass(ret);
3284 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3285 ret = !ret;
3286 }
3287
3288 if (ret) {
3289 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003290 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003291 break;
3292 }
3293 }
3294
3295 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003296 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003297 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003298 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003299 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3300 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003301 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003302 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003303 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003304 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3305 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003306 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003307 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003308 else {
3309 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003310 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003311 }
3312
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003313 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003314 return 1;
3315}
3316
Lukas Tribus67db8df2013-06-23 17:37:13 +02003317/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3318 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3319 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003320void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003321{
3322#ifdef IP_TOS
3323 if (from.ss_family == AF_INET)
3324 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3325#endif
3326#ifdef IPV6_TCLASS
3327 if (from.ss_family == AF_INET6) {
3328 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3329 /* v4-mapped addresses need IP_TOS */
3330 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3331 else
3332 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3333 }
3334#endif
3335}
3336
Willy Tarreau87b09662015-04-03 00:22:06 +02003337int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003338 const char* name, unsigned int name_len,
3339 const char *str, struct my_regex *re,
3340 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003341{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003342 struct hdr_ctx ctx;
3343 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003344 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003345 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3346 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003347 struct chunk *output = get_trash_chunk();
3348
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003349 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003350
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003351 /* Choose the header browsing function. */
3352 switch (action) {
3353 case HTTP_REQ_ACT_REPLACE_VAL:
3354 case HTTP_RES_ACT_REPLACE_VAL:
3355 http_find_hdr_func = http_find_header2;
3356 break;
3357 case HTTP_REQ_ACT_REPLACE_HDR:
3358 case HTTP_RES_ACT_REPLACE_HDR:
3359 http_find_hdr_func = http_find_full_header2;
3360 break;
3361 default: /* impossible */
3362 return -1;
3363 }
3364
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003365 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3366 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003367 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003368 char *val = ctx.line + ctx.val;
3369 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003370
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003371 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3372 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003373
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003374 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003375 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003376 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003377
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003378 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003379
3380 hdr->len += delta;
3381 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003382
3383 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003384 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003385 }
3386
3387 return 0;
3388}
3389
Willy Tarreau87b09662015-04-03 00:22:06 +02003390static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003391 const char* name, unsigned int name_len,
3392 struct list *fmt, struct my_regex *re,
3393 int action)
3394{
3395 struct chunk *replace = get_trash_chunk();
3396
3397 replace->len = build_logline(s, replace->str, replace->size, fmt);
3398 if (replace->len >= replace->size - 1)
3399 return -1;
3400
3401 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3402}
3403
Willy Tarreau87b09662015-04-03 00:22:06 +02003404/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003405 * transaction <txn>. Returns the verdict of the first rule that prevents
3406 * further processing of the request (auth, deny, ...), and defaults to
3407 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3408 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3409 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003410 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003411enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003412http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003413{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003414 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003415 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003416 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003417 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003418 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003419 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003420
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003421 /* If "the current_rule_list" match the executed rule list, we are in
3422 * resume condition. If a resume is needed it is always in the action
3423 * and never in the ACL or converters. In this case, we initialise the
3424 * current rule, and go to the action execution point.
3425 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003426 if (s->current_rule) {
3427 rule = s->current_rule;
3428 s->current_rule = NULL;
3429 if (s->current_rule_list == rules)
3430 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003431 }
3432 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003433
Willy Tarreauff011f22011-01-06 17:51:27 +01003434 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003435 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003436 continue;
3437
Willy Tarreau96257ec2012-12-27 10:46:37 +01003438 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003439 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003440 int ret;
3441
Willy Tarreau192252e2015-04-04 01:47:55 +02003442 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003443 ret = acl_pass(ret);
3444
Willy Tarreauff011f22011-01-06 17:51:27 +01003445 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003446 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003447
3448 if (!ret) /* condition not matched */
3449 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003450 }
3451
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003452resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003453 switch (rule->action) {
3454 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003455 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003456
3457 case HTTP_REQ_ACT_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003458 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003459 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003460
Willy Tarreauccbcc372012-12-27 12:37:57 +01003461 case HTTP_REQ_ACT_TARPIT:
3462 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003463 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003464 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003465
Willy Tarreau96257ec2012-12-27 10:46:37 +01003466 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003467 /* Auth might be performed on regular http-req rules as well as on stats */
3468 auth_realm = rule->arg.auth.realm;
3469 if (!auth_realm) {
3470 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3471 auth_realm = STATS_DEFAULT_REALM;
3472 else
3473 auth_realm = px->id;
3474 }
3475 /* send 401/407 depending on whether we use a proxy or not. We still
3476 * count one error, because normal browsing won't significantly
3477 * increase the counter but brute force attempts will.
3478 */
3479 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3480 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3481 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003482 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003483 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003484
Willy Tarreau81499eb2012-12-27 12:19:02 +01003485 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003486 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3487 return HTTP_RULE_RES_BADREQ;
3488 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003489
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003490 case HTTP_REQ_ACT_SET_NICE:
3491 s->task->nice = rule->arg.nice;
3492 break;
3493
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003494 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003495 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003496 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003497 break;
3498
Willy Tarreau51347ed2013-06-11 19:34:13 +02003499 case HTTP_REQ_ACT_SET_MARK:
3500#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003501 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003502 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003503#endif
3504 break;
3505
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003506 case HTTP_REQ_ACT_SET_LOGL:
3507 s->logs.level = rule->arg.loglevel;
3508 break;
3509
Sasha Pachev218f0642014-06-16 12:05:59 -06003510 case HTTP_REQ_ACT_REPLACE_HDR:
3511 case HTTP_REQ_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003512 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3513 rule->arg.hdr_add.name_len,
3514 &rule->arg.hdr_add.fmt,
3515 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003516 return HTTP_RULE_RES_BADREQ;
3517 break;
3518
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003519 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003520 ctx.idx = 0;
3521 /* remove all occurrences of the header */
3522 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3523 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3524 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003525 }
Willy Tarreau85603282015-01-21 20:39:27 +01003526 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003527
Willy Tarreau85603282015-01-21 20:39:27 +01003528 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003529 case HTTP_REQ_ACT_ADD_HDR:
3530 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3531 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3532 trash.len = rule->arg.hdr_add.name_len;
3533 trash.str[trash.len++] = ':';
3534 trash.str[trash.len++] = ' ';
3535 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01003536
3537 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3538 /* remove all occurrences of the header */
3539 ctx.idx = 0;
3540 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3541 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3542 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3543 }
3544 }
3545
Willy Tarreau96257ec2012-12-27 10:46:37 +01003546 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3547 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003548
3549 case HTTP_REQ_ACT_DEL_ACL:
3550 case HTTP_REQ_ACT_DEL_MAP: {
3551 struct pat_ref *ref;
3552 char *key;
3553 int len;
3554
3555 /* collect reference */
3556 ref = pat_ref_lookup(rule->arg.map.ref);
3557 if (!ref)
3558 continue;
3559
3560 /* collect key */
3561 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3562 key = trash.str;
3563 key[len] = '\0';
3564
3565 /* perform update */
3566 /* returned code: 1=ok, 0=ko */
3567 pat_ref_delete(ref, key);
3568
3569 break;
3570 }
3571
3572 case HTTP_REQ_ACT_ADD_ACL: {
3573 struct pat_ref *ref;
3574 char *key;
3575 struct chunk *trash_key;
3576 int len;
3577
3578 trash_key = get_trash_chunk();
3579
3580 /* collect reference */
3581 ref = pat_ref_lookup(rule->arg.map.ref);
3582 if (!ref)
3583 continue;
3584
3585 /* collect key */
3586 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3587 key = trash_key->str;
3588 key[len] = '\0';
3589
3590 /* perform update */
3591 /* add entry only if it does not already exist */
3592 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003593 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003594
3595 break;
3596 }
3597
3598 case HTTP_REQ_ACT_SET_MAP: {
3599 struct pat_ref *ref;
3600 char *key, *value;
3601 struct chunk *trash_key, *trash_value;
3602 int len;
3603
3604 trash_key = get_trash_chunk();
3605 trash_value = get_trash_chunk();
3606
3607 /* collect reference */
3608 ref = pat_ref_lookup(rule->arg.map.ref);
3609 if (!ref)
3610 continue;
3611
3612 /* collect key */
3613 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3614 key = trash_key->str;
3615 key[len] = '\0';
3616
3617 /* collect value */
3618 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3619 value = trash_value->str;
3620 value[len] = '\0';
3621
3622 /* perform update */
3623 if (pat_ref_find_elt(ref, key) != NULL)
3624 /* update entry if it exists */
3625 pat_ref_set(ref, key, value, NULL);
3626 else
3627 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003628 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003629
3630 break;
3631 }
William Lallemand73025dd2014-04-24 14:38:37 +02003632
3633 case HTTP_REQ_ACT_CUSTOM_CONT:
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003634 if (!rule->action_ptr(rule, px, s->sess, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003635 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003636 return HTTP_RULE_RES_YIELD;
3637 }
William Lallemand73025dd2014-04-24 14:38:37 +02003638 break;
3639
3640 case HTTP_REQ_ACT_CUSTOM_STOP:
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003641 rule->action_ptr(rule, px, s->sess, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003642 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003643
3644 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3645 /* Note: only the first valid tracking parameter of each
3646 * applies.
3647 */
3648
3649 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3650 struct stktable *t;
3651 struct stksess *ts;
3652 struct stktable_key *key;
3653 void *ptr;
3654
3655 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau192252e2015-04-04 01:47:55 +02003656 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
Willy Tarreau09448f72014-06-25 18:12:15 +02003657
3658 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003659 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003660
3661 /* let's count a new HTTP request as it's the first time we do it */
3662 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3663 if (ptr)
3664 stktable_data_cast(ptr, http_req_cnt)++;
3665
3666 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3667 if (ptr)
3668 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3669 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3670
3671 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003672 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003673 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3674 }
3675 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003676 break;
3677
3678 case HTTP_REQ_ACT_SET_SRC:
3679 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3680 struct sample *smp;
3681
3682 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.act.p[0], SMP_T_ADDR);
3683
3684 if (smp) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003685 if (smp->data.type == SMP_T_IPV4) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003686 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003687 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003688 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003689 } else if (smp->data.type == SMP_T_IPV6) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003690 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003691 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003692 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3693 }
3694 }
3695 }
3696 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003697 }
3698 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003699
3700 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003701 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003702}
3703
Willy Tarreau71241ab2012-12-27 11:30:54 +01003704
Willy Tarreau51d861a2015-05-22 17:30:48 +02003705/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3706 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3707 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3708 * is returned, the process can continue the evaluation of next rule list. If
3709 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3710 * is returned, it means the operation could not be processed and a server error
3711 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3712 * deny rule. If *YIELD is returned, the caller must call again the function
3713 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003714 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003715static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003716http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003717{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003718 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003719 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003720 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003721 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003722 struct hdr_ctx ctx;
3723
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003724 /* If "the current_rule_list" match the executed rule list, we are in
3725 * resume condition. If a resume is needed it is always in the action
3726 * and never in the ACL or converters. In this case, we initialise the
3727 * current rule, and go to the action execution point.
3728 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003729 if (s->current_rule) {
3730 rule = s->current_rule;
3731 s->current_rule = NULL;
3732 if (s->current_rule_list == rules)
3733 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003734 }
3735 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003736
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003737 list_for_each_entry(rule, rules, list) {
3738 if (rule->action >= HTTP_RES_ACT_MAX)
3739 continue;
3740
3741 /* check optional condition */
3742 if (rule->cond) {
3743 int ret;
3744
Willy Tarreau192252e2015-04-04 01:47:55 +02003745 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003746 ret = acl_pass(ret);
3747
3748 if (rule->cond->pol == ACL_COND_UNLESS)
3749 ret = !ret;
3750
3751 if (!ret) /* condition not matched */
3752 continue;
3753 }
3754
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003755resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003756 switch (rule->action) {
3757 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003758 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003759
3760 case HTTP_RES_ACT_DENY:
3761 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003762 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003763
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003764 case HTTP_RES_ACT_SET_NICE:
3765 s->task->nice = rule->arg.nice;
3766 break;
3767
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003768 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003769 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003770 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003771 break;
3772
Willy Tarreau51347ed2013-06-11 19:34:13 +02003773 case HTTP_RES_ACT_SET_MARK:
3774#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003775 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003776 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003777#endif
3778 break;
3779
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003780 case HTTP_RES_ACT_SET_LOGL:
3781 s->logs.level = rule->arg.loglevel;
3782 break;
3783
Sasha Pachev218f0642014-06-16 12:05:59 -06003784 case HTTP_RES_ACT_REPLACE_HDR:
3785 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003786 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3787 rule->arg.hdr_add.name_len,
3788 &rule->arg.hdr_add.fmt,
3789 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003790 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003791 break;
3792
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003793 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003794 ctx.idx = 0;
3795 /* remove all occurrences of the header */
3796 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3797 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3798 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3799 }
Willy Tarreau85603282015-01-21 20:39:27 +01003800 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003801
Willy Tarreau85603282015-01-21 20:39:27 +01003802 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003803 case HTTP_RES_ACT_ADD_HDR:
3804 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3805 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3806 trash.len = rule->arg.hdr_add.name_len;
3807 trash.str[trash.len++] = ':';
3808 trash.str[trash.len++] = ' ';
3809 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01003810
3811 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3812 /* remove all occurrences of the header */
3813 ctx.idx = 0;
3814 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3815 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3816 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3817 }
3818 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003819 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3820 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003821
3822 case HTTP_RES_ACT_DEL_ACL:
3823 case HTTP_RES_ACT_DEL_MAP: {
3824 struct pat_ref *ref;
3825 char *key;
3826 int len;
3827
3828 /* collect reference */
3829 ref = pat_ref_lookup(rule->arg.map.ref);
3830 if (!ref)
3831 continue;
3832
3833 /* collect key */
3834 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3835 key = trash.str;
3836 key[len] = '\0';
3837
3838 /* perform update */
3839 /* returned code: 1=ok, 0=ko */
3840 pat_ref_delete(ref, key);
3841
3842 break;
3843 }
3844
3845 case HTTP_RES_ACT_ADD_ACL: {
3846 struct pat_ref *ref;
3847 char *key;
3848 struct chunk *trash_key;
3849 int len;
3850
3851 trash_key = get_trash_chunk();
3852
3853 /* collect reference */
3854 ref = pat_ref_lookup(rule->arg.map.ref);
3855 if (!ref)
3856 continue;
3857
3858 /* collect key */
3859 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3860 key = trash_key->str;
3861 key[len] = '\0';
3862
3863 /* perform update */
3864 /* check if the entry already exists */
3865 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003866 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003867
3868 break;
3869 }
3870
3871 case HTTP_RES_ACT_SET_MAP: {
3872 struct pat_ref *ref;
3873 char *key, *value;
3874 struct chunk *trash_key, *trash_value;
3875 int len;
3876
3877 trash_key = get_trash_chunk();
3878 trash_value = get_trash_chunk();
3879
3880 /* collect reference */
3881 ref = pat_ref_lookup(rule->arg.map.ref);
3882 if (!ref)
3883 continue;
3884
3885 /* collect key */
3886 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3887 key = trash_key->str;
3888 key[len] = '\0';
3889
3890 /* collect value */
3891 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3892 value = trash_value->str;
3893 value[len] = '\0';
3894
3895 /* perform update */
3896 if (pat_ref_find_elt(ref, key) != NULL)
3897 /* update entry if it exists */
3898 pat_ref_set(ref, key, value, NULL);
3899 else
3900 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003901 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003902
3903 break;
3904 }
William Lallemand73025dd2014-04-24 14:38:37 +02003905
Willy Tarreau51d861a2015-05-22 17:30:48 +02003906 case HTTP_RES_ACT_REDIR:
3907 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3908 return HTTP_RULE_RES_BADREQ;
3909 return HTTP_RULE_RES_DONE;
3910
William Lallemand73025dd2014-04-24 14:38:37 +02003911 case HTTP_RES_ACT_CUSTOM_CONT:
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003912 if (!rule->action_ptr(rule, px, s->sess, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003913 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003914 return HTTP_RULE_RES_YIELD;
3915 }
William Lallemand73025dd2014-04-24 14:38:37 +02003916 break;
3917
3918 case HTTP_RES_ACT_CUSTOM_STOP:
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003919 rule->action_ptr(rule, px, s->sess, s);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003920 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003921 }
3922 }
3923
3924 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003925 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003926}
3927
3928
Willy Tarreau71241ab2012-12-27 11:30:54 +01003929/* Perform an HTTP redirect based on the information in <rule>. The function
3930 * returns non-zero on success, or zero in case of a, irrecoverable error such
3931 * as too large a request to build a valid response.
3932 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003933static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003934{
Willy Tarreaub329a312015-05-22 16:27:37 +02003935 struct http_msg *req = &txn->req;
3936 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003937 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003938 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003939
3940 /* build redirect message */
3941 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003942 case 308:
3943 msg_fmt = HTTP_308;
3944 break;
3945 case 307:
3946 msg_fmt = HTTP_307;
3947 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003948 case 303:
3949 msg_fmt = HTTP_303;
3950 break;
3951 case 301:
3952 msg_fmt = HTTP_301;
3953 break;
3954 case 302:
3955 default:
3956 msg_fmt = HTTP_302;
3957 break;
3958 }
3959
3960 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3961 return 0;
3962
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003963 location = trash.str + trash.len;
3964
Willy Tarreau71241ab2012-12-27 11:30:54 +01003965 switch(rule->type) {
3966 case REDIRECT_TYPE_SCHEME: {
3967 const char *path;
3968 const char *host;
3969 struct hdr_ctx ctx;
3970 int pathlen;
3971 int hostlen;
3972
3973 host = "";
3974 hostlen = 0;
3975 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003976 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003977 host = ctx.line + ctx.val;
3978 hostlen = ctx.vlen;
3979 }
3980
3981 path = http_get_path(txn);
3982 /* build message using path */
3983 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003984 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003985 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3986 int qs = 0;
3987 while (qs < pathlen) {
3988 if (path[qs] == '?') {
3989 pathlen = qs;
3990 break;
3991 }
3992 qs++;
3993 }
3994 }
3995 } else {
3996 path = "/";
3997 pathlen = 1;
3998 }
3999
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004000 if (rule->rdr_str) { /* this is an old "redirect" rule */
4001 /* check if we can add scheme + "://" + host + path */
4002 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4003 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004004
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004005 /* add scheme */
4006 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4007 trash.len += rule->rdr_len;
4008 }
4009 else {
4010 /* add scheme with executing log format */
4011 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004012
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004013 /* check if we can add scheme + "://" + host + path */
4014 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4015 return 0;
4016 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004017 /* add "://" */
4018 memcpy(trash.str + trash.len, "://", 3);
4019 trash.len += 3;
4020
4021 /* add host */
4022 memcpy(trash.str + trash.len, host, hostlen);
4023 trash.len += hostlen;
4024
4025 /* add path */
4026 memcpy(trash.str + trash.len, path, pathlen);
4027 trash.len += pathlen;
4028
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004029 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004030 if (trash.len && trash.str[trash.len - 1] != '/' &&
4031 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4032 if (trash.len > trash.size - 5)
4033 return 0;
4034 trash.str[trash.len] = '/';
4035 trash.len++;
4036 }
4037
4038 break;
4039 }
4040 case REDIRECT_TYPE_PREFIX: {
4041 const char *path;
4042 int pathlen;
4043
4044 path = http_get_path(txn);
4045 /* build message using path */
4046 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004047 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004048 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4049 int qs = 0;
4050 while (qs < pathlen) {
4051 if (path[qs] == '?') {
4052 pathlen = qs;
4053 break;
4054 }
4055 qs++;
4056 }
4057 }
4058 } else {
4059 path = "/";
4060 pathlen = 1;
4061 }
4062
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004063 if (rule->rdr_str) { /* this is an old "redirect" rule */
4064 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4065 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004066
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004067 /* add prefix. Note that if prefix == "/", we don't want to
4068 * add anything, otherwise it makes it hard for the user to
4069 * configure a self-redirection.
4070 */
4071 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4072 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4073 trash.len += rule->rdr_len;
4074 }
4075 }
4076 else {
4077 /* add prefix with executing log format */
4078 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4079
4080 /* Check length */
4081 if (trash.len + pathlen > trash.size - 4)
4082 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004083 }
4084
4085 /* add path */
4086 memcpy(trash.str + trash.len, path, pathlen);
4087 trash.len += pathlen;
4088
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004089 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004090 if (trash.len && trash.str[trash.len - 1] != '/' &&
4091 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4092 if (trash.len > trash.size - 5)
4093 return 0;
4094 trash.str[trash.len] = '/';
4095 trash.len++;
4096 }
4097
4098 break;
4099 }
4100 case REDIRECT_TYPE_LOCATION:
4101 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004102 if (rule->rdr_str) { /* this is an old "redirect" rule */
4103 if (trash.len + rule->rdr_len > trash.size - 4)
4104 return 0;
4105
4106 /* add location */
4107 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4108 trash.len += rule->rdr_len;
4109 }
4110 else {
4111 /* add location with executing log format */
4112 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004113
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004114 /* Check left length */
4115 if (trash.len > trash.size - 4)
4116 return 0;
4117 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004118 break;
4119 }
4120
4121 if (rule->cookie_len) {
4122 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4123 trash.len += 14;
4124 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4125 trash.len += rule->cookie_len;
4126 memcpy(trash.str + trash.len, "\r\n", 2);
4127 trash.len += 2;
4128 }
4129
4130 /* add end of headers and the keep-alive/close status.
4131 * We may choose to set keep-alive if the Location begins
4132 * with a slash, because the client will come back to the
4133 * same server.
4134 */
4135 txn->status = rule->code;
4136 /* let's log the request time */
4137 s->logs.tv_request = now;
4138
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004139 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004140 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004141 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004142 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4143 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4144 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004145 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004146 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4147 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4148 trash.len += 30;
4149 } else {
4150 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4151 trash.len += 24;
4152 }
4153 }
4154 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4155 trash.len += 4;
Willy Tarreaub329a312015-05-22 16:27:37 +02004156 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004157 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004158 bi_fast_delete(req->chn->buf, req->sov);
4159 req->next -= req->sov;
4160 req->sov = 0;
4161 s->req.analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004162 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreaub329a312015-05-22 16:27:37 +02004163 req->msg_state = HTTP_MSG_CLOSED;
4164 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004165 /* Trim any possible response */
4166 res->chn->buf->i = 0;
4167 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004168 } else {
4169 /* keep-alive not possible */
4170 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4171 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4172 trash.len += 29;
4173 } else {
4174 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4175 trash.len += 23;
4176 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004177 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreaub329a312015-05-22 16:27:37 +02004178 req->chn->analysers = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004179 }
4180
Willy Tarreaue7dff022015-04-03 01:14:29 +02004181 if (!(s->flags & SF_ERR_MASK))
4182 s->flags |= SF_ERR_LOCAL;
4183 if (!(s->flags & SF_FINST_MASK))
4184 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004185
4186 return 1;
4187}
4188
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004189/* This stream analyser runs all HTTP request processing which is common to
4190 * frontends and backends, which means blocking ACLs, filters, connection-close,
4191 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004192 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004193 * either needs more data or wants to immediately abort the request (eg: deny,
4194 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004195 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004196int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004197{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004198 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004199 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004200 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004201 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004202 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004203 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004204
Willy Tarreau655dce92009-11-08 13:10:58 +01004205 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004206 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004207 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004208 }
4209
Willy Tarreau87b09662015-04-03 00:22:06 +02004210 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02004211 now_ms, __FUNCTION__,
4212 s,
4213 req,
4214 req->rex, req->wex,
4215 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004216 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004217 req->analysers);
4218
Willy Tarreau65410832014-04-28 21:25:43 +02004219 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004220 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004221
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004222 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004223 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004224 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004225
Willy Tarreau0b748332014-04-29 00:13:29 +02004226 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004227 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4228 goto return_prx_yield;
4229
Willy Tarreau0b748332014-04-29 00:13:29 +02004230 case HTTP_RULE_RES_CONT:
4231 case HTTP_RULE_RES_STOP: /* nothing to do */
4232 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004233
Willy Tarreau0b748332014-04-29 00:13:29 +02004234 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4235 if (txn->flags & TX_CLTARPIT)
4236 goto tarpit;
4237 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004238
Willy Tarreau0b748332014-04-29 00:13:29 +02004239 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4240 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004241
Willy Tarreau0b748332014-04-29 00:13:29 +02004242 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004243 goto done;
4244
Willy Tarreau0b748332014-04-29 00:13:29 +02004245 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4246 goto return_bad_req;
4247 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004248 }
4249
Willy Tarreau52542592014-04-28 18:33:26 +02004250 /* OK at this stage, we know that the request was accepted according to
4251 * the http-request rules, we can check for the stats. Note that the
4252 * URI is detected *before* the req* rules in order not to be affected
4253 * by a possible reqrep, while they are processed *after* so that a
4254 * reqdeny can still block them. This clearly needs to change in 1.6!
4255 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004256 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004257 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004258 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004259 txn->status = 500;
4260 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004261 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004262
Willy Tarreaue7dff022015-04-03 01:14:29 +02004263 if (!(s->flags & SF_ERR_MASK))
4264 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004265 goto return_prx_cond;
4266 }
4267
4268 /* parse the whole stats request and extract the relevant information */
4269 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004270 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004271 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004272
Willy Tarreau0b748332014-04-29 00:13:29 +02004273 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4274 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004275
Willy Tarreau0b748332014-04-29 00:13:29 +02004276 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4277 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004278 }
4279
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004280 /* evaluate the req* rules except reqadd */
4281 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004282 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004283 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004284
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004285 if (txn->flags & TX_CLDENY)
4286 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004287
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004288 if (txn->flags & TX_CLTARPIT)
4289 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004290 }
Willy Tarreau06619262006-12-17 08:37:22 +01004291
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004292 /* add request headers from the rule sets in the same order */
4293 list_for_each_entry(wl, &px->req_add, list) {
4294 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004295 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004296 ret = acl_pass(ret);
4297 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4298 ret = !ret;
4299 if (!ret)
4300 continue;
4301 }
4302
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004303 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004304 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004305 }
4306
Willy Tarreau52542592014-04-28 18:33:26 +02004307
4308 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004309 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004310 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004311 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4312 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004313
Willy Tarreaue7dff022015-04-03 01:14:29 +02004314 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4315 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4316 if (!(s->flags & SF_FINST_MASK))
4317 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004318
Willy Tarreau70730dd2014-04-24 18:06:27 +02004319 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004320 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004321 select_compression_request_header(s, req->buf);
4322
4323 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004324 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004325 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004326 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004327
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004328 /* check whether we have some ACLs set to redirect this request */
4329 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004330 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004331 int ret;
4332
Willy Tarreau192252e2015-04-04 01:47:55 +02004333 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004334 ret = acl_pass(ret);
4335 if (rule->cond->pol == ACL_COND_UNLESS)
4336 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004337 if (!ret)
4338 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004339 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004340 if (!http_apply_redirect_rule(rule, s, txn))
4341 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004342 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004343 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004344
Willy Tarreau2be39392010-01-03 17:24:51 +01004345 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4346 * If this happens, then the data will not come immediately, so we must
4347 * send all what we have without waiting. Note that due to the small gain
4348 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004349 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004350 * itself once used.
4351 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004352 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004353
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004354 done: /* done with this analyser, continue with next ones that the calling
4355 * points will have set, if any.
4356 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004357 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004358 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4359 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004360 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004361
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004362 tarpit:
4363 /* When a connection is tarpitted, we use the tarpit timeout,
4364 * which may be the same as the connect timeout if unspecified.
4365 * If unset, then set it to zero because we really want it to
4366 * eventually expire. We build the tarpit as an analyser.
4367 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004368 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004369
4370 /* wipe the request out so that we can drop the connection early
4371 * if the client closes first.
4372 */
4373 channel_dont_connect(req);
4374 req->analysers = 0; /* remove switching rules etc... */
4375 req->analysers |= AN_REQ_HTTP_TARPIT;
4376 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4377 if (!req->analyse_exp)
4378 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004379 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004380 sess->fe->fe_counters.denied_req++;
4381 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004382 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004383 if (sess->listener->counters)
4384 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004385 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004386
4387 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004388 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004389 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004390 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004391 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004392 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004393 sess->fe->fe_counters.denied_req++;
4394 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004395 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004396 if (sess->listener->counters)
4397 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004398 goto return_prx_cond;
4399
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004400 return_bad_req:
4401 /* We centralize bad requests processing here */
4402 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4403 /* we detected a parsing error. We want to archive this request
4404 * in the dedicated proxy area for later troubleshooting.
4405 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004406 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004407 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004408
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004409 txn->req.msg_state = HTTP_MSG_ERROR;
4410 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004411 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004412
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004413 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004414 if (sess->listener->counters)
4415 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004416
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004417 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004418 if (!(s->flags & SF_ERR_MASK))
4419 s->flags |= SF_ERR_PRXCOND;
4420 if (!(s->flags & SF_FINST_MASK))
4421 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004422
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004423 req->analysers = 0;
4424 req->analyse_exp = TICK_ETERNITY;
4425 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004426
4427 return_prx_yield:
4428 channel_dont_connect(req);
4429 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004430}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004431
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004432/* This function performs all the processing enabled for the current request.
4433 * It returns 1 if the processing can continue on next analysers, or zero if it
4434 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004435 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004436 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004437int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004438{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004439 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004440 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004441 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004442 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004443
Willy Tarreau655dce92009-11-08 13:10:58 +01004444 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004445 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004446 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004447 return 0;
4448 }
4449
Willy Tarreau87b09662015-04-03 00:22:06 +02004450 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004451 now_ms, __FUNCTION__,
4452 s,
4453 req,
4454 req->rex, req->wex,
4455 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004456 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004457 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004458
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004459 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004460 select_compression_request_header(s, req->buf);
4461
Willy Tarreau59234e92008-11-30 23:51:27 +01004462 /*
4463 * Right now, we know that we have processed the entire headers
4464 * and that unwanted requests have been filtered out. We can do
4465 * whatever we want with the remaining request. Also, now we
4466 * may have separate values for ->fe, ->be.
4467 */
Willy Tarreau06619262006-12-17 08:37:22 +01004468
Willy Tarreau59234e92008-11-30 23:51:27 +01004469 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004470 * If HTTP PROXY is set we simply get remote server address parsing
4471 * incoming request. Note that this requires that a connection is
4472 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004473 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004474 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004475 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004476 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004477
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004478 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004479 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004480 txn->req.msg_state = HTTP_MSG_ERROR;
4481 txn->status = 500;
4482 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004483 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004484
Willy Tarreaue7dff022015-04-03 01:14:29 +02004485 if (!(s->flags & SF_ERR_MASK))
4486 s->flags |= SF_ERR_RESOURCE;
4487 if (!(s->flags & SF_FINST_MASK))
4488 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004489
4490 return 0;
4491 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004492
4493 path = http_get_path(txn);
4494 url2sa(req->buf->p + msg->sl.rq.u,
4495 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004496 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004497 /* if the path was found, we have to remove everything between
4498 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4499 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4500 * u_l characters by a single "/".
4501 */
4502 if (path) {
4503 char *cur_ptr = req->buf->p;
4504 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4505 int delta;
4506
4507 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4508 http_msg_move_end(&txn->req, delta);
4509 cur_end += delta;
4510 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4511 goto return_bad_req;
4512 }
4513 else {
4514 char *cur_ptr = req->buf->p;
4515 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4516 int delta;
4517
4518 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4519 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4520 http_msg_move_end(&txn->req, delta);
4521 cur_end += delta;
4522 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4523 goto return_bad_req;
4524 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004525 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004526
Willy Tarreau59234e92008-11-30 23:51:27 +01004527 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004528 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004529 * Note that doing so might move headers in the request, but
4530 * the fields will stay coherent and the URI will not move.
4531 * This should only be performed in the backend.
4532 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004533 if ((s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004534 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4535 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004536
William Lallemanda73203e2012-03-12 12:48:57 +01004537 /* add unique-id if "header-unique-id" is specified */
4538
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004539 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004540 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4541 goto return_bad_req;
4542 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004543 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004544 }
William Lallemanda73203e2012-03-12 12:48:57 +01004545
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004546 if (sess->fe->header_unique_id && s->unique_id) {
4547 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004548 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004549 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004550 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004551 goto return_bad_req;
4552 }
4553
Cyril Bontéb21570a2009-11-29 20:04:48 +01004554 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004555 * 9: add X-Forwarded-For if either the frontend or the backend
4556 * asks for it.
4557 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004558 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004559 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004560 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4561 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4562 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004563 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004564 /* The header is set to be added only if none is present
4565 * and we found it, so don't do anything.
4566 */
4567 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004568 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004569 /* Add an X-Forwarded-For header unless the source IP is
4570 * in the 'except' network range.
4571 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004572 if ((!sess->fe->except_mask.s_addr ||
4573 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4574 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004575 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004576 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004577 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004578 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004579 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004580 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004581
4582 /* Note: we rely on the backend to get the header name to be used for
4583 * x-forwarded-for, because the header is really meant for the backends.
4584 * However, if the backend did not specify any option, we have to rely
4585 * on the frontend's header name.
4586 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004587 if (s->be->fwdfor_hdr_len) {
4588 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004589 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004590 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004591 len = sess->fe->fwdfor_hdr_len;
4592 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004593 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004594 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01004595
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004596 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004597 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004598 }
4599 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004600 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004601 /* FIXME: for the sake of completeness, we should also support
4602 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004603 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004604 int len;
4605 char pn[INET6_ADDRSTRLEN];
4606 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004607 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004608 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004609
Willy Tarreau59234e92008-11-30 23:51:27 +01004610 /* Note: we rely on the backend to get the header name to be used for
4611 * x-forwarded-for, because the header is really meant for the backends.
4612 * However, if the backend did not specify any option, we have to rely
4613 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004614 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004615 if (s->be->fwdfor_hdr_len) {
4616 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004617 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004618 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004619 len = sess->fe->fwdfor_hdr_len;
4620 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004621 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004622 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004623
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004624 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004625 goto return_bad_req;
4626 }
4627 }
4628
4629 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004630 * 10: add X-Original-To if either the frontend or the backend
4631 * asks for it.
4632 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004633 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004634
4635 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004636 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004637 /* Add an X-Original-To header unless the destination IP is
4638 * in the 'except' network range.
4639 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004640 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004641
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004642 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004643 ((!sess->fe->except_mask_to.s_addr ||
4644 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4645 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004646 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004647 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004648 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004649 int len;
4650 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004651 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004652
4653 /* Note: we rely on the backend to get the header name to be used for
4654 * x-original-to, because the header is really meant for the backends.
4655 * However, if the backend did not specify any option, we have to rely
4656 * on the frontend's header name.
4657 */
4658 if (s->be->orgto_hdr_len) {
4659 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004660 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004661 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004662 len = sess->fe->orgto_hdr_len;
4663 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004664 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004665 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004666
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004667 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004668 goto return_bad_req;
4669 }
4670 }
4671 }
4672
Willy Tarreau50fc7772012-11-11 22:19:57 +01004673 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4674 * If an "Upgrade" token is found, the header is left untouched in order not to have
4675 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4676 * "Upgrade" is present in the Connection header.
4677 */
4678 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4679 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004680 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004681 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004682 unsigned int want_flags = 0;
4683
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004684 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004685 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004686 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004687 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004688 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004689 want_flags |= TX_CON_CLO_SET;
4690 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004691 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004692 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004693 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004694 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004695 want_flags |= TX_CON_KAL_SET;
4696 }
4697
4698 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004699 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004700 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004701
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004702
Willy Tarreau522d6c02009-12-06 18:49:18 +01004703 /* If we have no server assigned yet and we're balancing on url_param
4704 * with a POST request, we may be interested in checking the body for
4705 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004706 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004707 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004708 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004709 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004710 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004711 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004712 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004713
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004714 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004715 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004716#ifdef TCP_QUICKACK
4717 /* We expect some data from the client. Unless we know for sure
4718 * we already have a full request, we have to re-enable quick-ack
4719 * in case we previously disabled it, otherwise we might cause
4720 * the client to delay further data.
4721 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004722 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004723 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004724 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004725 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004726 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004727#endif
4728 }
Willy Tarreau03945942009-12-22 16:50:27 +01004729
Willy Tarreau59234e92008-11-30 23:51:27 +01004730 /*************************************************************
4731 * OK, that's finished for the headers. We have done what we *
4732 * could. Let's switch to the DATA state. *
4733 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004734 req->analyse_exp = TICK_ETERNITY;
4735 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004736
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004737 /* if the server closes the connection, we want to immediately react
4738 * and close the socket to save packets and syscalls.
4739 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004740 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004741 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004742
Willy Tarreau59234e92008-11-30 23:51:27 +01004743 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004744 /* OK let's go on with the BODY now */
4745 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004746
Willy Tarreau59234e92008-11-30 23:51:27 +01004747 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004748 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004749 /* we detected a parsing error. We want to archive this request
4750 * in the dedicated proxy area for later troubleshooting.
4751 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004752 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004753 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004754
Willy Tarreau59234e92008-11-30 23:51:27 +01004755 txn->req.msg_state = HTTP_MSG_ERROR;
4756 txn->status = 400;
4757 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004758 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004759
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004760 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004761 if (sess->listener->counters)
4762 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004763
Willy Tarreaue7dff022015-04-03 01:14:29 +02004764 if (!(s->flags & SF_ERR_MASK))
4765 s->flags |= SF_ERR_PRXCOND;
4766 if (!(s->flags & SF_FINST_MASK))
4767 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004768 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004769}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004770
Willy Tarreau60b85b02008-11-30 23:28:40 +01004771/* This function is an analyser which processes the HTTP tarpit. It always
4772 * returns zero, at the beginning because it prevents any other processing
4773 * from occurring, and at the end because it terminates the request.
4774 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004775int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004776{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004777 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004778
4779 /* This connection is being tarpitted. The CLIENT side has
4780 * already set the connect expiration date to the right
4781 * timeout. We just have to check that the client is still
4782 * there and that the timeout has not expired.
4783 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004784 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004785 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004786 !tick_is_expired(req->analyse_exp, now_ms))
4787 return 0;
4788
4789 /* We will set the queue timer to the time spent, just for
4790 * logging purposes. We fake a 500 server error, so that the
4791 * attacker will not suspect his connection has been tarpitted.
4792 * It will not cause trouble to the logs because we can exclude
4793 * the tarpitted connections by filtering on the 'PT' status flags.
4794 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004795 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4796
4797 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004798 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004799 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004800
4801 req->analysers = 0;
4802 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004803
Willy Tarreaue7dff022015-04-03 01:14:29 +02004804 if (!(s->flags & SF_ERR_MASK))
4805 s->flags |= SF_ERR_PRXCOND;
4806 if (!(s->flags & SF_FINST_MASK))
4807 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004808 return 0;
4809}
4810
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004811/* This function is an analyser which waits for the HTTP request body. It waits
4812 * for either the buffer to be full, or the full advertised contents to have
4813 * reached the buffer. It must only be called after the standard HTTP request
4814 * processing has occurred, because it expects the request to be parsed and will
4815 * look for the Expect header. It may send a 100-Continue interim response. It
4816 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4817 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4818 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004819 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004820int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004821{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004822 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004823 struct http_txn *txn = s->txn;
4824 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004825
4826 /* We have to parse the HTTP request body to find any required data.
4827 * "balance url_param check_post" should have been the only way to get
4828 * into this. We were brought here after HTTP header analysis, so all
4829 * related structures are ready.
4830 */
4831
Willy Tarreau890988f2014-04-10 11:59:33 +02004832 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4833 /* This is the first call */
4834 if (msg->msg_state < HTTP_MSG_BODY)
4835 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004836
Willy Tarreau890988f2014-04-10 11:59:33 +02004837 if (msg->msg_state < HTTP_MSG_100_SENT) {
4838 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4839 * send an HTTP/1.1 100 Continue intermediate response.
4840 */
4841 if (msg->flags & HTTP_MSGF_VER_11) {
4842 struct hdr_ctx ctx;
4843 ctx.idx = 0;
4844 /* Expect is allowed in 1.1, look for it */
4845 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4846 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004847 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004848 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004849 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004850 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004851 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004852
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004853 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004854 * req->buf->p still points to the beginning of the message. We
4855 * must save the body in msg->next because it survives buffer
4856 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004857 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004858 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004859
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004860 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004861 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4862 else
4863 msg->msg_state = HTTP_MSG_DATA;
4864 }
4865
Willy Tarreau890988f2014-04-10 11:59:33 +02004866 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4867 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004868 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004869 goto missing_data;
4870
4871 /* OK we have everything we need now */
4872 goto http_end;
4873 }
4874
4875 /* OK here we're parsing a chunked-encoded message */
4876
Willy Tarreau522d6c02009-12-06 18:49:18 +01004877 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004878 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004879 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004880 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004881 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004882 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004883
Willy Tarreau115acb92009-12-26 13:56:06 +01004884 if (!ret)
4885 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004886 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004887 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004888 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004889 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004890 }
4891
Willy Tarreaud98cf932009-12-27 22:54:55 +01004892 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004893 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4894 * for at least a whole chunk or the whole content length bytes after
4895 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004896 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004897 if (msg->msg_state == HTTP_MSG_TRAILERS)
4898 goto http_end;
4899
Willy Tarreaue115b492015-05-01 23:05:14 +02004900 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004901 goto http_end;
4902
4903 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004904 /* we get here if we need to wait for more data. If the buffer is full,
4905 * we have the maximum we can expect.
4906 */
4907 if (buffer_full(req->buf, global.tune.maxrewrite))
4908 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004909
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004910 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004911 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004912 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004913
Willy Tarreaue7dff022015-04-03 01:14:29 +02004914 if (!(s->flags & SF_ERR_MASK))
4915 s->flags |= SF_ERR_CLITO;
4916 if (!(s->flags & SF_FINST_MASK))
4917 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004918 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004919 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004920
4921 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004922 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004923 /* Not enough data. We'll re-use the http-request
4924 * timeout here. Ideally, we should set the timeout
4925 * relative to the accept() date. We just set the
4926 * request timeout once at the beginning of the
4927 * request.
4928 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004929 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004930 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004931 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004932 return 0;
4933 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004934
4935 http_end:
4936 /* The situation will not evolve, so let's give up on the analysis. */
4937 s->logs.tv_request = now; /* update the request timer to reflect full request */
4938 req->analysers &= ~an_bit;
4939 req->analyse_exp = TICK_ETERNITY;
4940 return 1;
4941
4942 return_bad_req: /* let's centralize all bad requests */
4943 txn->req.msg_state = HTTP_MSG_ERROR;
4944 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004945 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004946
Willy Tarreaue7dff022015-04-03 01:14:29 +02004947 if (!(s->flags & SF_ERR_MASK))
4948 s->flags |= SF_ERR_PRXCOND;
4949 if (!(s->flags & SF_FINST_MASK))
4950 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004951
Willy Tarreau522d6c02009-12-06 18:49:18 +01004952 return_err_msg:
4953 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004954 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004955 if (sess->listener->counters)
4956 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004957 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004958}
4959
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004960/* send a server's name with an outgoing request over an established connection.
4961 * Note: this function is designed to be called once the request has been scheduled
4962 * for being forwarded. This is the reason why it rewinds the buffer before
4963 * proceeding.
4964 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004965int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004966
4967 struct hdr_ctx ctx;
4968
Mark Lamourinec2247f02012-01-04 13:02:01 -05004969 char *hdr_name = be->server_id_hdr_name;
4970 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004971 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004972 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004973 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004974
William Lallemandd9e90662012-01-30 17:27:17 +01004975 ctx.idx = 0;
4976
Willy Tarreau211cdec2014-04-17 20:18:08 +02004977 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004978 if (old_o) {
4979 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004980 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004981 txn->req.next += old_o;
4982 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004983 }
4984
Willy Tarreau9b28e032012-10-12 23:49:43 +02004985 old_i = chn->buf->i;
4986 while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004987 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004988 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004989 }
4990
4991 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004992 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004993 memcpy(hdr_val, hdr_name, hdr_name_len);
4994 hdr_val += hdr_name_len;
4995 *hdr_val++ = ':';
4996 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004997 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4998 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004999
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005000 if (old_o) {
5001 /* If this was a forwarded request, we must readjust the amount of
5002 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005003 * variations. Note that the current state is >= HTTP_MSG_BODY,
5004 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005005 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005006 old_o += chn->buf->i - old_i;
5007 b_adv(chn->buf, old_o);
5008 txn->req.next -= old_o;
5009 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005010 }
5011
Mark Lamourinec2247f02012-01-04 13:02:01 -05005012 return 0;
5013}
5014
Willy Tarreau610ecce2010-01-04 21:15:02 +01005015/* Terminate current transaction and prepare a new one. This is very tricky
5016 * right now but it works.
5017 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005018void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005019{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005020 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005021 struct proxy *fe = strm_fe(s);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005022 struct connection *srv_conn;
5023 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005024 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005025
Willy Tarreau610ecce2010-01-04 21:15:02 +01005026 /* FIXME: We need a more portable way of releasing a backend's and a
5027 * server's connections. We need a safer way to reinitialize buffer
5028 * flags. We also need a more accurate method for computing per-request
5029 * data.
5030 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005031 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005032
Willy Tarreau4213a112013-12-15 10:25:42 +01005033 /* unless we're doing keep-alive, we want to quickly close the connection
5034 * to the server.
5035 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005036 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005037 !si_conn_ready(&s->si[1])) {
5038 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5039 si_shutr(&s->si[1]);
5040 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005041 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005042
Willy Tarreaue7dff022015-04-03 01:14:29 +02005043 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005044 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005045 if (unlikely(s->srv_conn))
5046 sess_change_server(s, NULL);
5047 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005048
5049 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005050 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005051
Willy Tarreaueee5b512015-04-03 23:46:31 +02005052 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005053 int n;
5054
Willy Tarreaueee5b512015-04-03 23:46:31 +02005055 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005056 if (n < 1 || n > 5)
5057 n = 0;
5058
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005059 if (fe->mode == PR_MODE_HTTP) {
5060 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005061 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005062 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005063 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005064 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005065 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005066 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005067 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005068 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005069 s->be->be_counters.p.http.comp_rsp++;
5070 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005071 }
5072
5073 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005074 s->logs.bytes_in -= s->req.buf->i;
5075 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005076
5077 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005078 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005079 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005080 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005081 s->do_log(s);
5082 }
5083
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005084 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005085 stream_stop_content_counters(s);
5086 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005087
Willy Tarreau610ecce2010-01-04 21:15:02 +01005088 s->logs.accept_date = date; /* user-visible date for logging */
5089 s->logs.tv_accept = now; /* corrected date for internal use */
5090 tv_zero(&s->logs.tv_request);
5091 s->logs.t_queue = -1;
5092 s->logs.t_connect = -1;
5093 s->logs.t_data = -1;
5094 s->logs.t_close = 0;
5095 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5096 s->logs.srv_queue_size = 0; /* we will get this number soon */
5097
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005098 s->logs.bytes_in = s->req.total = s->req.buf->i;
5099 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005100
5101 if (s->pend_pos)
5102 pendconn_free(s->pend_pos);
5103
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005104 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005105 if (s->flags & SF_CURR_SESS) {
5106 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005107 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005108 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005109 if (may_dequeue_tasks(objt_server(s->target), s->be))
5110 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005111 }
5112
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005113 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005114
Willy Tarreau4213a112013-12-15 10:25:42 +01005115 /* only release our endpoint if we don't intend to reuse the
5116 * connection.
5117 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005118 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005119 !si_conn_ready(&s->si[1])) {
5120 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005121 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005122 }
5123
Willy Tarreau350f4872014-11-28 14:42:25 +01005124 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5125 s->si[1].err_type = SI_ET_NONE;
5126 s->si[1].conn_retries = 0; /* used for logging too */
5127 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005128 s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005129 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
5130 s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA);
Willy Tarreaue7dff022015-04-03 01:14:29 +02005131 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5132 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5133 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005134
Willy Tarreaueee5b512015-04-03 23:46:31 +02005135 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005136 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005137 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005138
5139 if (prev_status == 401 || prev_status == 407) {
5140 /* In HTTP keep-alive mode, if we receive a 401, we still have
5141 * a chance of being able to send the visitor again to the same
5142 * server over the same connection. This is required by some
5143 * broken protocols such as NTLM, and anyway whenever there is
5144 * an opportunity for sending the challenge to the proper place,
5145 * it's better to do it (at least it helps with debugging).
5146 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005147 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau387ebf82015-08-04 19:24:13 +02005148 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005149 }
5150
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005151 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005152 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005153
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005154 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005155 s->req.flags |= CF_NEVER_WAIT;
5156 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005157 }
5158
Willy Tarreau610ecce2010-01-04 21:15:02 +01005159 /* if the request buffer is not empty, it means we're
5160 * about to process another request, so send pending
5161 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005162 * Just don't do this if the buffer is close to be full,
5163 * because the request will wait for it to flush a little
5164 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005165 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005166 if (s->req.buf->i) {
5167 if (s->res.buf->o &&
5168 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5169 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5170 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005171 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005172
5173 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005174 channel_auto_read(&s->req);
5175 channel_auto_close(&s->req);
5176 channel_auto_read(&s->res);
5177 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005178
Willy Tarreau27375622013-12-17 00:00:28 +01005179 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005180 if (srv_conn) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005181 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005182 if (!srv)
5183 si_idle_conn(&s->si[1], NULL);
5184 else if ((srv_conn->flags & CO_FL_PRIVATE) ||
5185 ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR))
5186 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005187 else if (prev_flags & TX_NOT_FIRST)
5188 /* note: we check the request, not the connection, but
5189 * this is valid for strategies SAFE and AGGR, and in
5190 * case of ALWS, we don't care anyway.
5191 */
5192 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005193 else
5194 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005195 }
Willy Tarreau27375622013-12-17 00:00:28 +01005196
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005197 s->req.analysers = strm_li(s)->analysers;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005198 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005199}
5200
5201
5202/* This function updates the request state machine according to the response
5203 * state machine and buffer flags. It returns 1 if it changes anything (flag
5204 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5205 * it is only used to find when a request/response couple is complete. Both
5206 * this function and its equivalent should loop until both return zero. It
5207 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5208 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005209int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005210{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005211 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005212 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005213 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005214 unsigned int old_state = txn->req.msg_state;
5215
Willy Tarreau610ecce2010-01-04 21:15:02 +01005216 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5217 return 0;
5218
5219 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005220 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005221 * We can shut the read side unless we want to abort_on_close,
5222 * or we have a POST request. The issue with POST requests is
5223 * that some browsers still send a CRLF after the request, and
5224 * this CRLF must be read so that it does not remain in the kernel
5225 * buffers, otherwise a close could cause an RST on some systems
5226 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005227 * Note that if we're using keep-alive on the client side, we'd
5228 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005229 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005230 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005231 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005232 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5233 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5234 !(s->be->options & PR_O_ABRT_CLOSE) &&
5235 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005236 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005237
Willy Tarreau40f151a2012-12-20 12:10:09 +01005238 /* if the server closes the connection, we want to immediately react
5239 * and close the socket to save packets and syscalls.
5240 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005241 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005242
Willy Tarreau610ecce2010-01-04 21:15:02 +01005243 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5244 goto wait_other_side;
5245
5246 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5247 /* The server has not finished to respond, so we
5248 * don't want to move in order not to upset it.
5249 */
5250 goto wait_other_side;
5251 }
5252
5253 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5254 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005255 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005256 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005257 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005258 goto wait_other_side;
5259 }
5260
5261 /* When we get here, it means that both the request and the
5262 * response have finished receiving. Depending on the connection
5263 * mode, we'll have to wait for the last bytes to leave in either
5264 * direction, and sometimes for a close to be effective.
5265 */
5266
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005267 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5268 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005269 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5270 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005271 }
5272 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5273 /* Option forceclose is set, or either side wants to close,
5274 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005275 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005276 * once both states are CLOSED.
5277 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005278 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5279 channel_shutr_now(chn);
5280 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005281 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005282 }
5283 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005284 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5285 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005286 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005287 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5288 channel_auto_read(chn);
5289 txn->req.msg_state = HTTP_MSG_TUNNEL;
5290 chn->flags |= CF_NEVER_WAIT;
5291 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005292 }
5293
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005294 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005295 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005296 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005297
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005298 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005299 txn->req.msg_state = HTTP_MSG_CLOSING;
5300 goto http_msg_closing;
5301 }
5302 else {
5303 txn->req.msg_state = HTTP_MSG_CLOSED;
5304 goto http_msg_closed;
5305 }
5306 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005307 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005308 }
5309
5310 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5311 http_msg_closing:
5312 /* nothing else to forward, just waiting for the output buffer
5313 * to be empty and for the shutw_now to take effect.
5314 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005315 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005316 txn->req.msg_state = HTTP_MSG_CLOSED;
5317 goto http_msg_closed;
5318 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005319 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005320 txn->req.msg_state = HTTP_MSG_ERROR;
5321 goto wait_other_side;
5322 }
5323 }
5324
5325 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5326 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005327 /* see above in MSG_DONE why we only do this in these states */
5328 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5329 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5330 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005331 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005332 goto wait_other_side;
5333 }
5334
5335 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005336 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005337}
5338
5339
5340/* This function updates the response state machine according to the request
5341 * state machine and buffer flags. It returns 1 if it changes anything (flag
5342 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5343 * it is only used to find when a request/response couple is complete. Both
5344 * this function and its equivalent should loop until both return zero. It
5345 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5346 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005347int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005348{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005349 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005350 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005351 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005352 unsigned int old_state = txn->rsp.msg_state;
5353
Willy Tarreau610ecce2010-01-04 21:15:02 +01005354 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5355 return 0;
5356
5357 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5358 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005359 * still monitor the server connection for a possible close
5360 * while the request is being uploaded, so we don't disable
5361 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005362 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005363 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005364
5365 if (txn->req.msg_state == HTTP_MSG_ERROR)
5366 goto wait_other_side;
5367
5368 if (txn->req.msg_state < HTTP_MSG_DONE) {
5369 /* The client seems to still be sending data, probably
5370 * because we got an error response during an upload.
5371 * We have the choice of either breaking the connection
5372 * or letting it pass through. Let's do the later.
5373 */
5374 goto wait_other_side;
5375 }
5376
5377 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5378 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005379 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005380 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005381 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005382 goto wait_other_side;
5383 }
5384
5385 /* When we get here, it means that both the request and the
5386 * response have finished receiving. Depending on the connection
5387 * mode, we'll have to wait for the last bytes to leave in either
5388 * direction, and sometimes for a close to be effective.
5389 */
5390
5391 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5392 /* Server-close mode : shut read and wait for the request
5393 * side to close its output buffer. The caller will detect
5394 * when we're in DONE and the other is in CLOSED and will
5395 * catch that for the final cleanup.
5396 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005397 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5398 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005399 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005400 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5401 /* Option forceclose is set, or either side wants to close,
5402 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005403 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005404 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005405 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005406 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5407 channel_shutr_now(chn);
5408 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005409 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005410 }
5411 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005412 /* The last possible modes are keep-alive and tunnel. Tunnel will
5413 * need to forward remaining data. Keep-alive will need to monitor
5414 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005415 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005416 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005417 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005418 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5419 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005420 }
5421
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005422 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005423 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005424 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005425 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5426 goto http_msg_closing;
5427 }
5428 else {
5429 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5430 goto http_msg_closed;
5431 }
5432 }
5433 goto wait_other_side;
5434 }
5435
5436 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5437 http_msg_closing:
5438 /* nothing else to forward, just waiting for the output buffer
5439 * to be empty and for the shutw_now to take effect.
5440 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005441 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005442 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5443 goto http_msg_closed;
5444 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005445 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005446 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005447 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005448 if (objt_server(s->target))
5449 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005450 goto wait_other_side;
5451 }
5452 }
5453
5454 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5455 http_msg_closed:
5456 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005457 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005458 channel_auto_close(chn);
5459 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005460 goto wait_other_side;
5461 }
5462
5463 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005464 /* We force the response to leave immediately if we're waiting for the
5465 * other side, since there is no pending shutdown to push it out.
5466 */
5467 if (!channel_is_empty(chn))
5468 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005469 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005470}
5471
5472
5473/* Resync the request and response state machines. Return 1 if either state
5474 * changes.
5475 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005476int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005477{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005478 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005479 int old_req_state = txn->req.msg_state;
5480 int old_res_state = txn->rsp.msg_state;
5481
Willy Tarreau610ecce2010-01-04 21:15:02 +01005482 http_sync_req_state(s);
5483 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005484 if (!http_sync_res_state(s))
5485 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005486 if (!http_sync_req_state(s))
5487 break;
5488 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005489
Willy Tarreau610ecce2010-01-04 21:15:02 +01005490 /* OK, both state machines agree on a compatible state.
5491 * There are a few cases we're interested in :
5492 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5493 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5494 * directions, so let's simply disable both analysers.
5495 * - HTTP_MSG_CLOSED on the response only means we must abort the
5496 * request.
5497 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5498 * with server-close mode means we've completed one request and we
5499 * must re-initialize the server connection.
5500 */
5501
5502 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5503 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5504 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5505 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005506 s->req.analysers = 0;
5507 channel_auto_close(&s->req);
5508 channel_auto_read(&s->req);
5509 s->res.analysers = 0;
5510 channel_auto_close(&s->res);
5511 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005512 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005513 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005514 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005515 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005516 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005517 s->res.analysers = 0;
5518 channel_auto_close(&s->res);
5519 channel_auto_read(&s->res);
5520 s->req.analysers = 0;
5521 channel_abort(&s->req);
5522 channel_auto_close(&s->req);
5523 channel_auto_read(&s->req);
5524 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005525 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005526 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5527 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005528 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005529 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5530 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5531 /* server-close/keep-alive: terminate this transaction,
5532 * possibly killing the server connection and reinitialize
5533 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005534 */
5535 http_end_txn_clean_session(s);
5536 }
5537
Willy Tarreau610ecce2010-01-04 21:15:02 +01005538 return txn->req.msg_state != old_req_state ||
5539 txn->rsp.msg_state != old_res_state;
5540}
5541
Willy Tarreaud98cf932009-12-27 22:54:55 +01005542/* This function is an analyser which forwards request body (including chunk
5543 * sizes if any). It is called as soon as we must forward, even if we forward
5544 * zero byte. The only situation where it must not be called is when we're in
5545 * tunnel mode and we want to forward till the close. It's used both to forward
5546 * remaining data and to resync after end of body. It expects the msg_state to
5547 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005548 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005549 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005550 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005551 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005552int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005553{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005554 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005555 struct http_txn *txn = s->txn;
5556 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005557
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005558 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5559 return 0;
5560
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005561 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005562 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005563 /* Output closed while we were sending data. We must abort and
5564 * wake the other side up.
5565 */
5566 msg->msg_state = HTTP_MSG_ERROR;
5567 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005568 return 1;
5569 }
5570
Willy Tarreaud98cf932009-12-27 22:54:55 +01005571 /* Note that we don't have to send 100-continue back because we don't
5572 * need the data to complete our job, and it's up to the server to
5573 * decide whether to return 100, 417 or anything else in return of
5574 * an "Expect: 100-continue" header.
5575 */
5576
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005577 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005578 /* we have msg->sov which points to the first byte of message
5579 * body, and req->buf.p still points to the beginning of the
5580 * message. We forward the headers now, as we don't need them
5581 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005582 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005583 b_adv(req->buf, msg->sov);
5584 msg->next -= msg->sov;
5585 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005586
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005587 /* The previous analysers guarantee that the state is somewhere
5588 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5589 * msg->next are always correct.
5590 */
5591 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5592 if (msg->flags & HTTP_MSGF_TE_CHNK)
5593 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5594 else
5595 msg->msg_state = HTTP_MSG_DATA;
5596 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005597 }
5598
Willy Tarreau7ba23542014-04-17 21:50:00 +02005599 /* Some post-connect processing might want us to refrain from starting to
5600 * forward data. Currently, the only reason for this is "balance url_param"
5601 * whichs need to parse/process the request after we've enabled forwarding.
5602 */
5603 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005604 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005605 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005606 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005607 goto missing_data;
5608 }
5609 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5610 }
5611
Willy Tarreau80a92c02014-03-12 10:41:13 +01005612 /* in most states, we should abort in case of early close */
5613 channel_auto_close(req);
5614
Willy Tarreauefdf0942014-04-24 20:08:57 +02005615 if (req->to_forward) {
5616 /* We can't process the buffer's contents yet */
5617 req->flags |= CF_WAKE_WRITE;
5618 goto missing_data;
5619 }
5620
Willy Tarreaud98cf932009-12-27 22:54:55 +01005621 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005622 if (msg->msg_state == HTTP_MSG_DATA) {
5623 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005624 /* we may have some pending data starting at req->buf->p */
5625 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005626 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005627 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005628 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005629 msg->next += msg->chunk_len;
5630 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005631
5632 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005633 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005634 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005635 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005636 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005637 }
5638 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005639 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005640 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005641 * TRAILERS state.
5642 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005643 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005644
Willy Tarreau54d23df2012-10-25 19:04:45 +02005645 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005646 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005647 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005648 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005649 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005650 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005651 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005652 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005653 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005654 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005655 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005656 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005657 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005658
5659 if (ret == 0)
5660 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005661 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005662 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005663 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005664 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005665 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005666 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005667 /* we're in MSG_CHUNK_SIZE now */
5668 }
5669 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005670 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005671
5672 if (ret == 0)
5673 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005674 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005675 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005676 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005677 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005678 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005679 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005680 /* we're in HTTP_MSG_DONE now */
5681 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005682 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005683 int old_state = msg->msg_state;
5684
Willy Tarreau610ecce2010-01-04 21:15:02 +01005685 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005686
5687 /* we may have some pending data starting at req->buf->p
5688 * such as last chunk of data or trailers.
5689 */
5690 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005691 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005692 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005693 msg->next = 0;
5694
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005695 /* we don't want to forward closes on DONE except in
5696 * tunnel mode.
5697 */
5698 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005699 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005700 if (http_resync_states(s)) {
5701 /* some state changes occurred, maybe the analyser
5702 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005703 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005704 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005705 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005706 /* request errors are most likely due to
5707 * the server aborting the transfer.
5708 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005709 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005710 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005711 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005712 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005713 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005714 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005715 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005716 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005717
5718 /* If "option abortonclose" is set on the backend, we
5719 * want to monitor the client's connection and forward
5720 * any shutdown notification to the server, which will
5721 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005722 * request. We only do that in tunnel mode, and not in
5723 * other modes since it can be abused to exhaust source
5724 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005725 */
5726 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005727 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005728 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5729 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5730 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005731 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005732 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005733 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005734 /* POST requests may require to read extra CRLF
5735 * sent by broken browsers and which could cause
5736 * an RST to be sent upon close on some systems
5737 * (eg: Linux).
5738 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005739 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005740 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005741
Willy Tarreau610ecce2010-01-04 21:15:02 +01005742 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005743 }
5744 }
5745
Willy Tarreaud98cf932009-12-27 22:54:55 +01005746 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005747 /* we may have some pending data starting at req->buf->p */
5748 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005749 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005750 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5751
Willy Tarreaubed410e2014-04-22 08:19:34 +02005752 msg->next = 0;
5753 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5754
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005755 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005756 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005757 if (!(s->flags & SF_ERR_MASK))
5758 s->flags |= SF_ERR_CLICL;
5759 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005760 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005761 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005762 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005763 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005764 }
5765
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005766 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005767 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005768 if (objt_server(s->target))
5769 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005770
5771 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005772 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005773
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005774 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005775 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005776 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005777
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005778 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005779 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005780 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005781 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005782 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005783
Willy Tarreau5c620922011-05-11 19:56:11 +02005784 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005785 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005786 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005787 * modes are already handled by the stream sock layer. We must not do
5788 * this in content-length mode because it could present the MSG_MORE
5789 * flag with the last block of forwarded data, which would cause an
5790 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005791 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005792 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005793 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005794
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005795 return 0;
5796
Willy Tarreaud98cf932009-12-27 22:54:55 +01005797 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005798 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005799 if (sess->listener->counters)
5800 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005801
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005802 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005803 /* we may have some pending data starting at req->buf->p */
5804 b_adv(req->buf, msg->next);
5805 msg->next = 0;
5806
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005807 txn->req.msg_state = HTTP_MSG_ERROR;
5808 if (txn->status) {
5809 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005810 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005811 } else {
5812 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005813 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005814 }
5815 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005816 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005817
Willy Tarreaue7dff022015-04-03 01:14:29 +02005818 if (!(s->flags & SF_ERR_MASK))
5819 s->flags |= SF_ERR_PRXCOND;
5820 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005821 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005822 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005823 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005824 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005825 }
5826 return 0;
5827
5828 aborted_xfer:
5829 txn->req.msg_state = HTTP_MSG_ERROR;
5830 if (txn->status) {
5831 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005832 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005833 } else {
5834 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005835 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005836 }
5837 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005838 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005839
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005840 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005841 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005842 if (objt_server(s->target))
5843 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005844
Willy Tarreaue7dff022015-04-03 01:14:29 +02005845 if (!(s->flags & SF_ERR_MASK))
5846 s->flags |= SF_ERR_SRVCL;
5847 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005848 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005849 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005850 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005851 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005852 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005853 return 0;
5854}
5855
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005856/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5857 * processing can continue on next analysers, or zero if it either needs more
5858 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005859 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005860 * when it has nothing left to do, and may remove any analyser when it wants to
5861 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005862 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005863int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005864{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005865 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005866 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005867 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005868 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005869 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005870 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005871 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005872
Willy Tarreau87b09662015-04-03 00:22:06 +02005873 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005874 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005875 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005876 rep,
5877 rep->rex, rep->wex,
5878 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005879 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005880 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005881
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005882 /*
5883 * Now parse the partial (or complete) lines.
5884 * We will check the response syntax, and also join multi-line
5885 * headers. An index of all the lines will be elaborated while
5886 * parsing.
5887 *
5888 * For the parsing, we use a 28 states FSM.
5889 *
5890 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005891 * rep->buf->p = beginning of response
5892 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5893 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005894 * msg->eol = end of current header or line (LF or CRLF)
5895 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005896 */
5897
Willy Tarreau628c40c2014-04-24 19:11:26 +02005898 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005899 /* There's a protected area at the end of the buffer for rewriting
5900 * purposes. We don't want to start to parse the request if the
5901 * protected area is affected, because we may have to move processed
5902 * data later, which is much more complicated.
5903 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005904 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005905 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005906 /* some data has still not left the buffer, wake us once that's done */
5907 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5908 goto abort_response;
5909 channel_dont_close(rep);
5910 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005911 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005912 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005913 }
5914
Willy Tarreau379357a2013-06-08 12:55:46 +02005915 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5916 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5917 buffer_slow_realign(rep->buf);
5918
Willy Tarreau9b28e032012-10-12 23:49:43 +02005919 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005920 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005921 }
5922
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005923 /* 1: we might have to print this header in debug mode */
5924 if (unlikely((global.mode & MODE_DEBUG) &&
5925 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005926 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005927 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005928
Willy Tarreau9b28e032012-10-12 23:49:43 +02005929 sol = rep->buf->p;
5930 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005931 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005932
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005933 sol += hdr_idx_first_pos(&txn->hdr_idx);
5934 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005935
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005936 while (cur_idx) {
5937 eol = sol + txn->hdr_idx.v[cur_idx].len;
5938 debug_hdr("srvhdr", s, sol, eol);
5939 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5940 cur_idx = txn->hdr_idx.v[cur_idx].next;
5941 }
5942 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005943
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005944 /*
5945 * Now we quickly check if we have found a full valid response.
5946 * If not so, we check the FD and buffer states before leaving.
5947 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005948 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005949 * responses are checked first.
5950 *
5951 * Depending on whether the client is still there or not, we
5952 * may send an error response back or not. Note that normally
5953 * we should only check for HTTP status there, and check I/O
5954 * errors somewhere else.
5955 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005956
Willy Tarreau655dce92009-11-08 13:10:58 +01005957 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005958 /* Invalid response */
5959 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5960 /* we detected a parsing error. We want to archive this response
5961 * in the dedicated proxy area for later troubleshooting.
5962 */
5963 hdr_response_bad:
5964 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005965 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005966
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005967 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005968 if (objt_server(s->target)) {
5969 objt_server(s->target)->counters.failed_resp++;
5970 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005971 }
Willy Tarreau64648412010-03-05 10:41:54 +01005972 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005973 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005974 rep->analysers = 0;
5975 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005976 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005977 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005978 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005979
Willy Tarreaue7dff022015-04-03 01:14:29 +02005980 if (!(s->flags & SF_ERR_MASK))
5981 s->flags |= SF_ERR_PRXCOND;
5982 if (!(s->flags & SF_FINST_MASK))
5983 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005984
5985 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005986 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005987
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005988 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005989 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005990 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005991 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005992 goto hdr_response_bad;
5993 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005994
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005995 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005996 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005997 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005998 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005999 else if (txn->flags & TX_NOT_FIRST)
6000 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006001
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006002 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006003 if (objt_server(s->target)) {
6004 objt_server(s->target)->counters.failed_resp++;
6005 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006006 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006007
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006008 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006009 rep->analysers = 0;
6010 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006011 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006012 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006013 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006014
Willy Tarreaue7dff022015-04-03 01:14:29 +02006015 if (!(s->flags & SF_ERR_MASK))
6016 s->flags |= SF_ERR_SRVCL;
6017 if (!(s->flags & SF_FINST_MASK))
6018 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006019 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006020 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006021
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006022 /* read timeout : return a 504 to the client. */
6023 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006024 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006025 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006026 else if (txn->flags & TX_NOT_FIRST)
6027 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006028
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006029 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006030 if (objt_server(s->target)) {
6031 objt_server(s->target)->counters.failed_resp++;
6032 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006033 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006034
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006035 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006036 rep->analysers = 0;
6037 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006038 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006039 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006040 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006041
Willy Tarreaue7dff022015-04-03 01:14:29 +02006042 if (!(s->flags & SF_ERR_MASK))
6043 s->flags |= SF_ERR_SRVTO;
6044 if (!(s->flags & SF_FINST_MASK))
6045 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006046 return 0;
6047 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006048
Willy Tarreauf003d372012-11-26 13:35:37 +01006049 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006050 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006051 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006052 s->be->be_counters.cli_aborts++;
6053 if (objt_server(s->target))
6054 objt_server(s->target)->counters.cli_aborts++;
6055
6056 rep->analysers = 0;
6057 channel_auto_close(rep);
6058
6059 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006060 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006061 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006062
Willy Tarreaue7dff022015-04-03 01:14:29 +02006063 if (!(s->flags & SF_ERR_MASK))
6064 s->flags |= SF_ERR_CLICL;
6065 if (!(s->flags & SF_FINST_MASK))
6066 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006067
Willy Tarreau87b09662015-04-03 00:22:06 +02006068 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006069 return 0;
6070 }
6071
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006072 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006073 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006074 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006075 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006076 else if (txn->flags & TX_NOT_FIRST)
6077 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006078
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006079 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006080 if (objt_server(s->target)) {
6081 objt_server(s->target)->counters.failed_resp++;
6082 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006083 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006084
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006085 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006086 rep->analysers = 0;
6087 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006088 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006089 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006090 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006091
Willy Tarreaue7dff022015-04-03 01:14:29 +02006092 if (!(s->flags & SF_ERR_MASK))
6093 s->flags |= SF_ERR_SRVCL;
6094 if (!(s->flags & SF_FINST_MASK))
6095 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006096 return 0;
6097 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006098
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006099 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006100 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006101 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006102 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006103 else if (txn->flags & TX_NOT_FIRST)
6104 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006105
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006106 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006107 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006108 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006109
Willy Tarreaue7dff022015-04-03 01:14:29 +02006110 if (!(s->flags & SF_ERR_MASK))
6111 s->flags |= SF_ERR_CLICL;
6112 if (!(s->flags & SF_FINST_MASK))
6113 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006114
Willy Tarreau87b09662015-04-03 00:22:06 +02006115 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006116 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006117 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006118
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006119 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006120 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006121 return 0;
6122 }
6123
6124 /* More interesting part now : we know that we have a complete
6125 * response which at least looks like HTTP. We have an indicator
6126 * of each header's length, so we can parse them quickly.
6127 */
6128
6129 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006130 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006131
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006132 /*
6133 * 1: get the status code
6134 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006135 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006136 if (n < 1 || n > 5)
6137 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006138 /* when the client triggers a 4xx from the server, it's most often due
6139 * to a missing object or permission. These events should be tracked
6140 * because if they happen often, it may indicate a brute force or a
6141 * vulnerability scan.
6142 */
6143 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006144 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006145
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006146 if (objt_server(s->target))
6147 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006148
Willy Tarreau91852eb2015-05-01 13:26:00 +02006149 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6150 * exactly one digit "." one digit. This check may be disabled using
6151 * option accept-invalid-http-response.
6152 */
6153 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6154 if (msg->sl.st.v_l != 8) {
6155 msg->err_pos = 0;
6156 goto hdr_response_bad;
6157 }
6158
6159 if (rep->buf->p[4] != '/' ||
6160 !isdigit((unsigned char)rep->buf->p[5]) ||
6161 rep->buf->p[6] != '.' ||
6162 !isdigit((unsigned char)rep->buf->p[7])) {
6163 msg->err_pos = 4;
6164 goto hdr_response_bad;
6165 }
6166 }
6167
Willy Tarreau5b154472009-12-21 20:11:07 +01006168 /* check if the response is HTTP/1.1 or above */
6169 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006170 ((rep->buf->p[5] > '1') ||
6171 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006172 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006173
6174 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006175 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01006176
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006177 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006178 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006179
Willy Tarreau9b28e032012-10-12 23:49:43 +02006180 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006181
Willy Tarreau39650402010-03-15 19:44:39 +01006182 /* Adjust server's health based on status code. Note: status codes 501
6183 * and 505 are triggered on demand by client request, so we must not
6184 * count them as server failures.
6185 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006186 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006187 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006188 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006189 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006190 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006191 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006192
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006193 /*
6194 * 2: check for cacheability.
6195 */
6196
6197 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006198 case 100:
6199 /*
6200 * We may be facing a 100-continue response, in which case this
6201 * is not the right response, and we're waiting for the next one.
6202 * Let's allow this response to go to the client and wait for the
6203 * next one.
6204 */
6205 hdr_idx_init(&txn->hdr_idx);
6206 msg->next -= channel_forward(rep, msg->next);
6207 msg->msg_state = HTTP_MSG_RPBEFORE;
6208 txn->status = 0;
6209 s->logs.t_data = -1; /* was not a response yet */
6210 goto next_one;
6211
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006212 case 200:
6213 case 203:
6214 case 206:
6215 case 300:
6216 case 301:
6217 case 410:
6218 /* RFC2616 @13.4:
6219 * "A response received with a status code of
6220 * 200, 203, 206, 300, 301 or 410 MAY be stored
6221 * by a cache (...) unless a cache-control
6222 * directive prohibits caching."
6223 *
6224 * RFC2616 @9.5: POST method :
6225 * "Responses to this method are not cacheable,
6226 * unless the response includes appropriate
6227 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006228 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006229 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006230 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006231 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6232 break;
6233 default:
6234 break;
6235 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006236
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006237 /*
6238 * 3: we may need to capture headers
6239 */
6240 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006241 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006242 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006243 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006244
Willy Tarreau557f1992015-05-01 10:05:17 +02006245 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6246 * by RFC7230#3.3.3 :
6247 *
6248 * The length of a message body is determined by one of the following
6249 * (in order of precedence):
6250 *
6251 * 1. Any response to a HEAD request and any response with a 1xx
6252 * (Informational), 204 (No Content), or 304 (Not Modified) status
6253 * code is always terminated by the first empty line after the
6254 * header fields, regardless of the header fields present in the
6255 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006256 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006257 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6258 * the connection will become a tunnel immediately after the empty
6259 * line that concludes the header fields. A client MUST ignore any
6260 * Content-Length or Transfer-Encoding header fields received in
6261 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006262 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006263 * 3. If a Transfer-Encoding header field is present and the chunked
6264 * transfer coding (Section 4.1) is the final encoding, the message
6265 * body length is determined by reading and decoding the chunked
6266 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006267 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006268 * If a Transfer-Encoding header field is present in a response and
6269 * the chunked transfer coding is not the final encoding, the
6270 * message body length is determined by reading the connection until
6271 * it is closed by the server. If a Transfer-Encoding header field
6272 * is present in a request and the chunked transfer coding is not
6273 * the final encoding, the message body length cannot be determined
6274 * reliably; the server MUST respond with the 400 (Bad Request)
6275 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006276 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006277 * If a message is received with both a Transfer-Encoding and a
6278 * Content-Length header field, the Transfer-Encoding overrides the
6279 * Content-Length. Such a message might indicate an attempt to
6280 * perform request smuggling (Section 9.5) or response splitting
6281 * (Section 9.4) and ought to be handled as an error. A sender MUST
6282 * remove the received Content-Length field prior to forwarding such
6283 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006284 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006285 * 4. If a message is received without Transfer-Encoding and with
6286 * either multiple Content-Length header fields having differing
6287 * field-values or a single Content-Length header field having an
6288 * invalid value, then the message framing is invalid and the
6289 * recipient MUST treat it as an unrecoverable error. If this is a
6290 * request message, the server MUST respond with a 400 (Bad Request)
6291 * status code and then close the connection. If this is a response
6292 * message received by a proxy, the proxy MUST close the connection
6293 * to the server, discard the received response, and send a 502 (Bad
6294 * Gateway) response to the client. If this is a response message
6295 * received by a user agent, the user agent MUST close the
6296 * connection to the server and discard the received response.
6297 *
6298 * 5. If a valid Content-Length header field is present without
6299 * Transfer-Encoding, its decimal value defines the expected message
6300 * body length in octets. If the sender closes the connection or
6301 * the recipient times out before the indicated number of octets are
6302 * received, the recipient MUST consider the message to be
6303 * incomplete and close the connection.
6304 *
6305 * 6. If this is a request message and none of the above are true, then
6306 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006307 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006308 * 7. Otherwise, this is a response message without a declared message
6309 * body length, so the message body length is determined by the
6310 * number of octets received prior to the server closing the
6311 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006312 */
6313
6314 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006315 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006316 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006317 * FIXME: should we parse anyway and return an error on chunked encoding ?
6318 */
6319 if (txn->meth == HTTP_METH_HEAD ||
6320 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006321 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006322 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006323 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006324 goto skip_content_length;
6325 }
6326
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006327 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006328 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006329 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006330 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006331 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6332 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006333 /* bad transfer-encoding (chunked followed by something else) */
6334 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006335 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006336 break;
6337 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006338 }
6339
Willy Tarreau1c913912015-04-30 10:57:51 +02006340 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006341 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006342 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006343 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6344 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6345 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006346 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006347 signed long long cl;
6348
Willy Tarreauad14f752011-09-02 20:33:27 +02006349 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006350 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006351 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006352 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006353
Willy Tarreauad14f752011-09-02 20:33:27 +02006354 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006355 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006356 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006357 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006358
Willy Tarreauad14f752011-09-02 20:33:27 +02006359 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006360 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006361 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006362 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006363
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006364 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006365 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006366 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006367 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006368
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006369 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006370 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006371 }
6372
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006373 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006374 select_compression_response_header(s, rep->buf);
6375
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006376skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006377 /* Now we have to check if we need to modify the Connection header.
6378 * This is more difficult on the response than it is on the request,
6379 * because we can have two different HTTP versions and we don't know
6380 * how the client will interprete a response. For instance, let's say
6381 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6382 * HTTP/1.1 response without any header. Maybe it will bound itself to
6383 * HTTP/1.0 because it only knows about it, and will consider the lack
6384 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6385 * the lack of header as a keep-alive. Thus we will use two flags
6386 * indicating how a request MAY be understood by the client. In case
6387 * of multiple possibilities, we'll fix the header to be explicit. If
6388 * ambiguous cases such as both close and keepalive are seen, then we
6389 * will fall back to explicit close. Note that we won't take risks with
6390 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006391 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006392 */
6393
Willy Tarreaudc008c52010-02-01 16:20:08 +01006394 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6395 txn->status == 101)) {
6396 /* Either we've established an explicit tunnel, or we're
6397 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006398 * to understand the next protocols. We have to switch to tunnel
6399 * mode, so that we transfer the request and responses then let
6400 * this protocol pass unmodified. When we later implement specific
6401 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006402 * header which contains information about that protocol for
6403 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006404 */
6405 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6406 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006407 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6408 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006409 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006410 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006411 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006412
Willy Tarreau70dffda2014-01-30 03:07:23 +01006413 /* this situation happens when combining pretend-keepalive with httpclose. */
6414 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006415 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006416 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006417 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6418
Willy Tarreau60466522010-01-18 19:08:45 +01006419 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006420 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006421 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6422 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006423
Willy Tarreau60466522010-01-18 19:08:45 +01006424 /* now adjust header transformations depending on current state */
6425 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6426 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6427 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006428 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006429 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006430 }
Willy Tarreau60466522010-01-18 19:08:45 +01006431 else { /* SCL / KAL */
6432 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006433 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006434 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006435 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006436
Willy Tarreau60466522010-01-18 19:08:45 +01006437 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006438 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006439
Willy Tarreau60466522010-01-18 19:08:45 +01006440 /* Some keep-alive responses are converted to Server-close if
6441 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006442 */
Willy Tarreau60466522010-01-18 19:08:45 +01006443 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6444 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006445 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006446 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006447 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006448 }
6449
Willy Tarreau7959a552013-09-23 16:44:27 +02006450 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006451 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006452
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006453 /* end of job, return OK */
6454 rep->analysers &= ~an_bit;
6455 rep->analyse_exp = TICK_ETERNITY;
6456 channel_auto_close(rep);
6457 return 1;
6458
6459 abort_keep_alive:
6460 /* A keep-alive request to the server failed on a network error.
6461 * The client is required to retry. We need to close without returning
6462 * any other information so that the client retries.
6463 */
6464 txn->status = 0;
6465 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006466 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006467 channel_auto_close(rep);
6468 s->logs.logwait = 0;
6469 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006470 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006471 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006472 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006473 return 0;
6474}
6475
6476/* This function performs all the processing enabled for the current response.
6477 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006478 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006479 * other functions. It works like process_request (see indications above).
6480 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006481int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006482{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006483 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006484 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006485 struct http_msg *msg = &txn->rsp;
6486 struct proxy *cur_proxy;
6487 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006488 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006489
Willy Tarreau87b09662015-04-03 00:22:06 +02006490 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006491 now_ms, __FUNCTION__,
6492 s,
6493 rep,
6494 rep->rex, rep->wex,
6495 rep->flags,
6496 rep->buf->i,
6497 rep->analysers);
6498
6499 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6500 return 0;
6501
Willy Tarreau70730dd2014-04-24 18:06:27 +02006502 /* The stats applet needs to adjust the Connection header but we don't
6503 * apply any filter there.
6504 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006505 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6506 rep->analysers &= ~an_bit;
6507 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006508 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006509 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006510
Willy Tarreau58975672014-04-24 21:13:57 +02006511 /*
6512 * We will have to evaluate the filters.
6513 * As opposed to version 1.2, now they will be evaluated in the
6514 * filters order and not in the header order. This means that
6515 * each filter has to be validated among all headers.
6516 *
6517 * Filters are tried with ->be first, then with ->fe if it is
6518 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006519 *
6520 * Maybe we are in resume condiion. In this case I choose the
6521 * "struct proxy" which contains the rule list matching the resume
6522 * pointer. If none of theses "struct proxy" match, I initialise
6523 * the process with the first one.
6524 *
6525 * In fact, I check only correspondance betwwen the current list
6526 * pointer and the ->fe rule list. If it doesn't match, I initialize
6527 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006528 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006529 if (s->current_rule_list == &sess->fe->http_res_rules)
6530 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006531 else
6532 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006533 while (1) {
6534 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006535
Willy Tarreau58975672014-04-24 21:13:57 +02006536 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006537 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006538 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006539
Willy Tarreau51d861a2015-05-22 17:30:48 +02006540 if (ret == HTTP_RULE_RES_BADREQ)
6541 goto return_srv_prx_502;
6542
6543 if (ret == HTTP_RULE_RES_DONE) {
6544 rep->analysers &= ~an_bit;
6545 rep->analyse_exp = TICK_ETERNITY;
6546 return 1;
6547 }
6548 }
6549
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006550 /* we need to be called again. */
6551 if (ret == HTTP_RULE_RES_YIELD) {
6552 channel_dont_close(rep);
6553 return 0;
6554 }
6555
Willy Tarreau58975672014-04-24 21:13:57 +02006556 /* try headers filters */
6557 if (rule_set->rsp_exp != NULL) {
6558 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6559 return_bad_resp:
6560 if (objt_server(s->target)) {
6561 objt_server(s->target)->counters.failed_resp++;
6562 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006563 }
Willy Tarreau58975672014-04-24 21:13:57 +02006564 s->be->be_counters.failed_resp++;
6565 return_srv_prx_502:
6566 rep->analysers = 0;
6567 txn->status = 502;
6568 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006569 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006570 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006571 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006572 if (!(s->flags & SF_ERR_MASK))
6573 s->flags |= SF_ERR_PRXCOND;
6574 if (!(s->flags & SF_FINST_MASK))
6575 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006576 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006577 }
Willy Tarreau58975672014-04-24 21:13:57 +02006578 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006579
Willy Tarreau58975672014-04-24 21:13:57 +02006580 /* has the response been denied ? */
6581 if (txn->flags & TX_SVDENY) {
6582 if (objt_server(s->target))
6583 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006584
Willy Tarreau58975672014-04-24 21:13:57 +02006585 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006586 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006587 if (sess->listener->counters)
6588 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006589
Willy Tarreau58975672014-04-24 21:13:57 +02006590 goto return_srv_prx_502;
6591 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006592
Willy Tarreau58975672014-04-24 21:13:57 +02006593 /* add response headers from the rule sets in the same order */
6594 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006595 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006596 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006597 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006598 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006599 ret = acl_pass(ret);
6600 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6601 ret = !ret;
6602 if (!ret)
6603 continue;
6604 }
6605 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6606 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006607 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006608
Willy Tarreau58975672014-04-24 21:13:57 +02006609 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006610 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006611 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006612 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006613 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006614
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006615 /* After this point, this anayzer can't return yield, so we can
6616 * remove the bit corresponding to this analyzer from the list.
6617 *
6618 * Note that the intermediate returns and goto found previously
6619 * reset the analyzers.
6620 */
6621 rep->analysers &= ~an_bit;
6622 rep->analyse_exp = TICK_ETERNITY;
6623
Willy Tarreau58975672014-04-24 21:13:57 +02006624 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006625 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006626 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006627
Willy Tarreau58975672014-04-24 21:13:57 +02006628 /*
6629 * Now check for a server cookie.
6630 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006631 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006632 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006633
Willy Tarreau58975672014-04-24 21:13:57 +02006634 /*
6635 * Check for cache-control or pragma headers if required.
6636 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006637 if (((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) && txn->status != 101)
Willy Tarreau58975672014-04-24 21:13:57 +02006638 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006639
Willy Tarreau58975672014-04-24 21:13:57 +02006640 /*
6641 * Add server cookie in the response if needed
6642 */
6643 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6644 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006645 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006646 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6647 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6648 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6649 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6650 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006651 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006652 /* the server is known, it's not the one the client requested, or the
6653 * cookie's last seen date needs to be refreshed. We have to
6654 * insert a set-cookie here, except if we want to insert only on POST
6655 * requests and this one isn't. Note that servers which don't have cookies
6656 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006657 */
Willy Tarreau58975672014-04-24 21:13:57 +02006658 if (!objt_server(s->target)->cookie) {
6659 chunk_printf(&trash,
6660 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6661 s->be->cookie_name);
6662 }
6663 else {
6664 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006665
Willy Tarreau58975672014-04-24 21:13:57 +02006666 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6667 /* emit last_date, which is mandatory */
6668 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6669 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6670 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006671
Willy Tarreau58975672014-04-24 21:13:57 +02006672 if (s->be->cookie_maxlife) {
6673 /* emit first_date, which is either the original one or
6674 * the current date.
6675 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006676 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006677 s30tob64(txn->cookie_first_date ?
6678 txn->cookie_first_date >> 2 :
6679 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006680 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006681 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006682 }
Willy Tarreau58975672014-04-24 21:13:57 +02006683 chunk_appendf(&trash, "; path=/");
6684 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006685
Willy Tarreau58975672014-04-24 21:13:57 +02006686 if (s->be->cookie_domain)
6687 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006688
Willy Tarreau58975672014-04-24 21:13:57 +02006689 if (s->be->ck_opts & PR_CK_HTTPONLY)
6690 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006691
Willy Tarreau58975672014-04-24 21:13:57 +02006692 if (s->be->ck_opts & PR_CK_SECURE)
6693 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006694
Willy Tarreau58975672014-04-24 21:13:57 +02006695 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6696 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006697
Willy Tarreau58975672014-04-24 21:13:57 +02006698 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006699 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006700 /* the server did not change, only the date was updated */
6701 txn->flags |= TX_SCK_UPDATED;
6702 else
6703 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006704
Willy Tarreau58975672014-04-24 21:13:57 +02006705 /* Here, we will tell an eventual cache on the client side that we don't
6706 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6707 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6708 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006709 */
Willy Tarreau58975672014-04-24 21:13:57 +02006710 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006711
Willy Tarreau58975672014-04-24 21:13:57 +02006712 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006713
Willy Tarreau58975672014-04-24 21:13:57 +02006714 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6715 "Cache-control: private", 22) < 0))
6716 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006717 }
Willy Tarreau58975672014-04-24 21:13:57 +02006718 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006719
Willy Tarreau58975672014-04-24 21:13:57 +02006720 /*
6721 * Check if result will be cacheable with a cookie.
6722 * We'll block the response if security checks have caught
6723 * nasty things such as a cacheable cookie.
6724 */
6725 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6726 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6727 (s->be->options & PR_O_CHK_CACHE)) {
6728 /* we're in presence of a cacheable response containing
6729 * a set-cookie header. We'll block it as requested by
6730 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006731 */
Willy Tarreau58975672014-04-24 21:13:57 +02006732 if (objt_server(s->target))
6733 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006734
Willy Tarreau58975672014-04-24 21:13:57 +02006735 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006736 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006737 if (sess->listener->counters)
6738 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006739
Willy Tarreau58975672014-04-24 21:13:57 +02006740 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6741 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6742 send_log(s->be, LOG_ALERT,
6743 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6744 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6745 goto return_srv_prx_502;
6746 }
Willy Tarreau03945942009-12-22 16:50:27 +01006747
Willy Tarreau70730dd2014-04-24 18:06:27 +02006748 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006749 /*
6750 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6751 * If an "Upgrade" token is found, the header is left untouched in order
6752 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006753 * if anything but "Upgrade" is present in the Connection header. We don't
6754 * want to touch any 101 response either since it's switching to another
6755 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006756 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006757 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006758 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006759 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006760 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6761 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006762
Willy Tarreau58975672014-04-24 21:13:57 +02006763 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6764 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6765 /* we want a keep-alive response here. Keep-alive header
6766 * required if either side is not 1.1.
6767 */
6768 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6769 want_flags |= TX_CON_KAL_SET;
6770 }
6771 else {
6772 /* we want a close response here. Close header required if
6773 * the server is 1.1, regardless of the client.
6774 */
6775 if (msg->flags & HTTP_MSGF_VER_11)
6776 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006777 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006778
Willy Tarreau58975672014-04-24 21:13:57 +02006779 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6780 http_change_connection_header(txn, msg, want_flags);
6781 }
6782
6783 skip_header_mangling:
6784 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6785 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6786 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006787
Willy Tarreau58975672014-04-24 21:13:57 +02006788 /* if the user wants to log as soon as possible, without counting
6789 * bytes from the server, then this is the right moment. We have
6790 * to temporarily assign bytes_out to log what we currently have.
6791 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006792 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006793 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6794 s->logs.bytes_out = txn->rsp.eoh;
6795 s->do_log(s);
6796 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006797 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006798 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006799}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006800
Willy Tarreaud98cf932009-12-27 22:54:55 +01006801/* This function is an analyser which forwards response body (including chunk
6802 * sizes if any). It is called as soon as we must forward, even if we forward
6803 * zero byte. The only situation where it must not be called is when we're in
6804 * tunnel mode and we want to forward till the close. It's used both to forward
6805 * remaining data and to resync after end of body. It expects the msg_state to
6806 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006807 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006808 *
6809 * It is capable of compressing response data both in content-length mode and
6810 * in chunked mode. The state machines follows different flows depending on
6811 * whether content-length and chunked modes are used, since there are no
6812 * trailers in content-length :
6813 *
6814 * chk-mode cl-mode
6815 * ,----- BODY -----.
6816 * / \
6817 * V size > 0 V chk-mode
6818 * .--> SIZE -------------> DATA -------------> CRLF
6819 * | | size == 0 | last byte |
6820 * | v final crlf v inspected |
6821 * | TRAILERS -----------> DONE |
6822 * | |
6823 * `----------------------------------------------'
6824 *
6825 * Compression only happens in the DATA state, and must be flushed in final
6826 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6827 * is performed at once on final states for all bytes parsed, or when leaving
6828 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006829 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006830int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006831{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006832 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006833 struct http_txn *txn = s->txn;
6834 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006835 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006836 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006837 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006838
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006839 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6840 return 0;
6841
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006842 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006843 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006844 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006845 /* Output closed while we were sending data. We must abort and
6846 * wake the other side up.
6847 */
6848 msg->msg_state = HTTP_MSG_ERROR;
6849 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006850 return 1;
6851 }
6852
Willy Tarreau4fe41902010-06-07 22:27:41 +02006853 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006854 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006855
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006856 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006857 /* we have msg->sov which points to the first byte of message
6858 * body, and res->buf.p still points to the beginning of the
6859 * message. We forward the headers now, as we don't need them
6860 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006861 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006862 b_adv(res->buf, msg->sov);
6863 msg->next -= msg->sov;
6864 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006865
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006866 /* The previous analysers guarantee that the state is somewhere
6867 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6868 * msg->next are always correct.
6869 */
6870 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6871 if (msg->flags & HTTP_MSGF_TE_CHNK)
6872 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6873 else
6874 msg->msg_state = HTTP_MSG_DATA;
6875 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006876 }
6877
Willy Tarreauefdf0942014-04-24 20:08:57 +02006878 if (res->to_forward) {
6879 /* We can't process the buffer's contents yet */
6880 res->flags |= CF_WAKE_WRITE;
6881 goto missing_data;
6882 }
6883
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006884 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6885 /* We need a compression buffer in the DATA state to put the
6886 * output of compressed data, and in CRLF state to let the
6887 * TRAILERS state finish the job of removing the trailing CRLF.
6888 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006889 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006890 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006891 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006892 goto aborted_xfer; /* no memory */
6893 }
6894
6895 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006896 if (ret < 0) {
6897 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006898 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006899 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006900 compressing = 1;
6901 }
6902
Willy Tarreaud98cf932009-12-27 22:54:55 +01006903 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006904 switch (msg->msg_state - HTTP_MSG_DATA) {
6905 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006906 /* we may have some pending data starting at res->buf->p */
6907 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006908 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006909 if (ret < 0)
6910 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006911
Willy Tarreaud5a67832014-04-21 10:54:27 +02006912 if (msg->chunk_len) {
6913 /* input empty or output full */
6914 if (res->buf->i > msg->next)
6915 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006916 goto missing_data;
6917 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006918 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006919 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006920 if (msg->chunk_len > res->buf->i - msg->next) {
6921 /* output full */
6922 res->flags |= CF_WAKE_WRITE;
6923 goto missing_data;
6924 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006925 msg->next += msg->chunk_len;
6926 msg->chunk_len = 0;
6927 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006928
6929 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006930 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006931 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006932 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006933 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006934 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006935 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006936 /* fall through for HTTP_MSG_CHUNK_CRLF */
6937
6938 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6939 /* we want the CRLF after the data */
6940
6941 ret = http_skip_chunk_crlf(msg);
6942 if (ret == 0)
6943 goto missing_data;
6944 else if (ret < 0) {
6945 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006946 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006947 goto return_bad_res;
6948 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006949 /* we're in MSG_CHUNK_SIZE now, fall through */
6950
6951 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006952 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006953 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006954 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006955 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006956
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006957 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006958 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006959 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006960 else if (ret < 0) {
6961 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006962 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006963 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006964 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006965 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006966 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006967
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006968 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006969 if (unlikely(compressing)) {
6970 /* we need to flush output contents before syncing FSMs */
6971 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6972 compressing = 0;
6973 }
6974
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006975 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006976 if (ret == 0)
6977 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006978 else if (ret < 0) {
6979 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006980 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006981 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006982 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006983 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006984
6985 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006986 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006987 if (unlikely(compressing)) {
6988 /* we need to flush output contents before syncing FSMs */
6989 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6990 compressing = 0;
6991 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006992
Willy Tarreauc623c172014-04-18 09:53:50 +02006993 /* we may have some pending data starting at res->buf->p
6994 * such as a last chunk of data or trailers.
6995 */
6996 b_adv(res->buf, msg->next);
6997 msg->next = 0;
6998
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006999 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02007000 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007001 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7002 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007003 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02007004
Willy Tarreau610ecce2010-01-04 21:15:02 +01007005 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01007006 /* some state changes occurred, maybe the analyser
7007 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01007008 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007009 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007010 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007011 /* response errors are most likely due to
7012 * the client aborting the transfer.
7013 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007014 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007015 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007016 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007017 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01007018 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007019 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007020 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01007021 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007022 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007023 }
7024 }
7025
Willy Tarreaud98cf932009-12-27 22:54:55 +01007026 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02007027 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007028 if (unlikely(compressing)) {
7029 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02007030 compressing = 0;
7031 }
Willy Tarreauf003d372012-11-26 13:35:37 +01007032
Willy Tarreauc623c172014-04-18 09:53:50 +02007033 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
7034 b_adv(res->buf, msg->next);
7035 msg->next = 0;
7036 msg->chunk_len -= channel_forward(res, msg->chunk_len);
7037 }
7038
Willy Tarreauf003d372012-11-26 13:35:37 +01007039 if (res->flags & CF_SHUTW)
7040 goto aborted_xfer;
7041
7042 /* stop waiting for data if the input is closed before the end. If the
7043 * client side was already closed, it means that the client has aborted,
7044 * so we don't want to count this as a server abort. Otherwise it's a
7045 * server abort.
7046 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007047 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007048 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01007049 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007050 if (!(s->flags & SF_ERR_MASK))
7051 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007052 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007053 if (objt_server(s->target))
7054 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007055 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01007056 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007057
Willy Tarreau40dba092010-03-04 18:14:51 +01007058 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007059 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007060 goto return_bad_res;
7061
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007062 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007063 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007064 * Similarly, with keep-alive on the client side, we don't want to forward a
7065 * close.
7066 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007067 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007068 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7069 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007070 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007071
Willy Tarreau5c620922011-05-11 19:56:11 +02007072 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007073 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007074 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007075 * modes are already handled by the stream sock layer. We must not do
7076 * this in content-length mode because it could present the MSG_MORE
7077 * flag with the last block of forwarded data, which would cause an
7078 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007079 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007080 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007081 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007082
Willy Tarreau87b09662015-04-03 00:22:06 +02007083 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007084 return 0;
7085
Willy Tarreau40dba092010-03-04 18:14:51 +01007086 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007087 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007088 if (objt_server(s->target))
7089 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007090
7091 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007092 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007093 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007094 compressing = 0;
7095 }
7096
Willy Tarreauc623c172014-04-18 09:53:50 +02007097 /* we may have some pending data starting at res->buf->p */
7098 if (s->comp_algo == NULL) {
7099 b_adv(res->buf, msg->next);
7100 msg->next = 0;
7101 }
7102
Willy Tarreaud98cf932009-12-27 22:54:55 +01007103 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007104 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007105 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007106 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007107 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007108 if (objt_server(s->target))
7109 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007110
Willy Tarreaue7dff022015-04-03 01:14:29 +02007111 if (!(s->flags & SF_ERR_MASK))
7112 s->flags |= SF_ERR_PRXCOND;
7113 if (!(s->flags & SF_FINST_MASK))
7114 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007115 return 0;
7116
7117 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007118 if (unlikely(compressing)) {
7119 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7120 compressing = 0;
7121 }
7122
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007123 txn->rsp.msg_state = HTTP_MSG_ERROR;
7124 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007125 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007126 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007127 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007128
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007129 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007130 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007131 if (objt_server(s->target))
7132 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007133
Willy Tarreaue7dff022015-04-03 01:14:29 +02007134 if (!(s->flags & SF_ERR_MASK))
7135 s->flags |= SF_ERR_CLICL;
7136 if (!(s->flags & SF_FINST_MASK))
7137 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007138 return 0;
7139}
7140
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007141/* Iterate the same filter through all request headers.
7142 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143 * Since it can manage the switch to another backend, it updates the per-proxy
7144 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007145 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007146int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007147{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007148 char *cur_ptr, *cur_end, *cur_next;
7149 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007150 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007151 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007152 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007153
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007154 last_hdr = 0;
7155
Willy Tarreau9b28e032012-10-12 23:49:43 +02007156 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007157 old_idx = 0;
7158
7159 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007160 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007161 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007162 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007163 (exp->action == ACT_ALLOW ||
7164 exp->action == ACT_DENY ||
7165 exp->action == ACT_TARPIT))
7166 return 0;
7167
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007168 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007169 if (!cur_idx)
7170 break;
7171
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007172 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007173 cur_ptr = cur_next;
7174 cur_end = cur_ptr + cur_hdr->len;
7175 cur_next = cur_end + cur_hdr->cr + 1;
7176
7177 /* Now we have one header between cur_ptr and cur_end,
7178 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007179 */
7180
Willy Tarreau15a53a42015-01-21 13:39:42 +01007181 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007182 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007183 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007184 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007185 last_hdr = 1;
7186 break;
7187
7188 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007189 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007190 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007191 break;
7192
7193 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007194 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007195 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007196 break;
7197
7198 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007199 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7200 if (trash.len < 0)
7201 return -1;
7202
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007203 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007204 /* FIXME: if the user adds a newline in the replacement, the
7205 * index will not be recalculated for now, and the new line
7206 * will not be counted as a new header.
7207 */
7208
7209 cur_end += delta;
7210 cur_next += delta;
7211 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007212 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007213 break;
7214
7215 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007216 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007217 cur_next += delta;
7218
Willy Tarreaufa355d42009-11-29 18:12:29 +01007219 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007220 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7221 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007222 cur_hdr->len = 0;
7223 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007224 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007225 break;
7226
7227 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007228 }
7229
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007230 /* keep the link from this header to next one in case of later
7231 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007232 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007233 old_idx = cur_idx;
7234 }
7235 return 0;
7236}
7237
7238
7239/* Apply the filter to the request line.
7240 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7241 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242 * Since it can manage the switch to another backend, it updates the per-proxy
7243 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007244 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007245int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007246{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007247 char *cur_ptr, *cur_end;
7248 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007249 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007250 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007251
Willy Tarreau3d300592007-03-18 18:34:41 +01007252 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007253 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007254 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007255 (exp->action == ACT_ALLOW ||
7256 exp->action == ACT_DENY ||
7257 exp->action == ACT_TARPIT))
7258 return 0;
7259 else if (exp->action == ACT_REMOVE)
7260 return 0;
7261
7262 done = 0;
7263
Willy Tarreau9b28e032012-10-12 23:49:43 +02007264 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007265 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007266
7267 /* Now we have the request line between cur_ptr and cur_end */
7268
Willy Tarreau15a53a42015-01-21 13:39:42 +01007269 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007270 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007272 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007273 done = 1;
7274 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007275
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007276 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007277 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007278 done = 1;
7279 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007280
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007281 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007282 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007283 done = 1;
7284 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007285
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007286 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007287 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7288 if (trash.len < 0)
7289 return -1;
7290
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007291 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007292 /* FIXME: if the user adds a newline in the replacement, the
7293 * index will not be recalculated for now, and the new line
7294 * will not be counted as a new header.
7295 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007296
Willy Tarreaufa355d42009-11-29 18:12:29 +01007297 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007298 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007299 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007300 HTTP_MSG_RQMETH,
7301 cur_ptr, cur_end + 1,
7302 NULL, NULL);
7303 if (unlikely(!cur_end))
7304 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007306 /* we have a full request and we know that we have either a CR
7307 * or an LF at <ptr>.
7308 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007309 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7310 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007311 /* there is no point trying this regex on headers */
7312 return 1;
7313 }
7314 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007315 return done;
7316}
Willy Tarreau97de6242006-12-27 17:18:38 +01007317
Willy Tarreau58f10d72006-12-04 02:26:12 +01007318
Willy Tarreau58f10d72006-12-04 02:26:12 +01007319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007320/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007321 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007322 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007323 * unparsable request. Since it can manage the switch to another backend, it
7324 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007325 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007326int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007327{
Willy Tarreau192252e2015-04-04 01:47:55 +02007328 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007329 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007330 struct hdr_exp *exp;
7331
7332 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007333 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007334
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007335 /*
7336 * The interleaving of transformations and verdicts
7337 * makes it difficult to decide to continue or stop
7338 * the evaluation.
7339 */
7340
Willy Tarreau6c123b12010-01-28 20:22:06 +01007341 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7342 break;
7343
Willy Tarreau3d300592007-03-18 18:34:41 +01007344 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007345 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007346 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007347 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007348
7349 /* if this filter had a condition, evaluate it now and skip to
7350 * next filter if the condition does not match.
7351 */
7352 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007353 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007354 ret = acl_pass(ret);
7355 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7356 ret = !ret;
7357
7358 if (!ret)
7359 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007360 }
7361
7362 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007363 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007364 if (unlikely(ret < 0))
7365 return -1;
7366
7367 if (likely(ret == 0)) {
7368 /* The filter did not match the request, it can be
7369 * iterated through all headers.
7370 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007371 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7372 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007373 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007374 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007375 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007376}
7377
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007378
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007379/* Find the end of a cookie value contained between <s> and <e>. It works the
7380 * same way as with headers above except that the semi-colon also ends a token.
7381 * See RFC2965 for more information. Note that it requires a valid header to
7382 * return a valid result.
7383 */
7384char *find_cookie_value_end(char *s, const char *e)
7385{
7386 int quoted, qdpair;
7387
7388 quoted = qdpair = 0;
7389 for (; s < e; s++) {
7390 if (qdpair) qdpair = 0;
7391 else if (quoted) {
7392 if (*s == '\\') qdpair = 1;
7393 else if (*s == '"') quoted = 0;
7394 }
7395 else if (*s == '"') quoted = 1;
7396 else if (*s == ',' || *s == ';') return s;
7397 }
7398 return s;
7399}
7400
7401/* Delete a value in a header between delimiters <from> and <next> in buffer
7402 * <buf>. The number of characters displaced is returned, and the pointer to
7403 * the first delimiter is updated if required. The function tries as much as
7404 * possible to respect the following principles :
7405 * - replace <from> delimiter by the <next> one unless <from> points to a
7406 * colon, in which case <next> is simply removed
7407 * - set exactly one space character after the new first delimiter, unless
7408 * there are not enough characters in the block being moved to do so.
7409 * - remove unneeded spaces before the previous delimiter and after the new
7410 * one.
7411 *
7412 * It is the caller's responsibility to ensure that :
7413 * - <from> points to a valid delimiter or the colon ;
7414 * - <next> points to a valid delimiter or the final CR/LF ;
7415 * - there are non-space chars before <from> ;
7416 * - there is a CR/LF at or after <next>.
7417 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007418int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007419{
7420 char *prev = *from;
7421
7422 if (*prev == ':') {
7423 /* We're removing the first value, preserve the colon and add a
7424 * space if possible.
7425 */
7426 if (!http_is_crlf[(unsigned char)*next])
7427 next++;
7428 prev++;
7429 if (prev < next)
7430 *prev++ = ' ';
7431
7432 while (http_is_spht[(unsigned char)*next])
7433 next++;
7434 } else {
7435 /* Remove useless spaces before the old delimiter. */
7436 while (http_is_spht[(unsigned char)*(prev-1)])
7437 prev--;
7438 *from = prev;
7439
7440 /* copy the delimiter and if possible a space if we're
7441 * not at the end of the line.
7442 */
7443 if (!http_is_crlf[(unsigned char)*next]) {
7444 *prev++ = *next++;
7445 if (prev + 1 < next)
7446 *prev++ = ' ';
7447 while (http_is_spht[(unsigned char)*next])
7448 next++;
7449 }
7450 }
7451 return buffer_replace2(buf, prev, next, NULL, 0);
7452}
7453
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007454/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007455 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007456 * desirable to call it only when needed. This code is quite complex because
7457 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7458 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007459 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007460void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007461{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007462 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007463 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007464 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007465 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007466 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7467 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007468
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007469 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007470 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007471 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007472
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007473 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007474 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007475 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007476
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007477 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007478 hdr_beg = hdr_next;
7479 hdr_end = hdr_beg + cur_hdr->len;
7480 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007481
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007482 /* We have one full header between hdr_beg and hdr_end, and the
7483 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007484 * "Cookie:" headers.
7485 */
7486
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007487 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007488 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007489 old_idx = cur_idx;
7490 continue;
7491 }
7492
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007493 del_from = NULL; /* nothing to be deleted */
7494 preserve_hdr = 0; /* assume we may kill the whole header */
7495
Willy Tarreau58f10d72006-12-04 02:26:12 +01007496 /* Now look for cookies. Conforming to RFC2109, we have to support
7497 * attributes whose name begin with a '$', and associate them with
7498 * the right cookie, if we want to delete this cookie.
7499 * So there are 3 cases for each cookie read :
7500 * 1) it's a special attribute, beginning with a '$' : ignore it.
7501 * 2) it's a server id cookie that we *MAY* want to delete : save
7502 * some pointers on it (last semi-colon, beginning of cookie...)
7503 * 3) it's an application cookie : we *MAY* have to delete a previous
7504 * "special" cookie.
7505 * At the end of loop, if a "special" cookie remains, we may have to
7506 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007507 * *MUST* delete it.
7508 *
7509 * Note: RFC2965 is unclear about the processing of spaces around
7510 * the equal sign in the ATTR=VALUE form. A careful inspection of
7511 * the RFC explicitly allows spaces before it, and not within the
7512 * tokens (attrs or values). An inspection of RFC2109 allows that
7513 * too but section 10.1.3 lets one think that spaces may be allowed
7514 * after the equal sign too, resulting in some (rare) buggy
7515 * implementations trying to do that. So let's do what servers do.
7516 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7517 * allowed quoted strings in values, with any possible character
7518 * after a backslash, including control chars and delimitors, which
7519 * causes parsing to become ambiguous. Browsers also allow spaces
7520 * within values even without quotes.
7521 *
7522 * We have to keep multiple pointers in order to support cookie
7523 * removal at the beginning, middle or end of header without
7524 * corrupting the header. All of these headers are valid :
7525 *
7526 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7527 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7528 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7529 * | | | | | | | | |
7530 * | | | | | | | | hdr_end <--+
7531 * | | | | | | | +--> next
7532 * | | | | | | +----> val_end
7533 * | | | | | +-----------> val_beg
7534 * | | | | +--------------> equal
7535 * | | | +----------------> att_end
7536 * | | +---------------------> att_beg
7537 * | +--------------------------> prev
7538 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007539 */
7540
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007541 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7542 /* Iterate through all cookies on this line */
7543
7544 /* find att_beg */
7545 att_beg = prev + 1;
7546 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7547 att_beg++;
7548
7549 /* find att_end : this is the first character after the last non
7550 * space before the equal. It may be equal to hdr_end.
7551 */
7552 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007553
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007554 while (equal < hdr_end) {
7555 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007556 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007557 if (http_is_spht[(unsigned char)*equal++])
7558 continue;
7559 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007560 }
7561
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007562 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7563 * is between <att_beg> and <equal>, both may be identical.
7564 */
7565
7566 /* look for end of cookie if there is an equal sign */
7567 if (equal < hdr_end && *equal == '=') {
7568 /* look for the beginning of the value */
7569 val_beg = equal + 1;
7570 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7571 val_beg++;
7572
7573 /* find the end of the value, respecting quotes */
7574 next = find_cookie_value_end(val_beg, hdr_end);
7575
7576 /* make val_end point to the first white space or delimitor after the value */
7577 val_end = next;
7578 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7579 val_end--;
7580 } else {
7581 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007582 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007583
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007584 /* We have nothing to do with attributes beginning with '$'. However,
7585 * they will automatically be removed if a header before them is removed,
7586 * since they're supposed to be linked together.
7587 */
7588 if (*att_beg == '$')
7589 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007590
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007591 /* Ignore cookies with no equal sign */
7592 if (equal == next) {
7593 /* This is not our cookie, so we must preserve it. But if we already
7594 * scheduled another cookie for removal, we cannot remove the
7595 * complete header, but we can remove the previous block itself.
7596 */
7597 preserve_hdr = 1;
7598 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007599 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007600 val_end += delta;
7601 next += delta;
7602 hdr_end += delta;
7603 hdr_next += delta;
7604 cur_hdr->len += delta;
7605 http_msg_move_end(&txn->req, delta);
7606 prev = del_from;
7607 del_from = NULL;
7608 }
7609 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007610 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007611
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007612 /* if there are spaces around the equal sign, we need to
7613 * strip them otherwise we'll get trouble for cookie captures,
7614 * or even for rewrites. Since this happens extremely rarely,
7615 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007616 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007617 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7618 int stripped_before = 0;
7619 int stripped_after = 0;
7620
7621 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007622 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007623 equal += stripped_before;
7624 val_beg += stripped_before;
7625 }
7626
7627 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007628 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007629 val_beg += stripped_after;
7630 stripped_before += stripped_after;
7631 }
7632
7633 val_end += stripped_before;
7634 next += stripped_before;
7635 hdr_end += stripped_before;
7636 hdr_next += stripped_before;
7637 cur_hdr->len += stripped_before;
7638 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007639 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007640 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007641
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007642 /* First, let's see if we want to capture this cookie. We check
7643 * that we don't already have a client side cookie, because we
7644 * can only capture one. Also as an optimisation, we ignore
7645 * cookies shorter than the declared name.
7646 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007647 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7648 (val_end - att_beg >= sess->fe->capture_namelen) &&
7649 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007650 int log_len = val_end - att_beg;
7651
7652 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7653 Alert("HTTP logging : out of memory.\n");
7654 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007655 if (log_len > sess->fe->capture_len)
7656 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007657 memcpy(txn->cli_cookie, att_beg, log_len);
7658 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007659 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007660 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007661
Willy Tarreaubca99692010-10-06 19:25:55 +02007662 /* Persistence cookies in passive, rewrite or insert mode have the
7663 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007664 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007665 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007666 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007667 * For cookies in prefix mode, the form is :
7668 *
7669 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007670 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007671 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7672 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7673 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007674 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007675
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007676 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7677 * have the server ID between val_beg and delim, and the original cookie between
7678 * delim+1 and val_end. Otherwise, delim==val_end :
7679 *
7680 * Cookie: NAME=SRV; # in all but prefix modes
7681 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7682 * | || || | |+-> next
7683 * | || || | +--> val_end
7684 * | || || +---------> delim
7685 * | || |+------------> val_beg
7686 * | || +-------------> att_end = equal
7687 * | |+-----------------> att_beg
7688 * | +------------------> prev
7689 * +-------------------------> hdr_beg
7690 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007691
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007692 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007693 for (delim = val_beg; delim < val_end; delim++)
7694 if (*delim == COOKIE_DELIM)
7695 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007696 } else {
7697 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007698 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007699 /* Now check if the cookie contains a date field, which would
7700 * appear after a vertical bar ('|') just after the server name
7701 * and before the delimiter.
7702 */
7703 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7704 if (vbar1) {
7705 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007706 * right is the last seen date. It is a base64 encoded
7707 * 30-bit value representing the UNIX date since the
7708 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007709 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007710 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007711 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007712 if (val_end - vbar1 >= 5) {
7713 val = b64tos30(vbar1);
7714 if (val > 0)
7715 txn->cookie_last_date = val << 2;
7716 }
7717 /* look for a second vertical bar */
7718 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7719 if (vbar1 && (val_end - vbar1 > 5)) {
7720 val = b64tos30(vbar1 + 1);
7721 if (val > 0)
7722 txn->cookie_first_date = val << 2;
7723 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007724 }
7725 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007726
Willy Tarreauf64d1412010-10-07 20:06:11 +02007727 /* if the cookie has an expiration date and the proxy wants to check
7728 * it, then we do that now. We first check if the cookie is too old,
7729 * then only if it has expired. We detect strict overflow because the
7730 * time resolution here is not great (4 seconds). Cookies with dates
7731 * in the future are ignored if their offset is beyond one day. This
7732 * allows an admin to fix timezone issues without expiring everyone
7733 * and at the same time avoids keeping unwanted side effects for too
7734 * long.
7735 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007736 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7737 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007738 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007739 txn->flags &= ~TX_CK_MASK;
7740 txn->flags |= TX_CK_OLD;
7741 delim = val_beg; // let's pretend we have not found the cookie
7742 txn->cookie_first_date = 0;
7743 txn->cookie_last_date = 0;
7744 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007745 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7746 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007747 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007748 txn->flags &= ~TX_CK_MASK;
7749 txn->flags |= TX_CK_EXPIRED;
7750 delim = val_beg; // let's pretend we have not found the cookie
7751 txn->cookie_first_date = 0;
7752 txn->cookie_last_date = 0;
7753 }
7754
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007755 /* Here, we'll look for the first running server which supports the cookie.
7756 * This allows to share a same cookie between several servers, for example
7757 * to dedicate backup servers to specific servers only.
7758 * However, to prevent clients from sticking to cookie-less backup server
7759 * when they have incidentely learned an empty cookie, we simply ignore
7760 * empty cookies and mark them as invalid.
7761 * The same behaviour is applied when persistence must be ignored.
7762 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007763 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007764 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007765
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007766 while (srv) {
7767 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7768 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007769 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007770 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007771 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007772 /* we found the server and we can use it */
7773 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007774 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007775 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007776 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007777 break;
7778 } else {
7779 /* we found a server, but it's down,
7780 * mark it as such and go on in case
7781 * another one is available.
7782 */
7783 txn->flags &= ~TX_CK_MASK;
7784 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007785 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007786 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007787 srv = srv->next;
7788 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007789
Willy Tarreauf64d1412010-10-07 20:06:11 +02007790 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007791 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007792 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007793 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007794 txn->flags |= TX_CK_UNUSED;
7795 else
7796 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007797 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007798
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007799 /* depending on the cookie mode, we may have to either :
7800 * - delete the complete cookie if we're in insert+indirect mode, so that
7801 * the server never sees it ;
7802 * - remove the server id from the cookie value, and tag the cookie as an
7803 * application cookie so that it does not get accidentely removed later,
7804 * if we're in cookie prefix mode
7805 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007806 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007807 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007808
Willy Tarreau9b28e032012-10-12 23:49:43 +02007809 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007810 val_end += delta;
7811 next += delta;
7812 hdr_end += delta;
7813 hdr_next += delta;
7814 cur_hdr->len += delta;
7815 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007816
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007817 del_from = NULL;
7818 preserve_hdr = 1; /* we want to keep this cookie */
7819 }
7820 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007821 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007822 del_from = prev;
7823 }
7824 } else {
7825 /* This is not our cookie, so we must preserve it. But if we already
7826 * scheduled another cookie for removal, we cannot remove the
7827 * complete header, but we can remove the previous block itself.
7828 */
7829 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007830
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007831 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007832 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007833 if (att_beg >= del_from)
7834 att_beg += delta;
7835 if (att_end >= del_from)
7836 att_end += delta;
7837 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007838 val_end += delta;
7839 next += delta;
7840 hdr_end += delta;
7841 hdr_next += delta;
7842 cur_hdr->len += delta;
7843 http_msg_move_end(&txn->req, delta);
7844 prev = del_from;
7845 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007846 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007847 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007848
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007849 /* continue with next cookie on this header line */
7850 att_beg = next;
7851 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007852
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007853 /* There are no more cookies on this line.
7854 * We may still have one (or several) marked for deletion at the
7855 * end of the line. We must do this now in two ways :
7856 * - if some cookies must be preserved, we only delete from the
7857 * mark to the end of line ;
7858 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007859 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007860 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007861 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007862 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007863 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007864 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007865 cur_hdr->len += delta;
7866 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007867 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007868
7869 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007870 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7871 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007872 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007873 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007874 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007875 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007876 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007877 }
7878
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007879 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007880 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007881 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007882}
7883
7884
Willy Tarreaua15645d2007-03-18 16:22:39 +01007885/* Iterate the same filter through all response headers contained in <rtr>.
7886 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7887 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007888int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007889{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007890 char *cur_ptr, *cur_end, *cur_next;
7891 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007892 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007893 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007894 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007895
7896 last_hdr = 0;
7897
Willy Tarreau9b28e032012-10-12 23:49:43 +02007898 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007899 old_idx = 0;
7900
7901 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007902 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007903 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007904 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007905 (exp->action == ACT_ALLOW ||
7906 exp->action == ACT_DENY))
7907 return 0;
7908
7909 cur_idx = txn->hdr_idx.v[old_idx].next;
7910 if (!cur_idx)
7911 break;
7912
7913 cur_hdr = &txn->hdr_idx.v[cur_idx];
7914 cur_ptr = cur_next;
7915 cur_end = cur_ptr + cur_hdr->len;
7916 cur_next = cur_end + cur_hdr->cr + 1;
7917
7918 /* Now we have one header between cur_ptr and cur_end,
7919 * and the next header starts at cur_next.
7920 */
7921
Willy Tarreau15a53a42015-01-21 13:39:42 +01007922 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007923 switch (exp->action) {
7924 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007925 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007926 last_hdr = 1;
7927 break;
7928
7929 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007930 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007931 last_hdr = 1;
7932 break;
7933
7934 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007935 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7936 if (trash.len < 0)
7937 return -1;
7938
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007939 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007940 /* FIXME: if the user adds a newline in the replacement, the
7941 * index will not be recalculated for now, and the new line
7942 * will not be counted as a new header.
7943 */
7944
7945 cur_end += delta;
7946 cur_next += delta;
7947 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007948 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007949 break;
7950
7951 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007952 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007953 cur_next += delta;
7954
Willy Tarreaufa355d42009-11-29 18:12:29 +01007955 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007956 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7957 txn->hdr_idx.used--;
7958 cur_hdr->len = 0;
7959 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007960 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007961 break;
7962
7963 }
7964 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007965
7966 /* keep the link from this header to next one in case of later
7967 * removal of next header.
7968 */
7969 old_idx = cur_idx;
7970 }
7971 return 0;
7972}
7973
7974
7975/* Apply the filter to the status line in the response buffer <rtr>.
7976 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7977 * or -1 if a replacement resulted in an invalid status line.
7978 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007979int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007980{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007981 char *cur_ptr, *cur_end;
7982 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007983 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007984 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007985
7986
Willy Tarreau3d300592007-03-18 18:34:41 +01007987 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007988 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007989 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007990 (exp->action == ACT_ALLOW ||
7991 exp->action == ACT_DENY))
7992 return 0;
7993 else if (exp->action == ACT_REMOVE)
7994 return 0;
7995
7996 done = 0;
7997
Willy Tarreau9b28e032012-10-12 23:49:43 +02007998 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007999 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008000
8001 /* Now we have the status line between cur_ptr and cur_end */
8002
Willy Tarreau15a53a42015-01-21 13:39:42 +01008003 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008004 switch (exp->action) {
8005 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008006 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008007 done = 1;
8008 break;
8009
8010 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008011 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012 done = 1;
8013 break;
8014
8015 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008016 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8017 if (trash.len < 0)
8018 return -1;
8019
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008020 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008021 /* FIXME: if the user adds a newline in the replacement, the
8022 * index will not be recalculated for now, and the new line
8023 * will not be counted as a new header.
8024 */
8025
Willy Tarreaufa355d42009-11-29 18:12:29 +01008026 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008027 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008028 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008029 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008030 cur_ptr, cur_end + 1,
8031 NULL, NULL);
8032 if (unlikely(!cur_end))
8033 return -1;
8034
8035 /* we have a full respnse and we know that we have either a CR
8036 * or an LF at <ptr>.
8037 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008038 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008039 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008040 /* there is no point trying this regex on headers */
8041 return 1;
8042 }
8043 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008044 return done;
8045}
8046
8047
8048
8049/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008050 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008051 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8052 * unparsable response.
8053 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008054int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008055{
Willy Tarreau192252e2015-04-04 01:47:55 +02008056 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008057 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008058 struct hdr_exp *exp;
8059
8060 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008061 int ret;
8062
8063 /*
8064 * The interleaving of transformations and verdicts
8065 * makes it difficult to decide to continue or stop
8066 * the evaluation.
8067 */
8068
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008069 if (txn->flags & TX_SVDENY)
8070 break;
8071
Willy Tarreau3d300592007-03-18 18:34:41 +01008072 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008073 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8074 exp->action == ACT_PASS)) {
8075 exp = exp->next;
8076 continue;
8077 }
8078
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008079 /* if this filter had a condition, evaluate it now and skip to
8080 * next filter if the condition does not match.
8081 */
8082 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008083 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008084 ret = acl_pass(ret);
8085 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8086 ret = !ret;
8087 if (!ret)
8088 continue;
8089 }
8090
Willy Tarreaua15645d2007-03-18 16:22:39 +01008091 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008092 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008093 if (unlikely(ret < 0))
8094 return -1;
8095
8096 if (likely(ret == 0)) {
8097 /* The filter did not match the response, it can be
8098 * iterated through all headers.
8099 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008100 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8101 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008102 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008103 }
8104 return 0;
8105}
8106
8107
Willy Tarreaua15645d2007-03-18 16:22:39 +01008108/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008109 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008110 * desirable to call it only when needed. This function is also used when we
8111 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008112 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008113void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008114{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008115 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008116 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008117 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008118 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008119 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008120 char *hdr_beg, *hdr_end, *hdr_next;
8121 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008122
Willy Tarreaua15645d2007-03-18 16:22:39 +01008123 /* Iterate through the headers.
8124 * we start with the start line.
8125 */
8126 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008127 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008128
8129 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8130 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008131 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008132
8133 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008134 hdr_beg = hdr_next;
8135 hdr_end = hdr_beg + cur_hdr->len;
8136 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008137
Willy Tarreau24581ba2010-08-31 22:39:35 +02008138 /* We have one full header between hdr_beg and hdr_end, and the
8139 * next header starts at hdr_next. We're only interested in
8140 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008141 */
8142
Willy Tarreau24581ba2010-08-31 22:39:35 +02008143 is_cookie2 = 0;
8144 prev = hdr_beg + 10;
8145 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008146 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008147 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8148 if (!val) {
8149 old_idx = cur_idx;
8150 continue;
8151 }
8152 is_cookie2 = 1;
8153 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008154 }
8155
Willy Tarreau24581ba2010-08-31 22:39:35 +02008156 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8157 * <prev> points to the colon.
8158 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008159 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008160
Willy Tarreau24581ba2010-08-31 22:39:35 +02008161 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8162 * check-cache is enabled) and we are not interested in checking
8163 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008164 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008165 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008166 return;
8167
Willy Tarreau24581ba2010-08-31 22:39:35 +02008168 /* OK so now we know we have to process this response cookie.
8169 * The format of the Set-Cookie header is slightly different
8170 * from the format of the Cookie header in that it does not
8171 * support the comma as a cookie delimiter (thus the header
8172 * cannot be folded) because the Expires attribute described in
8173 * the original Netscape's spec may contain an unquoted date
8174 * with a comma inside. We have to live with this because
8175 * many browsers don't support Max-Age and some browsers don't
8176 * support quoted strings. However the Set-Cookie2 header is
8177 * clean.
8178 *
8179 * We have to keep multiple pointers in order to support cookie
8180 * removal at the beginning, middle or end of header without
8181 * corrupting the header (in case of set-cookie2). A special
8182 * pointer, <scav> points to the beginning of the set-cookie-av
8183 * fields after the first semi-colon. The <next> pointer points
8184 * either to the end of line (set-cookie) or next unquoted comma
8185 * (set-cookie2). All of these headers are valid :
8186 *
8187 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8188 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8189 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8190 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8191 * | | | | | | | | | |
8192 * | | | | | | | | +-> next hdr_end <--+
8193 * | | | | | | | +------------> scav
8194 * | | | | | | +--------------> val_end
8195 * | | | | | +--------------------> val_beg
8196 * | | | | +----------------------> equal
8197 * | | | +------------------------> att_end
8198 * | | +----------------------------> att_beg
8199 * | +------------------------------> prev
8200 * +-----------------------------------------> hdr_beg
8201 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008202
Willy Tarreau24581ba2010-08-31 22:39:35 +02008203 for (; prev < hdr_end; prev = next) {
8204 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008205
Willy Tarreau24581ba2010-08-31 22:39:35 +02008206 /* find att_beg */
8207 att_beg = prev + 1;
8208 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8209 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008210
Willy Tarreau24581ba2010-08-31 22:39:35 +02008211 /* find att_end : this is the first character after the last non
8212 * space before the equal. It may be equal to hdr_end.
8213 */
8214 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008215
Willy Tarreau24581ba2010-08-31 22:39:35 +02008216 while (equal < hdr_end) {
8217 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8218 break;
8219 if (http_is_spht[(unsigned char)*equal++])
8220 continue;
8221 att_end = equal;
8222 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008223
Willy Tarreau24581ba2010-08-31 22:39:35 +02008224 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8225 * is between <att_beg> and <equal>, both may be identical.
8226 */
8227
8228 /* look for end of cookie if there is an equal sign */
8229 if (equal < hdr_end && *equal == '=') {
8230 /* look for the beginning of the value */
8231 val_beg = equal + 1;
8232 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8233 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008234
Willy Tarreau24581ba2010-08-31 22:39:35 +02008235 /* find the end of the value, respecting quotes */
8236 next = find_cookie_value_end(val_beg, hdr_end);
8237
8238 /* make val_end point to the first white space or delimitor after the value */
8239 val_end = next;
8240 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8241 val_end--;
8242 } else {
8243 /* <equal> points to next comma, semi-colon or EOL */
8244 val_beg = val_end = next = equal;
8245 }
8246
8247 if (next < hdr_end) {
8248 /* Set-Cookie2 supports multiple cookies, and <next> points to
8249 * a colon or semi-colon before the end. So skip all attr-value
8250 * pairs and look for the next comma. For Set-Cookie, since
8251 * commas are permitted in values, skip to the end.
8252 */
8253 if (is_cookie2)
8254 next = find_hdr_value_end(next, hdr_end);
8255 else
8256 next = hdr_end;
8257 }
8258
8259 /* Now everything is as on the diagram above */
8260
8261 /* Ignore cookies with no equal sign */
8262 if (equal == val_end)
8263 continue;
8264
8265 /* If there are spaces around the equal sign, we need to
8266 * strip them otherwise we'll get trouble for cookie captures,
8267 * or even for rewrites. Since this happens extremely rarely,
8268 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008269 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008270 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8271 int stripped_before = 0;
8272 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008273
Willy Tarreau24581ba2010-08-31 22:39:35 +02008274 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008275 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008276 equal += stripped_before;
8277 val_beg += stripped_before;
8278 }
8279
8280 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008281 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008282 val_beg += stripped_after;
8283 stripped_before += stripped_after;
8284 }
8285
8286 val_end += stripped_before;
8287 next += stripped_before;
8288 hdr_end += stripped_before;
8289 hdr_next += stripped_before;
8290 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008291 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008292 }
8293
8294 /* First, let's see if we want to capture this cookie. We check
8295 * that we don't already have a server side cookie, because we
8296 * can only capture one. Also as an optimisation, we ignore
8297 * cookies shorter than the declared name.
8298 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008299 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008300 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008301 (val_end - att_beg >= sess->fe->capture_namelen) &&
8302 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008303 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008304 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008305 Alert("HTTP logging : out of memory.\n");
8306 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008307 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008308 if (log_len > sess->fe->capture_len)
8309 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008310 memcpy(txn->srv_cookie, att_beg, log_len);
8311 txn->srv_cookie[log_len] = 0;
8312 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008313 }
8314
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008315 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008316 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008317 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008318 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8319 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008320 /* assume passive cookie by default */
8321 txn->flags &= ~TX_SCK_MASK;
8322 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008323
8324 /* If the cookie is in insert mode on a known server, we'll delete
8325 * this occurrence because we'll insert another one later.
8326 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008327 * a direct access.
8328 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008329 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008330 /* The "preserve" flag was set, we don't want to touch the
8331 * server's cookie.
8332 */
8333 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008334 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008335 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008336 /* this cookie must be deleted */
8337 if (*prev == ':' && next == hdr_end) {
8338 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008339 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008340 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8341 txn->hdr_idx.used--;
8342 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008343 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008344 hdr_next += delta;
8345 http_msg_move_end(&txn->rsp, delta);
8346 /* note: while both invalid now, <next> and <hdr_end>
8347 * are still equal, so the for() will stop as expected.
8348 */
8349 } else {
8350 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008351 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008352 next = prev;
8353 hdr_end += delta;
8354 hdr_next += delta;
8355 cur_hdr->len += delta;
8356 http_msg_move_end(&txn->rsp, delta);
8357 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008358 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008359 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008360 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008361 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008362 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008363 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008364 * with this server since we know it.
8365 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008366 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008367 next += delta;
8368 hdr_end += delta;
8369 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008370 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008371 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008372
Willy Tarreauf1348312010-10-07 15:54:11 +02008373 txn->flags &= ~TX_SCK_MASK;
8374 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008375 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008376 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008377 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008378 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008379 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008380 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008381 next += delta;
8382 hdr_end += delta;
8383 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008384 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008385 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008386
Willy Tarreau827aee92011-03-10 16:55:02 +01008387 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008388 txn->flags &= ~TX_SCK_MASK;
8389 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008390 }
8391 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008392 /* that's done for this cookie, check the next one on the same
8393 * line when next != hdr_end (only if is_cookie2).
8394 */
8395 }
8396 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008397 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008398 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008399}
8400
8401
Willy Tarreaua15645d2007-03-18 16:22:39 +01008402/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008403 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008404 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008405void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008406{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008407 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008408 char *p1, *p2;
8409
8410 char *cur_ptr, *cur_end, *cur_next;
8411 int cur_idx;
8412
Willy Tarreau5df51872007-11-25 16:20:08 +01008413 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008414 return;
8415
8416 /* Iterate through the headers.
8417 * we start with the start line.
8418 */
8419 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008420 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008421
8422 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8423 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008424 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008425
8426 cur_hdr = &txn->hdr_idx.v[cur_idx];
8427 cur_ptr = cur_next;
8428 cur_end = cur_ptr + cur_hdr->len;
8429 cur_next = cur_end + cur_hdr->cr + 1;
8430
8431 /* We have one full header between cur_ptr and cur_end, and the
8432 * next header starts at cur_next. We're only interested in
8433 * "Cookie:" headers.
8434 */
8435
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008436 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8437 if (val) {
8438 if ((cur_end - (cur_ptr + val) >= 8) &&
8439 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8440 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8441 return;
8442 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008443 }
8444
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008445 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8446 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008447 continue;
8448
8449 /* OK, right now we know we have a cache-control header at cur_ptr */
8450
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008451 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008452
8453 if (p1 >= cur_end) /* no more info */
8454 continue;
8455
8456 /* p1 is at the beginning of the value */
8457 p2 = p1;
8458
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008459 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008460 p2++;
8461
8462 /* we have a complete value between p1 and p2 */
8463 if (p2 < cur_end && *p2 == '=') {
8464 /* we have something of the form no-cache="set-cookie" */
8465 if ((cur_end - p1 >= 21) &&
8466 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8467 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008468 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008469 continue;
8470 }
8471
8472 /* OK, so we know that either p2 points to the end of string or to a comma */
8473 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008474 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008475 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8476 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8477 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008478 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008479 return;
8480 }
8481
8482 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008483 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008484 continue;
8485 }
8486 }
8487}
8488
Willy Tarreau58f10d72006-12-04 02:26:12 +01008489
Willy Tarreaub2513902006-12-17 14:52:38 +01008490/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008491 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008492 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008493 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008494 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008495 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008496 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008497 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008498 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008499int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008500{
8501 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008502 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008503 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008504
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008505 if (!uri_auth)
8506 return 0;
8507
Cyril Bonté70be45d2010-10-12 00:14:35 +02008508 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008509 return 0;
8510
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008511 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008512 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008513 return 0;
8514
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008515 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008516 return 0;
8517
Willy Tarreaub2513902006-12-17 14:52:38 +01008518 return 1;
8519}
8520
Willy Tarreau4076a152009-04-02 15:18:36 +02008521/*
8522 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008523 * By default it tries to report the error position as msg->err_pos. However if
8524 * this one is not set, it will then report msg->next, which is the last known
8525 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008526 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008527 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008528void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008529 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008530 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008531{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008532 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008533 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008534 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008535
Willy Tarreau9b28e032012-10-12 23:49:43 +02008536 es->len = MIN(chn->buf->i, sizeof(es->buf));
8537 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008538 len1 = MIN(len1, es->len);
8539 len2 = es->len - len1; /* remaining data if buffer wraps */
8540
Willy Tarreau9b28e032012-10-12 23:49:43 +02008541 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008542 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008543 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008544
Willy Tarreau4076a152009-04-02 15:18:36 +02008545 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008546 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008547 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008548 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008549
Willy Tarreau4076a152009-04-02 15:18:36 +02008550 es->when = date; // user-visible date
8551 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008552 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008553 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008554 if (objt_conn(sess->origin))
8555 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008556 else
8557 memset(&es->src, 0, sizeof(es->src));
8558
Willy Tarreau078272e2010-12-12 12:46:33 +01008559 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008560 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008561 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008562 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008563 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008564 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008565 es->b_out = chn->buf->o;
8566 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008567 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008568 es->m_clen = msg->chunk_len;
8569 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008570}
Willy Tarreaub2513902006-12-17 14:52:38 +01008571
Willy Tarreau294c4732011-12-16 21:35:50 +01008572/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8573 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8574 * performed over the whole headers. Otherwise it must contain a valid header
8575 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8576 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8577 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8578 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008579 * -1. The value fetch stops at commas, so this function is suited for use with
8580 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008581 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008582 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008583unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008584 struct hdr_idx *idx, int occ,
8585 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008586{
Willy Tarreau294c4732011-12-16 21:35:50 +01008587 struct hdr_ctx local_ctx;
8588 char *ptr_hist[MAX_HDR_HISTORY];
8589 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008590 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008591 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008592
Willy Tarreau294c4732011-12-16 21:35:50 +01008593 if (!ctx) {
8594 local_ctx.idx = 0;
8595 ctx = &local_ctx;
8596 }
8597
Willy Tarreaubce70882009-09-07 11:51:47 +02008598 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008599 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008600 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008601 occ--;
8602 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008603 *vptr = ctx->line + ctx->val;
8604 *vlen = ctx->vlen;
8605 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008606 }
8607 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008608 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008609 }
8610
8611 /* negative occurrence, we scan all the list then walk back */
8612 if (-occ > MAX_HDR_HISTORY)
8613 return 0;
8614
Willy Tarreau294c4732011-12-16 21:35:50 +01008615 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008616 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008617 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8618 len_hist[hist_ptr] = ctx->vlen;
8619 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008620 hist_ptr = 0;
8621 found++;
8622 }
8623 if (-occ > found)
8624 return 0;
8625 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008626 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8627 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8628 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008629 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008630 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008631 if (hist_ptr >= MAX_HDR_HISTORY)
8632 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008633 *vptr = ptr_hist[hist_ptr];
8634 *vlen = len_hist[hist_ptr];
8635 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008636}
8637
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008638/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8639 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8640 * performed over the whole headers. Otherwise it must contain a valid header
8641 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8642 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8643 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8644 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8645 * -1. This function differs from http_get_hdr() in that it only returns full
8646 * line header values and does not stop at commas.
8647 * The return value is 0 if nothing was found, or non-zero otherwise.
8648 */
8649unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8650 struct hdr_idx *idx, int occ,
8651 struct hdr_ctx *ctx, char **vptr, int *vlen)
8652{
8653 struct hdr_ctx local_ctx;
8654 char *ptr_hist[MAX_HDR_HISTORY];
8655 int len_hist[MAX_HDR_HISTORY];
8656 unsigned int hist_ptr;
8657 int found;
8658
8659 if (!ctx) {
8660 local_ctx.idx = 0;
8661 ctx = &local_ctx;
8662 }
8663
8664 if (occ >= 0) {
8665 /* search from the beginning */
8666 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8667 occ--;
8668 if (occ <= 0) {
8669 *vptr = ctx->line + ctx->val;
8670 *vlen = ctx->vlen;
8671 return 1;
8672 }
8673 }
8674 return 0;
8675 }
8676
8677 /* negative occurrence, we scan all the list then walk back */
8678 if (-occ > MAX_HDR_HISTORY)
8679 return 0;
8680
8681 found = hist_ptr = 0;
8682 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8683 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8684 len_hist[hist_ptr] = ctx->vlen;
8685 if (++hist_ptr >= MAX_HDR_HISTORY)
8686 hist_ptr = 0;
8687 found++;
8688 }
8689 if (-occ > found)
8690 return 0;
8691 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8692 * find occurrence -occ, so we have to check [hist_ptr+occ].
8693 */
8694 hist_ptr += occ;
8695 if (hist_ptr >= MAX_HDR_HISTORY)
8696 hist_ptr -= MAX_HDR_HISTORY;
8697 *vptr = ptr_hist[hist_ptr];
8698 *vlen = len_hist[hist_ptr];
8699 return 1;
8700}
8701
Willy Tarreaubaaee002006-06-26 02:48:02 +02008702/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008703 * Print a debug line with a header. Always stop at the first CR or LF char,
8704 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8705 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008706 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008707void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008708{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008709 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008710 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008711
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008712 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008713 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008714 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008715 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008716
8717 for (max = 0; start + max < end; max++)
8718 if (start[max] == '\r' || start[max] == '\n')
8719 break;
8720
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008721 UBOUND(max, trash.size - trash.len - 3);
8722 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8723 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008724 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008725}
8726
Willy Tarreaueee5b512015-04-03 23:46:31 +02008727
8728/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8729 * The hdr_idx is allocated as well. In case of allocation failure, everything
8730 * allocated is freed and NULL is returned. Otherwise the new transaction is
8731 * assigned to the stream and returned.
8732 */
8733struct http_txn *http_alloc_txn(struct stream *s)
8734{
8735 struct http_txn *txn = s->txn;
8736
8737 if (txn)
8738 return txn;
8739
8740 txn = pool_alloc2(pool2_http_txn);
8741 if (!txn)
8742 return txn;
8743
8744 txn->hdr_idx.size = global.tune.max_http_hdr;
8745 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8746 if (!txn->hdr_idx.v) {
8747 pool_free2(pool2_http_txn, txn);
8748 return NULL;
8749 }
8750
8751 s->txn = txn;
8752 return txn;
8753}
8754
Willy Tarreau0937bc42009-12-22 15:03:09 +01008755/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008756 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008757 * the required fields are properly allocated and that we only need to (re)init
8758 * them. This should be used before processing any new request.
8759 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008760void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008761{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008762 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008763 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008764
8765 txn->flags = 0;
8766 txn->status = -1;
8767
Willy Tarreauf64d1412010-10-07 20:06:11 +02008768 txn->cookie_first_date = 0;
8769 txn->cookie_last_date = 0;
8770
Willy Tarreaueee5b512015-04-03 23:46:31 +02008771 txn->srv_cookie = NULL;
8772 txn->cli_cookie = NULL;
8773 txn->uri = NULL;
8774
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008775 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008776 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008777 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008778 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008779 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008780 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008781 txn->req.chunk_len = 0LL;
8782 txn->req.body_len = 0LL;
8783 txn->rsp.chunk_len = 0LL;
8784 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008785 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8786 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008787 txn->req.chn = &s->req;
8788 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008789
8790 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008791
8792 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8793 if (fe->options2 & PR_O2_REQBUG_OK)
8794 txn->req.err_pos = -1; /* let buggy requests pass */
8795
Willy Tarreau0937bc42009-12-22 15:03:09 +01008796 if (txn->hdr_idx.v)
8797 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008798
8799 vars_init(&s->vars_txn, SCOPE_TXN);
8800 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008801}
8802
8803/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008804void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008805{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008806 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008807 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008808
Willy Tarreau75195602014-03-11 15:48:55 +01008809 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008810 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008811 s->comp_algo->end(&s->comp_ctx);
8812 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008813 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008814
Willy Tarreau0937bc42009-12-22 15:03:09 +01008815 /* these ones will have been dynamically allocated */
8816 pool_free2(pool2_requri, txn->uri);
8817 pool_free2(pool2_capture, txn->cli_cookie);
8818 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008819 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008820
William Lallemanda73203e2012-03-12 12:48:57 +01008821 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008822 txn->uri = NULL;
8823 txn->srv_cookie = NULL;
8824 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008825
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008826 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008827 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008828 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008829 pool_free2(h->pool, s->req_cap[h->index]);
8830 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008831 }
8832
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008833 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008834 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008835 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008836 pool_free2(h->pool, s->res_cap[h->index]);
8837 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008838 }
8839
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008840 vars_prune(&s->vars_txn, s);
8841 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008842}
8843
8844/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008845void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008846{
8847 http_end_txn(s);
8848 http_init_txn(s);
8849
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008850 /* reinitialise the current rule list pointer to NULL. We are sure that
8851 * any rulelist match the NULL pointer.
8852 */
8853 s->current_rule_list = NULL;
8854
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008855 s->be = strm_fe(s);
8856 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008857 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008858 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008859 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008860 /* re-init store persistence */
8861 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008862 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008863
Willy Tarreau0937bc42009-12-22 15:03:09 +01008864 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008865
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008866 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008867
Willy Tarreau739cfba2010-01-25 23:11:14 +01008868 /* We must trim any excess data from the response buffer, because we
8869 * may have blocked an invalid response from a server that we don't
8870 * want to accidentely forward once we disable the analysers, nor do
8871 * we want those data to come along with next response. A typical
8872 * example of such data would be from a buggy server responding to
8873 * a HEAD with some data, or sending more than the advertised
8874 * content-length.
8875 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008876 if (unlikely(s->res.buf->i))
8877 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008878
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008879 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008880 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008881
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008882 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008883 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008884
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008885 s->req.rex = TICK_ETERNITY;
8886 s->req.wex = TICK_ETERNITY;
8887 s->req.analyse_exp = TICK_ETERNITY;
8888 s->res.rex = TICK_ETERNITY;
8889 s->res.wex = TICK_ETERNITY;
8890 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008891}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008892
Sasha Pachev218f0642014-06-16 12:05:59 -06008893void free_http_res_rules(struct list *r)
8894{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008895 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008896
8897 list_for_each_entry_safe(pr, tr, r, list) {
8898 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008899 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008900 free(pr);
8901 }
8902}
8903
8904void free_http_req_rules(struct list *r)
8905{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008906 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008907
8908 list_for_each_entry_safe(pr, tr, r, list) {
8909 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008910 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008911 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008912
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008913 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008914 free(pr);
8915 }
8916}
8917
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008918/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008919struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008920{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008921 struct act_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008922 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008923 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008924 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008925
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008926 rule = (struct act_rule*)calloc(1, sizeof(struct act_rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008927 if (!rule) {
8928 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008929 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008930 }
8931
CJ Ess108b1dd2015-04-07 12:03:37 -04008932 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008933 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008934 rule->action = HTTP_REQ_ACT_ALLOW;
8935 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008936 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008937 int code;
8938 int hc;
8939
Willy Tarreauff011f22011-01-06 17:51:27 +01008940 rule->action = HTTP_REQ_ACT_DENY;
8941 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008942 if (strcmp(args[cur_arg], "deny_status") == 0) {
8943 cur_arg++;
8944 if (!args[cur_arg]) {
8945 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8946 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8947 goto out_err;
8948 }
8949
8950 code = atol(args[cur_arg]);
8951 cur_arg++;
8952 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8953 if (http_err_codes[hc] == code) {
8954 rule->deny_status = hc;
8955 break;
8956 }
8957 }
8958
8959 if (hc >= HTTP_ERR_SIZE) {
8960 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
8961 file, linenum, code);
8962 }
8963 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01008964 } else if (!strcmp(args[0], "tarpit")) {
8965 rule->action = HTTP_REQ_ACT_TARPIT;
8966 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008967 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008968 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008969 cur_arg = 1;
8970
8971 while(*args[cur_arg]) {
8972 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008973 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008974 cur_arg+=2;
8975 continue;
8976 } else
8977 break;
8978 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008979 } else if (!strcmp(args[0], "set-nice")) {
8980 rule->action = HTTP_REQ_ACT_SET_NICE;
8981 cur_arg = 1;
8982
8983 if (!*args[cur_arg] ||
8984 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8985 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8986 file, linenum, args[0]);
8987 goto out_err;
8988 }
8989 rule->arg.nice = atoi(args[cur_arg]);
8990 if (rule->arg.nice < -1024)
8991 rule->arg.nice = -1024;
8992 else if (rule->arg.nice > 1024)
8993 rule->arg.nice = 1024;
8994 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008995 } else if (!strcmp(args[0], "set-tos")) {
8996#ifdef IP_TOS
8997 char *err;
8998 rule->action = HTTP_REQ_ACT_SET_TOS;
8999 cur_arg = 1;
9000
9001 if (!*args[cur_arg] ||
9002 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9003 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9004 file, linenum, args[0]);
9005 goto out_err;
9006 }
9007
9008 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9009 if (err && *err != '\0') {
9010 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9011 file, linenum, err, args[0]);
9012 goto out_err;
9013 }
9014 cur_arg++;
9015#else
9016 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9017 goto out_err;
9018#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009019 } else if (!strcmp(args[0], "set-mark")) {
9020#ifdef SO_MARK
9021 char *err;
9022 rule->action = HTTP_REQ_ACT_SET_MARK;
9023 cur_arg = 1;
9024
9025 if (!*args[cur_arg] ||
9026 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9027 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9028 file, linenum, args[0]);
9029 goto out_err;
9030 }
9031
9032 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9033 if (err && *err != '\0') {
9034 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9035 file, linenum, err, args[0]);
9036 goto out_err;
9037 }
9038 cur_arg++;
9039 global.last_checks |= LSTCHK_NETADM;
9040#else
9041 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9042 goto out_err;
9043#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009044 } else if (!strcmp(args[0], "set-log-level")) {
9045 rule->action = HTTP_REQ_ACT_SET_LOGL;
9046 cur_arg = 1;
9047
9048 if (!*args[cur_arg] ||
9049 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9050 bad_log_level:
9051 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9052 file, linenum, args[0]);
9053 goto out_err;
9054 }
9055 if (strcmp(args[cur_arg], "silent") == 0)
9056 rule->arg.loglevel = -1;
9057 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9058 goto bad_log_level;
9059 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009060 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9061 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9062 cur_arg = 1;
9063
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009064 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9065 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01009066 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9067 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009068 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009069 }
9070
9071 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9072 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9073 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009074
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009075 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009076 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009077 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9078 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009079 free(proxy->conf.lfs_file);
9080 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9081 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009082 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009083 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9084 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009085 cur_arg = 1;
9086
9087 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009088 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06009089 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9090 file, linenum, args[0]);
9091 goto out_err;
9092 }
9093
9094 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9095 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9096 LIST_INIT(&rule->arg.hdr_add.fmt);
9097
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009098 error = NULL;
9099 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9100 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9101 args[cur_arg + 1], error);
9102 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009103 goto out_err;
9104 }
9105
9106 proxy->conf.args.ctx = ARGC_HRQ;
9107 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9108 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9109 file, linenum);
9110
9111 free(proxy->conf.lfs_file);
9112 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9113 proxy->conf.lfs_line = proxy->conf.args.line;
9114 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009115 } else if (strcmp(args[0], "del-header") == 0) {
9116 rule->action = HTTP_REQ_ACT_DEL_HDR;
9117 cur_arg = 1;
9118
9119 if (!*args[cur_arg] ||
9120 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9121 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9122 file, linenum, args[0]);
9123 goto out_err;
9124 }
9125
9126 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9127 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9128
9129 proxy->conf.args.ctx = ARGC_HRQ;
9130 free(proxy->conf.lfs_file);
9131 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9132 proxy->conf.lfs_line = proxy->conf.args.line;
9133 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009134 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9135 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009136 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009137 struct sample_expr *expr;
9138 unsigned int where;
9139 char *err = NULL;
9140
9141 cur_arg = 1;
9142 proxy->conf.args.ctx = ARGC_TRK;
9143
9144 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9145 if (!expr) {
9146 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9147 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9148 free(err);
9149 goto out_err;
9150 }
9151
9152 where = 0;
9153 if (proxy->cap & PR_CAP_FE)
9154 where |= SMP_VAL_FE_HRQ_HDR;
9155 if (proxy->cap & PR_CAP_BE)
9156 where |= SMP_VAL_BE_HRQ_HDR;
9157
9158 if (!(expr->fetch->val & where)) {
9159 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9160 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9161 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9162 args[cur_arg-1], sample_src_names(expr->fetch->use));
9163 free(expr);
9164 goto out_err;
9165 }
9166
9167 if (strcmp(args[cur_arg], "table") == 0) {
9168 cur_arg++;
9169 if (!args[cur_arg]) {
9170 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9171 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9172 free(expr);
9173 goto out_err;
9174 }
9175 /* we copy the table name for now, it will be resolved later */
9176 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9177 cur_arg++;
9178 }
9179 rule->act_prm.trk_ctr.expr = expr;
9180 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009181 } else if (strcmp(args[0], "redirect") == 0) {
9182 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009183 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009184
Willy Tarreaube4653b2015-05-28 15:26:58 +02009185 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009186 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9187 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9188 goto out_err;
9189 }
9190
9191 /* this redirect rule might already contain a parsed condition which
9192 * we'll pass to the http-request rule.
9193 */
9194 rule->action = HTTP_REQ_ACT_REDIR;
9195 rule->arg.redir = redir;
9196 rule->cond = redir->cond;
9197 redir->cond = NULL;
9198 cur_arg = 2;
9199 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009200 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9201 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9202 rule->action = HTTP_REQ_ACT_ADD_ACL;
9203 /*
9204 * '+ 8' for 'add-acl('
9205 * '- 9' for 'add-acl(' + trailing ')'
9206 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009207 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009208
9209 cur_arg = 1;
9210
9211 if (!*args[cur_arg] ||
9212 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9213 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9214 file, linenum, args[0]);
9215 goto out_err;
9216 }
9217
9218 LIST_INIT(&rule->arg.map.key);
9219 proxy->conf.args.ctx = ARGC_HRQ;
9220 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9221 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9222 file, linenum);
9223 free(proxy->conf.lfs_file);
9224 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9225 proxy->conf.lfs_line = proxy->conf.args.line;
9226 cur_arg += 1;
9227 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9228 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9229 rule->action = HTTP_REQ_ACT_DEL_ACL;
9230 /*
9231 * '+ 8' for 'del-acl('
9232 * '- 9' for 'del-acl(' + trailing ')'
9233 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009234 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009235
9236 cur_arg = 1;
9237
9238 if (!*args[cur_arg] ||
9239 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9240 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9241 file, linenum, args[0]);
9242 goto out_err;
9243 }
9244
9245 LIST_INIT(&rule->arg.map.key);
9246 proxy->conf.args.ctx = ARGC_HRQ;
9247 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9248 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9249 file, linenum);
9250 free(proxy->conf.lfs_file);
9251 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9252 proxy->conf.lfs_line = proxy->conf.args.line;
9253 cur_arg += 1;
9254 } else if (strncmp(args[0], "del-map", 7) == 0) {
9255 /* http-request del-map(<reference (map name)>) <key pattern> */
9256 rule->action = HTTP_REQ_ACT_DEL_MAP;
9257 /*
9258 * '+ 8' for 'del-map('
9259 * '- 9' for 'del-map(' + trailing ')'
9260 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009261 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009262
9263 cur_arg = 1;
9264
9265 if (!*args[cur_arg] ||
9266 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9267 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9268 file, linenum, args[0]);
9269 goto out_err;
9270 }
9271
9272 LIST_INIT(&rule->arg.map.key);
9273 proxy->conf.args.ctx = ARGC_HRQ;
9274 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9275 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9276 file, linenum);
9277 free(proxy->conf.lfs_file);
9278 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9279 proxy->conf.lfs_line = proxy->conf.args.line;
9280 cur_arg += 1;
9281 } else if (strncmp(args[0], "set-map", 7) == 0) {
9282 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9283 rule->action = HTTP_REQ_ACT_SET_MAP;
9284 /*
9285 * '+ 8' for 'set-map('
9286 * '- 9' for 'set-map(' + trailing ')'
9287 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009288 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009289
9290 cur_arg = 1;
9291
9292 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9293 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9294 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9295 file, linenum, args[0]);
9296 goto out_err;
9297 }
9298
9299 LIST_INIT(&rule->arg.map.key);
9300 LIST_INIT(&rule->arg.map.value);
9301 proxy->conf.args.ctx = ARGC_HRQ;
9302
9303 /* key pattern */
9304 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9305 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9306 file, linenum);
9307
9308 /* value pattern */
9309 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9310 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9311 file, linenum);
9312 free(proxy->conf.lfs_file);
9313 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9314 proxy->conf.lfs_line = proxy->conf.args.line;
9315
9316 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009317 } else if (strncmp(args[0], "set-src", 7) == 0) {
9318 struct sample_expr *expr;
9319 unsigned int where;
9320 char *err = NULL;
9321
9322 cur_arg = 1;
9323 proxy->conf.args.ctx = ARGC_HRQ;
9324
9325 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9326 if (!expr) {
9327 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9328 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9329 free(err);
9330 goto out_err;
9331 }
9332
9333 where = 0;
9334 if (proxy->cap & PR_CAP_FE)
9335 where |= SMP_VAL_FE_HRQ_HDR;
9336 if (proxy->cap & PR_CAP_BE)
9337 where |= SMP_VAL_BE_HRQ_HDR;
9338
9339 if (!(expr->fetch->val & where)) {
9340 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9341 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9342 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9343 args[cur_arg-1], sample_src_names(expr->fetch->use));
9344 free(expr);
9345 goto out_err;
9346 }
9347
9348 rule->arg.act.p[0] = expr;
9349 rule->action = HTTP_REQ_ACT_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009350 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9351 char *errmsg = NULL;
9352 cur_arg = 1;
9353 /* try in the module list */
9354 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9355 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9356 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9357 free(errmsg);
9358 goto out_err;
9359 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009360 } else {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009361 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'set-var', 'set-src', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009362 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009363 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009364 }
9365
9366 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9367 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009368 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009369
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009370 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9371 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9372 file, linenum, args[0], errmsg);
9373 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009374 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009375 }
9376 rule->cond = cond;
9377 }
9378 else if (*args[cur_arg]) {
9379 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9380 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9381 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009382 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009383 }
9384
9385 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009386 out_err:
9387 free(rule);
9388 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009389}
9390
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009391/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009392struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009393{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009394 struct act_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009395 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009396 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009397 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009398
9399 rule = calloc(1, sizeof(*rule));
9400 if (!rule) {
9401 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9402 goto out_err;
9403 }
9404
9405 if (!strcmp(args[0], "allow")) {
9406 rule->action = HTTP_RES_ACT_ALLOW;
9407 cur_arg = 1;
9408 } else if (!strcmp(args[0], "deny")) {
9409 rule->action = HTTP_RES_ACT_DENY;
9410 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009411 } else if (!strcmp(args[0], "set-nice")) {
9412 rule->action = HTTP_RES_ACT_SET_NICE;
9413 cur_arg = 1;
9414
9415 if (!*args[cur_arg] ||
9416 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9417 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9418 file, linenum, args[0]);
9419 goto out_err;
9420 }
9421 rule->arg.nice = atoi(args[cur_arg]);
9422 if (rule->arg.nice < -1024)
9423 rule->arg.nice = -1024;
9424 else if (rule->arg.nice > 1024)
9425 rule->arg.nice = 1024;
9426 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009427 } else if (!strcmp(args[0], "set-tos")) {
9428#ifdef IP_TOS
9429 char *err;
9430 rule->action = HTTP_RES_ACT_SET_TOS;
9431 cur_arg = 1;
9432
9433 if (!*args[cur_arg] ||
9434 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9435 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9436 file, linenum, args[0]);
9437 goto out_err;
9438 }
9439
9440 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9441 if (err && *err != '\0') {
9442 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9443 file, linenum, err, args[0]);
9444 goto out_err;
9445 }
9446 cur_arg++;
9447#else
9448 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9449 goto out_err;
9450#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009451 } else if (!strcmp(args[0], "set-mark")) {
9452#ifdef SO_MARK
9453 char *err;
9454 rule->action = HTTP_RES_ACT_SET_MARK;
9455 cur_arg = 1;
9456
9457 if (!*args[cur_arg] ||
9458 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9459 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9460 file, linenum, args[0]);
9461 goto out_err;
9462 }
9463
9464 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9465 if (err && *err != '\0') {
9466 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9467 file, linenum, err, args[0]);
9468 goto out_err;
9469 }
9470 cur_arg++;
9471 global.last_checks |= LSTCHK_NETADM;
9472#else
9473 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9474 goto out_err;
9475#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009476 } else if (!strcmp(args[0], "set-log-level")) {
9477 rule->action = HTTP_RES_ACT_SET_LOGL;
9478 cur_arg = 1;
9479
9480 if (!*args[cur_arg] ||
9481 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9482 bad_log_level:
9483 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9484 file, linenum, args[0]);
9485 goto out_err;
9486 }
9487 if (strcmp(args[cur_arg], "silent") == 0)
9488 rule->arg.loglevel = -1;
9489 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9490 goto bad_log_level;
9491 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009492 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9493 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9494 cur_arg = 1;
9495
9496 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9497 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9498 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9499 file, linenum, args[0]);
9500 goto out_err;
9501 }
9502
9503 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9504 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9505 LIST_INIT(&rule->arg.hdr_add.fmt);
9506
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009507 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009508 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009509 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9510 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009511 free(proxy->conf.lfs_file);
9512 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9513 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009514 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009515 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009516 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009517 cur_arg = 1;
9518
9519 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009520 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9521 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009522 file, linenum, args[0]);
9523 goto out_err;
9524 }
9525
9526 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9527 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9528 LIST_INIT(&rule->arg.hdr_add.fmt);
9529
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009530 error = NULL;
9531 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9532 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9533 args[cur_arg + 1], error);
9534 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009535 goto out_err;
9536 }
9537
9538 proxy->conf.args.ctx = ARGC_HRQ;
9539 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9540 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9541 file, linenum);
9542
9543 free(proxy->conf.lfs_file);
9544 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9545 proxy->conf.lfs_line = proxy->conf.args.line;
9546 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009547 } else if (strcmp(args[0], "del-header") == 0) {
9548 rule->action = HTTP_RES_ACT_DEL_HDR;
9549 cur_arg = 1;
9550
9551 if (!*args[cur_arg] ||
9552 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9553 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9554 file, linenum, args[0]);
9555 goto out_err;
9556 }
9557
9558 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9559 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9560
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009561 proxy->conf.args.ctx = ARGC_HRS;
9562 free(proxy->conf.lfs_file);
9563 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9564 proxy->conf.lfs_line = proxy->conf.args.line;
9565 cur_arg += 1;
9566 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9567 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9568 rule->action = HTTP_RES_ACT_ADD_ACL;
9569 /*
9570 * '+ 8' for 'add-acl('
9571 * '- 9' for 'add-acl(' + trailing ')'
9572 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009573 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009574
9575 cur_arg = 1;
9576
9577 if (!*args[cur_arg] ||
9578 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9579 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9580 file, linenum, args[0]);
9581 goto out_err;
9582 }
9583
9584 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009585 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009586 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9587 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9588 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009589 free(proxy->conf.lfs_file);
9590 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9591 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009592
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009593 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009594 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9595 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9596 rule->action = HTTP_RES_ACT_DEL_ACL;
9597 /*
9598 * '+ 8' for 'del-acl('
9599 * '- 9' for 'del-acl(' + trailing ')'
9600 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009601 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009602
9603 cur_arg = 1;
9604
9605 if (!*args[cur_arg] ||
9606 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9607 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9608 file, linenum, args[0]);
9609 goto out_err;
9610 }
9611
9612 LIST_INIT(&rule->arg.map.key);
9613 proxy->conf.args.ctx = ARGC_HRS;
9614 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9615 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9616 file, linenum);
9617 free(proxy->conf.lfs_file);
9618 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9619 proxy->conf.lfs_line = proxy->conf.args.line;
9620 cur_arg += 1;
9621 } else if (strncmp(args[0], "del-map", 7) == 0) {
9622 /* http-response del-map(<reference (map name)>) <key pattern> */
9623 rule->action = HTTP_RES_ACT_DEL_MAP;
9624 /*
9625 * '+ 8' for 'del-map('
9626 * '- 9' for 'del-map(' + trailing ')'
9627 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009628 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009629
9630 cur_arg = 1;
9631
9632 if (!*args[cur_arg] ||
9633 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9634 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9635 file, linenum, args[0]);
9636 goto out_err;
9637 }
9638
9639 LIST_INIT(&rule->arg.map.key);
9640 proxy->conf.args.ctx = ARGC_HRS;
9641 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9642 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9643 file, linenum);
9644 free(proxy->conf.lfs_file);
9645 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9646 proxy->conf.lfs_line = proxy->conf.args.line;
9647 cur_arg += 1;
9648 } else if (strncmp(args[0], "set-map", 7) == 0) {
9649 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9650 rule->action = HTTP_RES_ACT_SET_MAP;
9651 /*
9652 * '+ 8' for 'set-map('
9653 * '- 9' for 'set-map(' + trailing ')'
9654 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009655 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009656
9657 cur_arg = 1;
9658
9659 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9660 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9661 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9662 file, linenum, args[0]);
9663 goto out_err;
9664 }
9665
9666 LIST_INIT(&rule->arg.map.key);
9667 LIST_INIT(&rule->arg.map.value);
9668
9669 proxy->conf.args.ctx = ARGC_HRS;
9670
9671 /* key pattern */
9672 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9673 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9674 file, linenum);
9675
9676 /* value pattern */
9677 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9678 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9679 file, linenum);
9680
9681 free(proxy->conf.lfs_file);
9682 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9683 proxy->conf.lfs_line = proxy->conf.args.line;
9684
9685 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009686 } else if (strcmp(args[0], "redirect") == 0) {
9687 struct redirect_rule *redir;
9688 char *errmsg = NULL;
9689
9690 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9691 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9692 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9693 goto out_err;
9694 }
9695
9696 /* this redirect rule might already contain a parsed condition which
9697 * we'll pass to the http-request rule.
9698 */
9699 rule->action = HTTP_RES_ACT_REDIR;
9700 rule->arg.redir = redir;
9701 rule->cond = redir->cond;
9702 redir->cond = NULL;
9703 cur_arg = 2;
9704 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009705 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9706 char *errmsg = NULL;
9707 cur_arg = 1;
9708 /* try in the module list */
9709 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9710 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9711 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9712 free(errmsg);
9713 goto out_err;
9714 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009715 } else {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02009716 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'del-acl', 'add-acl', 'del-map', 'set-map', 'set-var' but got '%s'%s.\n",
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009717 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9718 goto out_err;
9719 }
9720
9721 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9722 struct acl_cond *cond;
9723 char *errmsg = NULL;
9724
9725 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9726 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9727 file, linenum, args[0], errmsg);
9728 free(errmsg);
9729 goto out_err;
9730 }
9731 rule->cond = cond;
9732 }
9733 else if (*args[cur_arg]) {
9734 Alert("parsing [%s:%d]: 'http-response %s' expects"
9735 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9736 file, linenum, args[0], args[cur_arg]);
9737 goto out_err;
9738 }
9739
9740 return rule;
9741 out_err:
9742 free(rule);
9743 return NULL;
9744}
9745
Willy Tarreau4baae242012-12-27 12:00:31 +01009746/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009747 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009748 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9749 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009750 */
9751struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009752 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009753{
9754 struct redirect_rule *rule;
9755 int cur_arg;
9756 int type = REDIRECT_TYPE_NONE;
9757 int code = 302;
9758 const char *destination = NULL;
9759 const char *cookie = NULL;
9760 int cookie_set = 0;
9761 unsigned int flags = REDIRECT_FLAG_NONE;
9762 struct acl_cond *cond = NULL;
9763
9764 cur_arg = 0;
9765 while (*(args[cur_arg])) {
9766 if (strcmp(args[cur_arg], "location") == 0) {
9767 if (!*args[cur_arg + 1])
9768 goto missing_arg;
9769
9770 type = REDIRECT_TYPE_LOCATION;
9771 cur_arg++;
9772 destination = args[cur_arg];
9773 }
9774 else if (strcmp(args[cur_arg], "prefix") == 0) {
9775 if (!*args[cur_arg + 1])
9776 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009777 type = REDIRECT_TYPE_PREFIX;
9778 cur_arg++;
9779 destination = args[cur_arg];
9780 }
9781 else if (strcmp(args[cur_arg], "scheme") == 0) {
9782 if (!*args[cur_arg + 1])
9783 goto missing_arg;
9784
9785 type = REDIRECT_TYPE_SCHEME;
9786 cur_arg++;
9787 destination = args[cur_arg];
9788 }
9789 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9790 if (!*args[cur_arg + 1])
9791 goto missing_arg;
9792
9793 cur_arg++;
9794 cookie = args[cur_arg];
9795 cookie_set = 1;
9796 }
9797 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9798 if (!*args[cur_arg + 1])
9799 goto missing_arg;
9800
9801 cur_arg++;
9802 cookie = args[cur_arg];
9803 cookie_set = 0;
9804 }
9805 else if (strcmp(args[cur_arg], "code") == 0) {
9806 if (!*args[cur_arg + 1])
9807 goto missing_arg;
9808
9809 cur_arg++;
9810 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009811 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009812 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009813 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009814 args[cur_arg - 1], args[cur_arg]);
9815 return NULL;
9816 }
9817 }
9818 else if (!strcmp(args[cur_arg],"drop-query")) {
9819 flags |= REDIRECT_FLAG_DROP_QS;
9820 }
9821 else if (!strcmp(args[cur_arg],"append-slash")) {
9822 flags |= REDIRECT_FLAG_APPEND_SLASH;
9823 }
9824 else if (strcmp(args[cur_arg], "if") == 0 ||
9825 strcmp(args[cur_arg], "unless") == 0) {
9826 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9827 if (!cond) {
9828 memprintf(errmsg, "error in condition: %s", *errmsg);
9829 return NULL;
9830 }
9831 break;
9832 }
9833 else {
9834 memprintf(errmsg,
9835 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9836 args[cur_arg]);
9837 return NULL;
9838 }
9839 cur_arg++;
9840 }
9841
9842 if (type == REDIRECT_TYPE_NONE) {
9843 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9844 return NULL;
9845 }
9846
Willy Tarreaube4653b2015-05-28 15:26:58 +02009847 if (dir && type != REDIRECT_TYPE_LOCATION) {
9848 memprintf(errmsg, "response only supports redirect type 'location'");
9849 return NULL;
9850 }
9851
Willy Tarreau4baae242012-12-27 12:00:31 +01009852 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9853 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009854 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009855
9856 if (!use_fmt) {
9857 /* old-style static redirect rule */
9858 rule->rdr_str = strdup(destination);
9859 rule->rdr_len = strlen(destination);
9860 }
9861 else {
9862 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009863
9864 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9865 * if prefix == "/", we don't want to add anything, otherwise it
9866 * makes it hard for the user to configure a self-redirection.
9867 */
Godbachd9722032014-12-18 15:44:58 +08009868 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009869 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009870 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009871 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9872 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009873 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009874 free(curproxy->conf.lfs_file);
9875 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9876 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009877 }
9878 }
9879
Willy Tarreau4baae242012-12-27 12:00:31 +01009880 if (cookie) {
9881 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9882 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9883 */
9884 rule->cookie_len = strlen(cookie);
9885 if (cookie_set) {
9886 rule->cookie_str = malloc(rule->cookie_len + 10);
9887 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9888 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9889 rule->cookie_len += 9;
9890 } else {
9891 rule->cookie_str = malloc(rule->cookie_len + 21);
9892 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9893 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9894 rule->cookie_len += 20;
9895 }
9896 }
9897 rule->type = type;
9898 rule->code = code;
9899 rule->flags = flags;
9900 LIST_INIT(&rule->list);
9901 return rule;
9902
9903 missing_arg:
9904 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9905 return NULL;
9906}
9907
Willy Tarreau8797c062007-05-07 00:55:35 +02009908/************************************************************************/
9909/* The code below is dedicated to ACL parsing and matching */
9910/************************************************************************/
9911
9912
Willy Tarreau14174bc2012-04-16 14:34:04 +02009913/* This function ensures that the prerequisites for an L7 fetch are ready,
9914 * which means that a request or response is ready. If some data is missing,
9915 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009916 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9917 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009918 *
9919 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009920 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9921 * decide whether or not an HTTP message is present ;
9922 * 0 if the requested data cannot be fetched or if it is certain that
9923 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009924 * 1 if an HTTP message is ready
9925 */
9926static int
Willy Tarreau15e91e12015-04-04 00:52:09 +02009927smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009928 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009929{
Willy Tarreau192252e2015-04-04 01:47:55 +02009930 struct http_txn *txn;
9931 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009932
Willy Tarreaueee5b512015-04-03 23:46:31 +02009933 /* Note: this function may only be used from places where
9934 * http_init_txn() has already been done, and implies that <s>,
9935 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
9936 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009937 */
9938
Willy Tarreau192252e2015-04-04 01:47:55 +02009939 if (!s)
9940 return 0;
9941 txn = s->txn;
9942
9943 if (!txn)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009944 return 0;
Willy Tarreau192252e2015-04-04 01:47:55 +02009945 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009946
9947 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009948 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009949
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009950 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009951 /* If the buffer does not leave enough free space at the end,
9952 * we must first realign it.
9953 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009954 if (s->req.buf->p > s->req.buf->data &&
9955 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9956 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009957
Willy Tarreau14174bc2012-04-16 14:34:04 +02009958 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009959 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009960 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009961
9962 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009963 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009964 http_msg_analyzer(msg, &txn->hdr_idx);
9965
9966 /* Still no valid request ? */
9967 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009968 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009969 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009970 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009971 }
9972 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009973 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009974 return 0;
9975 }
9976
9977 /* OK we just got a valid HTTP request. We have some minor
9978 * preparation to perform so that further checks can rely
9979 * on HTTP tests.
9980 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009981
9982 /* If the request was parsed but was too large, we must absolutely
9983 * return an error so that it is not processed. At the moment this
9984 * cannot happen, but if the parsers are to change in the future,
9985 * we want this check to be maintained.
9986 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009987 if (unlikely(s->req.buf->i + s->req.buf->p >
9988 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009989 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009990 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009991 return 1;
9992 }
9993
Willy Tarreau9b28e032012-10-12 23:49:43 +02009994 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009995 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009996 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009997
Willy Tarreau506d0502013-07-06 13:29:24 +02009998 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9999 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010000 }
10001
Willy Tarreau506d0502013-07-06 13:29:24 +020010002 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010003 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010004 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010005
10006 /* otherwise everything's ready for the request */
10007 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010008 else {
10009 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010010 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10011 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010012 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010013 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010014 }
10015
10016 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010017 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010018 return 1;
10019}
Willy Tarreau8797c062007-05-07 00:55:35 +020010020
Willy Tarreau6c616e02014-06-25 16:56:41 +020010021/* Note: these functinos *do* modify the sample. Even in case of success, at
10022 * least the type and uint value are modified.
10023 */
Willy Tarreauc0239e02012-04-16 14:42:55 +020010024#define CHECK_HTTP_MESSAGE_FIRST() \
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010025 do { int r = smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +020010026
Willy Tarreau24e32d82012-04-23 23:55:44 +020010027#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010028 do { int r = smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +020010029
Willy Tarreau8797c062007-05-07 00:55:35 +020010030
10031/* 1. Check on METHOD
10032 * We use the pre-parsed method if it is known, and store its number as an
10033 * integer. If it is unknown, we use the pointer and the length.
10034 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010035static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010036{
10037 int len, meth;
10038
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010039 len = strlen(text);
10040 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010041
10042 pattern->val.i = meth;
10043 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010044 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010045 pattern->len = len;
10046 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010047 else {
10048 pattern->ptr.str = NULL;
10049 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010050 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010051 return 1;
10052}
10053
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010054/* This function fetches the method of current HTTP request and stores
10055 * it in the global pattern struct as a chunk. There are two possibilities :
10056 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10057 * in <len> and <ptr> is NULL ;
10058 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10059 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010060 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010061 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010062static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010063smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010064{
10065 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010066 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010067
Willy Tarreau24e32d82012-04-23 23:55:44 +020010068 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010069
Willy Tarreau8797c062007-05-07 00:55:35 +020010070 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010071 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010072 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010073 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010074 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10075 /* ensure the indexes are not affected */
10076 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010077 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010078 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10079 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010080 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010081 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010082 return 1;
10083}
10084
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010085/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010086static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010087{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010088 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010089 struct pattern_list *lst;
10090 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010091
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010092 list_for_each_entry(lst, &expr->patterns, list) {
10093 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010094
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010095 /* well-known method */
10096 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010097 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010098 return pattern;
10099 else
10100 continue;
10101 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010102
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010103 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010104 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010105 continue;
10106
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010107 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010108 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10109 (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010110 return pattern;
10111 }
10112 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010113}
10114
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010115static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010116smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010117{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010118 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010119 char *ptr;
10120 int len;
10121
Willy Tarreauc0239e02012-04-16 14:42:55 +020010122 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010123
Willy Tarreau8797c062007-05-07 00:55:35 +020010124 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010125 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010126
10127 while ((len-- > 0) && (*ptr++ != '/'));
10128 if (len <= 0)
10129 return 0;
10130
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010131 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010132 smp->data.u.str.str = ptr;
10133 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010134
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010135 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010136 return 1;
10137}
10138
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010139static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010140smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010141{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010142 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010143 char *ptr;
10144 int len;
10145
Willy Tarreauc0239e02012-04-16 14:42:55 +020010146 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010147
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010148 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010149 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10150 return 0;
10151
Willy Tarreau8797c062007-05-07 00:55:35 +020010152 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010153 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010154
10155 while ((len-- > 0) && (*ptr++ != '/'));
10156 if (len <= 0)
10157 return 0;
10158
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010159 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010160 smp->data.u.str.str = ptr;
10161 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010162
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010163 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010164 return 1;
10165}
10166
10167/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010168static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010169smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010170{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010171 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010172 char *ptr;
10173 int len;
10174
Willy Tarreauc0239e02012-04-16 14:42:55 +020010175 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010176
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010177 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010178 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10179 return 0;
10180
Willy Tarreau8797c062007-05-07 00:55:35 +020010181 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010182 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010183
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010184 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010185 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010186 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010187 return 1;
10188}
10189
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010190/* returns the longest available part of the body. This requires that the body
10191 * has been waited for using http-buffer-request.
10192 */
10193static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010194smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010195{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010196 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010197 struct http_msg *msg;
10198 unsigned long len;
10199 unsigned long block1;
10200 char *body;
10201 struct chunk *temp;
10202
10203 CHECK_HTTP_MESSAGE_FIRST();
10204
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010205 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010206 msg = &txn->req;
10207 else
10208 msg = &txn->rsp;
10209
10210 len = http_body_bytes(msg);
10211 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10212
10213 block1 = len;
10214 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10215 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10216
10217 if (block1 == len) {
10218 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010219 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010220 smp->data.u.str.str = body;
10221 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010222 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10223 }
10224 else {
10225 /* buffer is wrapped, we need to defragment it */
10226 temp = get_trash_chunk();
10227 memcpy(temp->str, body, block1);
10228 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010229 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010230 smp->data.u.str.str = temp->str;
10231 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010232 smp->flags = SMP_F_VOL_TEST;
10233 }
10234 return 1;
10235}
10236
10237
10238/* returns the available length of the body. This requires that the body
10239 * has been waited for using http-buffer-request.
10240 */
10241static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010242smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010243{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010244 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010245 struct http_msg *msg;
10246
10247 CHECK_HTTP_MESSAGE_FIRST();
10248
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010249 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010250 msg = &txn->req;
10251 else
10252 msg = &txn->rsp;
10253
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010254 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010255 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010256
10257 smp->flags = SMP_F_VOL_TEST;
10258 return 1;
10259}
10260
10261
10262/* returns the advertised length of the body, or the advertised size of the
10263 * chunks available in the buffer. This requires that the body has been waited
10264 * for using http-buffer-request.
10265 */
10266static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010267smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010268{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010269 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010270 struct http_msg *msg;
10271
10272 CHECK_HTTP_MESSAGE_FIRST();
10273
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010274 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010275 msg = &txn->req;
10276 else
10277 msg = &txn->rsp;
10278
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010279 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010280 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010281
10282 smp->flags = SMP_F_VOL_TEST;
10283 return 1;
10284}
10285
10286
Willy Tarreau8797c062007-05-07 00:55:35 +020010287/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010288static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010289smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010290{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010291 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010292
Willy Tarreauc0239e02012-04-16 14:42:55 +020010293 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010294
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010295 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010296 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010297 smp->data.u.str.len = txn->req.sl.rq.u_l;
10298 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010299 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010300 return 1;
10301}
10302
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010303static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010304smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010305{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010306 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010307 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010308
Willy Tarreauc0239e02012-04-16 14:42:55 +020010309 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010310
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010311 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010312 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010313 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010314 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010315
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010316 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010317 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010318 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010319 return 1;
10320}
10321
10322static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010323smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010324{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010325 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010326 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010327
Willy Tarreauc0239e02012-04-16 14:42:55 +020010328 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010329
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010330 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010331 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010332 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10333 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010334
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010335 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010336 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010337 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010338 return 1;
10339}
10340
Willy Tarreau185b5c42012-04-26 15:11:51 +020010341/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10342 * Accepts an optional argument of type string containing the header field name,
10343 * and an optional argument of type signed or unsigned integer to request an
10344 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010345 * headers are considered from the first one. It does not stop on commas and
10346 * returns full lines instead (useful for User-Agent or Date for example).
10347 */
10348static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010349smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010350{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010351 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010352 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010353 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010354 int occ = 0;
10355 const char *name_str = NULL;
10356 int name_len = 0;
10357
10358 if (!ctx) {
10359 /* first call */
10360 ctx = &static_hdr_ctx;
10361 ctx->idx = 0;
10362 smp->ctx.a[0] = ctx;
10363 }
10364
10365 if (args) {
10366 if (args[0].type != ARGT_STR)
10367 return 0;
10368 name_str = args[0].data.str.str;
10369 name_len = args[0].data.str.len;
10370
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010371 if (args[1].type == ARGT_SINT)
10372 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010373 }
10374
10375 CHECK_HTTP_MESSAGE_FIRST();
10376
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010377 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010378 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010379
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010380 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10381 /* search for header from the beginning */
10382 ctx->idx = 0;
10383
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010384 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010385 /* no explicit occurrence and single fetch => last header by default */
10386 occ = -1;
10387
10388 if (!occ)
10389 /* prepare to report multiple occurrences for ACL fetches */
10390 smp->flags |= SMP_F_NOT_LAST;
10391
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010392 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010393 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010394 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010395 return 1;
10396
10397 smp->flags &= ~SMP_F_NOT_LAST;
10398 return 0;
10399}
10400
10401/* 6. Check on HTTP header count. The number of occurrences is returned.
10402 * Accepts exactly 1 argument of type string. It does not stop on commas and
10403 * returns full lines instead (useful for User-Agent or Date for example).
10404 */
10405static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010406smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010407{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010408 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010409 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010410 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010411 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010412 const char *name = NULL;
10413 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010414
Willy Tarreau601a4d12015-04-01 19:16:09 +020010415 if (args && args->type == ARGT_STR) {
10416 name = args->data.str.str;
10417 len = args->data.str.len;
10418 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010419
10420 CHECK_HTTP_MESSAGE_FIRST();
10421
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010422 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010423 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010424
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010425 ctx.idx = 0;
10426 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010427 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010428 cnt++;
10429
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010430 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010431 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010432 smp->flags = SMP_F_VOL_HDR;
10433 return 1;
10434}
10435
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010436static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010437smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010438{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010439 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010440 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010441 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010442 struct chunk *temp;
10443 char del = ',';
10444
10445 if (args && args->type == ARGT_STR)
10446 del = *args[0].data.str.str;
10447
10448 CHECK_HTTP_MESSAGE_FIRST();
10449
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010450 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010451 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010452
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010453 temp = get_trash_chunk();
10454
10455 ctx.idx = 0;
10456 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10457 if (temp->len)
10458 temp->str[temp->len++] = del;
10459 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10460 temp->len += ctx.del;
10461 }
10462
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010463 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010464 smp->data.u.str.str = temp->str;
10465 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010466 smp->flags = SMP_F_VOL_HDR;
10467 return 1;
10468}
10469
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010470/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10471 * Accepts an optional argument of type string containing the header field name,
10472 * and an optional argument of type signed or unsigned integer to request an
10473 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010474 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010475 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010476static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010477smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010478{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010479 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010480 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010481 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010482 int occ = 0;
10483 const char *name_str = NULL;
10484 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010485
Willy Tarreaua890d072013-04-02 12:01:06 +020010486 if (!ctx) {
10487 /* first call */
10488 ctx = &static_hdr_ctx;
10489 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010490 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010491 }
10492
Willy Tarreau185b5c42012-04-26 15:11:51 +020010493 if (args) {
10494 if (args[0].type != ARGT_STR)
10495 return 0;
10496 name_str = args[0].data.str.str;
10497 name_len = args[0].data.str.len;
10498
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010499 if (args[1].type == ARGT_SINT)
10500 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010501 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010502
Willy Tarreaue333ec92012-04-16 16:26:40 +020010503 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010504
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010505 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010506 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010507
Willy Tarreau185b5c42012-04-26 15:11:51 +020010508 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010509 /* search for header from the beginning */
10510 ctx->idx = 0;
10511
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010512 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010513 /* no explicit occurrence and single fetch => last header by default */
10514 occ = -1;
10515
10516 if (!occ)
10517 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010518 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010519
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010520 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010521 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010522 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010523 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010524
Willy Tarreau37406352012-04-23 16:16:37 +020010525 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010526 return 0;
10527}
10528
Willy Tarreauc11416f2007-06-17 16:58:38 +020010529/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010530 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010531 */
10532static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010533smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010534{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010535 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010536 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010537 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010538 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010539 const char *name = NULL;
10540 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010541
Willy Tarreau601a4d12015-04-01 19:16:09 +020010542 if (args && args->type == ARGT_STR) {
10543 name = args->data.str.str;
10544 len = args->data.str.len;
10545 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010546
Willy Tarreaue333ec92012-04-16 16:26:40 +020010547 CHECK_HTTP_MESSAGE_FIRST();
10548
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010549 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010550 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010551
Willy Tarreau33a7e692007-06-10 19:45:56 +020010552 ctx.idx = 0;
10553 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010554 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010555 cnt++;
10556
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010557 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010558 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010559 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010560 return 1;
10561}
10562
Willy Tarreau185b5c42012-04-26 15:11:51 +020010563/* Fetch an HTTP header's integer value. The integer value is returned. It
10564 * takes a mandatory argument of type string and an optional one of type int
10565 * to designate a specific occurrence. It returns an unsigned integer, which
10566 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010567 */
10568static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010569smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010570{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010571 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010572
Willy Tarreauf853c462012-04-23 18:53:56 +020010573 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010574 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010575 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010576 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010577
Willy Tarreaud53e2422012-04-16 17:21:11 +020010578 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010579}
10580
Cyril Bonté69fa9922012-10-25 00:01:06 +020010581/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10582 * and an optional one of type int to designate a specific occurrence.
10583 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010584 */
10585static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010586smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010587{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010588 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010589
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010590 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010591 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010592 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010593 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010594 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010595 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010596 if (smp->data.u.str.len < temp->size - 1) {
10597 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10598 temp->str[smp->data.u.str.len] = '\0';
10599 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010600 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010601 break;
10602 }
10603 }
10604 }
10605
Willy Tarreaud53e2422012-04-16 17:21:11 +020010606 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010607 if (!(smp->flags & SMP_F_NOT_LAST))
10608 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010609 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010610 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010611}
10612
Willy Tarreau737b0c12007-06-10 21:28:46 +020010613/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10614 * the first '/' after the possible hostname, and ends before the possible '?'.
10615 */
10616static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010617smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010618{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010619 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010620 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010621
Willy Tarreauc0239e02012-04-16 14:42:55 +020010622 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010623
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010624 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010625 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010626 ptr = http_get_path(txn);
10627 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010628 return 0;
10629
10630 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010631 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010632 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010633
10634 while (ptr < end && *ptr != '?')
10635 ptr++;
10636
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010637 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010638 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010639 return 1;
10640}
10641
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010642/* This produces a concatenation of the first occurrence of the Host header
10643 * followed by the path component if it begins with a slash ('/'). This means
10644 * that '*' will not be added, resulting in exactly the first Host entry.
10645 * If no Host header is found, then the path is returned as-is. The returned
10646 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010647 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010648 */
10649static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010650smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010651{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010652 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010653 char *ptr, *end, *beg;
10654 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010655 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010656
10657 CHECK_HTTP_MESSAGE_FIRST();
10658
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010659 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010660 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010661 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010662 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010663
10664 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010665 temp = get_trash_chunk();
10666 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010667 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010668 smp->data.u.str.str = temp->str;
10669 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010670
10671 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010672 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010673 beg = http_get_path(txn);
10674 if (!beg)
10675 beg = end;
10676
10677 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10678
10679 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010680 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10681 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010682 }
10683
10684 smp->flags = SMP_F_VOL_1ST;
10685 return 1;
10686}
10687
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010688/* This produces a 32-bit hash of the concatenation of the first occurrence of
10689 * the Host header followed by the path component if it begins with a slash ('/').
10690 * This means that '*' will not be added, resulting in exactly the first Host
10691 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010692 * is hashed using the path hash followed by a full avalanche hash and provides a
10693 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010694 * high-traffic sites without having to store whole paths.
10695 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010696int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010697smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010698{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010699 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010700 struct hdr_ctx ctx;
10701 unsigned int hash = 0;
10702 char *ptr, *beg, *end;
10703 int len;
10704
10705 CHECK_HTTP_MESSAGE_FIRST();
10706
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010707 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010708 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010709 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010710 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10711 ptr = ctx.line + ctx.val;
10712 len = ctx.vlen;
10713 while (len--)
10714 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10715 }
10716
10717 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010718 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010719 beg = http_get_path(txn);
10720 if (!beg)
10721 beg = end;
10722
10723 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10724
10725 if (beg < ptr && *beg == '/') {
10726 while (beg < ptr)
10727 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10728 }
10729 hash = full_hash(hash);
10730
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010731 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010732 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010733 smp->flags = SMP_F_VOL_1ST;
10734 return 1;
10735}
10736
Willy Tarreau4a550602012-12-09 14:53:32 +010010737/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010738 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10739 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10740 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010741 * that in environments where IPv6 is insignificant, truncating the output to
10742 * 8 bytes would still work.
10743 */
10744static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010745smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010746{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010747 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010748 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010749
10750 if (!cli_conn)
10751 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010752
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010753 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010754 return 0;
10755
Willy Tarreau47ca5452012-12-23 20:22:19 +010010756 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010757 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010758 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010759
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010760 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010761 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010762 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010763 temp->len += 4;
10764 break;
10765 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010766 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010767 temp->len += 16;
10768 break;
10769 default:
10770 return 0;
10771 }
10772
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010773 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010774 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010775 return 1;
10776}
10777
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010778/* Extracts the query string, which comes after the question mark '?'. If no
10779 * question mark is found, nothing is returned. Otherwise it returns a sample
10780 * of type string carrying the whole query string.
10781 */
10782static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010783smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010784{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010785 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010786 char *ptr, *end;
10787
10788 CHECK_HTTP_MESSAGE_FIRST();
10789
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010790 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010791 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10792 end = ptr + txn->req.sl.rq.u_l;
10793
10794 /* look up the '?' */
10795 do {
10796 if (ptr == end)
10797 return 0;
10798 } while (*ptr++ != '?');
10799
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010800 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010801 smp->data.u.str.str = ptr;
10802 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010803 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10804 return 1;
10805}
10806
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010807static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010808smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010809{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010810 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10811 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10812 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010813
Willy Tarreau24e32d82012-04-23 23:55:44 +020010814 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010815
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010816 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010817 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010818 return 1;
10819}
10820
Willy Tarreau7f18e522010-10-22 20:04:13 +020010821/* return a valid test if the current request is the first one on the connection */
10822static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010823smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010824{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010825 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010826 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010827 return 1;
10828}
10829
Willy Tarreau34db1082012-04-19 17:16:54 +020010830/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010831static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010832smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010833{
10834
Willy Tarreau24e32d82012-04-23 23:55:44 +020010835 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010836 return 0;
10837
Willy Tarreauc0239e02012-04-16 14:42:55 +020010838 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010839
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010840 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010841 return 0;
10842
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010843 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010844 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010845 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010846 return 1;
10847}
Willy Tarreau8797c062007-05-07 00:55:35 +020010848
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010849/* Accepts exactly 1 argument of type userlist */
10850static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010851smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010852{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010853 if (!args || args->type != ARGT_USR)
10854 return 0;
10855
10856 CHECK_HTTP_MESSAGE_FIRST();
10857
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010858 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010859 return 0;
10860
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010861 /* if the user does not belong to the userlist or has a wrong password,
10862 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010863 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010864 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010865 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10866 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010867 return 0;
10868
10869 /* pat_match_auth() will need the user list */
10870 smp->ctx.a[0] = args->data.usr;
10871
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010872 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010873 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010874 smp->data.u.str.str = smp->strm->txn->auth.user;
10875 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010876
10877 return 1;
10878}
10879
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010880/* Try to find the next occurrence of a cookie name in a cookie header value.
10881 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10882 * the cookie value is returned into *value and *value_l, and the function
10883 * returns a pointer to the next pointer to search from if the value was found.
10884 * Otherwise if the cookie was not found, NULL is returned and neither value
10885 * nor value_l are touched. The input <hdr> string should first point to the
10886 * header's value, and the <hdr_end> pointer must point to the first character
10887 * not part of the value. <list> must be non-zero if value may represent a list
10888 * of values (cookie headers). This makes it faster to abort parsing when no
10889 * list is expected.
10890 */
10891static char *
10892extract_cookie_value(char *hdr, const char *hdr_end,
10893 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010894 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010895{
10896 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10897 char *next;
10898
10899 /* we search at least a cookie name followed by an equal, and more
10900 * generally something like this :
10901 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10902 */
10903 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10904 /* Iterate through all cookies on this line */
10905
10906 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10907 att_beg++;
10908
10909 /* find att_end : this is the first character after the last non
10910 * space before the equal. It may be equal to hdr_end.
10911 */
10912 equal = att_end = att_beg;
10913
10914 while (equal < hdr_end) {
10915 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10916 break;
10917 if (http_is_spht[(unsigned char)*equal++])
10918 continue;
10919 att_end = equal;
10920 }
10921
10922 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10923 * is between <att_beg> and <equal>, both may be identical.
10924 */
10925
10926 /* look for end of cookie if there is an equal sign */
10927 if (equal < hdr_end && *equal == '=') {
10928 /* look for the beginning of the value */
10929 val_beg = equal + 1;
10930 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10931 val_beg++;
10932
10933 /* find the end of the value, respecting quotes */
10934 next = find_cookie_value_end(val_beg, hdr_end);
10935
10936 /* make val_end point to the first white space or delimitor after the value */
10937 val_end = next;
10938 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10939 val_end--;
10940 } else {
10941 val_beg = val_end = next = equal;
10942 }
10943
10944 /* We have nothing to do with attributes beginning with '$'. However,
10945 * they will automatically be removed if a header before them is removed,
10946 * since they're supposed to be linked together.
10947 */
10948 if (*att_beg == '$')
10949 continue;
10950
10951 /* Ignore cookies with no equal sign */
10952 if (equal == next)
10953 continue;
10954
10955 /* Now we have the cookie name between att_beg and att_end, and
10956 * its value between val_beg and val_end.
10957 */
10958
10959 if (att_end - att_beg == cookie_name_l &&
10960 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10961 /* let's return this value and indicate where to go on from */
10962 *value = val_beg;
10963 *value_l = val_end - val_beg;
10964 return next + 1;
10965 }
10966
10967 /* Set-Cookie headers only have the name in the first attr=value part */
10968 if (!list)
10969 break;
10970 }
10971
10972 return NULL;
10973}
10974
William Lallemanda43ba4e2014-01-28 18:14:25 +010010975/* Fetch a captured HTTP request header. The index is the position of
10976 * the "capture" option in the configuration file
10977 */
10978static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010979smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010980{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010981 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010982 int idx;
10983
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010984 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010985 return 0;
10986
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010987 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010988
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010989 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010990 return 0;
10991
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010992 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010993 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010994 smp->data.u.str.str = smp->strm->req_cap[idx];
10995 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010996
10997 return 1;
10998}
10999
11000/* Fetch a captured HTTP response header. The index is the position of
11001 * the "capture" option in the configuration file
11002 */
11003static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011004smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011005{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011006 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011007 int idx;
11008
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011009 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011010 return 0;
11011
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011012 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011013
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011014 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011015 return 0;
11016
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011017 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011018 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011019 smp->data.u.str.str = smp->strm->res_cap[idx];
11020 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011021
11022 return 1;
11023}
11024
William Lallemand65ad6e12014-01-31 15:08:02 +010011025/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11026static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011027smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011028{
11029 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011030 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011031 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011032
Willy Tarreau15e91e12015-04-04 00:52:09 +020011033 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011034 return 0;
11035
William Lallemand96a77852014-02-05 00:30:02 +010011036 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011037
William Lallemand96a77852014-02-05 00:30:02 +010011038 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11039 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011040
William Lallemand96a77852014-02-05 00:30:02 +010011041 temp = get_trash_chunk();
11042 temp->str = txn->uri;
11043 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011044 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011045 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011046 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011047
11048 return 1;
11049
11050}
11051
11052/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11053static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011054smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011055{
11056 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011057 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011058 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011059
Willy Tarreau15e91e12015-04-04 00:52:09 +020011060 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011061 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011062
William Lallemand65ad6e12014-01-31 15:08:02 +010011063 ptr = txn->uri;
11064
11065 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11066 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011067
William Lallemand65ad6e12014-01-31 15:08:02 +010011068 if (!*ptr)
11069 return 0;
11070
11071 ptr++; /* skip the space */
11072
11073 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011074 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011075 if (!ptr)
11076 return 0;
11077 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11078 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011079
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011080 smp->data.u.str = *temp;
11081 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011082 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011083 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011084
11085 return 1;
11086}
11087
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011088/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11089 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11090 */
11091static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011092smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011093{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011094 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011095
Willy Tarreau15e91e12015-04-04 00:52:09 +020011096 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011097 return 0;
11098
11099 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011100 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011101 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011102 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011103
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011104 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011105 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011106 smp->flags = SMP_F_CONST;
11107 return 1;
11108
11109}
11110
11111/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11112 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11113 */
11114static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011115smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011116{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011117 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011118
Willy Tarreau15e91e12015-04-04 00:52:09 +020011119 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011120 return 0;
11121
11122 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011123 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011124 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011125 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011126
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011127 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011128 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011129 smp->flags = SMP_F_CONST;
11130 return 1;
11131
11132}
11133
William Lallemand65ad6e12014-01-31 15:08:02 +010011134
Willy Tarreaue333ec92012-04-16 16:26:40 +020011135/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011136 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011137 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011138 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011139 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011140 * Accepts exactly 1 argument of type string. If the input options indicate
11141 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011142 * The returned sample is of type CSTR. Can be used to parse cookies in other
11143 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011144 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011145int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011146{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011147 struct http_txn *txn;
11148 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011149 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011150 const struct http_msg *msg;
11151 const char *hdr_name;
11152 int hdr_name_len;
11153 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011154 int occ = 0;
11155 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011156
Willy Tarreau24e32d82012-04-23 23:55:44 +020011157 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011158 return 0;
11159
Willy Tarreaua890d072013-04-02 12:01:06 +020011160 if (!ctx) {
11161 /* first call */
11162 ctx = &static_hdr_ctx;
11163 ctx->idx = 0;
11164 smp->ctx.a[2] = ctx;
11165 }
11166
Willy Tarreaue333ec92012-04-16 16:26:40 +020011167 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011168
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011169 txn = smp->strm->txn;
11170 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011171
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011172 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011173 msg = &txn->req;
11174 hdr_name = "Cookie";
11175 hdr_name_len = 6;
11176 } else {
11177 msg = &txn->rsp;
11178 hdr_name = "Set-Cookie";
11179 hdr_name_len = 10;
11180 }
11181
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011182 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011183 /* no explicit occurrence and single fetch => last cookie by default */
11184 occ = -1;
11185
11186 /* OK so basically here, either we want only one value and it's the
11187 * last one, or we want to iterate over all of them and we fetch the
11188 * next one.
11189 */
11190
Willy Tarreau9b28e032012-10-12 23:49:43 +020011191 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011192 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011193 /* search for the header from the beginning, we must first initialize
11194 * the search parameters.
11195 */
Willy Tarreau37406352012-04-23 16:16:37 +020011196 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011197 ctx->idx = 0;
11198 }
11199
Willy Tarreau28376d62012-04-26 21:26:10 +020011200 smp->flags |= SMP_F_VOL_HDR;
11201
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011202 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011203 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11204 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011205 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11206 goto out;
11207
Willy Tarreau24e32d82012-04-23 23:55:44 +020011208 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011209 continue;
11210
Willy Tarreau37406352012-04-23 16:16:37 +020011211 smp->ctx.a[0] = ctx->line + ctx->val;
11212 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011213 }
11214
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011215 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011216 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011217 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011218 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011219 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011220 &smp->data.u.str.str,
11221 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011222 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011223 found = 1;
11224 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011225 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011226 smp->flags |= SMP_F_NOT_LAST;
11227 return 1;
11228 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011229 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011230 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011231 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011232 /* all cookie headers and values were scanned. If we're looking for the
11233 * last occurrence, we may return it now.
11234 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011235 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011236 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011237 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011238}
11239
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011240/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011241 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011242 * multiple cookies may be parsed on the same line. The returned sample is of
11243 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011244 */
11245static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011246smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011247{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011248 struct http_txn *txn;
11249 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011250 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011251 const struct http_msg *msg;
11252 const char *hdr_name;
11253 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011254 int cnt;
11255 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011256 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011257
Willy Tarreau24e32d82012-04-23 23:55:44 +020011258 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011259 return 0;
11260
Willy Tarreaue333ec92012-04-16 16:26:40 +020011261 CHECK_HTTP_MESSAGE_FIRST();
11262
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011263 txn = smp->strm->txn;
11264 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011265
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011266 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011267 msg = &txn->req;
11268 hdr_name = "Cookie";
11269 hdr_name_len = 6;
11270 } else {
11271 msg = &txn->rsp;
11272 hdr_name = "Set-Cookie";
11273 hdr_name_len = 10;
11274 }
11275
Willy Tarreau9b28e032012-10-12 23:49:43 +020011276 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011277 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011278 ctx.idx = 0;
11279 cnt = 0;
11280
11281 while (1) {
11282 /* Note: val_beg == NULL every time we need to fetch a new header */
11283 if (!val_beg) {
11284 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11285 break;
11286
Willy Tarreau24e32d82012-04-23 23:55:44 +020011287 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011288 continue;
11289
11290 val_beg = ctx.line + ctx.val;
11291 val_end = val_beg + ctx.vlen;
11292 }
11293
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011294 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011295 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011296 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011297 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011298 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011299 &smp->data.u.str.str,
11300 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011301 cnt++;
11302 }
11303 }
11304
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011305 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011306 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011307 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011308 return 1;
11309}
11310
Willy Tarreau51539362012-05-08 12:46:28 +020011311/* Fetch an cookie's integer value. The integer value is returned. It
11312 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11313 */
11314static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011315smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011316{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011317 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011318
11319 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011320 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011321 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011322 }
11323
11324 return ret;
11325}
11326
Willy Tarreau8797c062007-05-07 00:55:35 +020011327/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011328/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011329/************************************************************************/
11330
David Cournapeau16023ee2010-12-23 20:55:41 +090011331/*
11332 * Given a path string and its length, find the position of beginning of the
11333 * query string. Returns NULL if no query string is found in the path.
11334 *
11335 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11336 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011337 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011338 */
bedis4c75cca2012-10-05 08:38:24 +020011339static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011340{
11341 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011342
bedis4c75cca2012-10-05 08:38:24 +020011343 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011344 return p ? p + 1 : NULL;
11345}
11346
bedis4c75cca2012-10-05 08:38:24 +020011347static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011348{
bedis4c75cca2012-10-05 08:38:24 +020011349 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011350}
11351
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011352/* after increasing a pointer value, it can exceed the first buffer
11353 * size. This function transform the value of <ptr> according with
11354 * the expected position. <chunks> is an array of the one or two
11355 * avalaible chunks. The first value is the start of the first chunk,
11356 * the second value if the end+1 of the first chunks. The third value
11357 * is NULL or the start of the second chunk and the fourth value is
11358 * the end+1 of the second chunk. The function returns 1 if does a
11359 * wrap, else returns 0.
11360 */
11361static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11362{
11363 if (*ptr < chunks[1])
11364 return 0;
11365 if (!chunks[2])
11366 return 0;
11367 *ptr = chunks[2] + ( *ptr - chunks[1] );
11368 return 1;
11369}
11370
David Cournapeau16023ee2010-12-23 20:55:41 +090011371/*
11372 * Given a url parameter, find the starting position of the first occurence,
11373 * or NULL if the parameter is not found.
11374 *
11375 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11376 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011377 *
11378 * Warning:this function returns a pointer that can be point to the first chunk
11379 * or the second chunk. The caller must be check the position before using the
11380 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011381 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011382static const char *
11383find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011384 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011385 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011386{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011387 const char *pos, *last, *equal;
11388 const char **bufs = chunks;
11389 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011390
David Cournapeau16023ee2010-12-23 20:55:41 +090011391
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011392 pos = bufs[0];
11393 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011394 while (pos <= last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011395 /* Check the equal. */
11396 equal = pos + url_param_name_l;
11397 if (fix_pointer_if_wrap(chunks, &equal)) {
11398 if (equal >= chunks[3])
11399 return NULL;
11400 } else {
11401 if (equal >= chunks[1])
11402 return NULL;
11403 }
11404 if (*equal == '=') {
11405 if (pos + url_param_name_l > last) {
11406 /* process wrap case, we detect a wrap. In this case, the
11407 * comparison is performed in two parts.
11408 */
11409
11410 /* This is the end, we dont have any other chunk. */
11411 if (bufs != chunks || !bufs[2])
11412 return NULL;
11413
11414 /* Compute the length of each part of the comparison. */
11415 l1 = last - pos;
11416 l2 = url_param_name_l - l1;
11417
11418 /* The second buffer is too short to contain the compared string. */
11419 if (bufs[2] + l2 > bufs[3])
11420 return NULL;
11421
11422 if (memcmp(pos, url_param_name, l1) == 0 &&
11423 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11424 return pos;
11425
11426 /* Perform wrapping and jump the string who fail the comparison. */
11427 bufs += 2;
11428 pos = bufs[0] + l2;
11429 last = bufs[1];
11430
11431 } else {
11432 /* process a simple comparison. */
11433 if (memcmp(pos, url_param_name, url_param_name_l) == 0) {
11434 return pos; }
11435 pos += url_param_name_l + 1;
11436 if (fix_pointer_if_wrap(chunks, &pos))
11437 last = bufs[2];
11438 }
11439 }
11440
11441 while (1) {
11442 /* Look for the next delimiter. */
11443 while (pos <= last && !is_param_delimiter(*pos, delim))
11444 pos++;
11445 if (pos < last)
11446 break;
11447 /* process buffer wrapping. */
11448 if (bufs != chunks || !bufs[2])
11449 return NULL;
11450 bufs += 2;
11451 pos = bufs[0];
11452 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011453 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011454 pos++;
11455 }
11456 return NULL;
11457}
11458
11459/*
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011460 * Given a url parameter name and a query string, returns its value and size
11461 * into *value and *value_l respectively, and returns non-zero. An empty
11462 * url_param_name matches the first available parameter. If the parameter is
11463 * not found, zero is returned and value/value_l are not touched.
David Cournapeau16023ee2010-12-23 20:55:41 +090011464 */
11465static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011466find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011467 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011468 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011469{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011470 const char *arg_start, *qs_end;
11471 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011472
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011473 arg_start = chunks[0];
11474 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011475 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011476 /* Looks for an argument name. */
11477 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011478 url_param_name, url_param_name_l,
11479 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011480 /* Check for wrapping. */
11481 if (arg_start > qs_end)
11482 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011483 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011484 if (!arg_start)
11485 return 0;
11486
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011487 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011488 while (1) {
11489 /* looks for the first argument. */
11490 value_start = memchr(arg_start, '=', qs_end - arg_start);
11491 if (!value_start) {
11492
11493 /* Check for wrapping. */
11494 if (arg_start >= chunks[0] &&
11495 arg_start <= chunks[1] &&
11496 chunks[2]) {
11497 arg_start = chunks[2];
11498 qs_end = chunks[3];
11499 continue;
11500 }
11501 return 0;
11502 }
11503 break;
11504 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011505 value_start++;
11506 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011507 else {
11508 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011509 value_start = arg_start + url_param_name_l + 1;
11510
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011511 /* Check for pointer wrapping. */
11512 if (fix_pointer_if_wrap(chunks, &value_start)) {
11513 /* Update the end pointer. */
11514 qs_end = chunks[3];
11515
11516 /* Check for overflow. */
11517 if (value_start > qs_end)
11518 return 0;
11519 }
11520 }
11521
David Cournapeau16023ee2010-12-23 20:55:41 +090011522 value_end = value_start;
11523
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011524 while (1) {
11525 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11526 value_end++;
11527 if (value_end < qs_end)
11528 break;
11529 /* process buffer wrapping. */
11530 if (value_end >= chunks[0] &&
11531 value_end <= chunks[1] &&
11532 chunks[2]) {
11533 value_end = chunks[2];
11534 qs_end = chunks[3];
11535 continue;
11536 }
11537 break;
11538 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011539
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011540 *vstart = value_start;
11541 *vend = value_end;
Willy Tarreau00134332011-01-04 14:57:34 +010011542 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011543}
11544
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011545/* This scans a URL-encoded query string. It takes an optionally wrapping
11546 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11547 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11548 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011549 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011550static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011551smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011552{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011553 const char *vstart, *vend;
11554 struct chunk *temp;
11555 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011556
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011557 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011558 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011559 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011560 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011561 return 0;
11562
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011563 /* Create sample. If the value is contiguous, return the pointer as CONST,
11564 * if the value is wrapped, copy-it in a buffer.
11565 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011566 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011567 if (chunks[2] &&
11568 vstart >= chunks[0] && vstart <= chunks[1] &&
11569 vend >= chunks[2] && vend <= chunks[3]) {
11570 /* Wrapped case. */
11571 temp = get_trash_chunk();
11572 memcpy(temp->str, vstart, chunks[1] - vstart);
11573 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011574 smp->data.u.str.str = temp->str;
11575 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011576 } else {
11577 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011578 smp->data.u.str.str = (char *)vstart;
11579 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011580 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11581 }
11582
11583 /* Update context, check wrapping. */
11584 chunks[0] = vend;
11585 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11586 chunks[1] = chunks[3];
11587 chunks[2] = NULL;
11588 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011589
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011590 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011591 smp->flags |= SMP_F_NOT_LAST;
11592
David Cournapeau16023ee2010-12-23 20:55:41 +090011593 return 1;
11594}
11595
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011596/* This function iterates over each parameter of the query string. It uses
11597 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011598 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11599 * An optional parameter name is passed in args[0], otherwise any parameter is
11600 * considered. It supports an optional delimiter argument for the beginning of
11601 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011602 */
11603static int
11604smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11605{
11606 struct http_msg *msg;
11607 char delim = '?';
11608 const char *name;
11609 int name_len;
11610
Dragan Dosen26f77e52015-05-25 10:02:11 +020011611 if (!args ||
11612 (args[0].type && args[0].type != ARGT_STR) ||
11613 (args[1].type && args[1].type != ARGT_STR))
11614 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011615
Dragan Dosen26f77e52015-05-25 10:02:11 +020011616 name = "";
11617 name_len = 0;
11618 if (args->type == ARGT_STR) {
11619 name = args->data.str.str;
11620 name_len = args->data.str.len;
11621 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011622
Dragan Dosen26f77e52015-05-25 10:02:11 +020011623 if (args[1].type)
11624 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011625
Dragan Dosen26f77e52015-05-25 10:02:11 +020011626 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011627 CHECK_HTTP_MESSAGE_FIRST();
11628
11629 msg = &smp->strm->txn->req;
11630
11631 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11632 msg->sl.rq.u_l, delim);
11633 if (!smp->ctx.a[0])
11634 return 0;
11635
11636 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011637
11638 /* Assume that the context is filled with NULL pointer
11639 * before the first call.
11640 * smp->ctx.a[2] = NULL;
11641 * smp->ctx.a[3] = NULL;
11642 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011643 }
11644
11645 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11646}
11647
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011648/* This function iterates over each parameter of the body. This requires
11649 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011650 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11651 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11652 * optional second part if the body wraps at the end of the buffer. An optional
11653 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011654 */
11655static int
11656smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11657{
11658 struct http_txn *txn = smp->strm->txn;
11659 struct http_msg *msg;
11660 unsigned long len;
11661 unsigned long block1;
11662 char *body;
11663 const char *name;
11664 int name_len;
11665
11666 if (!args || (args[0].type && args[0].type != ARGT_STR))
11667 return 0;
11668
11669 name = "";
11670 name_len = 0;
11671 if (args[0].type == ARGT_STR) {
11672 name = args[0].data.str.str;
11673 name_len = args[0].data.str.len;
11674 }
11675
11676 if (!smp->ctx.a[0]) { // first call, find the query string
11677 CHECK_HTTP_MESSAGE_FIRST();
11678
11679 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11680 msg = &txn->req;
11681 else
11682 msg = &txn->rsp;
11683
11684 len = http_body_bytes(msg);
11685 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11686
11687 block1 = len;
11688 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11689 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11690
11691 if (block1 == len) {
11692 /* buffer is not wrapped (or empty) */
11693 smp->ctx.a[0] = body;
11694 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011695
11696 /* Assume that the context is filled with NULL pointer
11697 * before the first call.
11698 * smp->ctx.a[2] = NULL;
11699 * smp->ctx.a[3] = NULL;
11700 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011701 }
11702 else {
11703 /* buffer is wrapped, we need to defragment it */
11704 smp->ctx.a[0] = body;
11705 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011706 smp->ctx.a[2] = msg->chn->buf->data;
11707 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011708 }
11709 }
11710 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11711}
11712
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011713/* Return the signed integer value for the specified url parameter (see url_param
11714 * above).
11715 */
11716static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011717smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011718{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011719 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011720
11721 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011722 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011723 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011724 }
11725
11726 return ret;
11727}
11728
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011729/* This produces a 32-bit hash of the concatenation of the first occurrence of
11730 * the Host header followed by the path component if it begins with a slash ('/').
11731 * This means that '*' will not be added, resulting in exactly the first Host
11732 * entry. If no Host header is found, then the path is used. The resulting value
11733 * is hashed using the url hash followed by a full avalanche hash and provides a
11734 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11735 * high-traffic sites without having to store whole paths.
11736 * this differs from the base32 functions in that it includes the url parameters
11737 * as well as the path
11738 */
11739static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011740smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011741{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011742 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011743 struct hdr_ctx ctx;
11744 unsigned int hash = 0;
11745 char *ptr, *beg, *end;
11746 int len;
11747
11748 CHECK_HTTP_MESSAGE_FIRST();
11749
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011750 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011751 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011752 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011753 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11754 ptr = ctx.line + ctx.val;
11755 len = ctx.vlen;
11756 while (len--)
11757 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11758 }
11759
11760 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011761 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011762 beg = http_get_path(txn);
11763 if (!beg)
11764 beg = end;
11765
11766 for (ptr = beg; ptr < end ; ptr++);
11767
11768 if (beg < ptr && *beg == '/') {
11769 while (beg < ptr)
11770 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11771 }
11772 hash = full_hash(hash);
11773
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011774 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011775 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011776 smp->flags = SMP_F_VOL_1ST;
11777 return 1;
11778}
11779
11780/* This concatenates the source address with the 32-bit hash of the Host and
11781 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11782 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11783 * on the source address length. The URL hash is stored before the address so
11784 * that in environments where IPv6 is insignificant, truncating the output to
11785 * 8 bytes would still work.
11786 */
11787static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011788smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011789{
11790 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011791 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011792 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011793
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011794 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011795 return 0;
11796
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011797 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011798 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011799
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011800 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011801 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11802 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011803
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011804 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011805 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011806 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011807 temp->len += 4;
11808 break;
11809 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011810 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011811 temp->len += 16;
11812 break;
11813 default:
11814 return 0;
11815 }
11816
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011817 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011818 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011819 return 1;
11820}
11821
Willy Tarreau185b5c42012-04-26 15:11:51 +020011822/* This function is used to validate the arguments passed to any "hdr" fetch
11823 * keyword. These keywords support an optional positive or negative occurrence
11824 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11825 * is assumed that the types are already the correct ones. Returns 0 on error,
11826 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11827 * error message in case of error, that the caller is responsible for freeing.
11828 * The initial location must either be freeable or NULL.
11829 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011830int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011831{
11832 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011833 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011834 return 0;
11835 }
11836 return 1;
11837}
11838
Willy Tarreau276fae92013-07-25 14:36:01 +020011839/* takes an UINT value on input supposed to represent the time since EPOCH,
11840 * adds an optional offset found in args[0] and emits a string representing
11841 * the date in RFC-1123/5322 format.
11842 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011843static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011844{
11845 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11846 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11847 struct chunk *temp;
11848 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011849 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011850 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011851
11852 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011853 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011854 curr_date += args[0].data.sint;
11855
11856 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011857 if (!tm)
11858 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011859
11860 temp = get_trash_chunk();
11861 temp->len = snprintf(temp->str, temp->size - temp->len,
11862 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11863 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11864 tm->tm_hour, tm->tm_min, tm->tm_sec);
11865
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011866 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011867 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011868 return 1;
11869}
11870
Thierry FOURNIERad903512014-04-11 17:51:01 +020011871/* Match language range with language tag. RFC2616 14.4:
11872 *
11873 * A language-range matches a language-tag if it exactly equals
11874 * the tag, or if it exactly equals a prefix of the tag such
11875 * that the first tag character following the prefix is "-".
11876 *
11877 * Return 1 if the strings match, else return 0.
11878 */
11879static inline int language_range_match(const char *range, int range_len,
11880 const char *tag, int tag_len)
11881{
11882 const char *end = range + range_len;
11883 const char *tend = tag + tag_len;
11884 while (range < end) {
11885 if (*range == '-' && tag == tend)
11886 return 1;
11887 if (*range != *tag || tag == tend)
11888 return 0;
11889 range++;
11890 tag++;
11891 }
11892 /* Return true only if the last char of the tag is matched. */
11893 return tag == tend;
11894}
11895
11896/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011897static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011898{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011899 const char *al = smp->data.u.str.str;
11900 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011901 const char *token;
11902 int toklen;
11903 int qvalue;
11904 const char *str;
11905 const char *w;
11906 int best_q = 0;
11907
11908 /* Set the constant to the sample, because the output of the
11909 * function will be peek in the constant configuration string.
11910 */
11911 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011912 smp->data.u.str.size = 0;
11913 smp->data.u.str.str = "";
11914 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011915
11916 /* Parse the accept language */
11917 while (1) {
11918
11919 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011920 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011921 al++;
11922 if (al >= end)
11923 break;
11924
11925 /* Start of the fisrt word. */
11926 token = al;
11927
11928 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011929 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011930 al++;
11931 if (al == token)
11932 goto expect_comma;
11933
11934 /* Length of the token. */
11935 toklen = al - token;
11936 qvalue = 1000;
11937
11938 /* Check if the token exists in the list. If the token not exists,
11939 * jump to the next token.
11940 */
11941 str = args[0].data.str.str;
11942 w = str;
11943 while (1) {
11944 if (*str == ';' || *str == '\0') {
11945 if (language_range_match(token, toklen, w, str-w))
11946 goto look_for_q;
11947 if (*str == '\0')
11948 goto expect_comma;
11949 w = str + 1;
11950 }
11951 str++;
11952 }
11953 goto expect_comma;
11954
11955look_for_q:
11956
11957 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011958 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011959 al++;
11960 if (al >= end)
11961 goto process_value;
11962
11963 /* If ',' is found, process the result */
11964 if (*al == ',')
11965 goto process_value;
11966
11967 /* If the character is different from ';', look
11968 * for the end of the header part in best effort.
11969 */
11970 if (*al != ';')
11971 goto expect_comma;
11972
11973 /* Assumes that the char is ';', now expect "q=". */
11974 al++;
11975
11976 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011977 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011978 al++;
11979 if (al >= end)
11980 goto process_value;
11981
11982 /* Expect 'q'. If no 'q', continue in best effort */
11983 if (*al != 'q')
11984 goto process_value;
11985 al++;
11986
11987 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011988 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011989 al++;
11990 if (al >= end)
11991 goto process_value;
11992
11993 /* Expect '='. If no '=', continue in best effort */
11994 if (*al != '=')
11995 goto process_value;
11996 al++;
11997
11998 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011999 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012000 al++;
12001 if (al >= end)
12002 goto process_value;
12003
12004 /* Parse the q value. */
12005 qvalue = parse_qvalue(al, &al);
12006
12007process_value:
12008
12009 /* If the new q value is the best q value, then store the associated
12010 * language in the response. If qvalue is the biggest value (1000),
12011 * break the process.
12012 */
12013 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012014 smp->data.u.str.str = (char *)w;
12015 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012016 if (qvalue >= 1000)
12017 break;
12018 best_q = qvalue;
12019 }
12020
12021expect_comma:
12022
12023 /* Expect comma or end. If the end is detected, quit the loop. */
12024 while (al < end && *al != ',')
12025 al++;
12026 if (al >= end)
12027 break;
12028
12029 /* Comma is found, jump it and restart the analyzer. */
12030 al++;
12031 }
12032
12033 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012034 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12035 smp->data.u.str.str = args[1].data.str.str;
12036 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012037 }
12038
12039 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012040 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012041}
12042
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012043/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012044static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012045{
12046 /* If the constant flag is set or if not size is avalaible at
12047 * the end of the buffer, copy the string in other buffer
12048 * before decoding.
12049 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012050 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012051 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012052 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12053 smp->data.u.str.str = str->str;
12054 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012055 smp->flags &= ~SMP_F_CONST;
12056 }
12057
12058 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012059 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12060 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012061 return 1;
12062}
12063
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012064static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12065{
12066 struct proxy *fe = strm_fe(smp->strm);
12067 int idx, i;
12068 struct cap_hdr *hdr;
12069 int len;
12070
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012071 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012072 return 0;
12073
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012074 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012075
12076 /* Check the availibity of the capture id. */
12077 if (idx > fe->nb_req_cap - 1)
12078 return 0;
12079
12080 /* Look for the original configuration. */
12081 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12082 hdr != NULL && i != idx ;
12083 i--, hdr = hdr->next);
12084 if (!hdr)
12085 return 0;
12086
12087 /* check for the memory allocation */
12088 if (smp->strm->req_cap[hdr->index] == NULL)
12089 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12090 if (smp->strm->req_cap[hdr->index] == NULL)
12091 return 0;
12092
12093 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012094 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012095 if (len > hdr->len)
12096 len = hdr->len;
12097
12098 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012099 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012100 smp->strm->req_cap[idx][len] = '\0';
12101
12102 return 1;
12103}
12104
12105static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12106{
12107 struct proxy *fe = strm_fe(smp->strm);
12108 int idx, i;
12109 struct cap_hdr *hdr;
12110 int len;
12111
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012112 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012113 return 0;
12114
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012115 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012116
12117 /* Check the availibity of the capture id. */
12118 if (idx > fe->nb_rsp_cap - 1)
12119 return 0;
12120
12121 /* Look for the original configuration. */
12122 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12123 hdr != NULL && i != idx ;
12124 i--, hdr = hdr->next);
12125 if (!hdr)
12126 return 0;
12127
12128 /* check for the memory allocation */
12129 if (smp->strm->res_cap[hdr->index] == NULL)
12130 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12131 if (smp->strm->res_cap[hdr->index] == NULL)
12132 return 0;
12133
12134 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012135 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012136 if (len > hdr->len)
12137 len = hdr->len;
12138
12139 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012140 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012141 smp->strm->res_cap[idx][len] = '\0';
12142
12143 return 1;
12144}
12145
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012146/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012147 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012148 * the relevant part of the request line accordingly. Then it updates various
12149 * pointers to the next elements which were moved, and the total buffer length.
12150 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012151 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12152 * error, though this can be revisited when this code is finally exploited.
12153 *
12154 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12155 * query string and 3 to replace uri.
12156 *
12157 * In query string case, the mark question '?' must be set at the start of the
12158 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012159 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012160int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012161 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012162{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012163 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012164 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012165 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012166 int delta;
12167
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012168 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012169 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012170 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012171 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12172
12173 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012174 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012175 txn->req.sl.rq.m_l += delta;
12176 txn->req.sl.rq.u += delta;
12177 txn->req.sl.rq.v += delta;
12178 break;
12179
12180 case 1: // path
12181 cur_ptr = http_get_path(txn);
12182 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012183 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012184
12185 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012186 while (cur_end < s->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012187 cur_end++;
12188
12189 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012190 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012191 txn->req.sl.rq.u_l += delta;
12192 txn->req.sl.rq.v += delta;
12193 break;
12194
12195 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012196 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012197 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012198 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12199 while (cur_ptr < cur_end && *cur_ptr != '?')
12200 cur_ptr++;
12201
12202 /* skip the question mark or indicate that we must insert it
12203 * (but only if the format string is not empty then).
12204 */
12205 if (cur_ptr < cur_end)
12206 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012207 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012208 offset = 0;
12209
12210 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012211 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012212 txn->req.sl.rq.u_l += delta;
12213 txn->req.sl.rq.v += delta;
12214 break;
12215
12216 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012217 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012218 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12219
12220 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012221 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012222 txn->req.sl.rq.u_l += delta;
12223 txn->req.sl.rq.v += delta;
12224 break;
12225
12226 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012227 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012228 }
12229
12230 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012231 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012232 txn->req.sl.rq.l += delta;
12233 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012234 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012235 return 0;
12236}
12237
12238/* This function executes one of the set-{method,path,query,uri} actions. It
12239 * builds a string in the trash from the specified format string. It finds
12240 * the action to be performed in p[2], previously filled by function
12241 * parse_set_req_line(). The replacement action is excuted by the function
12242 * http_action_set_req_line_exec(). It always returns 1. If an error occurs
12243 * the action is canceled, but the rule processing continue.
12244 */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +020012245int http_action_set_req_line(struct act_rule *rule, struct proxy *px,
12246 struct session *sess, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012247{
12248 chunk_reset(&trash);
12249
12250 /* If we have to create a query string, prepare a '?'. */
12251 if (*(int *)&rule->arg.act.p[2] == 2)
12252 trash.str[trash.len++] = '?';
12253 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
12254
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012255 http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s);
Thierry FOURNIER01c30122015-03-14 14:14:47 +010012256 return 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012257}
12258
12259/* parse an http-request action among :
12260 * set-method
12261 * set-path
12262 * set-query
12263 * set-uri
12264 *
12265 * All of them accept a single argument of type string representing a log-format.
12266 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12267 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
12268 * It returns 0 on success, < 0 on error.
12269 */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +020012270int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012271{
12272 int cur_arg = *orig_arg;
12273
12274 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
12275
12276 switch (args[0][4]) {
12277 case 'm' :
12278 *(int *)&rule->arg.act.p[2] = 0;
12279 rule->action_ptr = http_action_set_req_line;
12280 break;
12281 case 'p' :
12282 *(int *)&rule->arg.act.p[2] = 1;
12283 rule->action_ptr = http_action_set_req_line;
12284 break;
12285 case 'q' :
12286 *(int *)&rule->arg.act.p[2] = 2;
12287 rule->action_ptr = http_action_set_req_line;
12288 break;
12289 case 'u' :
12290 *(int *)&rule->arg.act.p[2] = 3;
12291 rule->action_ptr = http_action_set_req_line;
12292 break;
12293 default:
12294 memprintf(err, "internal error: unhandled action '%s'", args[0]);
12295 return -1;
12296 }
12297
12298 if (!*args[cur_arg] ||
12299 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12300 memprintf(err, "expects exactly 1 argument <format>");
12301 return -1;
12302 }
12303
12304 LIST_INIT((struct list *)&rule->arg.act.p[0]);
12305 proxy->conf.args.ctx = ARGC_HRQ;
12306 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
12307 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12308 proxy->conf.args.file, proxy->conf.args.line);
12309
12310 (*orig_arg)++;
12311 return 0;
12312}
12313
Willy Tarreaua9083d02015-05-08 15:27:59 +020012314/* This function executes the "capture" action. It executes a fetch expression,
12315 * turns the result into a string and puts it in a capture slot. It always
12316 * returns 1. If an error occurs the action is cancelled, but the rule
12317 * processing continues.
12318 */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +020012319int http_action_req_capture(struct act_rule *rule, struct proxy *px,
12320 struct session *sess, struct stream *s)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012321{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012322 struct sample *key;
12323 struct sample_expr *expr = rule->arg.act.p[0];
12324 struct cap_hdr *h = rule->arg.act.p[1];
12325 char **cap = s->req_cap;
12326 int len;
12327
Adis Nezirovic79beb242015-07-06 15:41:02 +020012328 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, expr, SMP_T_STR);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012329 if (!key)
12330 return 1;
12331
12332 if (cap[h->index] == NULL)
12333 cap[h->index] = pool_alloc2(h->pool);
12334
12335 if (cap[h->index] == NULL) /* no more capture memory */
12336 return 1;
12337
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012338 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012339 if (len > h->len)
12340 len = h->len;
12341
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012342 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012343 cap[h->index][len] = 0;
12344 return 1;
12345}
12346
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012347/* This function executes the "capture" action and store the result in a
12348 * capture slot if exists. It executes a fetch expression, turns the result
12349 * into a string and puts it in a capture slot. It always returns 1. If an
12350 * error occurs the action is cancelled, but the rule processing continues.
12351 */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +020012352int http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
12353 struct session *sess, struct stream *s)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012354{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012355 struct sample *key;
12356 struct sample_expr *expr = rule->arg.act.p[0];
12357 struct cap_hdr *h;
12358 int idx = (long)rule->arg.act.p[1];
12359 char **cap = s->req_cap;
12360 struct proxy *fe = strm_fe(s);
12361 int len;
12362 int i;
12363
12364 /* Look for the original configuration. */
12365 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
12366 h != NULL && i != idx ;
12367 i--, h = h->next);
12368 if (!h)
12369 return 1;
12370
Adis Nezirovic79beb242015-07-06 15:41:02 +020012371 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, expr, SMP_T_STR);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012372 if (!key)
12373 return 1;
12374
12375 if (cap[h->index] == NULL)
12376 cap[h->index] = pool_alloc2(h->pool);
12377
12378 if (cap[h->index] == NULL) /* no more capture memory */
12379 return 1;
12380
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012381 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012382 if (len > h->len)
12383 len = h->len;
12384
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012385 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012386 cap[h->index][len] = 0;
12387 return 1;
12388}
12389
Willy Tarreaua9083d02015-05-08 15:27:59 +020012390/* parse an "http-request capture" action. It takes a single argument which is
12391 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012392 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Willy Tarreaua9083d02015-05-08 15:27:59 +020012393 * It returns 0 on success, < 0 on error.
12394 */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +020012395int parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012396{
12397 struct sample_expr *expr;
12398 struct cap_hdr *hdr;
12399 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012400 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012401
12402 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12403 if (strcmp(args[cur_arg], "if") == 0 ||
12404 strcmp(args[cur_arg], "unless") == 0)
12405 break;
12406
12407 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012408 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Willy Tarreaua9083d02015-05-08 15:27:59 +020012409 return -1;
12410 }
12411
Willy Tarreaua9083d02015-05-08 15:27:59 +020012412 cur_arg = *orig_arg;
12413 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12414 if (!expr)
12415 return -1;
12416
12417 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12418 memprintf(err,
12419 "fetch method '%s' extracts information from '%s', none of which is available here",
12420 args[cur_arg-1], sample_src_names(expr->fetch->use));
12421 free(expr);
12422 return -1;
12423 }
12424
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012425 if (!args[cur_arg] || !*args[cur_arg]) {
12426 memprintf(err, "expects 'len or 'id'");
12427 free(expr);
12428 return -1;
12429 }
12430
Willy Tarreaua9083d02015-05-08 15:27:59 +020012431 if (strcmp(args[cur_arg], "len") == 0) {
12432 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012433
12434 if (!(px->cap & PR_CAP_FE)) {
12435 memprintf(err, "proxy '%s' has no frontend capability", px->id);
12436 return -1;
12437 }
12438
12439 proxy->conf.args.ctx = ARGC_CAP;
12440
Willy Tarreaua9083d02015-05-08 15:27:59 +020012441 if (!args[cur_arg]) {
12442 memprintf(err, "missing length value");
12443 free(expr);
12444 return -1;
12445 }
12446 /* we copy the table name for now, it will be resolved later */
12447 len = atoi(args[cur_arg]);
12448 if (len <= 0) {
12449 memprintf(err, "length must be > 0");
12450 free(expr);
12451 return -1;
12452 }
12453 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012454
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012455 if (!len) {
12456 memprintf(err, "a positive 'len' argument is mandatory");
12457 free(expr);
12458 return -1;
12459 }
12460
12461 hdr = calloc(sizeof(struct cap_hdr), 1);
12462 hdr->next = px->req_cap;
12463 hdr->name = NULL; /* not a header capture */
12464 hdr->namelen = 0;
12465 hdr->len = len;
12466 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12467 hdr->index = px->nb_req_cap++;
12468
12469 px->req_cap = hdr;
12470 px->to_log |= LW_REQHDR;
12471
12472 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
12473 rule->action_ptr = http_action_req_capture;
12474 rule->arg.act.p[0] = expr;
12475 rule->arg.act.p[1] = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012476 }
12477
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012478 else if (strcmp(args[cur_arg], "id") == 0) {
12479 int id;
12480 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012481
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012482 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012483
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012484 if (!args[cur_arg]) {
12485 memprintf(err, "missing id value");
12486 free(expr);
12487 return -1;
12488 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012489
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012490 id = strtol(args[cur_arg], &error, 10);
12491 if (*error != '\0') {
12492 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12493 free(expr);
12494 return -1;
12495 }
12496 cur_arg++;
12497
12498 proxy->conf.args.ctx = ARGC_CAP;
12499
12500 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
12501 rule->action_ptr = http_action_req_capture_by_id;
12502 rule->arg.act.p[0] = expr;
12503 rule->arg.act.p[1] = (void *)(long)id;
12504 }
12505
12506 else {
12507 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12508 free(expr);
12509 return -1;
12510 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012511
12512 *orig_arg = cur_arg;
12513 return 0;
12514}
12515
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012516/* This function executes the "capture" action and store the result in a
12517 * capture slot if exists. It executes a fetch expression, turns the result
12518 * into a string and puts it in a capture slot. It always returns 1. If an
12519 * error occurs the action is cancelled, but the rule processing continues.
12520 */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +020012521int http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
12522 struct session *sess, struct stream *s)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012523{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012524 struct sample *key;
12525 struct sample_expr *expr = rule->arg.act.p[0];
12526 struct cap_hdr *h;
12527 int idx = (long)rule->arg.act.p[1];
12528 char **cap = s->res_cap;
12529 struct proxy *fe = strm_fe(s);
12530 int len;
12531 int i;
12532
12533 /* Look for the original configuration. */
12534 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12535 h != NULL && i != idx ;
12536 i--, h = h->next);
12537 if (!h)
12538 return 1;
12539
Adis Nezirovic79beb242015-07-06 15:41:02 +020012540 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, expr, SMP_T_STR);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012541 if (!key)
12542 return 1;
12543
12544 if (cap[h->index] == NULL)
12545 cap[h->index] = pool_alloc2(h->pool);
12546
12547 if (cap[h->index] == NULL) /* no more capture memory */
12548 return 1;
12549
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012550 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012551 if (len > h->len)
12552 len = h->len;
12553
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012554 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012555 cap[h->index][len] = 0;
12556 return 1;
12557}
12558
12559/* parse an "http-response capture" action. It takes a single argument which is
12560 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12561 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
12562 * It returns 0 on success, < 0 on error.
12563 */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +020012564int parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012565{
12566 struct sample_expr *expr;
12567 int cur_arg;
12568 int id;
12569 char *error;
12570
12571 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12572 if (strcmp(args[cur_arg], "if") == 0 ||
12573 strcmp(args[cur_arg], "unless") == 0)
12574 break;
12575
12576 if (cur_arg < *orig_arg + 3) {
12577 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
12578 return -1;
12579 }
12580
12581 cur_arg = *orig_arg;
12582 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12583 if (!expr)
12584 return -1;
12585
12586 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12587 memprintf(err,
12588 "fetch method '%s' extracts information from '%s', none of which is available here",
12589 args[cur_arg-1], sample_src_names(expr->fetch->use));
12590 free(expr);
12591 return -1;
12592 }
12593
12594 if (!args[cur_arg] || !*args[cur_arg]) {
12595 memprintf(err, "expects 'len or 'id'");
12596 free(expr);
12597 return -1;
12598 }
12599
12600 if (strcmp(args[cur_arg], "id") != 0) {
12601 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12602 free(expr);
12603 return -1;
12604 }
12605
12606 cur_arg++;
12607
12608 if (!args[cur_arg]) {
12609 memprintf(err, "missing id value");
12610 free(expr);
12611 return -1;
12612 }
12613
12614 id = strtol(args[cur_arg], &error, 10);
12615 if (*error != '\0') {
12616 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12617 free(expr);
12618 return -1;
12619 }
12620 cur_arg++;
12621
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012622 proxy->conf.args.ctx = ARGC_CAP;
12623
12624 rule->action = HTTP_RES_ACT_CUSTOM_CONT;
12625 rule->action_ptr = http_action_res_capture_by_id;
12626 rule->arg.act.p[0] = expr;
12627 rule->arg.act.p[1] = (void *)(long)id;
12628
12629 *orig_arg = cur_arg;
12630 return 0;
12631}
12632
William Lallemand73025dd2014-04-24 14:38:37 +020012633/*
12634 * Return the struct http_req_action_kw associated to a keyword.
12635 */
12636struct http_req_action_kw *action_http_req_custom(const char *kw)
12637{
12638 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
12639 struct http_req_action_kw_list *kw_list;
12640 int i;
12641
12642 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
12643 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
Thierry FOURNIER0e118632015-06-04 11:44:06 +020012644 if (kw_list->kw[i].match_pfx &&
12645 strncmp(kw, kw_list->kw[i].kw, strlen(kw_list->kw[i].kw)) == 0)
12646 return &kw_list->kw[i];
William Lallemand73025dd2014-04-24 14:38:37 +020012647 if (!strcmp(kw, kw_list->kw[i].kw))
12648 return &kw_list->kw[i];
12649 }
12650 }
12651 }
12652 return NULL;
12653}
12654
12655/*
12656 * Return the struct http_res_action_kw associated to a keyword.
12657 */
12658struct http_res_action_kw *action_http_res_custom(const char *kw)
12659{
12660 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
12661 struct http_res_action_kw_list *kw_list;
12662 int i;
12663
12664 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
12665 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
Thierry FOURNIER0e118632015-06-04 11:44:06 +020012666 if (kw_list->kw[i].match_pfx &&
12667 strncmp(kw, kw_list->kw[i].kw, strlen(kw_list->kw[i].kw)) == 0)
12668 return &kw_list->kw[i];
William Lallemand73025dd2014-04-24 14:38:37 +020012669 if (!strcmp(kw, kw_list->kw[i].kw))
12670 return &kw_list->kw[i];
12671 }
12672 }
12673 }
12674 return NULL;
12675}
12676
Willy Tarreau4a568972010-05-12 08:08:50 +020012677/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012678/* All supported ACL keywords must be declared here. */
12679/************************************************************************/
12680
12681/* Note: must not be declared <const> as its list will be overwritten.
12682 * Please take care of keeping this list alphabetically sorted.
12683 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012684static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012685 { "base", "base", PAT_MATCH_STR },
12686 { "base_beg", "base", PAT_MATCH_BEG },
12687 { "base_dir", "base", PAT_MATCH_DIR },
12688 { "base_dom", "base", PAT_MATCH_DOM },
12689 { "base_end", "base", PAT_MATCH_END },
12690 { "base_len", "base", PAT_MATCH_LEN },
12691 { "base_reg", "base", PAT_MATCH_REG },
12692 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012693
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012694 { "cook", "req.cook", PAT_MATCH_STR },
12695 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12696 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12697 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12698 { "cook_end", "req.cook", PAT_MATCH_END },
12699 { "cook_len", "req.cook", PAT_MATCH_LEN },
12700 { "cook_reg", "req.cook", PAT_MATCH_REG },
12701 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012702
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012703 { "hdr", "req.hdr", PAT_MATCH_STR },
12704 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12705 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12706 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12707 { "hdr_end", "req.hdr", PAT_MATCH_END },
12708 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12709 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12710 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012711
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012712 /* these two declarations uses strings with list storage (in place
12713 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12714 * and delete functions are relative to the list management. The parse
12715 * and match method are related to the corresponding fetch methods. This
12716 * is very particular ACL declaration mode.
12717 */
12718 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12719 { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012720
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012721 { "path", "path", PAT_MATCH_STR },
12722 { "path_beg", "path", PAT_MATCH_BEG },
12723 { "path_dir", "path", PAT_MATCH_DIR },
12724 { "path_dom", "path", PAT_MATCH_DOM },
12725 { "path_end", "path", PAT_MATCH_END },
12726 { "path_len", "path", PAT_MATCH_LEN },
12727 { "path_reg", "path", PAT_MATCH_REG },
12728 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012729
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012730 { "req_ver", "req.ver", PAT_MATCH_STR },
12731 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012732
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012733 { "scook", "res.cook", PAT_MATCH_STR },
12734 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12735 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12736 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12737 { "scook_end", "res.cook", PAT_MATCH_END },
12738 { "scook_len", "res.cook", PAT_MATCH_LEN },
12739 { "scook_reg", "res.cook", PAT_MATCH_REG },
12740 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012741
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012742 { "shdr", "res.hdr", PAT_MATCH_STR },
12743 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12744 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12745 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12746 { "shdr_end", "res.hdr", PAT_MATCH_END },
12747 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12748 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12749 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012750
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012751 { "url", "url", PAT_MATCH_STR },
12752 { "url_beg", "url", PAT_MATCH_BEG },
12753 { "url_dir", "url", PAT_MATCH_DIR },
12754 { "url_dom", "url", PAT_MATCH_DOM },
12755 { "url_end", "url", PAT_MATCH_END },
12756 { "url_len", "url", PAT_MATCH_LEN },
12757 { "url_reg", "url", PAT_MATCH_REG },
12758 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012759
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012760 { "urlp", "urlp", PAT_MATCH_STR },
12761 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12762 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12763 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12764 { "urlp_end", "urlp", PAT_MATCH_END },
12765 { "urlp_len", "urlp", PAT_MATCH_LEN },
12766 { "urlp_reg", "urlp", PAT_MATCH_REG },
12767 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012768
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012769 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012770}};
12771
12772/************************************************************************/
12773/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012774/************************************************************************/
12775/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012776static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012777 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012778 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012779 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12780
Willy Tarreau87b09662015-04-03 00:22:06 +020012781 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012782 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012783
12784 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012785 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12786 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12787 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012788
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012789 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12790 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012791
Willy Tarreau409bcde2013-01-08 00:31:00 +010012792 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12793 * are only here to match the ACL's name, are request-only and are used
12794 * for ACL compatibility only.
12795 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012796 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12797 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012798 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12799 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012800
12801 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12802 * only here to match the ACL's name, are request-only and are used for
12803 * ACL compatibility only.
12804 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012805 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012806 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012807 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012808 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012809
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012810 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012811 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012812 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012813 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012814 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012815 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012816
12817 /* HTTP protocol on the request path */
12818 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012819 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012820
12821 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012822 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12823 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012824
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012825 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012826 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12827 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012828 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012829
Willy Tarreau18ed2562013-01-14 15:56:36 +010012830 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012831 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12832 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012833
Willy Tarreau18ed2562013-01-14 15:56:36 +010012834 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012835 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012836 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12837 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012838
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012839 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012840 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012841 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012842 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012843 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
Willy Tarreaueb27ec72015-02-20 13:55:29 +010012844 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012845 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012846
12847 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012848 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012849 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12850 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012851
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012852 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012853 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012854 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012855 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012856 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
Willy Tarreaueb27ec72015-02-20 13:55:29 +010012857 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012858 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012859
Willy Tarreau409bcde2013-01-08 00:31:00 +010012860 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012861 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012862 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12863 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012864 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012865
12866 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012867 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012868 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012869 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012870 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012871
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012872 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012873 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012874 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012875 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012876 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012877 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012878 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12879 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012880 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012881 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012882}};
12883
Willy Tarreau8797c062007-05-07 00:55:35 +020012884
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012885/************************************************************************/
12886/* All supported converter keywords must be declared here. */
12887/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012888/* Note: must not be declared <const> as its list will be overwritten */
12889static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012890 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012891 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012892 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12893 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012894 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012895 { NULL, NULL, 0, 0, 0 },
12896}};
12897
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012898
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012899/************************************************************************/
12900/* All supported http-request action keywords must be declared here. */
12901/************************************************************************/
12902struct http_req_action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012903 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012904 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012905 { "set-method", parse_set_req_line },
12906 { "set-path", parse_set_req_line },
12907 { "set-query", parse_set_req_line },
12908 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012909 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012910 }
12911};
12912
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012913struct http_res_action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012914 .kw = {
12915 { "capture", parse_http_res_capture },
12916 { NULL, NULL }
12917 }
12918};
12919
Willy Tarreau8797c062007-05-07 00:55:35 +020012920__attribute__((constructor))
12921static void __http_protocol_init(void)
12922{
12923 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012924 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012925 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012926 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012927 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012928}
12929
12930
Willy Tarreau58f10d72006-12-04 02:26:12 +010012931/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012932 * Local variables:
12933 * c-indent-level: 8
12934 * c-basic-offset: 8
12935 * End:
12936 */