blob: 01ee439eb2e48b51f433c33dfcd9064c3b6ab007 [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
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020064#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.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 Tarreau9fbe18e2015-05-01 22:42:08 +02003296 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0 && msg->msg_state < HTTP_MSG_BODY)) {
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 */
3299 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003300 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003301 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003302 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003303 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3304 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003305 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003306 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003307 else {
3308 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003309 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003310 }
3311
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003312 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003313 return 1;
3314}
3315
Lukas Tribus67db8df2013-06-23 17:37:13 +02003316/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3317 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3318 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003319void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003320{
3321#ifdef IP_TOS
3322 if (from.ss_family == AF_INET)
3323 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3324#endif
3325#ifdef IPV6_TCLASS
3326 if (from.ss_family == AF_INET6) {
3327 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3328 /* v4-mapped addresses need IP_TOS */
3329 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3330 else
3331 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3332 }
3333#endif
3334}
3335
Willy Tarreau87b09662015-04-03 00:22:06 +02003336int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003337 const char* name, unsigned int name_len,
3338 const char *str, struct my_regex *re,
3339 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003340{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003341 struct hdr_ctx ctx;
3342 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003343 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003344 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3345 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003346 struct chunk *output = get_trash_chunk();
3347
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003348 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003349
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003350 /* Choose the header browsing function. */
3351 switch (action) {
3352 case HTTP_REQ_ACT_REPLACE_VAL:
3353 case HTTP_RES_ACT_REPLACE_VAL:
3354 http_find_hdr_func = http_find_header2;
3355 break;
3356 case HTTP_REQ_ACT_REPLACE_HDR:
3357 case HTTP_RES_ACT_REPLACE_HDR:
3358 http_find_hdr_func = http_find_full_header2;
3359 break;
3360 default: /* impossible */
3361 return -1;
3362 }
3363
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003364 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3365 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003366 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003367 char *val = ctx.line + ctx.val;
3368 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003369
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003370 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3371 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003372
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003373 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003374 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003375 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003376
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003377 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003378
3379 hdr->len += delta;
3380 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003381
3382 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003383 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003384 }
3385
3386 return 0;
3387}
3388
Willy Tarreau87b09662015-04-03 00:22:06 +02003389static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003390 const char* name, unsigned int name_len,
3391 struct list *fmt, struct my_regex *re,
3392 int action)
3393{
3394 struct chunk *replace = get_trash_chunk();
3395
3396 replace->len = build_logline(s, replace->str, replace->size, fmt);
3397 if (replace->len >= replace->size - 1)
3398 return -1;
3399
3400 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3401}
3402
Willy Tarreau87b09662015-04-03 00:22:06 +02003403/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003404 * transaction <txn>. Returns the verdict of the first rule that prevents
3405 * further processing of the request (auth, deny, ...), and defaults to
3406 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3407 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3408 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003409 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003410enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003411http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003412{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003413 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003414 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003415 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003416 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003417 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003418 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003419
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003420 /* If "the current_rule_list" match the executed rule list, we are in
3421 * resume condition. If a resume is needed it is always in the action
3422 * and never in the ACL or converters. In this case, we initialise the
3423 * current rule, and go to the action execution point.
3424 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003425 if (s->current_rule) {
3426 rule = s->current_rule;
3427 s->current_rule = NULL;
3428 if (s->current_rule_list == rules)
3429 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003430 }
3431 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003432
Willy Tarreauff011f22011-01-06 17:51:27 +01003433 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003434 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003435 continue;
3436
Willy Tarreau96257ec2012-12-27 10:46:37 +01003437 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003438 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003439 int ret;
3440
Willy Tarreau192252e2015-04-04 01:47:55 +02003441 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003442 ret = acl_pass(ret);
3443
Willy Tarreauff011f22011-01-06 17:51:27 +01003444 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003445 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003446
3447 if (!ret) /* condition not matched */
3448 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003449 }
3450
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003451resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003452 switch (rule->action) {
3453 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003454 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003455
3456 case HTTP_REQ_ACT_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003457 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003458 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003459
Willy Tarreauccbcc372012-12-27 12:37:57 +01003460 case HTTP_REQ_ACT_TARPIT:
3461 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003462 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003463 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003464
Willy Tarreau96257ec2012-12-27 10:46:37 +01003465 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003466 /* Auth might be performed on regular http-req rules as well as on stats */
3467 auth_realm = rule->arg.auth.realm;
3468 if (!auth_realm) {
3469 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3470 auth_realm = STATS_DEFAULT_REALM;
3471 else
3472 auth_realm = px->id;
3473 }
3474 /* send 401/407 depending on whether we use a proxy or not. We still
3475 * count one error, because normal browsing won't significantly
3476 * increase the counter but brute force attempts will.
3477 */
3478 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3479 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3480 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003481 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003482 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003483
Willy Tarreau81499eb2012-12-27 12:19:02 +01003484 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003485 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3486 return HTTP_RULE_RES_BADREQ;
3487 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003488
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003489 case HTTP_REQ_ACT_SET_NICE:
3490 s->task->nice = rule->arg.nice;
3491 break;
3492
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003493 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003494 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003495 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003496 break;
3497
Willy Tarreau51347ed2013-06-11 19:34:13 +02003498 case HTTP_REQ_ACT_SET_MARK:
3499#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003500 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003501 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003502#endif
3503 break;
3504
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003505 case HTTP_REQ_ACT_SET_LOGL:
3506 s->logs.level = rule->arg.loglevel;
3507 break;
3508
Sasha Pachev218f0642014-06-16 12:05:59 -06003509 case HTTP_REQ_ACT_REPLACE_HDR:
3510 case HTTP_REQ_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003511 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3512 rule->arg.hdr_add.name_len,
3513 &rule->arg.hdr_add.fmt,
3514 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003515 return HTTP_RULE_RES_BADREQ;
3516 break;
3517
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003518 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003519 ctx.idx = 0;
3520 /* remove all occurrences of the header */
3521 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3522 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3523 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003524 }
Willy Tarreau85603282015-01-21 20:39:27 +01003525 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003526
Willy Tarreau85603282015-01-21 20:39:27 +01003527 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003528 case HTTP_REQ_ACT_ADD_HDR:
3529 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3530 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3531 trash.len = rule->arg.hdr_add.name_len;
3532 trash.str[trash.len++] = ':';
3533 trash.str[trash.len++] = ' ';
3534 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 +01003535
3536 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3537 /* remove all occurrences of the header */
3538 ctx.idx = 0;
3539 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3540 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3541 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3542 }
3543 }
3544
Willy Tarreau96257ec2012-12-27 10:46:37 +01003545 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3546 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003547
3548 case HTTP_REQ_ACT_DEL_ACL:
3549 case HTTP_REQ_ACT_DEL_MAP: {
3550 struct pat_ref *ref;
3551 char *key;
3552 int len;
3553
3554 /* collect reference */
3555 ref = pat_ref_lookup(rule->arg.map.ref);
3556 if (!ref)
3557 continue;
3558
3559 /* collect key */
3560 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3561 key = trash.str;
3562 key[len] = '\0';
3563
3564 /* perform update */
3565 /* returned code: 1=ok, 0=ko */
3566 pat_ref_delete(ref, key);
3567
3568 break;
3569 }
3570
3571 case HTTP_REQ_ACT_ADD_ACL: {
3572 struct pat_ref *ref;
3573 char *key;
3574 struct chunk *trash_key;
3575 int len;
3576
3577 trash_key = get_trash_chunk();
3578
3579 /* collect reference */
3580 ref = pat_ref_lookup(rule->arg.map.ref);
3581 if (!ref)
3582 continue;
3583
3584 /* collect key */
3585 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3586 key = trash_key->str;
3587 key[len] = '\0';
3588
3589 /* perform update */
3590 /* add entry only if it does not already exist */
3591 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003592 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003593
3594 break;
3595 }
3596
3597 case HTTP_REQ_ACT_SET_MAP: {
3598 struct pat_ref *ref;
3599 char *key, *value;
3600 struct chunk *trash_key, *trash_value;
3601 int len;
3602
3603 trash_key = get_trash_chunk();
3604 trash_value = get_trash_chunk();
3605
3606 /* collect reference */
3607 ref = pat_ref_lookup(rule->arg.map.ref);
3608 if (!ref)
3609 continue;
3610
3611 /* collect key */
3612 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3613 key = trash_key->str;
3614 key[len] = '\0';
3615
3616 /* collect value */
3617 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3618 value = trash_value->str;
3619 value[len] = '\0';
3620
3621 /* perform update */
3622 if (pat_ref_find_elt(ref, key) != NULL)
3623 /* update entry if it exists */
3624 pat_ref_set(ref, key, value, NULL);
3625 else
3626 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003627 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003628
3629 break;
3630 }
William Lallemand73025dd2014-04-24 14:38:37 +02003631
3632 case HTTP_REQ_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003633 if (!rule->action_ptr(rule, px, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003634 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003635 return HTTP_RULE_RES_YIELD;
3636 }
William Lallemand73025dd2014-04-24 14:38:37 +02003637 break;
3638
3639 case HTTP_REQ_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003640 rule->action_ptr(rule, px, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003641 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003642
3643 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3644 /* Note: only the first valid tracking parameter of each
3645 * applies.
3646 */
3647
3648 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3649 struct stktable *t;
3650 struct stksess *ts;
3651 struct stktable_key *key;
3652 void *ptr;
3653
3654 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau192252e2015-04-04 01:47:55 +02003655 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 +02003656
3657 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003658 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003659
3660 /* let's count a new HTTP request as it's the first time we do it */
3661 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3662 if (ptr)
3663 stktable_data_cast(ptr, http_req_cnt)++;
3664
3665 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3666 if (ptr)
3667 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3668 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3669
3670 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003671 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003672 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3673 }
3674 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003675 }
3676 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003677
3678 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003679 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003680}
3681
Willy Tarreau71241ab2012-12-27 11:30:54 +01003682
Willy Tarreau87b09662015-04-03 00:22:06 +02003683/* Executes the http-response rules <rules> for stream <s>, proxy <px> and
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003684 * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD
3685 * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the
3686 * evaluation of next rule list. If *STOP is returned, the process must stop
3687 * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny
3688 * rule. If *YIELD is returned, the czller must call again the function with
3689 * the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003690 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003691static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003692http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003693{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003694 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003695 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003696 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003697 struct http_res_rule *rule;
3698 struct hdr_ctx ctx;
3699
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003700 /* If "the current_rule_list" match the executed rule list, we are in
3701 * resume condition. If a resume is needed it is always in the action
3702 * and never in the ACL or converters. In this case, we initialise the
3703 * current rule, and go to the action execution point.
3704 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003705 if (s->current_rule) {
3706 rule = s->current_rule;
3707 s->current_rule = NULL;
3708 if (s->current_rule_list == rules)
3709 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003710 }
3711 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003712
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003713 list_for_each_entry(rule, rules, list) {
3714 if (rule->action >= HTTP_RES_ACT_MAX)
3715 continue;
3716
3717 /* check optional condition */
3718 if (rule->cond) {
3719 int ret;
3720
Willy Tarreau192252e2015-04-04 01:47:55 +02003721 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003722 ret = acl_pass(ret);
3723
3724 if (rule->cond->pol == ACL_COND_UNLESS)
3725 ret = !ret;
3726
3727 if (!ret) /* condition not matched */
3728 continue;
3729 }
3730
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003731resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003732 switch (rule->action) {
3733 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003734 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003735
3736 case HTTP_RES_ACT_DENY:
3737 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003738 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003739
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003740 case HTTP_RES_ACT_SET_NICE:
3741 s->task->nice = rule->arg.nice;
3742 break;
3743
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003744 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003745 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003746 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003747 break;
3748
Willy Tarreau51347ed2013-06-11 19:34:13 +02003749 case HTTP_RES_ACT_SET_MARK:
3750#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003751 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003752 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003753#endif
3754 break;
3755
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003756 case HTTP_RES_ACT_SET_LOGL:
3757 s->logs.level = rule->arg.loglevel;
3758 break;
3759
Sasha Pachev218f0642014-06-16 12:05:59 -06003760 case HTTP_RES_ACT_REPLACE_HDR:
3761 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003762 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3763 rule->arg.hdr_add.name_len,
3764 &rule->arg.hdr_add.fmt,
3765 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003766 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003767 break;
3768
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003769 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003770 ctx.idx = 0;
3771 /* remove all occurrences of the header */
3772 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3773 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3774 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3775 }
Willy Tarreau85603282015-01-21 20:39:27 +01003776 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003777
Willy Tarreau85603282015-01-21 20:39:27 +01003778 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003779 case HTTP_RES_ACT_ADD_HDR:
3780 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3781 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3782 trash.len = rule->arg.hdr_add.name_len;
3783 trash.str[trash.len++] = ':';
3784 trash.str[trash.len++] = ' ';
3785 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 +01003786
3787 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3788 /* remove all occurrences of the header */
3789 ctx.idx = 0;
3790 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3791 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3792 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3793 }
3794 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003795 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3796 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003797
3798 case HTTP_RES_ACT_DEL_ACL:
3799 case HTTP_RES_ACT_DEL_MAP: {
3800 struct pat_ref *ref;
3801 char *key;
3802 int len;
3803
3804 /* collect reference */
3805 ref = pat_ref_lookup(rule->arg.map.ref);
3806 if (!ref)
3807 continue;
3808
3809 /* collect key */
3810 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3811 key = trash.str;
3812 key[len] = '\0';
3813
3814 /* perform update */
3815 /* returned code: 1=ok, 0=ko */
3816 pat_ref_delete(ref, key);
3817
3818 break;
3819 }
3820
3821 case HTTP_RES_ACT_ADD_ACL: {
3822 struct pat_ref *ref;
3823 char *key;
3824 struct chunk *trash_key;
3825 int len;
3826
3827 trash_key = get_trash_chunk();
3828
3829 /* collect reference */
3830 ref = pat_ref_lookup(rule->arg.map.ref);
3831 if (!ref)
3832 continue;
3833
3834 /* collect key */
3835 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3836 key = trash_key->str;
3837 key[len] = '\0';
3838
3839 /* perform update */
3840 /* check if the entry already exists */
3841 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003842 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003843
3844 break;
3845 }
3846
3847 case HTTP_RES_ACT_SET_MAP: {
3848 struct pat_ref *ref;
3849 char *key, *value;
3850 struct chunk *trash_key, *trash_value;
3851 int len;
3852
3853 trash_key = get_trash_chunk();
3854 trash_value = get_trash_chunk();
3855
3856 /* collect reference */
3857 ref = pat_ref_lookup(rule->arg.map.ref);
3858 if (!ref)
3859 continue;
3860
3861 /* collect key */
3862 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3863 key = trash_key->str;
3864 key[len] = '\0';
3865
3866 /* collect value */
3867 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3868 value = trash_value->str;
3869 value[len] = '\0';
3870
3871 /* perform update */
3872 if (pat_ref_find_elt(ref, key) != NULL)
3873 /* update entry if it exists */
3874 pat_ref_set(ref, key, value, NULL);
3875 else
3876 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003877 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003878
3879 break;
3880 }
William Lallemand73025dd2014-04-24 14:38:37 +02003881
3882 case HTTP_RES_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003883 if (!rule->action_ptr(rule, px, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003884 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003885 return HTTP_RULE_RES_YIELD;
3886 }
William Lallemand73025dd2014-04-24 14:38:37 +02003887 break;
3888
3889 case HTTP_RES_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003890 rule->action_ptr(rule, px, s);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003891 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003892 }
3893 }
3894
3895 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003896 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003897}
3898
3899
Willy Tarreau71241ab2012-12-27 11:30:54 +01003900/* Perform an HTTP redirect based on the information in <rule>. The function
3901 * returns non-zero on success, or zero in case of a, irrecoverable error such
3902 * as too large a request to build a valid response.
3903 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003904static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003905{
Willy Tarreaub329a312015-05-22 16:27:37 +02003906 struct http_msg *req = &txn->req;
3907 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003908 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003909 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003910
3911 /* build redirect message */
3912 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003913 case 308:
3914 msg_fmt = HTTP_308;
3915 break;
3916 case 307:
3917 msg_fmt = HTTP_307;
3918 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003919 case 303:
3920 msg_fmt = HTTP_303;
3921 break;
3922 case 301:
3923 msg_fmt = HTTP_301;
3924 break;
3925 case 302:
3926 default:
3927 msg_fmt = HTTP_302;
3928 break;
3929 }
3930
3931 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3932 return 0;
3933
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003934 location = trash.str + trash.len;
3935
Willy Tarreau71241ab2012-12-27 11:30:54 +01003936 switch(rule->type) {
3937 case REDIRECT_TYPE_SCHEME: {
3938 const char *path;
3939 const char *host;
3940 struct hdr_ctx ctx;
3941 int pathlen;
3942 int hostlen;
3943
3944 host = "";
3945 hostlen = 0;
3946 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003947 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003948 host = ctx.line + ctx.val;
3949 hostlen = ctx.vlen;
3950 }
3951
3952 path = http_get_path(txn);
3953 /* build message using path */
3954 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003955 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003956 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3957 int qs = 0;
3958 while (qs < pathlen) {
3959 if (path[qs] == '?') {
3960 pathlen = qs;
3961 break;
3962 }
3963 qs++;
3964 }
3965 }
3966 } else {
3967 path = "/";
3968 pathlen = 1;
3969 }
3970
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003971 if (rule->rdr_str) { /* this is an old "redirect" rule */
3972 /* check if we can add scheme + "://" + host + path */
3973 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3974 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003975
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003976 /* add scheme */
3977 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3978 trash.len += rule->rdr_len;
3979 }
3980 else {
3981 /* add scheme with executing log format */
3982 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003983
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003984 /* check if we can add scheme + "://" + host + path */
3985 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3986 return 0;
3987 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003988 /* add "://" */
3989 memcpy(trash.str + trash.len, "://", 3);
3990 trash.len += 3;
3991
3992 /* add host */
3993 memcpy(trash.str + trash.len, host, hostlen);
3994 trash.len += hostlen;
3995
3996 /* add path */
3997 memcpy(trash.str + trash.len, path, pathlen);
3998 trash.len += pathlen;
3999
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004000 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004001 if (trash.len && trash.str[trash.len - 1] != '/' &&
4002 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4003 if (trash.len > trash.size - 5)
4004 return 0;
4005 trash.str[trash.len] = '/';
4006 trash.len++;
4007 }
4008
4009 break;
4010 }
4011 case REDIRECT_TYPE_PREFIX: {
4012 const char *path;
4013 int pathlen;
4014
4015 path = http_get_path(txn);
4016 /* build message using path */
4017 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004018 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004019 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4020 int qs = 0;
4021 while (qs < pathlen) {
4022 if (path[qs] == '?') {
4023 pathlen = qs;
4024 break;
4025 }
4026 qs++;
4027 }
4028 }
4029 } else {
4030 path = "/";
4031 pathlen = 1;
4032 }
4033
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004034 if (rule->rdr_str) { /* this is an old "redirect" rule */
4035 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4036 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004037
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004038 /* add prefix. Note that if prefix == "/", we don't want to
4039 * add anything, otherwise it makes it hard for the user to
4040 * configure a self-redirection.
4041 */
4042 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4043 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4044 trash.len += rule->rdr_len;
4045 }
4046 }
4047 else {
4048 /* add prefix with executing log format */
4049 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4050
4051 /* Check length */
4052 if (trash.len + pathlen > trash.size - 4)
4053 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004054 }
4055
4056 /* add path */
4057 memcpy(trash.str + trash.len, path, pathlen);
4058 trash.len += pathlen;
4059
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004060 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004061 if (trash.len && trash.str[trash.len - 1] != '/' &&
4062 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4063 if (trash.len > trash.size - 5)
4064 return 0;
4065 trash.str[trash.len] = '/';
4066 trash.len++;
4067 }
4068
4069 break;
4070 }
4071 case REDIRECT_TYPE_LOCATION:
4072 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004073 if (rule->rdr_str) { /* this is an old "redirect" rule */
4074 if (trash.len + rule->rdr_len > trash.size - 4)
4075 return 0;
4076
4077 /* add location */
4078 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4079 trash.len += rule->rdr_len;
4080 }
4081 else {
4082 /* add location with executing log format */
4083 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004084
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004085 /* Check left length */
4086 if (trash.len > trash.size - 4)
4087 return 0;
4088 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004089 break;
4090 }
4091
4092 if (rule->cookie_len) {
4093 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4094 trash.len += 14;
4095 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4096 trash.len += rule->cookie_len;
4097 memcpy(trash.str + trash.len, "\r\n", 2);
4098 trash.len += 2;
4099 }
4100
4101 /* add end of headers and the keep-alive/close status.
4102 * We may choose to set keep-alive if the Location begins
4103 * with a slash, because the client will come back to the
4104 * same server.
4105 */
4106 txn->status = rule->code;
4107 /* let's log the request time */
4108 s->logs.tv_request = now;
4109
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004110 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004111 (req->flags & HTTP_MSGF_XFER_LEN) &&
4112 !(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004113 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4114 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4115 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004116 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004117 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4118 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4119 trash.len += 30;
4120 } else {
4121 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4122 trash.len += 24;
4123 }
4124 }
4125 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4126 trash.len += 4;
Willy Tarreaub329a312015-05-22 16:27:37 +02004127 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004128 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004129 bi_fast_delete(req->chn->buf, req->sov);
4130 req->next -= req->sov;
4131 req->sov = 0;
4132 s->req.analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004133 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreaub329a312015-05-22 16:27:37 +02004134 req->msg_state = HTTP_MSG_CLOSED;
4135 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004136 } else {
4137 /* keep-alive not possible */
4138 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4139 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4140 trash.len += 29;
4141 } else {
4142 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4143 trash.len += 23;
4144 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004145 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreaub329a312015-05-22 16:27:37 +02004146 req->chn->analysers = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004147 }
4148
Willy Tarreaue7dff022015-04-03 01:14:29 +02004149 if (!(s->flags & SF_ERR_MASK))
4150 s->flags |= SF_ERR_LOCAL;
4151 if (!(s->flags & SF_FINST_MASK))
4152 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004153
4154 return 1;
4155}
4156
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004157/* This stream analyser runs all HTTP request processing which is common to
4158 * frontends and backends, which means blocking ACLs, filters, connection-close,
4159 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004160 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004161 * either needs more data or wants to immediately abort the request (eg: deny,
4162 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004163 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004164int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004165{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004166 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004167 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004168 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004169 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004170 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004171 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004172
Willy Tarreau655dce92009-11-08 13:10:58 +01004173 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004174 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004175 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004176 }
4177
Willy Tarreau87b09662015-04-03 00:22:06 +02004178 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 +02004179 now_ms, __FUNCTION__,
4180 s,
4181 req,
4182 req->rex, req->wex,
4183 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004184 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004185 req->analysers);
4186
Willy Tarreau65410832014-04-28 21:25:43 +02004187 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004188 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004189
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004190 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004191 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004192 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004193
Willy Tarreau0b748332014-04-29 00:13:29 +02004194 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004195 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4196 goto return_prx_yield;
4197
Willy Tarreau0b748332014-04-29 00:13:29 +02004198 case HTTP_RULE_RES_CONT:
4199 case HTTP_RULE_RES_STOP: /* nothing to do */
4200 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004201
Willy Tarreau0b748332014-04-29 00:13:29 +02004202 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4203 if (txn->flags & TX_CLTARPIT)
4204 goto tarpit;
4205 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004206
Willy Tarreau0b748332014-04-29 00:13:29 +02004207 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4208 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004209
Willy Tarreau0b748332014-04-29 00:13:29 +02004210 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004211 goto done;
4212
Willy Tarreau0b748332014-04-29 00:13:29 +02004213 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4214 goto return_bad_req;
4215 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004216 }
4217
Willy Tarreau52542592014-04-28 18:33:26 +02004218 /* OK at this stage, we know that the request was accepted according to
4219 * the http-request rules, we can check for the stats. Note that the
4220 * URI is detected *before* the req* rules in order not to be affected
4221 * by a possible reqrep, while they are processed *after* so that a
4222 * reqdeny can still block them. This clearly needs to change in 1.6!
4223 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004224 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004225 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004226 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004227 txn->status = 500;
4228 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004229 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004230
Willy Tarreaue7dff022015-04-03 01:14:29 +02004231 if (!(s->flags & SF_ERR_MASK))
4232 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004233 goto return_prx_cond;
4234 }
4235
4236 /* parse the whole stats request and extract the relevant information */
4237 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004238 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004239 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004240
Willy Tarreau0b748332014-04-29 00:13:29 +02004241 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4242 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004243
Willy Tarreau0b748332014-04-29 00:13:29 +02004244 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4245 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004246 }
4247
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004248 /* evaluate the req* rules except reqadd */
4249 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004250 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004251 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004252
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004253 if (txn->flags & TX_CLDENY)
4254 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004255
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004256 if (txn->flags & TX_CLTARPIT)
4257 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004258 }
Willy Tarreau06619262006-12-17 08:37:22 +01004259
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004260 /* add request headers from the rule sets in the same order */
4261 list_for_each_entry(wl, &px->req_add, list) {
4262 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004263 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004264 ret = acl_pass(ret);
4265 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4266 ret = !ret;
4267 if (!ret)
4268 continue;
4269 }
4270
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004271 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004272 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004273 }
4274
Willy Tarreau52542592014-04-28 18:33:26 +02004275
4276 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004277 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004278 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004279 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4280 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004281
Willy Tarreaue7dff022015-04-03 01:14:29 +02004282 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4283 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4284 if (!(s->flags & SF_FINST_MASK))
4285 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004286
Willy Tarreau70730dd2014-04-24 18:06:27 +02004287 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004288 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004289 select_compression_request_header(s, req->buf);
4290
4291 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004292 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004293 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004294 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004295
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004296 /* check whether we have some ACLs set to redirect this request */
4297 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004298 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004299 int ret;
4300
Willy Tarreau192252e2015-04-04 01:47:55 +02004301 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004302 ret = acl_pass(ret);
4303 if (rule->cond->pol == ACL_COND_UNLESS)
4304 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004305 if (!ret)
4306 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004307 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004308 if (!http_apply_redirect_rule(rule, s, txn))
4309 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004310 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004311 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004312
Willy Tarreau2be39392010-01-03 17:24:51 +01004313 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4314 * If this happens, then the data will not come immediately, so we must
4315 * send all what we have without waiting. Note that due to the small gain
4316 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004317 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004318 * itself once used.
4319 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004320 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004321
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004322 done: /* done with this analyser, continue with next ones that the calling
4323 * points will have set, if any.
4324 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004325 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004326 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4327 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004328 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004329
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004330 tarpit:
4331 /* When a connection is tarpitted, we use the tarpit timeout,
4332 * which may be the same as the connect timeout if unspecified.
4333 * If unset, then set it to zero because we really want it to
4334 * eventually expire. We build the tarpit as an analyser.
4335 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004336 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004337
4338 /* wipe the request out so that we can drop the connection early
4339 * if the client closes first.
4340 */
4341 channel_dont_connect(req);
4342 req->analysers = 0; /* remove switching rules etc... */
4343 req->analysers |= AN_REQ_HTTP_TARPIT;
4344 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4345 if (!req->analyse_exp)
4346 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004347 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004348 sess->fe->fe_counters.denied_req++;
4349 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004350 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004351 if (sess->listener->counters)
4352 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004353 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004354
4355 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004356 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004357 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004358 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004359 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004360 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004361 sess->fe->fe_counters.denied_req++;
4362 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004363 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004364 if (sess->listener->counters)
4365 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004366 goto return_prx_cond;
4367
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004368 return_bad_req:
4369 /* We centralize bad requests processing here */
4370 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4371 /* we detected a parsing error. We want to archive this request
4372 * in the dedicated proxy area for later troubleshooting.
4373 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004374 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004375 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004376
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004377 txn->req.msg_state = HTTP_MSG_ERROR;
4378 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004379 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004380
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004381 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004382 if (sess->listener->counters)
4383 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004384
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004385 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004386 if (!(s->flags & SF_ERR_MASK))
4387 s->flags |= SF_ERR_PRXCOND;
4388 if (!(s->flags & SF_FINST_MASK))
4389 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004390
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004391 req->analysers = 0;
4392 req->analyse_exp = TICK_ETERNITY;
4393 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004394
4395 return_prx_yield:
4396 channel_dont_connect(req);
4397 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004398}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004399
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004400/* This function performs all the processing enabled for the current request.
4401 * It returns 1 if the processing can continue on next analysers, or zero if it
4402 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004403 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004404 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004405int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004406{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004407 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004408 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004409 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004410 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004411
Willy Tarreau655dce92009-11-08 13:10:58 +01004412 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004413 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004414 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004415 return 0;
4416 }
4417
Willy Tarreau87b09662015-04-03 00:22:06 +02004418 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 +02004419 now_ms, __FUNCTION__,
4420 s,
4421 req,
4422 req->rex, req->wex,
4423 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004424 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004425 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004426
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004427 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004428 select_compression_request_header(s, req->buf);
4429
Willy Tarreau59234e92008-11-30 23:51:27 +01004430 /*
4431 * Right now, we know that we have processed the entire headers
4432 * and that unwanted requests have been filtered out. We can do
4433 * whatever we want with the remaining request. Also, now we
4434 * may have separate values for ->fe, ->be.
4435 */
Willy Tarreau06619262006-12-17 08:37:22 +01004436
Willy Tarreau59234e92008-11-30 23:51:27 +01004437 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004438 * If HTTP PROXY is set we simply get remote server address parsing
4439 * incoming request. Note that this requires that a connection is
4440 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004441 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004442 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004443 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004444 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004445
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004446 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau350f4872014-11-28 14:42:25 +01004447 if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004448 txn->req.msg_state = HTTP_MSG_ERROR;
4449 txn->status = 500;
4450 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004451 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004452
Willy Tarreaue7dff022015-04-03 01:14:29 +02004453 if (!(s->flags & SF_ERR_MASK))
4454 s->flags |= SF_ERR_RESOURCE;
4455 if (!(s->flags & SF_FINST_MASK))
4456 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004457
4458 return 0;
4459 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004460
4461 path = http_get_path(txn);
4462 url2sa(req->buf->p + msg->sl.rq.u,
4463 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004464 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004465 /* if the path was found, we have to remove everything between
4466 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4467 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4468 * u_l characters by a single "/".
4469 */
4470 if (path) {
4471 char *cur_ptr = req->buf->p;
4472 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4473 int delta;
4474
4475 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4476 http_msg_move_end(&txn->req, delta);
4477 cur_end += delta;
4478 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4479 goto return_bad_req;
4480 }
4481 else {
4482 char *cur_ptr = req->buf->p;
4483 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4484 int delta;
4485
4486 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4487 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4488 http_msg_move_end(&txn->req, delta);
4489 cur_end += delta;
4490 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4491 goto return_bad_req;
4492 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004493 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004494
Willy Tarreau59234e92008-11-30 23:51:27 +01004495 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004496 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004497 * Note that doing so might move headers in the request, but
4498 * the fields will stay coherent and the URI will not move.
4499 * This should only be performed in the backend.
4500 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004501 if ((s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004502 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4503 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004504
Willy Tarreau59234e92008-11-30 23:51:27 +01004505 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004506 * 8: the appsession cookie was looked up very early in 1.2,
4507 * so let's do the same now.
4508 */
4509
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004510 /* It needs to look into the URI unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004511 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004512 get_srv_from_appsession(s, req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01004513 }
4514
William Lallemanda73203e2012-03-12 12:48:57 +01004515 /* add unique-id if "header-unique-id" is specified */
4516
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004517 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004518 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4519 goto return_bad_req;
4520 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004521 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004522 }
William Lallemanda73203e2012-03-12 12:48:57 +01004523
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004524 if (sess->fe->header_unique_id && s->unique_id) {
4525 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004526 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004527 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004528 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004529 goto return_bad_req;
4530 }
4531
Cyril Bontéb21570a2009-11-29 20:04:48 +01004532 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004533 * 9: add X-Forwarded-For if either the frontend or the backend
4534 * asks for it.
4535 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004536 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004537 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004538 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4539 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4540 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004541 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004542 /* The header is set to be added only if none is present
4543 * and we found it, so don't do anything.
4544 */
4545 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004546 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004547 /* Add an X-Forwarded-For header unless the source IP is
4548 * in the 'except' network range.
4549 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004550 if ((!sess->fe->except_mask.s_addr ||
4551 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4552 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004553 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004554 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004555 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004556 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004557 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004558 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004559
4560 /* Note: we rely on the backend to get the header name to be used for
4561 * x-forwarded-for, because the header is really meant for the backends.
4562 * However, if the backend did not specify any option, we have to rely
4563 * on the frontend's header name.
4564 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004565 if (s->be->fwdfor_hdr_len) {
4566 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004567 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004568 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004569 len = sess->fe->fwdfor_hdr_len;
4570 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004571 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004572 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 +01004573
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004574 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004575 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004576 }
4577 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004578 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004579 /* FIXME: for the sake of completeness, we should also support
4580 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004581 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004582 int len;
4583 char pn[INET6_ADDRSTRLEN];
4584 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004585 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004586 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004587
Willy Tarreau59234e92008-11-30 23:51:27 +01004588 /* Note: we rely on the backend to get the header name to be used for
4589 * x-forwarded-for, because the header is really meant for the backends.
4590 * However, if the backend did not specify any option, we have to rely
4591 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004592 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004593 if (s->be->fwdfor_hdr_len) {
4594 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004595 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004596 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004597 len = sess->fe->fwdfor_hdr_len;
4598 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004599 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004600 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004601
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004602 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004603 goto return_bad_req;
4604 }
4605 }
4606
4607 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004608 * 10: add X-Original-To if either the frontend or the backend
4609 * asks for it.
4610 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004611 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004612
4613 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004614 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004615 /* Add an X-Original-To header unless the destination IP is
4616 * in the 'except' network range.
4617 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004618 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004619
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004620 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004621 ((!sess->fe->except_mask_to.s_addr ||
4622 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4623 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004624 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004625 (((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 +02004626 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004627 int len;
4628 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004629 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004630
4631 /* Note: we rely on the backend to get the header name to be used for
4632 * x-original-to, because the header is really meant for the backends.
4633 * However, if the backend did not specify any option, we have to rely
4634 * on the frontend's header name.
4635 */
4636 if (s->be->orgto_hdr_len) {
4637 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004638 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004639 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004640 len = sess->fe->orgto_hdr_len;
4641 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004642 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004643 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 +02004644
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004645 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004646 goto return_bad_req;
4647 }
4648 }
4649 }
4650
Willy Tarreau50fc7772012-11-11 22:19:57 +01004651 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4652 * If an "Upgrade" token is found, the header is left untouched in order not to have
4653 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4654 * "Upgrade" is present in the Connection header.
4655 */
4656 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4657 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004658 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004659 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004660 unsigned int want_flags = 0;
4661
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004662 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004663 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004664 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004665 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004666 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004667 want_flags |= TX_CON_CLO_SET;
4668 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004669 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004670 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004671 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004672 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004673 want_flags |= TX_CON_KAL_SET;
4674 }
4675
4676 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004677 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004678 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004679
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004680
Willy Tarreau522d6c02009-12-06 18:49:18 +01004681 /* If we have no server assigned yet and we're balancing on url_param
4682 * with a POST request, we may be interested in checking the body for
4683 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004684 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004685 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004686 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004687 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004688 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004689 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004690 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004691
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004692 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004693 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004694#ifdef TCP_QUICKACK
4695 /* We expect some data from the client. Unless we know for sure
4696 * we already have a full request, we have to re-enable quick-ack
4697 * in case we previously disabled it, otherwise we might cause
4698 * the client to delay further data.
4699 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004700 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004701 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004702 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004703 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004704 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004705#endif
4706 }
Willy Tarreau03945942009-12-22 16:50:27 +01004707
Willy Tarreau59234e92008-11-30 23:51:27 +01004708 /*************************************************************
4709 * OK, that's finished for the headers. We have done what we *
4710 * could. Let's switch to the DATA state. *
4711 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004712 req->analyse_exp = TICK_ETERNITY;
4713 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004714
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004715 /* if the server closes the connection, we want to immediately react
4716 * and close the socket to save packets and syscalls.
4717 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004718 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004719 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004720
Willy Tarreau59234e92008-11-30 23:51:27 +01004721 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004722 /* OK let's go on with the BODY now */
4723 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004724
Willy Tarreau59234e92008-11-30 23:51:27 +01004725 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004726 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004727 /* we detected a parsing error. We want to archive this request
4728 * in the dedicated proxy area for later troubleshooting.
4729 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004730 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004731 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004732
Willy Tarreau59234e92008-11-30 23:51:27 +01004733 txn->req.msg_state = HTTP_MSG_ERROR;
4734 txn->status = 400;
4735 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004736 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004737
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004738 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004739 if (sess->listener->counters)
4740 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004741
Willy Tarreaue7dff022015-04-03 01:14:29 +02004742 if (!(s->flags & SF_ERR_MASK))
4743 s->flags |= SF_ERR_PRXCOND;
4744 if (!(s->flags & SF_FINST_MASK))
4745 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004746 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004747}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004748
Willy Tarreau60b85b02008-11-30 23:28:40 +01004749/* This function is an analyser which processes the HTTP tarpit. It always
4750 * returns zero, at the beginning because it prevents any other processing
4751 * from occurring, and at the end because it terminates the request.
4752 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004753int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004754{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004755 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004756
4757 /* This connection is being tarpitted. The CLIENT side has
4758 * already set the connect expiration date to the right
4759 * timeout. We just have to check that the client is still
4760 * there and that the timeout has not expired.
4761 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004762 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004763 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004764 !tick_is_expired(req->analyse_exp, now_ms))
4765 return 0;
4766
4767 /* We will set the queue timer to the time spent, just for
4768 * logging purposes. We fake a 500 server error, so that the
4769 * attacker will not suspect his connection has been tarpitted.
4770 * It will not cause trouble to the logs because we can exclude
4771 * the tarpitted connections by filtering on the 'PT' status flags.
4772 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004773 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4774
4775 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004776 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004777 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004778
4779 req->analysers = 0;
4780 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004781
Willy Tarreaue7dff022015-04-03 01:14:29 +02004782 if (!(s->flags & SF_ERR_MASK))
4783 s->flags |= SF_ERR_PRXCOND;
4784 if (!(s->flags & SF_FINST_MASK))
4785 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004786 return 0;
4787}
4788
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004789/* This function is an analyser which waits for the HTTP request body. It waits
4790 * for either the buffer to be full, or the full advertised contents to have
4791 * reached the buffer. It must only be called after the standard HTTP request
4792 * processing has occurred, because it expects the request to be parsed and will
4793 * look for the Expect header. It may send a 100-Continue interim response. It
4794 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4795 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4796 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004797 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004798int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004799{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004800 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004801 struct http_txn *txn = s->txn;
4802 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004803
4804 /* We have to parse the HTTP request body to find any required data.
4805 * "balance url_param check_post" should have been the only way to get
4806 * into this. We were brought here after HTTP header analysis, so all
4807 * related structures are ready.
4808 */
4809
Willy Tarreau890988f2014-04-10 11:59:33 +02004810 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4811 /* This is the first call */
4812 if (msg->msg_state < HTTP_MSG_BODY)
4813 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004814
Willy Tarreau890988f2014-04-10 11:59:33 +02004815 if (msg->msg_state < HTTP_MSG_100_SENT) {
4816 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4817 * send an HTTP/1.1 100 Continue intermediate response.
4818 */
4819 if (msg->flags & HTTP_MSGF_VER_11) {
4820 struct hdr_ctx ctx;
4821 ctx.idx = 0;
4822 /* Expect is allowed in 1.1, look for it */
4823 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4824 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004825 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004826 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004827 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004828 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004829 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004830
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004831 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004832 * req->buf->p still points to the beginning of the message. We
4833 * must save the body in msg->next because it survives buffer
4834 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004835 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004836 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004837
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004838 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004839 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4840 else
4841 msg->msg_state = HTTP_MSG_DATA;
4842 }
4843
Willy Tarreau890988f2014-04-10 11:59:33 +02004844 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4845 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004846 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004847 goto missing_data;
4848
4849 /* OK we have everything we need now */
4850 goto http_end;
4851 }
4852
4853 /* OK here we're parsing a chunked-encoded message */
4854
Willy Tarreau522d6c02009-12-06 18:49:18 +01004855 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004856 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004857 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004858 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004859 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004860 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004861
Willy Tarreau115acb92009-12-26 13:56:06 +01004862 if (!ret)
4863 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004864 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004865 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004866 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004867 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004868 }
4869
Willy Tarreaud98cf932009-12-27 22:54:55 +01004870 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004871 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4872 * for at least a whole chunk or the whole content length bytes after
4873 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004874 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004875 if (msg->msg_state == HTTP_MSG_TRAILERS)
4876 goto http_end;
4877
Willy Tarreaue115b492015-05-01 23:05:14 +02004878 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004879 goto http_end;
4880
4881 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004882 /* we get here if we need to wait for more data. If the buffer is full,
4883 * we have the maximum we can expect.
4884 */
4885 if (buffer_full(req->buf, global.tune.maxrewrite))
4886 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004887
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004888 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004889 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004890 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004891
Willy Tarreaue7dff022015-04-03 01:14:29 +02004892 if (!(s->flags & SF_ERR_MASK))
4893 s->flags |= SF_ERR_CLITO;
4894 if (!(s->flags & SF_FINST_MASK))
4895 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004896 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004897 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004898
4899 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004900 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004901 /* Not enough data. We'll re-use the http-request
4902 * timeout here. Ideally, we should set the timeout
4903 * relative to the accept() date. We just set the
4904 * request timeout once at the beginning of the
4905 * request.
4906 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004907 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004908 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004909 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004910 return 0;
4911 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004912
4913 http_end:
4914 /* The situation will not evolve, so let's give up on the analysis. */
4915 s->logs.tv_request = now; /* update the request timer to reflect full request */
4916 req->analysers &= ~an_bit;
4917 req->analyse_exp = TICK_ETERNITY;
4918 return 1;
4919
4920 return_bad_req: /* let's centralize all bad requests */
4921 txn->req.msg_state = HTTP_MSG_ERROR;
4922 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004923 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004924
Willy Tarreaue7dff022015-04-03 01:14:29 +02004925 if (!(s->flags & SF_ERR_MASK))
4926 s->flags |= SF_ERR_PRXCOND;
4927 if (!(s->flags & SF_FINST_MASK))
4928 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004929
Willy Tarreau522d6c02009-12-06 18:49:18 +01004930 return_err_msg:
4931 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004932 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004933 if (sess->listener->counters)
4934 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004935 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004936}
4937
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004938/* send a server's name with an outgoing request over an established connection.
4939 * Note: this function is designed to be called once the request has been scheduled
4940 * for being forwarded. This is the reason why it rewinds the buffer before
4941 * proceeding.
4942 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004943int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004944
4945 struct hdr_ctx ctx;
4946
Mark Lamourinec2247f02012-01-04 13:02:01 -05004947 char *hdr_name = be->server_id_hdr_name;
4948 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004949 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004950 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004951 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004952
William Lallemandd9e90662012-01-30 17:27:17 +01004953 ctx.idx = 0;
4954
Willy Tarreau211cdec2014-04-17 20:18:08 +02004955 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004956 if (old_o) {
4957 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004958 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004959 txn->req.next += old_o;
4960 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004961 }
4962
Willy Tarreau9b28e032012-10-12 23:49:43 +02004963 old_i = chn->buf->i;
4964 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 -05004965 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004966 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004967 }
4968
4969 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004970 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004971 memcpy(hdr_val, hdr_name, hdr_name_len);
4972 hdr_val += hdr_name_len;
4973 *hdr_val++ = ':';
4974 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004975 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4976 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004977
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004978 if (old_o) {
4979 /* If this was a forwarded request, we must readjust the amount of
4980 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004981 * variations. Note that the current state is >= HTTP_MSG_BODY,
4982 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004983 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004984 old_o += chn->buf->i - old_i;
4985 b_adv(chn->buf, old_o);
4986 txn->req.next -= old_o;
4987 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004988 }
4989
Mark Lamourinec2247f02012-01-04 13:02:01 -05004990 return 0;
4991}
4992
Willy Tarreau610ecce2010-01-04 21:15:02 +01004993/* Terminate current transaction and prepare a new one. This is very tricky
4994 * right now but it works.
4995 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004996void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004997{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004998 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004999 struct proxy *fe = strm_fe(s);
Willy Tarreau068621e2013-12-23 15:11:25 +01005000
Willy Tarreau610ecce2010-01-04 21:15:02 +01005001 /* FIXME: We need a more portable way of releasing a backend's and a
5002 * server's connections. We need a safer way to reinitialize buffer
5003 * flags. We also need a more accurate method for computing per-request
5004 * data.
5005 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005006
Willy Tarreau4213a112013-12-15 10:25:42 +01005007 /* unless we're doing keep-alive, we want to quickly close the connection
5008 * to the server.
5009 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005010 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005011 !si_conn_ready(&s->si[1])) {
5012 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5013 si_shutr(&s->si[1]);
5014 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005015 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005016
Willy Tarreaue7dff022015-04-03 01:14:29 +02005017 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005018 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005019 if (unlikely(s->srv_conn))
5020 sess_change_server(s, NULL);
5021 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005022
5023 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005024 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005025
Willy Tarreaueee5b512015-04-03 23:46:31 +02005026 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005027 int n;
5028
Willy Tarreaueee5b512015-04-03 23:46:31 +02005029 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005030 if (n < 1 || n > 5)
5031 n = 0;
5032
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005033 if (fe->mode == PR_MODE_HTTP) {
5034 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005035 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005036 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005037 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005038 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005039 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005040 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005041 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005042 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005043 s->be->be_counters.p.http.comp_rsp++;
5044 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005045 }
5046
5047 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005048 s->logs.bytes_in -= s->req.buf->i;
5049 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005050
5051 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005052 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005053 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005054 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005055 s->do_log(s);
5056 }
5057
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005058 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005059 stream_stop_content_counters(s);
5060 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005061
Willy Tarreau610ecce2010-01-04 21:15:02 +01005062 s->logs.accept_date = date; /* user-visible date for logging */
5063 s->logs.tv_accept = now; /* corrected date for internal use */
5064 tv_zero(&s->logs.tv_request);
5065 s->logs.t_queue = -1;
5066 s->logs.t_connect = -1;
5067 s->logs.t_data = -1;
5068 s->logs.t_close = 0;
5069 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5070 s->logs.srv_queue_size = 0; /* we will get this number soon */
5071
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005072 s->logs.bytes_in = s->req.total = s->req.buf->i;
5073 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005074
5075 if (s->pend_pos)
5076 pendconn_free(s->pend_pos);
5077
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005078 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005079 if (s->flags & SF_CURR_SESS) {
5080 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005081 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005082 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005083 if (may_dequeue_tasks(objt_server(s->target), s->be))
5084 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005085 }
5086
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005087 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005088
Willy Tarreau4213a112013-12-15 10:25:42 +01005089 /* only release our endpoint if we don't intend to reuse the
5090 * connection.
5091 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005092 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005093 !si_conn_ready(&s->si[1])) {
5094 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005095 }
5096
Willy Tarreau350f4872014-11-28 14:42:25 +01005097 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5098 s->si[1].err_type = SI_ET_NONE;
5099 s->si[1].conn_retries = 0; /* used for logging too */
5100 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005101 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 +01005102 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);
5103 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 +02005104 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5105 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5106 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005107
Willy Tarreaueee5b512015-04-03 23:46:31 +02005108 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005109 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005110 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005111
5112 if (prev_status == 401 || prev_status == 407) {
5113 /* In HTTP keep-alive mode, if we receive a 401, we still have
5114 * a chance of being able to send the visitor again to the same
5115 * server over the same connection. This is required by some
5116 * broken protocols such as NTLM, and anyway whenever there is
5117 * an opportunity for sending the challenge to the proper place,
5118 * it's better to do it (at least it helps with debugging).
5119 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005120 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01005121 }
5122
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005123 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005124 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005125
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005126 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005127 s->req.flags |= CF_NEVER_WAIT;
5128 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005129 }
5130
Willy Tarreau610ecce2010-01-04 21:15:02 +01005131 /* if the request buffer is not empty, it means we're
5132 * about to process another request, so send pending
5133 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005134 * Just don't do this if the buffer is close to be full,
5135 * because the request will wait for it to flush a little
5136 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005137 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005138 if (s->req.buf->i) {
5139 if (s->res.buf->o &&
5140 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5141 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5142 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005143 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005144
5145 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005146 channel_auto_read(&s->req);
5147 channel_auto_close(&s->req);
5148 channel_auto_read(&s->res);
5149 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005150
Willy Tarreau27375622013-12-17 00:00:28 +01005151 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau350f4872014-11-28 14:42:25 +01005152 si_idle_conn(&s->si[1]);
Willy Tarreau27375622013-12-17 00:00:28 +01005153
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005154 s->req.analysers = strm_li(s)->analysers;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005155 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005156}
5157
5158
5159/* This function updates the request state machine according to the response
5160 * state machine and buffer flags. It returns 1 if it changes anything (flag
5161 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5162 * it is only used to find when a request/response couple is complete. Both
5163 * this function and its equivalent should loop until both return zero. It
5164 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5165 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005166int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005167{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005168 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005169 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005170 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005171 unsigned int old_state = txn->req.msg_state;
5172
Willy Tarreau610ecce2010-01-04 21:15:02 +01005173 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5174 return 0;
5175
5176 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005177 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005178 * We can shut the read side unless we want to abort_on_close,
5179 * or we have a POST request. The issue with POST requests is
5180 * that some browsers still send a CRLF after the request, and
5181 * this CRLF must be read so that it does not remain in the kernel
5182 * buffers, otherwise a close could cause an RST on some systems
5183 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005184 * Note that if we're using keep-alive on the client side, we'd
5185 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005186 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005187 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005188 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005189 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5190 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5191 !(s->be->options & PR_O_ABRT_CLOSE) &&
5192 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005193 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005194
Willy Tarreau40f151a2012-12-20 12:10:09 +01005195 /* if the server closes the connection, we want to immediately react
5196 * and close the socket to save packets and syscalls.
5197 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005198 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005199
Willy Tarreau610ecce2010-01-04 21:15:02 +01005200 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5201 goto wait_other_side;
5202
5203 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5204 /* The server has not finished to respond, so we
5205 * don't want to move in order not to upset it.
5206 */
5207 goto wait_other_side;
5208 }
5209
5210 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5211 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005212 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005213 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005214 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005215 goto wait_other_side;
5216 }
5217
5218 /* When we get here, it means that both the request and the
5219 * response have finished receiving. Depending on the connection
5220 * mode, we'll have to wait for the last bytes to leave in either
5221 * direction, and sometimes for a close to be effective.
5222 */
5223
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005224 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5225 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005226 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5227 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005228 }
5229 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5230 /* Option forceclose is set, or either side wants to close,
5231 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005232 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005233 * once both states are CLOSED.
5234 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005235 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5236 channel_shutr_now(chn);
5237 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005238 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005239 }
5240 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005241 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5242 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005243 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005244 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5245 channel_auto_read(chn);
5246 txn->req.msg_state = HTTP_MSG_TUNNEL;
5247 chn->flags |= CF_NEVER_WAIT;
5248 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005249 }
5250
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005251 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005252 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005253 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005254
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005255 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005256 txn->req.msg_state = HTTP_MSG_CLOSING;
5257 goto http_msg_closing;
5258 }
5259 else {
5260 txn->req.msg_state = HTTP_MSG_CLOSED;
5261 goto http_msg_closed;
5262 }
5263 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005264 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005265 }
5266
5267 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5268 http_msg_closing:
5269 /* nothing else to forward, just waiting for the output buffer
5270 * to be empty and for the shutw_now to take effect.
5271 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005272 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005273 txn->req.msg_state = HTTP_MSG_CLOSED;
5274 goto http_msg_closed;
5275 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005276 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005277 txn->req.msg_state = HTTP_MSG_ERROR;
5278 goto wait_other_side;
5279 }
5280 }
5281
5282 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5283 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005284 /* see above in MSG_DONE why we only do this in these states */
5285 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5286 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5287 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005288 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005289 goto wait_other_side;
5290 }
5291
5292 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005293 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005294}
5295
5296
5297/* This function updates the response state machine according to the request
5298 * state machine and buffer flags. It returns 1 if it changes anything (flag
5299 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5300 * it is only used to find when a request/response couple is complete. Both
5301 * this function and its equivalent should loop until both return zero. It
5302 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5303 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005304int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005305{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005306 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005307 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005308 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005309 unsigned int old_state = txn->rsp.msg_state;
5310
Willy Tarreau610ecce2010-01-04 21:15:02 +01005311 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5312 return 0;
5313
5314 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5315 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005316 * still monitor the server connection for a possible close
5317 * while the request is being uploaded, so we don't disable
5318 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005319 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005320 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005321
5322 if (txn->req.msg_state == HTTP_MSG_ERROR)
5323 goto wait_other_side;
5324
5325 if (txn->req.msg_state < HTTP_MSG_DONE) {
5326 /* The client seems to still be sending data, probably
5327 * because we got an error response during an upload.
5328 * We have the choice of either breaking the connection
5329 * or letting it pass through. Let's do the later.
5330 */
5331 goto wait_other_side;
5332 }
5333
5334 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5335 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005336 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005337 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005338 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005339 goto wait_other_side;
5340 }
5341
5342 /* When we get here, it means that both the request and the
5343 * response have finished receiving. Depending on the connection
5344 * mode, we'll have to wait for the last bytes to leave in either
5345 * direction, and sometimes for a close to be effective.
5346 */
5347
5348 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5349 /* Server-close mode : shut read and wait for the request
5350 * side to close its output buffer. The caller will detect
5351 * when we're in DONE and the other is in CLOSED and will
5352 * catch that for the final cleanup.
5353 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005354 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5355 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005356 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005357 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5358 /* Option forceclose is set, or either side wants to close,
5359 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005360 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005361 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005362 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005363 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5364 channel_shutr_now(chn);
5365 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005366 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005367 }
5368 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005369 /* The last possible modes are keep-alive and tunnel. Tunnel will
5370 * need to forward remaining data. Keep-alive will need to monitor
5371 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005372 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005373 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005374 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005375 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5376 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005377 }
5378
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005379 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005380 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005381 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005382 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5383 goto http_msg_closing;
5384 }
5385 else {
5386 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5387 goto http_msg_closed;
5388 }
5389 }
5390 goto wait_other_side;
5391 }
5392
5393 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5394 http_msg_closing:
5395 /* nothing else to forward, just waiting for the output buffer
5396 * to be empty and for the shutw_now to take effect.
5397 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005398 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005399 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5400 goto http_msg_closed;
5401 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005402 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005403 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005404 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005405 if (objt_server(s->target))
5406 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005407 goto wait_other_side;
5408 }
5409 }
5410
5411 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5412 http_msg_closed:
5413 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005414 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005415 channel_auto_close(chn);
5416 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005417 goto wait_other_side;
5418 }
5419
5420 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005421 /* We force the response to leave immediately if we're waiting for the
5422 * other side, since there is no pending shutdown to push it out.
5423 */
5424 if (!channel_is_empty(chn))
5425 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005426 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005427}
5428
5429
5430/* Resync the request and response state machines. Return 1 if either state
5431 * changes.
5432 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005433int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005434{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005435 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005436 int old_req_state = txn->req.msg_state;
5437 int old_res_state = txn->rsp.msg_state;
5438
Willy Tarreau610ecce2010-01-04 21:15:02 +01005439 http_sync_req_state(s);
5440 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005441 if (!http_sync_res_state(s))
5442 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005443 if (!http_sync_req_state(s))
5444 break;
5445 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005446
Willy Tarreau610ecce2010-01-04 21:15:02 +01005447 /* OK, both state machines agree on a compatible state.
5448 * There are a few cases we're interested in :
5449 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5450 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5451 * directions, so let's simply disable both analysers.
5452 * - HTTP_MSG_CLOSED on the response only means we must abort the
5453 * request.
5454 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5455 * with server-close mode means we've completed one request and we
5456 * must re-initialize the server connection.
5457 */
5458
5459 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5460 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5461 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5462 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005463 s->req.analysers = 0;
5464 channel_auto_close(&s->req);
5465 channel_auto_read(&s->req);
5466 s->res.analysers = 0;
5467 channel_auto_close(&s->res);
5468 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005469 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005470 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005471 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005472 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005473 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005474 s->res.analysers = 0;
5475 channel_auto_close(&s->res);
5476 channel_auto_read(&s->res);
5477 s->req.analysers = 0;
5478 channel_abort(&s->req);
5479 channel_auto_close(&s->req);
5480 channel_auto_read(&s->req);
5481 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005482 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005483 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5484 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005485 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005486 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5487 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5488 /* server-close/keep-alive: terminate this transaction,
5489 * possibly killing the server connection and reinitialize
5490 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005491 */
5492 http_end_txn_clean_session(s);
5493 }
5494
Willy Tarreau610ecce2010-01-04 21:15:02 +01005495 return txn->req.msg_state != old_req_state ||
5496 txn->rsp.msg_state != old_res_state;
5497}
5498
Willy Tarreaud98cf932009-12-27 22:54:55 +01005499/* This function is an analyser which forwards request body (including chunk
5500 * sizes if any). It is called as soon as we must forward, even if we forward
5501 * zero byte. The only situation where it must not be called is when we're in
5502 * tunnel mode and we want to forward till the close. It's used both to forward
5503 * remaining data and to resync after end of body. It expects the msg_state to
5504 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005505 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005506 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005507 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005508 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005509int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005510{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005511 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005512 struct http_txn *txn = s->txn;
5513 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005514
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005515 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5516 return 0;
5517
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005518 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005519 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005520 /* Output closed while we were sending data. We must abort and
5521 * wake the other side up.
5522 */
5523 msg->msg_state = HTTP_MSG_ERROR;
5524 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005525 return 1;
5526 }
5527
Willy Tarreaud98cf932009-12-27 22:54:55 +01005528 /* Note that we don't have to send 100-continue back because we don't
5529 * need the data to complete our job, and it's up to the server to
5530 * decide whether to return 100, 417 or anything else in return of
5531 * an "Expect: 100-continue" header.
5532 */
5533
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005534 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005535 /* we have msg->sov which points to the first byte of message
5536 * body, and req->buf.p still points to the beginning of the
5537 * message. We forward the headers now, as we don't need them
5538 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005539 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005540 b_adv(req->buf, msg->sov);
5541 msg->next -= msg->sov;
5542 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005543
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005544 /* The previous analysers guarantee that the state is somewhere
5545 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5546 * msg->next are always correct.
5547 */
5548 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5549 if (msg->flags & HTTP_MSGF_TE_CHNK)
5550 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5551 else
5552 msg->msg_state = HTTP_MSG_DATA;
5553 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005554 }
5555
Willy Tarreau7ba23542014-04-17 21:50:00 +02005556 /* Some post-connect processing might want us to refrain from starting to
5557 * forward data. Currently, the only reason for this is "balance url_param"
5558 * whichs need to parse/process the request after we've enabled forwarding.
5559 */
5560 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005561 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005562 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005563 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005564 goto missing_data;
5565 }
5566 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5567 }
5568
Willy Tarreau80a92c02014-03-12 10:41:13 +01005569 /* in most states, we should abort in case of early close */
5570 channel_auto_close(req);
5571
Willy Tarreauefdf0942014-04-24 20:08:57 +02005572 if (req->to_forward) {
5573 /* We can't process the buffer's contents yet */
5574 req->flags |= CF_WAKE_WRITE;
5575 goto missing_data;
5576 }
5577
Willy Tarreaud98cf932009-12-27 22:54:55 +01005578 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005579 if (msg->msg_state == HTTP_MSG_DATA) {
5580 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005581 /* we may have some pending data starting at req->buf->p */
5582 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005583 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005584 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005585 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005586 msg->next += msg->chunk_len;
5587 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005588
5589 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005590 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005591 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005592 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005593 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005594 }
5595 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005596 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005597 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005598 * TRAILERS state.
5599 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005600 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005601
Willy Tarreau54d23df2012-10-25 19:04:45 +02005602 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005603 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005604 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005605 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005606 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005607 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005608 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005609 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005610 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005611 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005612 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005613 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005614 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005615
5616 if (ret == 0)
5617 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005618 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005619 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005620 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005621 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005622 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005623 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005624 /* we're in MSG_CHUNK_SIZE now */
5625 }
5626 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005627 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005628
5629 if (ret == 0)
5630 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005631 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005632 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005633 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005634 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005635 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005636 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005637 /* we're in HTTP_MSG_DONE now */
5638 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005639 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005640 int old_state = msg->msg_state;
5641
Willy Tarreau610ecce2010-01-04 21:15:02 +01005642 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005643
5644 /* we may have some pending data starting at req->buf->p
5645 * such as last chunk of data or trailers.
5646 */
5647 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005648 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005649 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005650 msg->next = 0;
5651
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005652 /* we don't want to forward closes on DONE except in
5653 * tunnel mode.
5654 */
5655 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005656 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005657 if (http_resync_states(s)) {
5658 /* some state changes occurred, maybe the analyser
5659 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005660 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005661 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005662 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005663 /* request errors are most likely due to
5664 * the server aborting the transfer.
5665 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005666 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005667 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005668 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005669 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005670 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005671 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005672 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005673 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005674
5675 /* If "option abortonclose" is set on the backend, we
5676 * want to monitor the client's connection and forward
5677 * any shutdown notification to the server, which will
5678 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005679 * request. We only do that in tunnel mode, and not in
5680 * other modes since it can be abused to exhaust source
5681 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005682 */
5683 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005684 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005685 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5686 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5687 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005688 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005689 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005690 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005691 /* POST requests may require to read extra CRLF
5692 * sent by broken browsers and which could cause
5693 * an RST to be sent upon close on some systems
5694 * (eg: Linux).
5695 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005696 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005697 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005698
Willy Tarreau610ecce2010-01-04 21:15:02 +01005699 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005700 }
5701 }
5702
Willy Tarreaud98cf932009-12-27 22:54:55 +01005703 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005704 /* we may have some pending data starting at req->buf->p */
5705 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005706 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005707 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5708
Willy Tarreaubed410e2014-04-22 08:19:34 +02005709 msg->next = 0;
5710 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5711
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005712 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005713 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005714 if (!(s->flags & SF_ERR_MASK))
5715 s->flags |= SF_ERR_CLICL;
5716 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005717 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005718 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005719 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005720 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005721 }
5722
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005723 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005724 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005725 if (objt_server(s->target))
5726 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005727
5728 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005729 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005730
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005731 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005732 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005733 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005734
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005735 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005736 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005737 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005738 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005739 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005740
Willy Tarreau5c620922011-05-11 19:56:11 +02005741 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005742 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005743 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005744 * modes are already handled by the stream sock layer. We must not do
5745 * this in content-length mode because it could present the MSG_MORE
5746 * flag with the last block of forwarded data, which would cause an
5747 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005748 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005749 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005750 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005751
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005752 return 0;
5753
Willy Tarreaud98cf932009-12-27 22:54:55 +01005754 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005755 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005756 if (sess->listener->counters)
5757 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005758
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005759 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005760 /* we may have some pending data starting at req->buf->p */
5761 b_adv(req->buf, msg->next);
5762 msg->next = 0;
5763
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005764 txn->req.msg_state = HTTP_MSG_ERROR;
5765 if (txn->status) {
5766 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005767 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005768 } else {
5769 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005770 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005771 }
5772 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005773 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005774
Willy Tarreaue7dff022015-04-03 01:14:29 +02005775 if (!(s->flags & SF_ERR_MASK))
5776 s->flags |= SF_ERR_PRXCOND;
5777 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005778 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005779 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005780 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005781 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005782 }
5783 return 0;
5784
5785 aborted_xfer:
5786 txn->req.msg_state = HTTP_MSG_ERROR;
5787 if (txn->status) {
5788 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005789 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005790 } else {
5791 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005792 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005793 }
5794 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005795 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005796
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005797 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005798 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005799 if (objt_server(s->target))
5800 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005801
Willy Tarreaue7dff022015-04-03 01:14:29 +02005802 if (!(s->flags & SF_ERR_MASK))
5803 s->flags |= SF_ERR_SRVCL;
5804 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005805 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005806 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005807 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005808 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005809 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005810 return 0;
5811}
5812
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005813/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5814 * processing can continue on next analysers, or zero if it either needs more
5815 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005816 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005817 * when it has nothing left to do, and may remove any analyser when it wants to
5818 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005819 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005820int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005821{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005822 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005823 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005824 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005825 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005826 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005827 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005828 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005829
Willy Tarreau87b09662015-04-03 00:22:06 +02005830 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 +02005831 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005832 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005833 rep,
5834 rep->rex, rep->wex,
5835 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005836 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005837 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005838
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005839 /*
5840 * Now parse the partial (or complete) lines.
5841 * We will check the response syntax, and also join multi-line
5842 * headers. An index of all the lines will be elaborated while
5843 * parsing.
5844 *
5845 * For the parsing, we use a 28 states FSM.
5846 *
5847 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005848 * rep->buf->p = beginning of response
5849 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5850 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005851 * msg->eol = end of current header or line (LF or CRLF)
5852 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005853 */
5854
Willy Tarreau628c40c2014-04-24 19:11:26 +02005855 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005856 /* There's a protected area at the end of the buffer for rewriting
5857 * purposes. We don't want to start to parse the request if the
5858 * protected area is affected, because we may have to move processed
5859 * data later, which is much more complicated.
5860 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005861 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005862 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005863 /* some data has still not left the buffer, wake us once that's done */
5864 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5865 goto abort_response;
5866 channel_dont_close(rep);
5867 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005868 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005869 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005870 }
5871
Willy Tarreau379357a2013-06-08 12:55:46 +02005872 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5873 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5874 buffer_slow_realign(rep->buf);
5875
Willy Tarreau9b28e032012-10-12 23:49:43 +02005876 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005877 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005878 }
5879
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005880 /* 1: we might have to print this header in debug mode */
5881 if (unlikely((global.mode & MODE_DEBUG) &&
5882 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005883 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005884 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005885
Willy Tarreau9b28e032012-10-12 23:49:43 +02005886 sol = rep->buf->p;
5887 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005888 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005889
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005890 sol += hdr_idx_first_pos(&txn->hdr_idx);
5891 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005892
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005893 while (cur_idx) {
5894 eol = sol + txn->hdr_idx.v[cur_idx].len;
5895 debug_hdr("srvhdr", s, sol, eol);
5896 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5897 cur_idx = txn->hdr_idx.v[cur_idx].next;
5898 }
5899 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005900
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005901 /*
5902 * Now we quickly check if we have found a full valid response.
5903 * If not so, we check the FD and buffer states before leaving.
5904 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005905 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005906 * responses are checked first.
5907 *
5908 * Depending on whether the client is still there or not, we
5909 * may send an error response back or not. Note that normally
5910 * we should only check for HTTP status there, and check I/O
5911 * errors somewhere else.
5912 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005913
Willy Tarreau655dce92009-11-08 13:10:58 +01005914 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005915 /* Invalid response */
5916 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5917 /* we detected a parsing error. We want to archive this response
5918 * in the dedicated proxy area for later troubleshooting.
5919 */
5920 hdr_response_bad:
5921 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005922 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005923
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005924 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005925 if (objt_server(s->target)) {
5926 objt_server(s->target)->counters.failed_resp++;
5927 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005928 }
Willy Tarreau64648412010-03-05 10:41:54 +01005929 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005930 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005931 rep->analysers = 0;
5932 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005933 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005934 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005935 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005936
Willy Tarreaue7dff022015-04-03 01:14:29 +02005937 if (!(s->flags & SF_ERR_MASK))
5938 s->flags |= SF_ERR_PRXCOND;
5939 if (!(s->flags & SF_FINST_MASK))
5940 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005941
5942 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005943 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005944
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005945 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005946 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005947 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005948 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005949 goto hdr_response_bad;
5950 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005951
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005952 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005953 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005954 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005955 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005956 else if (txn->flags & TX_NOT_FIRST)
5957 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005958
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005959 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005960 if (objt_server(s->target)) {
5961 objt_server(s->target)->counters.failed_resp++;
5962 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005963 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005964
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005965 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005966 rep->analysers = 0;
5967 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005968 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005969 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005970 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005971
Willy Tarreaue7dff022015-04-03 01:14:29 +02005972 if (!(s->flags & SF_ERR_MASK))
5973 s->flags |= SF_ERR_SRVCL;
5974 if (!(s->flags & SF_FINST_MASK))
5975 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005976 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005977 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005978
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005979 /* read timeout : return a 504 to the client. */
5980 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005981 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005982 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005983 else if (txn->flags & TX_NOT_FIRST)
5984 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005985
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005986 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005987 if (objt_server(s->target)) {
5988 objt_server(s->target)->counters.failed_resp++;
5989 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005990 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005991
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005992 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005993 rep->analysers = 0;
5994 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005995 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005996 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005997 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005998
Willy Tarreaue7dff022015-04-03 01:14:29 +02005999 if (!(s->flags & SF_ERR_MASK))
6000 s->flags |= SF_ERR_SRVTO;
6001 if (!(s->flags & SF_FINST_MASK))
6002 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006003 return 0;
6004 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006005
Willy Tarreauf003d372012-11-26 13:35:37 +01006006 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006007 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006008 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006009 s->be->be_counters.cli_aborts++;
6010 if (objt_server(s->target))
6011 objt_server(s->target)->counters.cli_aborts++;
6012
6013 rep->analysers = 0;
6014 channel_auto_close(rep);
6015
6016 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006017 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006018 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006019
Willy Tarreaue7dff022015-04-03 01:14:29 +02006020 if (!(s->flags & SF_ERR_MASK))
6021 s->flags |= SF_ERR_CLICL;
6022 if (!(s->flags & SF_FINST_MASK))
6023 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006024
Willy Tarreau87b09662015-04-03 00:22:06 +02006025 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006026 return 0;
6027 }
6028
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006029 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006030 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006031 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006032 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006033 else if (txn->flags & TX_NOT_FIRST)
6034 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006035
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006036 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006037 if (objt_server(s->target)) {
6038 objt_server(s->target)->counters.failed_resp++;
6039 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006040 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006041
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006042 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006043 rep->analysers = 0;
6044 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006045 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006046 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006047 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006048
Willy Tarreaue7dff022015-04-03 01:14:29 +02006049 if (!(s->flags & SF_ERR_MASK))
6050 s->flags |= SF_ERR_SRVCL;
6051 if (!(s->flags & SF_FINST_MASK))
6052 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006053 return 0;
6054 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006055
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006056 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006057 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006058 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006059 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006060 else if (txn->flags & TX_NOT_FIRST)
6061 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006062
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006063 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006064 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006065 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006066
Willy Tarreaue7dff022015-04-03 01:14:29 +02006067 if (!(s->flags & SF_ERR_MASK))
6068 s->flags |= SF_ERR_CLICL;
6069 if (!(s->flags & SF_FINST_MASK))
6070 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006071
Willy Tarreau87b09662015-04-03 00:22:06 +02006072 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006073 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006074 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006075
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006076 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006077 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006078 return 0;
6079 }
6080
6081 /* More interesting part now : we know that we have a complete
6082 * response which at least looks like HTTP. We have an indicator
6083 * of each header's length, so we can parse them quickly.
6084 */
6085
6086 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006087 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006088
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006089 /*
6090 * 1: get the status code
6091 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006092 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006093 if (n < 1 || n > 5)
6094 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006095 /* when the client triggers a 4xx from the server, it's most often due
6096 * to a missing object or permission. These events should be tracked
6097 * because if they happen often, it may indicate a brute force or a
6098 * vulnerability scan.
6099 */
6100 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006101 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006102
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006103 if (objt_server(s->target))
6104 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006105
Willy Tarreau91852eb2015-05-01 13:26:00 +02006106 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6107 * exactly one digit "." one digit. This check may be disabled using
6108 * option accept-invalid-http-response.
6109 */
6110 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6111 if (msg->sl.st.v_l != 8) {
6112 msg->err_pos = 0;
6113 goto hdr_response_bad;
6114 }
6115
6116 if (rep->buf->p[4] != '/' ||
6117 !isdigit((unsigned char)rep->buf->p[5]) ||
6118 rep->buf->p[6] != '.' ||
6119 !isdigit((unsigned char)rep->buf->p[7])) {
6120 msg->err_pos = 4;
6121 goto hdr_response_bad;
6122 }
6123 }
6124
Willy Tarreau5b154472009-12-21 20:11:07 +01006125 /* check if the response is HTTP/1.1 or above */
6126 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006127 ((rep->buf->p[5] > '1') ||
6128 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006129 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006130
6131 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006132 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 +01006133
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006134 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006135 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006136
Willy Tarreau9b28e032012-10-12 23:49:43 +02006137 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006138
Willy Tarreau39650402010-03-15 19:44:39 +01006139 /* Adjust server's health based on status code. Note: status codes 501
6140 * and 505 are triggered on demand by client request, so we must not
6141 * count them as server failures.
6142 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006143 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006144 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006145 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006146 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006147 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006148 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006149
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006150 /*
6151 * 2: check for cacheability.
6152 */
6153
6154 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006155 case 100:
6156 /*
6157 * We may be facing a 100-continue response, in which case this
6158 * is not the right response, and we're waiting for the next one.
6159 * Let's allow this response to go to the client and wait for the
6160 * next one.
6161 */
6162 hdr_idx_init(&txn->hdr_idx);
6163 msg->next -= channel_forward(rep, msg->next);
6164 msg->msg_state = HTTP_MSG_RPBEFORE;
6165 txn->status = 0;
6166 s->logs.t_data = -1; /* was not a response yet */
6167 goto next_one;
6168
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006169 case 200:
6170 case 203:
6171 case 206:
6172 case 300:
6173 case 301:
6174 case 410:
6175 /* RFC2616 @13.4:
6176 * "A response received with a status code of
6177 * 200, 203, 206, 300, 301 or 410 MAY be stored
6178 * by a cache (...) unless a cache-control
6179 * directive prohibits caching."
6180 *
6181 * RFC2616 @9.5: POST method :
6182 * "Responses to this method are not cacheable,
6183 * unless the response includes appropriate
6184 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006185 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006186 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006187 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006188 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6189 break;
6190 default:
6191 break;
6192 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006193
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006194 /*
6195 * 3: we may need to capture headers
6196 */
6197 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006198 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006199 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006200 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006201
Willy Tarreau557f1992015-05-01 10:05:17 +02006202 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6203 * by RFC7230#3.3.3 :
6204 *
6205 * The length of a message body is determined by one of the following
6206 * (in order of precedence):
6207 *
6208 * 1. Any response to a HEAD request and any response with a 1xx
6209 * (Informational), 204 (No Content), or 304 (Not Modified) status
6210 * code is always terminated by the first empty line after the
6211 * header fields, regardless of the header fields present in the
6212 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006213 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006214 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6215 * the connection will become a tunnel immediately after the empty
6216 * line that concludes the header fields. A client MUST ignore any
6217 * Content-Length or Transfer-Encoding header fields received in
6218 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006219 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006220 * 3. If a Transfer-Encoding header field is present and the chunked
6221 * transfer coding (Section 4.1) is the final encoding, the message
6222 * body length is determined by reading and decoding the chunked
6223 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006224 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006225 * If a Transfer-Encoding header field is present in a response and
6226 * the chunked transfer coding is not the final encoding, the
6227 * message body length is determined by reading the connection until
6228 * it is closed by the server. If a Transfer-Encoding header field
6229 * is present in a request and the chunked transfer coding is not
6230 * the final encoding, the message body length cannot be determined
6231 * reliably; the server MUST respond with the 400 (Bad Request)
6232 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006233 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006234 * If a message is received with both a Transfer-Encoding and a
6235 * Content-Length header field, the Transfer-Encoding overrides the
6236 * Content-Length. Such a message might indicate an attempt to
6237 * perform request smuggling (Section 9.5) or response splitting
6238 * (Section 9.4) and ought to be handled as an error. A sender MUST
6239 * remove the received Content-Length field prior to forwarding such
6240 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006241 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006242 * 4. If a message is received without Transfer-Encoding and with
6243 * either multiple Content-Length header fields having differing
6244 * field-values or a single Content-Length header field having an
6245 * invalid value, then the message framing is invalid and the
6246 * recipient MUST treat it as an unrecoverable error. If this is a
6247 * request message, the server MUST respond with a 400 (Bad Request)
6248 * status code and then close the connection. If this is a response
6249 * message received by a proxy, the proxy MUST close the connection
6250 * to the server, discard the received response, and send a 502 (Bad
6251 * Gateway) response to the client. If this is a response message
6252 * received by a user agent, the user agent MUST close the
6253 * connection to the server and discard the received response.
6254 *
6255 * 5. If a valid Content-Length header field is present without
6256 * Transfer-Encoding, its decimal value defines the expected message
6257 * body length in octets. If the sender closes the connection or
6258 * the recipient times out before the indicated number of octets are
6259 * received, the recipient MUST consider the message to be
6260 * incomplete and close the connection.
6261 *
6262 * 6. If this is a request message and none of the above are true, then
6263 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006264 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006265 * 7. Otherwise, this is a response message without a declared message
6266 * body length, so the message body length is determined by the
6267 * number of octets received prior to the server closing the
6268 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006269 */
6270
6271 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006272 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006273 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006274 * FIXME: should we parse anyway and return an error on chunked encoding ?
6275 */
6276 if (txn->meth == HTTP_METH_HEAD ||
6277 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006278 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006279 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006280 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006281 goto skip_content_length;
6282 }
6283
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006284 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006285 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006286 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006287 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006288 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6289 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006290 /* bad transfer-encoding (chunked followed by something else) */
6291 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006292 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006293 break;
6294 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006295 }
6296
Willy Tarreau1c913912015-04-30 10:57:51 +02006297 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006298 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006299 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006300 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6301 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6302 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006303 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006304 signed long long cl;
6305
Willy Tarreauad14f752011-09-02 20:33:27 +02006306 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006307 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006308 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006309 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006310
Willy Tarreauad14f752011-09-02 20:33:27 +02006311 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006312 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006313 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006314 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006315
Willy Tarreauad14f752011-09-02 20:33:27 +02006316 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006317 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006318 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006319 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006320
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006321 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006322 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006323 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006324 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006325
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006326 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006327 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006328 }
6329
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006330 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006331 select_compression_response_header(s, rep->buf);
6332
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006333skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006334 /* Now we have to check if we need to modify the Connection header.
6335 * This is more difficult on the response than it is on the request,
6336 * because we can have two different HTTP versions and we don't know
6337 * how the client will interprete a response. For instance, let's say
6338 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6339 * HTTP/1.1 response without any header. Maybe it will bound itself to
6340 * HTTP/1.0 because it only knows about it, and will consider the lack
6341 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6342 * the lack of header as a keep-alive. Thus we will use two flags
6343 * indicating how a request MAY be understood by the client. In case
6344 * of multiple possibilities, we'll fix the header to be explicit. If
6345 * ambiguous cases such as both close and keepalive are seen, then we
6346 * will fall back to explicit close. Note that we won't take risks with
6347 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006348 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006349 */
6350
Willy Tarreaudc008c52010-02-01 16:20:08 +01006351 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6352 txn->status == 101)) {
6353 /* Either we've established an explicit tunnel, or we're
6354 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006355 * to understand the next protocols. We have to switch to tunnel
6356 * mode, so that we transfer the request and responses then let
6357 * this protocol pass unmodified. When we later implement specific
6358 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006359 * header which contains information about that protocol for
6360 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006361 */
6362 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6363 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006364 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6365 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006366 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006367 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006368 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006369
Willy Tarreau70dffda2014-01-30 03:07:23 +01006370 /* this situation happens when combining pretend-keepalive with httpclose. */
6371 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006372 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006373 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006374 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6375
Willy Tarreau60466522010-01-18 19:08:45 +01006376 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006377 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006378 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6379 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006380
Willy Tarreau60466522010-01-18 19:08:45 +01006381 /* now adjust header transformations depending on current state */
6382 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6383 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6384 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006385 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006386 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006387 }
Willy Tarreau60466522010-01-18 19:08:45 +01006388 else { /* SCL / KAL */
6389 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006390 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006391 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006392 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006393
Willy Tarreau60466522010-01-18 19:08:45 +01006394 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006395 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006396
Willy Tarreau60466522010-01-18 19:08:45 +01006397 /* Some keep-alive responses are converted to Server-close if
6398 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006399 */
Willy Tarreau60466522010-01-18 19:08:45 +01006400 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6401 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006402 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006403 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006404 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006405 }
6406
Willy Tarreau7959a552013-09-23 16:44:27 +02006407 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006408 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006409
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006410 /* end of job, return OK */
6411 rep->analysers &= ~an_bit;
6412 rep->analyse_exp = TICK_ETERNITY;
6413 channel_auto_close(rep);
6414 return 1;
6415
6416 abort_keep_alive:
6417 /* A keep-alive request to the server failed on a network error.
6418 * The client is required to retry. We need to close without returning
6419 * any other information so that the client retries.
6420 */
6421 txn->status = 0;
6422 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006423 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006424 channel_auto_close(rep);
6425 s->logs.logwait = 0;
6426 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006427 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006428 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006429 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006430 return 0;
6431}
6432
6433/* This function performs all the processing enabled for the current response.
6434 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006435 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006436 * other functions. It works like process_request (see indications above).
6437 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006438int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006439{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006440 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006441 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006442 struct http_msg *msg = &txn->rsp;
6443 struct proxy *cur_proxy;
6444 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006445 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006446
Willy Tarreau87b09662015-04-03 00:22:06 +02006447 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 +02006448 now_ms, __FUNCTION__,
6449 s,
6450 rep,
6451 rep->rex, rep->wex,
6452 rep->flags,
6453 rep->buf->i,
6454 rep->analysers);
6455
6456 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6457 return 0;
6458
Willy Tarreau70730dd2014-04-24 18:06:27 +02006459 /* The stats applet needs to adjust the Connection header but we don't
6460 * apply any filter there.
6461 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006462 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6463 rep->analysers &= ~an_bit;
6464 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006465 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006466 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006467
Willy Tarreau58975672014-04-24 21:13:57 +02006468 /*
6469 * We will have to evaluate the filters.
6470 * As opposed to version 1.2, now they will be evaluated in the
6471 * filters order and not in the header order. This means that
6472 * each filter has to be validated among all headers.
6473 *
6474 * Filters are tried with ->be first, then with ->fe if it is
6475 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006476 *
6477 * Maybe we are in resume condiion. In this case I choose the
6478 * "struct proxy" which contains the rule list matching the resume
6479 * pointer. If none of theses "struct proxy" match, I initialise
6480 * the process with the first one.
6481 *
6482 * In fact, I check only correspondance betwwen the current list
6483 * pointer and the ->fe rule list. If it doesn't match, I initialize
6484 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006485 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006486 if (s->current_rule_list == &sess->fe->http_res_rules)
6487 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006488 else
6489 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006490 while (1) {
6491 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006492
Willy Tarreau58975672014-04-24 21:13:57 +02006493 /* evaluate http-response rules */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006494 if (ret == HTTP_RULE_RES_CONT)
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006495 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006496
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006497 /* we need to be called again. */
6498 if (ret == HTTP_RULE_RES_YIELD) {
6499 channel_dont_close(rep);
6500 return 0;
6501 }
6502
Willy Tarreau58975672014-04-24 21:13:57 +02006503 /* try headers filters */
6504 if (rule_set->rsp_exp != NULL) {
6505 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6506 return_bad_resp:
6507 if (objt_server(s->target)) {
6508 objt_server(s->target)->counters.failed_resp++;
6509 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006510 }
Willy Tarreau58975672014-04-24 21:13:57 +02006511 s->be->be_counters.failed_resp++;
6512 return_srv_prx_502:
6513 rep->analysers = 0;
6514 txn->status = 502;
6515 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006516 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006517 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006518 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006519 if (!(s->flags & SF_ERR_MASK))
6520 s->flags |= SF_ERR_PRXCOND;
6521 if (!(s->flags & SF_FINST_MASK))
6522 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006523 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006524 }
Willy Tarreau58975672014-04-24 21:13:57 +02006525 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006526
Willy Tarreau58975672014-04-24 21:13:57 +02006527 /* has the response been denied ? */
6528 if (txn->flags & TX_SVDENY) {
6529 if (objt_server(s->target))
6530 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006531
Willy Tarreau58975672014-04-24 21:13:57 +02006532 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006533 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006534 if (sess->listener->counters)
6535 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006536
Willy Tarreau58975672014-04-24 21:13:57 +02006537 goto return_srv_prx_502;
6538 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006539
Willy Tarreau58975672014-04-24 21:13:57 +02006540 /* add response headers from the rule sets in the same order */
6541 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006542 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006543 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006544 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006545 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006546 ret = acl_pass(ret);
6547 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6548 ret = !ret;
6549 if (!ret)
6550 continue;
6551 }
6552 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6553 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006554 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006555
Willy Tarreau58975672014-04-24 21:13:57 +02006556 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006557 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006558 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006559 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006560 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006561
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006562 /* After this point, this anayzer can't return yield, so we can
6563 * remove the bit corresponding to this analyzer from the list.
6564 *
6565 * Note that the intermediate returns and goto found previously
6566 * reset the analyzers.
6567 */
6568 rep->analysers &= ~an_bit;
6569 rep->analyse_exp = TICK_ETERNITY;
6570
Willy Tarreau58975672014-04-24 21:13:57 +02006571 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006572 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006573 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006574
Willy Tarreau58975672014-04-24 21:13:57 +02006575 /*
6576 * Now check for a server cookie.
6577 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006578 if (s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name ||
Willy Tarreau58975672014-04-24 21:13:57 +02006579 (s->be->options & PR_O_CHK_CACHE))
6580 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006581
Willy Tarreau58975672014-04-24 21:13:57 +02006582 /*
6583 * Check for cache-control or pragma headers if required.
6584 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006585 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 +02006586 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006587
Willy Tarreau58975672014-04-24 21:13:57 +02006588 /*
6589 * Add server cookie in the response if needed
6590 */
6591 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6592 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006593 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006594 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6595 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6596 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6597 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6598 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006599 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006600 /* the server is known, it's not the one the client requested, or the
6601 * cookie's last seen date needs to be refreshed. We have to
6602 * insert a set-cookie here, except if we want to insert only on POST
6603 * requests and this one isn't. Note that servers which don't have cookies
6604 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006605 */
Willy Tarreau58975672014-04-24 21:13:57 +02006606 if (!objt_server(s->target)->cookie) {
6607 chunk_printf(&trash,
6608 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6609 s->be->cookie_name);
6610 }
6611 else {
6612 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006613
Willy Tarreau58975672014-04-24 21:13:57 +02006614 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6615 /* emit last_date, which is mandatory */
6616 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6617 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6618 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006619
Willy Tarreau58975672014-04-24 21:13:57 +02006620 if (s->be->cookie_maxlife) {
6621 /* emit first_date, which is either the original one or
6622 * the current date.
6623 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006624 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006625 s30tob64(txn->cookie_first_date ?
6626 txn->cookie_first_date >> 2 :
6627 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006628 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006629 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006630 }
Willy Tarreau58975672014-04-24 21:13:57 +02006631 chunk_appendf(&trash, "; path=/");
6632 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006633
Willy Tarreau58975672014-04-24 21:13:57 +02006634 if (s->be->cookie_domain)
6635 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006636
Willy Tarreau58975672014-04-24 21:13:57 +02006637 if (s->be->ck_opts & PR_CK_HTTPONLY)
6638 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006639
Willy Tarreau58975672014-04-24 21:13:57 +02006640 if (s->be->ck_opts & PR_CK_SECURE)
6641 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006642
Willy Tarreau58975672014-04-24 21:13:57 +02006643 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6644 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006645
Willy Tarreau58975672014-04-24 21:13:57 +02006646 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006647 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006648 /* the server did not change, only the date was updated */
6649 txn->flags |= TX_SCK_UPDATED;
6650 else
6651 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006652
Willy Tarreau58975672014-04-24 21:13:57 +02006653 /* Here, we will tell an eventual cache on the client side that we don't
6654 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6655 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6656 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006657 */
Willy Tarreau58975672014-04-24 21:13:57 +02006658 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006659
Willy Tarreau58975672014-04-24 21:13:57 +02006660 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006661
Willy Tarreau58975672014-04-24 21:13:57 +02006662 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6663 "Cache-control: private", 22) < 0))
6664 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006665 }
Willy Tarreau58975672014-04-24 21:13:57 +02006666 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006667
Willy Tarreau58975672014-04-24 21:13:57 +02006668 /*
6669 * Check if result will be cacheable with a cookie.
6670 * We'll block the response if security checks have caught
6671 * nasty things such as a cacheable cookie.
6672 */
6673 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6674 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6675 (s->be->options & PR_O_CHK_CACHE)) {
6676 /* we're in presence of a cacheable response containing
6677 * a set-cookie header. We'll block it as requested by
6678 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006679 */
Willy Tarreau58975672014-04-24 21:13:57 +02006680 if (objt_server(s->target))
6681 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006682
Willy Tarreau58975672014-04-24 21:13:57 +02006683 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006684 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006685 if (sess->listener->counters)
6686 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006687
Willy Tarreau58975672014-04-24 21:13:57 +02006688 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6689 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6690 send_log(s->be, LOG_ALERT,
6691 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6692 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6693 goto return_srv_prx_502;
6694 }
Willy Tarreau03945942009-12-22 16:50:27 +01006695
Willy Tarreau70730dd2014-04-24 18:06:27 +02006696 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006697 /*
6698 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6699 * If an "Upgrade" token is found, the header is left untouched in order
6700 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006701 * if anything but "Upgrade" is present in the Connection header. We don't
6702 * want to touch any 101 response either since it's switching to another
6703 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006704 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006705 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006706 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006707 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006708 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6709 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006710
Willy Tarreau58975672014-04-24 21:13:57 +02006711 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6712 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6713 /* we want a keep-alive response here. Keep-alive header
6714 * required if either side is not 1.1.
6715 */
6716 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6717 want_flags |= TX_CON_KAL_SET;
6718 }
6719 else {
6720 /* we want a close response here. Close header required if
6721 * the server is 1.1, regardless of the client.
6722 */
6723 if (msg->flags & HTTP_MSGF_VER_11)
6724 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006725 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006726
Willy Tarreau58975672014-04-24 21:13:57 +02006727 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6728 http_change_connection_header(txn, msg, want_flags);
6729 }
6730
6731 skip_header_mangling:
6732 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6733 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6734 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006735
Willy Tarreau58975672014-04-24 21:13:57 +02006736 /* if the user wants to log as soon as possible, without counting
6737 * bytes from the server, then this is the right moment. We have
6738 * to temporarily assign bytes_out to log what we currently have.
6739 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006740 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006741 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6742 s->logs.bytes_out = txn->rsp.eoh;
6743 s->do_log(s);
6744 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006745 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006746 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006747}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006748
Willy Tarreaud98cf932009-12-27 22:54:55 +01006749/* This function is an analyser which forwards response body (including chunk
6750 * sizes if any). It is called as soon as we must forward, even if we forward
6751 * zero byte. The only situation where it must not be called is when we're in
6752 * tunnel mode and we want to forward till the close. It's used both to forward
6753 * remaining data and to resync after end of body. It expects the msg_state to
6754 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006755 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006756 *
6757 * It is capable of compressing response data both in content-length mode and
6758 * in chunked mode. The state machines follows different flows depending on
6759 * whether content-length and chunked modes are used, since there are no
6760 * trailers in content-length :
6761 *
6762 * chk-mode cl-mode
6763 * ,----- BODY -----.
6764 * / \
6765 * V size > 0 V chk-mode
6766 * .--> SIZE -------------> DATA -------------> CRLF
6767 * | | size == 0 | last byte |
6768 * | v final crlf v inspected |
6769 * | TRAILERS -----------> DONE |
6770 * | |
6771 * `----------------------------------------------'
6772 *
6773 * Compression only happens in the DATA state, and must be flushed in final
6774 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6775 * is performed at once on final states for all bytes parsed, or when leaving
6776 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006777 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006778int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006779{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006780 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006781 struct http_txn *txn = s->txn;
6782 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006783 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006784 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006785 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006786
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006787 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6788 return 0;
6789
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006790 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006791 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006792 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006793 /* Output closed while we were sending data. We must abort and
6794 * wake the other side up.
6795 */
6796 msg->msg_state = HTTP_MSG_ERROR;
6797 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006798 return 1;
6799 }
6800
Willy Tarreau4fe41902010-06-07 22:27:41 +02006801 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006802 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006803
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006804 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006805 /* we have msg->sov which points to the first byte of message
6806 * body, and res->buf.p still points to the beginning of the
6807 * message. We forward the headers now, as we don't need them
6808 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006809 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006810 b_adv(res->buf, msg->sov);
6811 msg->next -= msg->sov;
6812 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006813
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006814 /* The previous analysers guarantee that the state is somewhere
6815 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6816 * msg->next are always correct.
6817 */
6818 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6819 if (msg->flags & HTTP_MSGF_TE_CHNK)
6820 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6821 else
6822 msg->msg_state = HTTP_MSG_DATA;
6823 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006824 }
6825
Willy Tarreauefdf0942014-04-24 20:08:57 +02006826 if (res->to_forward) {
6827 /* We can't process the buffer's contents yet */
6828 res->flags |= CF_WAKE_WRITE;
6829 goto missing_data;
6830 }
6831
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006832 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6833 /* We need a compression buffer in the DATA state to put the
6834 * output of compressed data, and in CRLF state to let the
6835 * TRAILERS state finish the job of removing the trailing CRLF.
6836 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006837 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006838 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006839 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006840 goto aborted_xfer; /* no memory */
6841 }
6842
6843 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006844 if (ret < 0) {
6845 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006846 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006847 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006848 compressing = 1;
6849 }
6850
Willy Tarreaud98cf932009-12-27 22:54:55 +01006851 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006852 switch (msg->msg_state - HTTP_MSG_DATA) {
6853 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006854 /* we may have some pending data starting at res->buf->p */
6855 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006856 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006857 if (ret < 0)
6858 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006859
Willy Tarreaud5a67832014-04-21 10:54:27 +02006860 if (msg->chunk_len) {
6861 /* input empty or output full */
6862 if (res->buf->i > msg->next)
6863 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006864 goto missing_data;
6865 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006866 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006867 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006868 if (msg->chunk_len > res->buf->i - msg->next) {
6869 /* output full */
6870 res->flags |= CF_WAKE_WRITE;
6871 goto missing_data;
6872 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006873 msg->next += msg->chunk_len;
6874 msg->chunk_len = 0;
6875 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006876
6877 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006878 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006879 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006880 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006881 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006882 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006883 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006884 /* fall through for HTTP_MSG_CHUNK_CRLF */
6885
6886 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6887 /* we want the CRLF after the data */
6888
6889 ret = http_skip_chunk_crlf(msg);
6890 if (ret == 0)
6891 goto missing_data;
6892 else if (ret < 0) {
6893 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006894 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006895 goto return_bad_res;
6896 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006897 /* we're in MSG_CHUNK_SIZE now, fall through */
6898
6899 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006900 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006901 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006902 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006903 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006904
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006905 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006906 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006907 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006908 else if (ret < 0) {
6909 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006910 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006911 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006912 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006913 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006914 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006915
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006916 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006917 if (unlikely(compressing)) {
6918 /* we need to flush output contents before syncing FSMs */
6919 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6920 compressing = 0;
6921 }
6922
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006923 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006924 if (ret == 0)
6925 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006926 else if (ret < 0) {
6927 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006928 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006929 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006930 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006931 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006932
6933 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006934 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006935 if (unlikely(compressing)) {
6936 /* we need to flush output contents before syncing FSMs */
6937 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6938 compressing = 0;
6939 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006940
Willy Tarreauc623c172014-04-18 09:53:50 +02006941 /* we may have some pending data starting at res->buf->p
6942 * such as a last chunk of data or trailers.
6943 */
6944 b_adv(res->buf, msg->next);
6945 msg->next = 0;
6946
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006947 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006948 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006949 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6950 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006951 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006952
Willy Tarreau610ecce2010-01-04 21:15:02 +01006953 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006954 /* some state changes occurred, maybe the analyser
6955 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006956 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006957 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006958 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006959 /* response errors are most likely due to
6960 * the client aborting the transfer.
6961 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006962 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006963 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006964 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006965 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006966 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006967 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006968 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006969 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006970 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006971 }
6972 }
6973
Willy Tarreaud98cf932009-12-27 22:54:55 +01006974 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006975 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006976 if (unlikely(compressing)) {
6977 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006978 compressing = 0;
6979 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006980
Willy Tarreauc623c172014-04-18 09:53:50 +02006981 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6982 b_adv(res->buf, msg->next);
6983 msg->next = 0;
6984 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6985 }
6986
Willy Tarreauf003d372012-11-26 13:35:37 +01006987 if (res->flags & CF_SHUTW)
6988 goto aborted_xfer;
6989
6990 /* stop waiting for data if the input is closed before the end. If the
6991 * client side was already closed, it means that the client has aborted,
6992 * so we don't want to count this as a server abort. Otherwise it's a
6993 * server abort.
6994 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006995 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006996 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006997 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006998 if (!(s->flags & SF_ERR_MASK))
6999 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007000 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007001 if (objt_server(s->target))
7002 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007003 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01007004 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007005
Willy Tarreau40dba092010-03-04 18:14:51 +01007006 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007007 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007008 goto return_bad_res;
7009
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007010 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007011 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007012 * Similarly, with keep-alive on the client side, we don't want to forward a
7013 * close.
7014 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007015 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007016 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7017 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007018 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007019
Willy Tarreau5c620922011-05-11 19:56:11 +02007020 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007021 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007022 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007023 * modes are already handled by the stream sock layer. We must not do
7024 * this in content-length mode because it could present the MSG_MORE
7025 * flag with the last block of forwarded data, which would cause an
7026 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007027 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007028 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007029 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007030
Willy Tarreau87b09662015-04-03 00:22:06 +02007031 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007032 return 0;
7033
Willy Tarreau40dba092010-03-04 18:14:51 +01007034 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007035 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007036 if (objt_server(s->target))
7037 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007038
7039 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007040 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007041 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007042 compressing = 0;
7043 }
7044
Willy Tarreauc623c172014-04-18 09:53:50 +02007045 /* we may have some pending data starting at res->buf->p */
7046 if (s->comp_algo == NULL) {
7047 b_adv(res->buf, msg->next);
7048 msg->next = 0;
7049 }
7050
Willy Tarreaud98cf932009-12-27 22:54:55 +01007051 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007052 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007053 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007054 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007055 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007056 if (objt_server(s->target))
7057 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007058
Willy Tarreaue7dff022015-04-03 01:14:29 +02007059 if (!(s->flags & SF_ERR_MASK))
7060 s->flags |= SF_ERR_PRXCOND;
7061 if (!(s->flags & SF_FINST_MASK))
7062 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007063 return 0;
7064
7065 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007066 if (unlikely(compressing)) {
7067 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7068 compressing = 0;
7069 }
7070
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007071 txn->rsp.msg_state = HTTP_MSG_ERROR;
7072 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007073 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007074 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007075 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007076
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007077 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007078 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007079 if (objt_server(s->target))
7080 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007081
Willy Tarreaue7dff022015-04-03 01:14:29 +02007082 if (!(s->flags & SF_ERR_MASK))
7083 s->flags |= SF_ERR_CLICL;
7084 if (!(s->flags & SF_FINST_MASK))
7085 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007086 return 0;
7087}
7088
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007089/* Iterate the same filter through all request headers.
7090 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007091 * Since it can manage the switch to another backend, it updates the per-proxy
7092 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007093 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007094int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007095{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007096 char *cur_ptr, *cur_end, *cur_next;
7097 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007098 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007099 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007100 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007101
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007102 last_hdr = 0;
7103
Willy Tarreau9b28e032012-10-12 23:49:43 +02007104 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007105 old_idx = 0;
7106
7107 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007108 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007109 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007110 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007111 (exp->action == ACT_ALLOW ||
7112 exp->action == ACT_DENY ||
7113 exp->action == ACT_TARPIT))
7114 return 0;
7115
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007116 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007117 if (!cur_idx)
7118 break;
7119
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007120 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007121 cur_ptr = cur_next;
7122 cur_end = cur_ptr + cur_hdr->len;
7123 cur_next = cur_end + cur_hdr->cr + 1;
7124
7125 /* Now we have one header between cur_ptr and cur_end,
7126 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007127 */
7128
Willy Tarreau15a53a42015-01-21 13:39:42 +01007129 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007130 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007131 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007132 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007133 last_hdr = 1;
7134 break;
7135
7136 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007137 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007138 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007139 break;
7140
7141 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007142 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007143 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007144 break;
7145
7146 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007147 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7148 if (trash.len < 0)
7149 return -1;
7150
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007151 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007152 /* FIXME: if the user adds a newline in the replacement, the
7153 * index will not be recalculated for now, and the new line
7154 * will not be counted as a new header.
7155 */
7156
7157 cur_end += delta;
7158 cur_next += delta;
7159 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007160 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007161 break;
7162
7163 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007164 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007165 cur_next += delta;
7166
Willy Tarreaufa355d42009-11-29 18:12:29 +01007167 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007168 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7169 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007170 cur_hdr->len = 0;
7171 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007172 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007173 break;
7174
7175 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007176 }
7177
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007178 /* keep the link from this header to next one in case of later
7179 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007180 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007181 old_idx = cur_idx;
7182 }
7183 return 0;
7184}
7185
7186
7187/* Apply the filter to the request line.
7188 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7189 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007190 * Since it can manage the switch to another backend, it updates the per-proxy
7191 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007192 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007193int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007194{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007195 char *cur_ptr, *cur_end;
7196 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007197 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007198 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007199
Willy Tarreau3d300592007-03-18 18:34:41 +01007200 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007201 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007202 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007203 (exp->action == ACT_ALLOW ||
7204 exp->action == ACT_DENY ||
7205 exp->action == ACT_TARPIT))
7206 return 0;
7207 else if (exp->action == ACT_REMOVE)
7208 return 0;
7209
7210 done = 0;
7211
Willy Tarreau9b28e032012-10-12 23:49:43 +02007212 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007213 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007214
7215 /* Now we have the request line between cur_ptr and cur_end */
7216
Willy Tarreau15a53a42015-01-21 13:39:42 +01007217 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007218 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007219 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007220 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007221 done = 1;
7222 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007223
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007224 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007225 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007226 done = 1;
7227 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007228
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007229 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007230 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007231 done = 1;
7232 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007233
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007234 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007235 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7236 if (trash.len < 0)
7237 return -1;
7238
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007239 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007240 /* FIXME: if the user adds a newline in the replacement, the
7241 * index will not be recalculated for now, and the new line
7242 * will not be counted as a new header.
7243 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007244
Willy Tarreaufa355d42009-11-29 18:12:29 +01007245 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007246 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007247 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007248 HTTP_MSG_RQMETH,
7249 cur_ptr, cur_end + 1,
7250 NULL, NULL);
7251 if (unlikely(!cur_end))
7252 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007253
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007254 /* we have a full request and we know that we have either a CR
7255 * or an LF at <ptr>.
7256 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007257 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7258 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007259 /* there is no point trying this regex on headers */
7260 return 1;
7261 }
7262 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007263 return done;
7264}
Willy Tarreau97de6242006-12-27 17:18:38 +01007265
Willy Tarreau58f10d72006-12-04 02:26:12 +01007266
Willy Tarreau58f10d72006-12-04 02:26:12 +01007267
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007268/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007269 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007270 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007271 * unparsable request. Since it can manage the switch to another backend, it
7272 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007273 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007274int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007275{
Willy Tarreau192252e2015-04-04 01:47:55 +02007276 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007277 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007278 struct hdr_exp *exp;
7279
7280 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007281 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007282
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007283 /*
7284 * The interleaving of transformations and verdicts
7285 * makes it difficult to decide to continue or stop
7286 * the evaluation.
7287 */
7288
Willy Tarreau6c123b12010-01-28 20:22:06 +01007289 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7290 break;
7291
Willy Tarreau3d300592007-03-18 18:34:41 +01007292 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007293 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007294 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007295 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007296
7297 /* if this filter had a condition, evaluate it now and skip to
7298 * next filter if the condition does not match.
7299 */
7300 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007301 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007302 ret = acl_pass(ret);
7303 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7304 ret = !ret;
7305
7306 if (!ret)
7307 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007308 }
7309
7310 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007311 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007312 if (unlikely(ret < 0))
7313 return -1;
7314
7315 if (likely(ret == 0)) {
7316 /* The filter did not match the request, it can be
7317 * iterated through all headers.
7318 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007319 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7320 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007321 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007322 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007323 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007324}
7325
7326
Willy Tarreaua15645d2007-03-18 16:22:39 +01007327
Willy Tarreau58f10d72006-12-04 02:26:12 +01007328/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007329 * Try to retrieve the server associated to the appsession.
Willy Tarreau87b09662015-04-03 00:22:06 +02007330 * If the server is found, it's assigned to the stream.
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007331 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007332void manage_client_side_appsession(struct stream *s, const char *buf, int len) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02007333 struct http_txn *txn = s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007334 appsess *asession = NULL;
7335 char *sessid_temp = NULL;
7336
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007337 if (len > s->be->appsession_len) {
7338 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007339 }
7340
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007341 if (s->be->options2 & PR_O2_AS_REQL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007342 /* request-learn option is enabled : store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007343 if (txn->sessid != NULL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007344 /* free previously allocated memory as we don't need the stream id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007345 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007346 }
7347
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007348 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007349 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007350 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007351 return;
7352 }
7353
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007354 memcpy(txn->sessid, buf, len);
7355 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007356 }
7357
7358 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7359 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007360 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007361 return;
7362 }
7363
Cyril Bontéb21570a2009-11-29 20:04:48 +01007364 memcpy(sessid_temp, buf, len);
7365 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007366
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007367 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007368 /* free previously allocated memory */
7369 pool_free2(apools.sessid, sessid_temp);
7370
7371 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007372 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7373 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007374 asession->request_count++;
7375
7376 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007377 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007378
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007379 while (srv) {
7380 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007381 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007382 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007383 (s->flags & SF_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007384 /* we found the server and it's usable */
7385 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007386 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007387 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007388 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007389
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007390 break;
7391 } else {
7392 txn->flags &= ~TX_CK_MASK;
7393 txn->flags |= TX_CK_DOWN;
7394 }
7395 }
7396 srv = srv->next;
7397 }
7398 }
7399 }
7400}
7401
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007402/* Find the end of a cookie value contained between <s> and <e>. It works the
7403 * same way as with headers above except that the semi-colon also ends a token.
7404 * See RFC2965 for more information. Note that it requires a valid header to
7405 * return a valid result.
7406 */
7407char *find_cookie_value_end(char *s, const char *e)
7408{
7409 int quoted, qdpair;
7410
7411 quoted = qdpair = 0;
7412 for (; s < e; s++) {
7413 if (qdpair) qdpair = 0;
7414 else if (quoted) {
7415 if (*s == '\\') qdpair = 1;
7416 else if (*s == '"') quoted = 0;
7417 }
7418 else if (*s == '"') quoted = 1;
7419 else if (*s == ',' || *s == ';') return s;
7420 }
7421 return s;
7422}
7423
7424/* Delete a value in a header between delimiters <from> and <next> in buffer
7425 * <buf>. The number of characters displaced is returned, and the pointer to
7426 * the first delimiter is updated if required. The function tries as much as
7427 * possible to respect the following principles :
7428 * - replace <from> delimiter by the <next> one unless <from> points to a
7429 * colon, in which case <next> is simply removed
7430 * - set exactly one space character after the new first delimiter, unless
7431 * there are not enough characters in the block being moved to do so.
7432 * - remove unneeded spaces before the previous delimiter and after the new
7433 * one.
7434 *
7435 * It is the caller's responsibility to ensure that :
7436 * - <from> points to a valid delimiter or the colon ;
7437 * - <next> points to a valid delimiter or the final CR/LF ;
7438 * - there are non-space chars before <from> ;
7439 * - there is a CR/LF at or after <next>.
7440 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007441int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007442{
7443 char *prev = *from;
7444
7445 if (*prev == ':') {
7446 /* We're removing the first value, preserve the colon and add a
7447 * space if possible.
7448 */
7449 if (!http_is_crlf[(unsigned char)*next])
7450 next++;
7451 prev++;
7452 if (prev < next)
7453 *prev++ = ' ';
7454
7455 while (http_is_spht[(unsigned char)*next])
7456 next++;
7457 } else {
7458 /* Remove useless spaces before the old delimiter. */
7459 while (http_is_spht[(unsigned char)*(prev-1)])
7460 prev--;
7461 *from = prev;
7462
7463 /* copy the delimiter and if possible a space if we're
7464 * not at the end of the line.
7465 */
7466 if (!http_is_crlf[(unsigned char)*next]) {
7467 *prev++ = *next++;
7468 if (prev + 1 < next)
7469 *prev++ = ' ';
7470 while (http_is_spht[(unsigned char)*next])
7471 next++;
7472 }
7473 }
7474 return buffer_replace2(buf, prev, next, NULL, 0);
7475}
7476
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007477/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007478 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007479 * desirable to call it only when needed. This code is quite complex because
7480 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7481 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007482 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007483void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007484{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007485 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007486 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007487 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007488 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007489 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7490 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007491
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007492 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007493 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007494 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007495
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007496 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007497 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007498 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007499
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007500 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007501 hdr_beg = hdr_next;
7502 hdr_end = hdr_beg + cur_hdr->len;
7503 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007504
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007505 /* We have one full header between hdr_beg and hdr_end, and the
7506 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007507 * "Cookie:" headers.
7508 */
7509
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007510 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007511 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007512 old_idx = cur_idx;
7513 continue;
7514 }
7515
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007516 del_from = NULL; /* nothing to be deleted */
7517 preserve_hdr = 0; /* assume we may kill the whole header */
7518
Willy Tarreau58f10d72006-12-04 02:26:12 +01007519 /* Now look for cookies. Conforming to RFC2109, we have to support
7520 * attributes whose name begin with a '$', and associate them with
7521 * the right cookie, if we want to delete this cookie.
7522 * So there are 3 cases for each cookie read :
7523 * 1) it's a special attribute, beginning with a '$' : ignore it.
7524 * 2) it's a server id cookie that we *MAY* want to delete : save
7525 * some pointers on it (last semi-colon, beginning of cookie...)
7526 * 3) it's an application cookie : we *MAY* have to delete a previous
7527 * "special" cookie.
7528 * At the end of loop, if a "special" cookie remains, we may have to
7529 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007530 * *MUST* delete it.
7531 *
7532 * Note: RFC2965 is unclear about the processing of spaces around
7533 * the equal sign in the ATTR=VALUE form. A careful inspection of
7534 * the RFC explicitly allows spaces before it, and not within the
7535 * tokens (attrs or values). An inspection of RFC2109 allows that
7536 * too but section 10.1.3 lets one think that spaces may be allowed
7537 * after the equal sign too, resulting in some (rare) buggy
7538 * implementations trying to do that. So let's do what servers do.
7539 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7540 * allowed quoted strings in values, with any possible character
7541 * after a backslash, including control chars and delimitors, which
7542 * causes parsing to become ambiguous. Browsers also allow spaces
7543 * within values even without quotes.
7544 *
7545 * We have to keep multiple pointers in order to support cookie
7546 * removal at the beginning, middle or end of header without
7547 * corrupting the header. All of these headers are valid :
7548 *
7549 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7550 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7551 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7552 * | | | | | | | | |
7553 * | | | | | | | | hdr_end <--+
7554 * | | | | | | | +--> next
7555 * | | | | | | +----> val_end
7556 * | | | | | +-----------> val_beg
7557 * | | | | +--------------> equal
7558 * | | | +----------------> att_end
7559 * | | +---------------------> att_beg
7560 * | +--------------------------> prev
7561 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007562 */
7563
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007564 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7565 /* Iterate through all cookies on this line */
7566
7567 /* find att_beg */
7568 att_beg = prev + 1;
7569 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7570 att_beg++;
7571
7572 /* find att_end : this is the first character after the last non
7573 * space before the equal. It may be equal to hdr_end.
7574 */
7575 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007576
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007577 while (equal < hdr_end) {
7578 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007579 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007580 if (http_is_spht[(unsigned char)*equal++])
7581 continue;
7582 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007583 }
7584
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007585 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7586 * is between <att_beg> and <equal>, both may be identical.
7587 */
7588
7589 /* look for end of cookie if there is an equal sign */
7590 if (equal < hdr_end && *equal == '=') {
7591 /* look for the beginning of the value */
7592 val_beg = equal + 1;
7593 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7594 val_beg++;
7595
7596 /* find the end of the value, respecting quotes */
7597 next = find_cookie_value_end(val_beg, hdr_end);
7598
7599 /* make val_end point to the first white space or delimitor after the value */
7600 val_end = next;
7601 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7602 val_end--;
7603 } else {
7604 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007605 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007606
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007607 /* We have nothing to do with attributes beginning with '$'. However,
7608 * they will automatically be removed if a header before them is removed,
7609 * since they're supposed to be linked together.
7610 */
7611 if (*att_beg == '$')
7612 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007613
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007614 /* Ignore cookies with no equal sign */
7615 if (equal == next) {
7616 /* This is not our cookie, so we must preserve it. But if we already
7617 * scheduled another cookie for removal, we cannot remove the
7618 * complete header, but we can remove the previous block itself.
7619 */
7620 preserve_hdr = 1;
7621 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007622 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007623 val_end += delta;
7624 next += delta;
7625 hdr_end += delta;
7626 hdr_next += delta;
7627 cur_hdr->len += delta;
7628 http_msg_move_end(&txn->req, delta);
7629 prev = del_from;
7630 del_from = NULL;
7631 }
7632 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007633 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007634
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007635 /* if there are spaces around the equal sign, we need to
7636 * strip them otherwise we'll get trouble for cookie captures,
7637 * or even for rewrites. Since this happens extremely rarely,
7638 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007639 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007640 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7641 int stripped_before = 0;
7642 int stripped_after = 0;
7643
7644 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007645 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007646 equal += stripped_before;
7647 val_beg += stripped_before;
7648 }
7649
7650 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007651 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007652 val_beg += stripped_after;
7653 stripped_before += stripped_after;
7654 }
7655
7656 val_end += stripped_before;
7657 next += stripped_before;
7658 hdr_end += stripped_before;
7659 hdr_next += stripped_before;
7660 cur_hdr->len += stripped_before;
7661 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007662 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007663 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007664
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007665 /* First, let's see if we want to capture this cookie. We check
7666 * that we don't already have a client side cookie, because we
7667 * can only capture one. Also as an optimisation, we ignore
7668 * cookies shorter than the declared name.
7669 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007670 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7671 (val_end - att_beg >= sess->fe->capture_namelen) &&
7672 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007673 int log_len = val_end - att_beg;
7674
7675 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7676 Alert("HTTP logging : out of memory.\n");
7677 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007678 if (log_len > sess->fe->capture_len)
7679 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007680 memcpy(txn->cli_cookie, att_beg, log_len);
7681 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007682 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007683 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007684
Willy Tarreaubca99692010-10-06 19:25:55 +02007685 /* Persistence cookies in passive, rewrite or insert mode have the
7686 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007687 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007688 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007689 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007690 * For cookies in prefix mode, the form is :
7691 *
7692 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007693 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007694 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7695 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7696 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007697 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007698
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007699 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7700 * have the server ID between val_beg and delim, and the original cookie between
7701 * delim+1 and val_end. Otherwise, delim==val_end :
7702 *
7703 * Cookie: NAME=SRV; # in all but prefix modes
7704 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7705 * | || || | |+-> next
7706 * | || || | +--> val_end
7707 * | || || +---------> delim
7708 * | || |+------------> val_beg
7709 * | || +-------------> att_end = equal
7710 * | |+-----------------> att_beg
7711 * | +------------------> prev
7712 * +-------------------------> hdr_beg
7713 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007714
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007715 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007716 for (delim = val_beg; delim < val_end; delim++)
7717 if (*delim == COOKIE_DELIM)
7718 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007719 } else {
7720 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007721 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007722 /* Now check if the cookie contains a date field, which would
7723 * appear after a vertical bar ('|') just after the server name
7724 * and before the delimiter.
7725 */
7726 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7727 if (vbar1) {
7728 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007729 * right is the last seen date. It is a base64 encoded
7730 * 30-bit value representing the UNIX date since the
7731 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007732 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007733 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007734 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007735 if (val_end - vbar1 >= 5) {
7736 val = b64tos30(vbar1);
7737 if (val > 0)
7738 txn->cookie_last_date = val << 2;
7739 }
7740 /* look for a second vertical bar */
7741 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7742 if (vbar1 && (val_end - vbar1 > 5)) {
7743 val = b64tos30(vbar1 + 1);
7744 if (val > 0)
7745 txn->cookie_first_date = val << 2;
7746 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007747 }
7748 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007749
Willy Tarreauf64d1412010-10-07 20:06:11 +02007750 /* if the cookie has an expiration date and the proxy wants to check
7751 * it, then we do that now. We first check if the cookie is too old,
7752 * then only if it has expired. We detect strict overflow because the
7753 * time resolution here is not great (4 seconds). Cookies with dates
7754 * in the future are ignored if their offset is beyond one day. This
7755 * allows an admin to fix timezone issues without expiring everyone
7756 * and at the same time avoids keeping unwanted side effects for too
7757 * long.
7758 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007759 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7760 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007761 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007762 txn->flags &= ~TX_CK_MASK;
7763 txn->flags |= TX_CK_OLD;
7764 delim = val_beg; // let's pretend we have not found the cookie
7765 txn->cookie_first_date = 0;
7766 txn->cookie_last_date = 0;
7767 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007768 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7769 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007770 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007771 txn->flags &= ~TX_CK_MASK;
7772 txn->flags |= TX_CK_EXPIRED;
7773 delim = val_beg; // let's pretend we have not found the cookie
7774 txn->cookie_first_date = 0;
7775 txn->cookie_last_date = 0;
7776 }
7777
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007778 /* Here, we'll look for the first running server which supports the cookie.
7779 * This allows to share a same cookie between several servers, for example
7780 * to dedicate backup servers to specific servers only.
7781 * However, to prevent clients from sticking to cookie-less backup server
7782 * when they have incidentely learned an empty cookie, we simply ignore
7783 * empty cookies and mark them as invalid.
7784 * The same behaviour is applied when persistence must be ignored.
7785 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007786 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007787 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007788
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007789 while (srv) {
7790 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7791 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007792 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007793 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007794 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007795 /* we found the server and we can use it */
7796 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007797 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007798 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007799 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007800 break;
7801 } else {
7802 /* we found a server, but it's down,
7803 * mark it as such and go on in case
7804 * another one is available.
7805 */
7806 txn->flags &= ~TX_CK_MASK;
7807 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007808 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007809 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007810 srv = srv->next;
7811 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007812
Willy Tarreauf64d1412010-10-07 20:06:11 +02007813 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007814 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007815 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007816 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007817 txn->flags |= TX_CK_UNUSED;
7818 else
7819 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007820 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007821
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007822 /* depending on the cookie mode, we may have to either :
7823 * - delete the complete cookie if we're in insert+indirect mode, so that
7824 * the server never sees it ;
7825 * - remove the server id from the cookie value, and tag the cookie as an
7826 * application cookie so that it does not get accidentely removed later,
7827 * if we're in cookie prefix mode
7828 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007829 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007830 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007831
Willy Tarreau9b28e032012-10-12 23:49:43 +02007832 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007833 val_end += delta;
7834 next += delta;
7835 hdr_end += delta;
7836 hdr_next += delta;
7837 cur_hdr->len += delta;
7838 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007839
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007840 del_from = NULL;
7841 preserve_hdr = 1; /* we want to keep this cookie */
7842 }
7843 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007844 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007845 del_from = prev;
7846 }
7847 } else {
7848 /* This is not our cookie, so we must preserve it. But if we already
7849 * scheduled another cookie for removal, we cannot remove the
7850 * complete header, but we can remove the previous block itself.
7851 */
7852 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007853
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007854 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007855 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007856 if (att_beg >= del_from)
7857 att_beg += delta;
7858 if (att_end >= del_from)
7859 att_end += delta;
7860 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007861 val_end += delta;
7862 next += delta;
7863 hdr_end += delta;
7864 hdr_next += delta;
7865 cur_hdr->len += delta;
7866 http_msg_move_end(&txn->req, delta);
7867 prev = del_from;
7868 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007869 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007870 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007871
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007872 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007873 if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007874 int cmp_len, value_len;
7875 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007876
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007877 if (s->be->options2 & PR_O2_AS_PFX) {
7878 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7879 value_begin = att_beg + s->be->appsession_name_len;
7880 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007881 } else {
7882 cmp_len = att_end - att_beg;
7883 value_begin = val_beg;
7884 value_len = val_end - val_beg;
7885 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007886
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007887 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007888 if (cmp_len == s->be->appsession_name_len &&
7889 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7890 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007891 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007892 }
7893
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007894 /* continue with next cookie on this header line */
7895 att_beg = next;
7896 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007897
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007898 /* There are no more cookies on this line.
7899 * We may still have one (or several) marked for deletion at the
7900 * end of the line. We must do this now in two ways :
7901 * - if some cookies must be preserved, we only delete from the
7902 * mark to the end of line ;
7903 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007904 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007905 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007906 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007907 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007908 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007909 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007910 cur_hdr->len += delta;
7911 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007912 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007913
7914 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007915 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7916 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007917 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007918 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007919 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007920 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007921 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007922 }
7923
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007924 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007925 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007926 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007927}
7928
7929
Willy Tarreaua15645d2007-03-18 16:22:39 +01007930/* Iterate the same filter through all response headers contained in <rtr>.
7931 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7932 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007933int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007934{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007935 char *cur_ptr, *cur_end, *cur_next;
7936 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007937 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007938 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007939 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007940
7941 last_hdr = 0;
7942
Willy Tarreau9b28e032012-10-12 23:49:43 +02007943 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007944 old_idx = 0;
7945
7946 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007947 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007948 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007949 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007950 (exp->action == ACT_ALLOW ||
7951 exp->action == ACT_DENY))
7952 return 0;
7953
7954 cur_idx = txn->hdr_idx.v[old_idx].next;
7955 if (!cur_idx)
7956 break;
7957
7958 cur_hdr = &txn->hdr_idx.v[cur_idx];
7959 cur_ptr = cur_next;
7960 cur_end = cur_ptr + cur_hdr->len;
7961 cur_next = cur_end + cur_hdr->cr + 1;
7962
7963 /* Now we have one header between cur_ptr and cur_end,
7964 * and the next header starts at cur_next.
7965 */
7966
Willy Tarreau15a53a42015-01-21 13:39:42 +01007967 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007968 switch (exp->action) {
7969 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007970 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007971 last_hdr = 1;
7972 break;
7973
7974 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007975 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007976 last_hdr = 1;
7977 break;
7978
7979 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007980 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7981 if (trash.len < 0)
7982 return -1;
7983
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007984 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007985 /* FIXME: if the user adds a newline in the replacement, the
7986 * index will not be recalculated for now, and the new line
7987 * will not be counted as a new header.
7988 */
7989
7990 cur_end += delta;
7991 cur_next += delta;
7992 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007993 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007994 break;
7995
7996 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007997 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007998 cur_next += delta;
7999
Willy Tarreaufa355d42009-11-29 18:12:29 +01008000 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008001 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8002 txn->hdr_idx.used--;
8003 cur_hdr->len = 0;
8004 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008005 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008006 break;
8007
8008 }
8009 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010
8011 /* keep the link from this header to next one in case of later
8012 * removal of next header.
8013 */
8014 old_idx = cur_idx;
8015 }
8016 return 0;
8017}
8018
8019
8020/* Apply the filter to the status line in the response buffer <rtr>.
8021 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8022 * or -1 if a replacement resulted in an invalid status line.
8023 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008024int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008025{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008026 char *cur_ptr, *cur_end;
8027 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008028 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008029 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008030
8031
Willy Tarreau3d300592007-03-18 18:34:41 +01008032 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008033 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008034 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008035 (exp->action == ACT_ALLOW ||
8036 exp->action == ACT_DENY))
8037 return 0;
8038 else if (exp->action == ACT_REMOVE)
8039 return 0;
8040
8041 done = 0;
8042
Willy Tarreau9b28e032012-10-12 23:49:43 +02008043 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008044 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008045
8046 /* Now we have the status line between cur_ptr and cur_end */
8047
Willy Tarreau15a53a42015-01-21 13:39:42 +01008048 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008049 switch (exp->action) {
8050 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008051 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008052 done = 1;
8053 break;
8054
8055 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008056 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008057 done = 1;
8058 break;
8059
8060 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008061 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8062 if (trash.len < 0)
8063 return -1;
8064
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008065 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008066 /* FIXME: if the user adds a newline in the replacement, the
8067 * index will not be recalculated for now, and the new line
8068 * will not be counted as a new header.
8069 */
8070
Willy Tarreaufa355d42009-11-29 18:12:29 +01008071 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008072 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008073 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008074 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008075 cur_ptr, cur_end + 1,
8076 NULL, NULL);
8077 if (unlikely(!cur_end))
8078 return -1;
8079
8080 /* we have a full respnse and we know that we have either a CR
8081 * or an LF at <ptr>.
8082 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008083 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008084 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008085 /* there is no point trying this regex on headers */
8086 return 1;
8087 }
8088 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008089 return done;
8090}
8091
8092
8093
8094/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008095 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008096 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8097 * unparsable response.
8098 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008099int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008100{
Willy Tarreau192252e2015-04-04 01:47:55 +02008101 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008102 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008103 struct hdr_exp *exp;
8104
8105 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008106 int ret;
8107
8108 /*
8109 * The interleaving of transformations and verdicts
8110 * makes it difficult to decide to continue or stop
8111 * the evaluation.
8112 */
8113
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008114 if (txn->flags & TX_SVDENY)
8115 break;
8116
Willy Tarreau3d300592007-03-18 18:34:41 +01008117 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008118 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8119 exp->action == ACT_PASS)) {
8120 exp = exp->next;
8121 continue;
8122 }
8123
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008124 /* if this filter had a condition, evaluate it now and skip to
8125 * next filter if the condition does not match.
8126 */
8127 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008128 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008129 ret = acl_pass(ret);
8130 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8131 ret = !ret;
8132 if (!ret)
8133 continue;
8134 }
8135
Willy Tarreaua15645d2007-03-18 16:22:39 +01008136 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008137 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008138 if (unlikely(ret < 0))
8139 return -1;
8140
8141 if (likely(ret == 0)) {
8142 /* The filter did not match the response, it can be
8143 * iterated through all headers.
8144 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008145 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8146 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008147 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008148 }
8149 return 0;
8150}
8151
8152
Willy Tarreaua15645d2007-03-18 16:22:39 +01008153/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008154 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008155 * desirable to call it only when needed. This function is also used when we
8156 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008157 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008158void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008159{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008160 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008161 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008162 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008163 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008164 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008165 char *hdr_beg, *hdr_end, *hdr_next;
8166 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008167
Willy Tarreaua15645d2007-03-18 16:22:39 +01008168 /* Iterate through the headers.
8169 * we start with the start line.
8170 */
8171 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008172 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008173
8174 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8175 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008176 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008177
8178 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008179 hdr_beg = hdr_next;
8180 hdr_end = hdr_beg + cur_hdr->len;
8181 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008182
Willy Tarreau24581ba2010-08-31 22:39:35 +02008183 /* We have one full header between hdr_beg and hdr_end, and the
8184 * next header starts at hdr_next. We're only interested in
8185 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008186 */
8187
Willy Tarreau24581ba2010-08-31 22:39:35 +02008188 is_cookie2 = 0;
8189 prev = hdr_beg + 10;
8190 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008191 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008192 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8193 if (!val) {
8194 old_idx = cur_idx;
8195 continue;
8196 }
8197 is_cookie2 = 1;
8198 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008199 }
8200
Willy Tarreau24581ba2010-08-31 22:39:35 +02008201 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8202 * <prev> points to the colon.
8203 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008204 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008205
Willy Tarreau24581ba2010-08-31 22:39:35 +02008206 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8207 * check-cache is enabled) and we are not interested in checking
8208 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008209 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008210 if (s->be->cookie_name == NULL &&
8211 s->be->appsession_name == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008212 sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008213 return;
8214
Willy Tarreau24581ba2010-08-31 22:39:35 +02008215 /* OK so now we know we have to process this response cookie.
8216 * The format of the Set-Cookie header is slightly different
8217 * from the format of the Cookie header in that it does not
8218 * support the comma as a cookie delimiter (thus the header
8219 * cannot be folded) because the Expires attribute described in
8220 * the original Netscape's spec may contain an unquoted date
8221 * with a comma inside. We have to live with this because
8222 * many browsers don't support Max-Age and some browsers don't
8223 * support quoted strings. However the Set-Cookie2 header is
8224 * clean.
8225 *
8226 * We have to keep multiple pointers in order to support cookie
8227 * removal at the beginning, middle or end of header without
8228 * corrupting the header (in case of set-cookie2). A special
8229 * pointer, <scav> points to the beginning of the set-cookie-av
8230 * fields after the first semi-colon. The <next> pointer points
8231 * either to the end of line (set-cookie) or next unquoted comma
8232 * (set-cookie2). All of these headers are valid :
8233 *
8234 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8235 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8236 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8237 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8238 * | | | | | | | | | |
8239 * | | | | | | | | +-> next hdr_end <--+
8240 * | | | | | | | +------------> scav
8241 * | | | | | | +--------------> val_end
8242 * | | | | | +--------------------> val_beg
8243 * | | | | +----------------------> equal
8244 * | | | +------------------------> att_end
8245 * | | +----------------------------> att_beg
8246 * | +------------------------------> prev
8247 * +-----------------------------------------> hdr_beg
8248 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008249
Willy Tarreau24581ba2010-08-31 22:39:35 +02008250 for (; prev < hdr_end; prev = next) {
8251 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008252
Willy Tarreau24581ba2010-08-31 22:39:35 +02008253 /* find att_beg */
8254 att_beg = prev + 1;
8255 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8256 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008257
Willy Tarreau24581ba2010-08-31 22:39:35 +02008258 /* find att_end : this is the first character after the last non
8259 * space before the equal. It may be equal to hdr_end.
8260 */
8261 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008262
Willy Tarreau24581ba2010-08-31 22:39:35 +02008263 while (equal < hdr_end) {
8264 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8265 break;
8266 if (http_is_spht[(unsigned char)*equal++])
8267 continue;
8268 att_end = equal;
8269 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008270
Willy Tarreau24581ba2010-08-31 22:39:35 +02008271 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8272 * is between <att_beg> and <equal>, both may be identical.
8273 */
8274
8275 /* look for end of cookie if there is an equal sign */
8276 if (equal < hdr_end && *equal == '=') {
8277 /* look for the beginning of the value */
8278 val_beg = equal + 1;
8279 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8280 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008281
Willy Tarreau24581ba2010-08-31 22:39:35 +02008282 /* find the end of the value, respecting quotes */
8283 next = find_cookie_value_end(val_beg, hdr_end);
8284
8285 /* make val_end point to the first white space or delimitor after the value */
8286 val_end = next;
8287 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8288 val_end--;
8289 } else {
8290 /* <equal> points to next comma, semi-colon or EOL */
8291 val_beg = val_end = next = equal;
8292 }
8293
8294 if (next < hdr_end) {
8295 /* Set-Cookie2 supports multiple cookies, and <next> points to
8296 * a colon or semi-colon before the end. So skip all attr-value
8297 * pairs and look for the next comma. For Set-Cookie, since
8298 * commas are permitted in values, skip to the end.
8299 */
8300 if (is_cookie2)
8301 next = find_hdr_value_end(next, hdr_end);
8302 else
8303 next = hdr_end;
8304 }
8305
8306 /* Now everything is as on the diagram above */
8307
8308 /* Ignore cookies with no equal sign */
8309 if (equal == val_end)
8310 continue;
8311
8312 /* If there are spaces around the equal sign, we need to
8313 * strip them otherwise we'll get trouble for cookie captures,
8314 * or even for rewrites. Since this happens extremely rarely,
8315 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008316 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008317 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8318 int stripped_before = 0;
8319 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008320
Willy Tarreau24581ba2010-08-31 22:39:35 +02008321 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008322 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008323 equal += stripped_before;
8324 val_beg += stripped_before;
8325 }
8326
8327 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008328 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008329 val_beg += stripped_after;
8330 stripped_before += stripped_after;
8331 }
8332
8333 val_end += stripped_before;
8334 next += stripped_before;
8335 hdr_end += stripped_before;
8336 hdr_next += stripped_before;
8337 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008338 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008339 }
8340
8341 /* First, let's see if we want to capture this cookie. We check
8342 * that we don't already have a server side cookie, because we
8343 * can only capture one. Also as an optimisation, we ignore
8344 * cookies shorter than the declared name.
8345 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008346 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008347 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008348 (val_end - att_beg >= sess->fe->capture_namelen) &&
8349 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008350 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008351 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008352 Alert("HTTP logging : out of memory.\n");
8353 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008354 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008355 if (log_len > sess->fe->capture_len)
8356 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008357 memcpy(txn->srv_cookie, att_beg, log_len);
8358 txn->srv_cookie[log_len] = 0;
8359 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008360 }
8361
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008362 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008363 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008364 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008365 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8366 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008367 /* assume passive cookie by default */
8368 txn->flags &= ~TX_SCK_MASK;
8369 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008370
8371 /* If the cookie is in insert mode on a known server, we'll delete
8372 * this occurrence because we'll insert another one later.
8373 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008374 * a direct access.
8375 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008376 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008377 /* The "preserve" flag was set, we don't want to touch the
8378 * server's cookie.
8379 */
8380 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008381 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008382 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008383 /* this cookie must be deleted */
8384 if (*prev == ':' && next == hdr_end) {
8385 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008386 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008387 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8388 txn->hdr_idx.used--;
8389 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008390 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008391 hdr_next += delta;
8392 http_msg_move_end(&txn->rsp, delta);
8393 /* note: while both invalid now, <next> and <hdr_end>
8394 * are still equal, so the for() will stop as expected.
8395 */
8396 } else {
8397 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008398 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008399 next = prev;
8400 hdr_end += delta;
8401 hdr_next += delta;
8402 cur_hdr->len += delta;
8403 http_msg_move_end(&txn->rsp, delta);
8404 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008405 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008406 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008407 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008408 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008409 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008410 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008411 * with this server since we know it.
8412 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008413 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008414 next += delta;
8415 hdr_end += delta;
8416 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008417 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008418 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008419
Willy Tarreauf1348312010-10-07 15:54:11 +02008420 txn->flags &= ~TX_SCK_MASK;
8421 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008422 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008423 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008424 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008425 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008426 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008427 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008428 next += delta;
8429 hdr_end += delta;
8430 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008431 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008432 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008433
Willy Tarreau827aee92011-03-10 16:55:02 +01008434 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008435 txn->flags &= ~TX_SCK_MASK;
8436 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008437 }
8438 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008439 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008440 else if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008441 int cmp_len, value_len;
8442 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008443
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008444 if (s->be->options2 & PR_O2_AS_PFX) {
8445 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8446 value_begin = att_beg + s->be->appsession_name_len;
8447 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008448 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008449 cmp_len = att_end - att_beg;
8450 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008451 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008452 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008453
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008454 if ((cmp_len == s->be->appsession_name_len) &&
8455 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008456 /* free a possibly previously allocated memory */
8457 pool_free2(apools.sessid, txn->sessid);
8458
Willy Tarreau87b09662015-04-03 00:22:06 +02008459 /* Store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008460 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008461 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008462 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008463 return;
8464 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008465 memcpy(txn->sessid, value_begin, value_len);
8466 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008467 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008468 }
8469 /* that's done for this cookie, check the next one on the same
8470 * line when next != hdr_end (only if is_cookie2).
8471 */
8472 }
8473 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008474 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008475 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008476
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008477 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008478 appsess *asession = NULL;
8479 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008480 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008481 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008482 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008483 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8484 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008485 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008486 return;
8487 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008488 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8489
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008490 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8491 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008492 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8493 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008494 return;
8495 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008496 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8497 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008498
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008499 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008500 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008501 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008502 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8503 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008504 return;
8505 }
8506 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008507 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008508
8509 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008510 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008511 }
8512
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008513 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008514 asession->request_count++;
8515 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008516}
8517
8518
Willy Tarreaua15645d2007-03-18 16:22:39 +01008519/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008520 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008521 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008522void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008523{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008524 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008525 char *p1, *p2;
8526
8527 char *cur_ptr, *cur_end, *cur_next;
8528 int cur_idx;
8529
Willy Tarreau5df51872007-11-25 16:20:08 +01008530 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008531 return;
8532
8533 /* Iterate through the headers.
8534 * we start with the start line.
8535 */
8536 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008537 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008538
8539 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8540 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008541 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008542
8543 cur_hdr = &txn->hdr_idx.v[cur_idx];
8544 cur_ptr = cur_next;
8545 cur_end = cur_ptr + cur_hdr->len;
8546 cur_next = cur_end + cur_hdr->cr + 1;
8547
8548 /* We have one full header between cur_ptr and cur_end, and the
8549 * next header starts at cur_next. We're only interested in
8550 * "Cookie:" headers.
8551 */
8552
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008553 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8554 if (val) {
8555 if ((cur_end - (cur_ptr + val) >= 8) &&
8556 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8557 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8558 return;
8559 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008560 }
8561
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008562 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8563 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008564 continue;
8565
8566 /* OK, right now we know we have a cache-control header at cur_ptr */
8567
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008568 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008569
8570 if (p1 >= cur_end) /* no more info */
8571 continue;
8572
8573 /* p1 is at the beginning of the value */
8574 p2 = p1;
8575
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008576 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008577 p2++;
8578
8579 /* we have a complete value between p1 and p2 */
8580 if (p2 < cur_end && *p2 == '=') {
8581 /* we have something of the form no-cache="set-cookie" */
8582 if ((cur_end - p1 >= 21) &&
8583 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8584 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008585 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008586 continue;
8587 }
8588
8589 /* OK, so we know that either p2 points to the end of string or to a comma */
8590 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008591 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008592 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8593 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8594 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008595 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008596 return;
8597 }
8598
8599 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008600 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008601 continue;
8602 }
8603 }
8604}
8605
8606
Willy Tarreau58f10d72006-12-04 02:26:12 +01008607/*
8608 * Try to retrieve a known appsession in the URI, then the associated server.
Willy Tarreau87b09662015-04-03 00:22:06 +02008609 * If the server is found, it's assigned to the stream.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008610 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008611void get_srv_from_appsession(struct stream *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008612{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008613 char *end_params, *first_param, *cur_param, *next_param;
8614 char separator;
8615 int value_len;
8616
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008617 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008618
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008619 if (s->be->appsession_name == NULL ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02008620 (s->txn->meth != HTTP_METH_GET && s->txn->meth != HTTP_METH_POST && s->txn->meth != HTTP_METH_HEAD)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008621 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008622 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008623
Cyril Bontéb21570a2009-11-29 20:04:48 +01008624 first_param = NULL;
8625 switch (mode) {
8626 case PR_O2_AS_M_PP:
8627 first_param = memchr(begin, ';', len);
8628 break;
8629 case PR_O2_AS_M_QS:
8630 first_param = memchr(begin, '?', len);
8631 break;
8632 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008633
Cyril Bontéb21570a2009-11-29 20:04:48 +01008634 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008635 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008636 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008637
Cyril Bontéb21570a2009-11-29 20:04:48 +01008638 switch (mode) {
8639 case PR_O2_AS_M_PP:
8640 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8641 end_params = (char *) begin + len;
8642 }
8643 separator = ';';
8644 break;
8645 case PR_O2_AS_M_QS:
8646 end_params = (char *) begin + len;
8647 separator = '&';
8648 break;
8649 default:
8650 /* unknown mode, shouldn't happen */
8651 return;
8652 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008653
Cyril Bontéb21570a2009-11-29 20:04:48 +01008654 cur_param = next_param = end_params;
8655 while (cur_param > first_param) {
8656 cur_param--;
8657 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8658 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008659 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8660 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8661 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008662 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008663 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8664 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008665 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008666 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008667 }
8668 break;
8669 }
8670 next_param = cur_param;
8671 }
8672 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008673#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008674 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008675 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008676#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008677}
8678
Willy Tarreaub2513902006-12-17 14:52:38 +01008679/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008680 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008681 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008682 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008683 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008684 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008685 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008686 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008687 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008688int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008689{
8690 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008691 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008692 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008693
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008694 if (!uri_auth)
8695 return 0;
8696
Cyril Bonté70be45d2010-10-12 00:14:35 +02008697 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008698 return 0;
8699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008700 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008701 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008702 return 0;
8703
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008704 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008705 return 0;
8706
Willy Tarreaub2513902006-12-17 14:52:38 +01008707 return 1;
8708}
8709
Willy Tarreau4076a152009-04-02 15:18:36 +02008710/*
8711 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008712 * By default it tries to report the error position as msg->err_pos. However if
8713 * this one is not set, it will then report msg->next, which is the last known
8714 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008715 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008716 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008717void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008718 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008719 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008720{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008721 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008722 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008723 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008724
Willy Tarreau9b28e032012-10-12 23:49:43 +02008725 es->len = MIN(chn->buf->i, sizeof(es->buf));
8726 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008727 len1 = MIN(len1, es->len);
8728 len2 = es->len - len1; /* remaining data if buffer wraps */
8729
Willy Tarreau9b28e032012-10-12 23:49:43 +02008730 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008731 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008732 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008733
Willy Tarreau4076a152009-04-02 15:18:36 +02008734 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008735 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008736 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008737 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008738
Willy Tarreau4076a152009-04-02 15:18:36 +02008739 es->when = date; // user-visible date
8740 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008741 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008742 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008743 if (objt_conn(sess->origin))
8744 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008745 else
8746 memset(&es->src, 0, sizeof(es->src));
8747
Willy Tarreau078272e2010-12-12 12:46:33 +01008748 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008749 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008750 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008751 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008752 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008753 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008754 es->b_out = chn->buf->o;
8755 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008756 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008757 es->m_clen = msg->chunk_len;
8758 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008759}
Willy Tarreaub2513902006-12-17 14:52:38 +01008760
Willy Tarreau294c4732011-12-16 21:35:50 +01008761/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8762 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8763 * performed over the whole headers. Otherwise it must contain a valid header
8764 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8765 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8766 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8767 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008768 * -1. The value fetch stops at commas, so this function is suited for use with
8769 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008770 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008771 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008772unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008773 struct hdr_idx *idx, int occ,
8774 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008775{
Willy Tarreau294c4732011-12-16 21:35:50 +01008776 struct hdr_ctx local_ctx;
8777 char *ptr_hist[MAX_HDR_HISTORY];
8778 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008779 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008780 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008781
Willy Tarreau294c4732011-12-16 21:35:50 +01008782 if (!ctx) {
8783 local_ctx.idx = 0;
8784 ctx = &local_ctx;
8785 }
8786
Willy Tarreaubce70882009-09-07 11:51:47 +02008787 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008788 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008789 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008790 occ--;
8791 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008792 *vptr = ctx->line + ctx->val;
8793 *vlen = ctx->vlen;
8794 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008795 }
8796 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008797 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008798 }
8799
8800 /* negative occurrence, we scan all the list then walk back */
8801 if (-occ > MAX_HDR_HISTORY)
8802 return 0;
8803
Willy Tarreau294c4732011-12-16 21:35:50 +01008804 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008805 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008806 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8807 len_hist[hist_ptr] = ctx->vlen;
8808 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008809 hist_ptr = 0;
8810 found++;
8811 }
8812 if (-occ > found)
8813 return 0;
8814 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008815 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8816 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8817 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008818 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008819 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008820 if (hist_ptr >= MAX_HDR_HISTORY)
8821 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008822 *vptr = ptr_hist[hist_ptr];
8823 *vlen = len_hist[hist_ptr];
8824 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008825}
8826
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008827/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8828 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8829 * performed over the whole headers. Otherwise it must contain a valid header
8830 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8831 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8832 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8833 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8834 * -1. This function differs from http_get_hdr() in that it only returns full
8835 * line header values and does not stop at commas.
8836 * The return value is 0 if nothing was found, or non-zero otherwise.
8837 */
8838unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8839 struct hdr_idx *idx, int occ,
8840 struct hdr_ctx *ctx, char **vptr, int *vlen)
8841{
8842 struct hdr_ctx local_ctx;
8843 char *ptr_hist[MAX_HDR_HISTORY];
8844 int len_hist[MAX_HDR_HISTORY];
8845 unsigned int hist_ptr;
8846 int found;
8847
8848 if (!ctx) {
8849 local_ctx.idx = 0;
8850 ctx = &local_ctx;
8851 }
8852
8853 if (occ >= 0) {
8854 /* search from the beginning */
8855 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8856 occ--;
8857 if (occ <= 0) {
8858 *vptr = ctx->line + ctx->val;
8859 *vlen = ctx->vlen;
8860 return 1;
8861 }
8862 }
8863 return 0;
8864 }
8865
8866 /* negative occurrence, we scan all the list then walk back */
8867 if (-occ > MAX_HDR_HISTORY)
8868 return 0;
8869
8870 found = hist_ptr = 0;
8871 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8872 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8873 len_hist[hist_ptr] = ctx->vlen;
8874 if (++hist_ptr >= MAX_HDR_HISTORY)
8875 hist_ptr = 0;
8876 found++;
8877 }
8878 if (-occ > found)
8879 return 0;
8880 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8881 * find occurrence -occ, so we have to check [hist_ptr+occ].
8882 */
8883 hist_ptr += occ;
8884 if (hist_ptr >= MAX_HDR_HISTORY)
8885 hist_ptr -= MAX_HDR_HISTORY;
8886 *vptr = ptr_hist[hist_ptr];
8887 *vlen = len_hist[hist_ptr];
8888 return 1;
8889}
8890
Willy Tarreaubaaee002006-06-26 02:48:02 +02008891/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008892 * Print a debug line with a header. Always stop at the first CR or LF char,
8893 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8894 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008895 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008896void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008897{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008898 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008899 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008900
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008901 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008902 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008903 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008904 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 +02008905
8906 for (max = 0; start + max < end; max++)
8907 if (start[max] == '\r' || start[max] == '\n')
8908 break;
8909
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008910 UBOUND(max, trash.size - trash.len - 3);
8911 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8912 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008913 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008914}
8915
Willy Tarreaueee5b512015-04-03 23:46:31 +02008916
8917/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8918 * The hdr_idx is allocated as well. In case of allocation failure, everything
8919 * allocated is freed and NULL is returned. Otherwise the new transaction is
8920 * assigned to the stream and returned.
8921 */
8922struct http_txn *http_alloc_txn(struct stream *s)
8923{
8924 struct http_txn *txn = s->txn;
8925
8926 if (txn)
8927 return txn;
8928
8929 txn = pool_alloc2(pool2_http_txn);
8930 if (!txn)
8931 return txn;
8932
8933 txn->hdr_idx.size = global.tune.max_http_hdr;
8934 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8935 if (!txn->hdr_idx.v) {
8936 pool_free2(pool2_http_txn, txn);
8937 return NULL;
8938 }
8939
8940 s->txn = txn;
8941 return txn;
8942}
8943
Willy Tarreau0937bc42009-12-22 15:03:09 +01008944/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008945 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008946 * the required fields are properly allocated and that we only need to (re)init
8947 * them. This should be used before processing any new request.
8948 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008949void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008950{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008951 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008952 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008953
8954 txn->flags = 0;
8955 txn->status = -1;
8956
Willy Tarreauf64d1412010-10-07 20:06:11 +02008957 txn->cookie_first_date = 0;
8958 txn->cookie_last_date = 0;
8959
Willy Tarreaueee5b512015-04-03 23:46:31 +02008960 txn->sessid = NULL;
8961 txn->srv_cookie = NULL;
8962 txn->cli_cookie = NULL;
8963 txn->uri = NULL;
8964
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008965 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008966 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008967 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008968 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008969 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008970 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008971 txn->req.chunk_len = 0LL;
8972 txn->req.body_len = 0LL;
8973 txn->rsp.chunk_len = 0LL;
8974 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008975 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8976 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008977 txn->req.chn = &s->req;
8978 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008979
8980 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008981
8982 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8983 if (fe->options2 & PR_O2_REQBUG_OK)
8984 txn->req.err_pos = -1; /* let buggy requests pass */
8985
Willy Tarreau0937bc42009-12-22 15:03:09 +01008986 if (txn->hdr_idx.v)
8987 hdr_idx_init(&txn->hdr_idx);
8988}
8989
8990/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008991void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008992{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008993 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008994 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008995
Willy Tarreau75195602014-03-11 15:48:55 +01008996 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008997 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008998 s->comp_algo->end(&s->comp_ctx);
8999 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02009000 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01009001
Willy Tarreau0937bc42009-12-22 15:03:09 +01009002 /* these ones will have been dynamically allocated */
9003 pool_free2(pool2_requri, txn->uri);
9004 pool_free2(pool2_capture, txn->cli_cookie);
9005 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01009006 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01009007 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009008
William Lallemanda73203e2012-03-12 12:48:57 +01009009 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01009010 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009011 txn->uri = NULL;
9012 txn->srv_cookie = NULL;
9013 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01009014
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009015 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01009016 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02009017 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009018 pool_free2(h->pool, s->req_cap[h->index]);
9019 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01009020 }
9021
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009022 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01009023 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02009024 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009025 pool_free2(h->pool, s->res_cap[h->index]);
9026 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01009027 }
9028
Willy Tarreau0937bc42009-12-22 15:03:09 +01009029}
9030
9031/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02009032void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01009033{
9034 http_end_txn(s);
9035 http_init_txn(s);
9036
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01009037 /* reinitialise the current rule list pointer to NULL. We are sure that
9038 * any rulelist match the NULL pointer.
9039 */
9040 s->current_rule_list = NULL;
9041
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009042 s->be = strm_fe(s);
9043 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02009044 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02009045 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01009046 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009047 /* re-init store persistence */
9048 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01009049 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009050
Willy Tarreau0937bc42009-12-22 15:03:09 +01009051 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009052
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009053 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01009054
Willy Tarreau739cfba2010-01-25 23:11:14 +01009055 /* We must trim any excess data from the response buffer, because we
9056 * may have blocked an invalid response from a server that we don't
9057 * want to accidentely forward once we disable the analysers, nor do
9058 * we want those data to come along with next response. A typical
9059 * example of such data would be from a buggy server responding to
9060 * a HEAD with some data, or sending more than the advertised
9061 * content-length.
9062 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009063 if (unlikely(s->res.buf->i))
9064 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009065
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009066 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009067 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009068
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009069 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009070 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009071
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009072 s->req.rex = TICK_ETERNITY;
9073 s->req.wex = TICK_ETERNITY;
9074 s->req.analyse_exp = TICK_ETERNITY;
9075 s->res.rex = TICK_ETERNITY;
9076 s->res.wex = TICK_ETERNITY;
9077 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009078}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009079
Sasha Pachev218f0642014-06-16 12:05:59 -06009080void free_http_res_rules(struct list *r)
9081{
9082 struct http_res_rule *tr, *pr;
9083
9084 list_for_each_entry_safe(pr, tr, r, list) {
9085 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009086 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009087 free(pr);
9088 }
9089}
9090
9091void free_http_req_rules(struct list *r)
9092{
Willy Tarreauff011f22011-01-06 17:51:27 +01009093 struct http_req_rule *tr, *pr;
9094
9095 list_for_each_entry_safe(pr, tr, r, list) {
9096 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01009097 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009098 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009099
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009100 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009101 free(pr);
9102 }
9103}
9104
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009105/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01009106struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9107{
9108 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009109 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009110 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009111 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009112
9113 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
9114 if (!rule) {
9115 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009116 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009117 }
9118
CJ Ess108b1dd2015-04-07 12:03:37 -04009119 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009120 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01009121 rule->action = HTTP_REQ_ACT_ALLOW;
9122 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009123 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009124 int code;
9125 int hc;
9126
Willy Tarreauff011f22011-01-06 17:51:27 +01009127 rule->action = HTTP_REQ_ACT_DENY;
9128 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009129 if (strcmp(args[cur_arg], "deny_status") == 0) {
9130 cur_arg++;
9131 if (!args[cur_arg]) {
9132 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9133 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9134 goto out_err;
9135 }
9136
9137 code = atol(args[cur_arg]);
9138 cur_arg++;
9139 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9140 if (http_err_codes[hc] == code) {
9141 rule->deny_status = hc;
9142 break;
9143 }
9144 }
9145
9146 if (hc >= HTTP_ERR_SIZE) {
9147 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9148 file, linenum, code);
9149 }
9150 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009151 } else if (!strcmp(args[0], "tarpit")) {
9152 rule->action = HTTP_REQ_ACT_TARPIT;
9153 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009154 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01009155 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009156 cur_arg = 1;
9157
9158 while(*args[cur_arg]) {
9159 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009160 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009161 cur_arg+=2;
9162 continue;
9163 } else
9164 break;
9165 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009166 } else if (!strcmp(args[0], "set-nice")) {
9167 rule->action = HTTP_REQ_ACT_SET_NICE;
9168 cur_arg = 1;
9169
9170 if (!*args[cur_arg] ||
9171 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9172 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9173 file, linenum, args[0]);
9174 goto out_err;
9175 }
9176 rule->arg.nice = atoi(args[cur_arg]);
9177 if (rule->arg.nice < -1024)
9178 rule->arg.nice = -1024;
9179 else if (rule->arg.nice > 1024)
9180 rule->arg.nice = 1024;
9181 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009182 } else if (!strcmp(args[0], "set-tos")) {
9183#ifdef IP_TOS
9184 char *err;
9185 rule->action = HTTP_REQ_ACT_SET_TOS;
9186 cur_arg = 1;
9187
9188 if (!*args[cur_arg] ||
9189 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9190 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9191 file, linenum, args[0]);
9192 goto out_err;
9193 }
9194
9195 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9196 if (err && *err != '\0') {
9197 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9198 file, linenum, err, args[0]);
9199 goto out_err;
9200 }
9201 cur_arg++;
9202#else
9203 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9204 goto out_err;
9205#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009206 } else if (!strcmp(args[0], "set-mark")) {
9207#ifdef SO_MARK
9208 char *err;
9209 rule->action = HTTP_REQ_ACT_SET_MARK;
9210 cur_arg = 1;
9211
9212 if (!*args[cur_arg] ||
9213 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9214 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9215 file, linenum, args[0]);
9216 goto out_err;
9217 }
9218
9219 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9220 if (err && *err != '\0') {
9221 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9222 file, linenum, err, args[0]);
9223 goto out_err;
9224 }
9225 cur_arg++;
9226 global.last_checks |= LSTCHK_NETADM;
9227#else
9228 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9229 goto out_err;
9230#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009231 } else if (!strcmp(args[0], "set-log-level")) {
9232 rule->action = HTTP_REQ_ACT_SET_LOGL;
9233 cur_arg = 1;
9234
9235 if (!*args[cur_arg] ||
9236 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9237 bad_log_level:
9238 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9239 file, linenum, args[0]);
9240 goto out_err;
9241 }
9242 if (strcmp(args[cur_arg], "silent") == 0)
9243 rule->arg.loglevel = -1;
9244 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9245 goto bad_log_level;
9246 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009247 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9248 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9249 cur_arg = 1;
9250
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009251 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9252 (*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 +01009253 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9254 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009255 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009256 }
9257
9258 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9259 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9260 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009261
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009262 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009263 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009264 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9265 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009266 free(proxy->conf.lfs_file);
9267 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9268 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009269 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009270 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9271 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009272 cur_arg = 1;
9273
9274 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009275 (*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 -06009276 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9277 file, linenum, args[0]);
9278 goto out_err;
9279 }
9280
9281 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9282 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9283 LIST_INIT(&rule->arg.hdr_add.fmt);
9284
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009285 error = NULL;
9286 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9287 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9288 args[cur_arg + 1], error);
9289 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009290 goto out_err;
9291 }
9292
9293 proxy->conf.args.ctx = ARGC_HRQ;
9294 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9295 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9296 file, linenum);
9297
9298 free(proxy->conf.lfs_file);
9299 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9300 proxy->conf.lfs_line = proxy->conf.args.line;
9301 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009302 } else if (strcmp(args[0], "del-header") == 0) {
9303 rule->action = HTTP_REQ_ACT_DEL_HDR;
9304 cur_arg = 1;
9305
9306 if (!*args[cur_arg] ||
9307 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9308 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9309 file, linenum, args[0]);
9310 goto out_err;
9311 }
9312
9313 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9314 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9315
9316 proxy->conf.args.ctx = ARGC_HRQ;
9317 free(proxy->conf.lfs_file);
9318 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9319 proxy->conf.lfs_line = proxy->conf.args.line;
9320 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009321 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9322 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009323 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009324 struct sample_expr *expr;
9325 unsigned int where;
9326 char *err = NULL;
9327
9328 cur_arg = 1;
9329 proxy->conf.args.ctx = ARGC_TRK;
9330
9331 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9332 if (!expr) {
9333 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9334 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9335 free(err);
9336 goto out_err;
9337 }
9338
9339 where = 0;
9340 if (proxy->cap & PR_CAP_FE)
9341 where |= SMP_VAL_FE_HRQ_HDR;
9342 if (proxy->cap & PR_CAP_BE)
9343 where |= SMP_VAL_BE_HRQ_HDR;
9344
9345 if (!(expr->fetch->val & where)) {
9346 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9347 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9348 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9349 args[cur_arg-1], sample_src_names(expr->fetch->use));
9350 free(expr);
9351 goto out_err;
9352 }
9353
9354 if (strcmp(args[cur_arg], "table") == 0) {
9355 cur_arg++;
9356 if (!args[cur_arg]) {
9357 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9358 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9359 free(expr);
9360 goto out_err;
9361 }
9362 /* we copy the table name for now, it will be resolved later */
9363 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9364 cur_arg++;
9365 }
9366 rule->act_prm.trk_ctr.expr = expr;
9367 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009368 } else if (strcmp(args[0], "redirect") == 0) {
9369 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009370 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009371
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009372 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009373 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9374 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9375 goto out_err;
9376 }
9377
9378 /* this redirect rule might already contain a parsed condition which
9379 * we'll pass to the http-request rule.
9380 */
9381 rule->action = HTTP_REQ_ACT_REDIR;
9382 rule->arg.redir = redir;
9383 rule->cond = redir->cond;
9384 redir->cond = NULL;
9385 cur_arg = 2;
9386 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009387 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9388 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9389 rule->action = HTTP_REQ_ACT_ADD_ACL;
9390 /*
9391 * '+ 8' for 'add-acl('
9392 * '- 9' for 'add-acl(' + trailing ')'
9393 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009394 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009395
9396 cur_arg = 1;
9397
9398 if (!*args[cur_arg] ||
9399 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9400 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9401 file, linenum, args[0]);
9402 goto out_err;
9403 }
9404
9405 LIST_INIT(&rule->arg.map.key);
9406 proxy->conf.args.ctx = ARGC_HRQ;
9407 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9408 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9409 file, linenum);
9410 free(proxy->conf.lfs_file);
9411 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9412 proxy->conf.lfs_line = proxy->conf.args.line;
9413 cur_arg += 1;
9414 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9415 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9416 rule->action = HTTP_REQ_ACT_DEL_ACL;
9417 /*
9418 * '+ 8' for 'del-acl('
9419 * '- 9' for 'del-acl(' + trailing ')'
9420 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009421 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009422
9423 cur_arg = 1;
9424
9425 if (!*args[cur_arg] ||
9426 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9427 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9428 file, linenum, args[0]);
9429 goto out_err;
9430 }
9431
9432 LIST_INIT(&rule->arg.map.key);
9433 proxy->conf.args.ctx = ARGC_HRQ;
9434 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9435 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9436 file, linenum);
9437 free(proxy->conf.lfs_file);
9438 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9439 proxy->conf.lfs_line = proxy->conf.args.line;
9440 cur_arg += 1;
9441 } else if (strncmp(args[0], "del-map", 7) == 0) {
9442 /* http-request del-map(<reference (map name)>) <key pattern> */
9443 rule->action = HTTP_REQ_ACT_DEL_MAP;
9444 /*
9445 * '+ 8' for 'del-map('
9446 * '- 9' for 'del-map(' + trailing ')'
9447 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009448 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009449
9450 cur_arg = 1;
9451
9452 if (!*args[cur_arg] ||
9453 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9454 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9455 file, linenum, args[0]);
9456 goto out_err;
9457 }
9458
9459 LIST_INIT(&rule->arg.map.key);
9460 proxy->conf.args.ctx = ARGC_HRQ;
9461 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9462 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9463 file, linenum);
9464 free(proxy->conf.lfs_file);
9465 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9466 proxy->conf.lfs_line = proxy->conf.args.line;
9467 cur_arg += 1;
9468 } else if (strncmp(args[0], "set-map", 7) == 0) {
9469 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9470 rule->action = HTTP_REQ_ACT_SET_MAP;
9471 /*
9472 * '+ 8' for 'set-map('
9473 * '- 9' for 'set-map(' + trailing ')'
9474 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009475 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009476
9477 cur_arg = 1;
9478
9479 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9480 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9481 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9482 file, linenum, args[0]);
9483 goto out_err;
9484 }
9485
9486 LIST_INIT(&rule->arg.map.key);
9487 LIST_INIT(&rule->arg.map.value);
9488 proxy->conf.args.ctx = ARGC_HRQ;
9489
9490 /* key pattern */
9491 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9492 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9493 file, linenum);
9494
9495 /* value pattern */
9496 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9497 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9498 file, linenum);
9499 free(proxy->conf.lfs_file);
9500 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9501 proxy->conf.lfs_line = proxy->conf.args.line;
9502
9503 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009504 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9505 char *errmsg = NULL;
9506 cur_arg = 1;
9507 /* try in the module list */
9508 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9509 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9510 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9511 free(errmsg);
9512 goto out_err;
9513 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009514 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009515 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', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009516 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009517 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009518 }
9519
9520 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9521 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009522 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009523
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009524 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9525 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9526 file, linenum, args[0], errmsg);
9527 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009528 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009529 }
9530 rule->cond = cond;
9531 }
9532 else if (*args[cur_arg]) {
9533 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9534 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9535 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009536 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009537 }
9538
9539 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009540 out_err:
9541 free(rule);
9542 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009543}
9544
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009545/* parse an "http-respose" rule */
9546struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9547{
9548 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009549 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009550 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009551 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009552
9553 rule = calloc(1, sizeof(*rule));
9554 if (!rule) {
9555 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9556 goto out_err;
9557 }
9558
9559 if (!strcmp(args[0], "allow")) {
9560 rule->action = HTTP_RES_ACT_ALLOW;
9561 cur_arg = 1;
9562 } else if (!strcmp(args[0], "deny")) {
9563 rule->action = HTTP_RES_ACT_DENY;
9564 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009565 } else if (!strcmp(args[0], "set-nice")) {
9566 rule->action = HTTP_RES_ACT_SET_NICE;
9567 cur_arg = 1;
9568
9569 if (!*args[cur_arg] ||
9570 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9571 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9572 file, linenum, args[0]);
9573 goto out_err;
9574 }
9575 rule->arg.nice = atoi(args[cur_arg]);
9576 if (rule->arg.nice < -1024)
9577 rule->arg.nice = -1024;
9578 else if (rule->arg.nice > 1024)
9579 rule->arg.nice = 1024;
9580 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009581 } else if (!strcmp(args[0], "set-tos")) {
9582#ifdef IP_TOS
9583 char *err;
9584 rule->action = HTTP_RES_ACT_SET_TOS;
9585 cur_arg = 1;
9586
9587 if (!*args[cur_arg] ||
9588 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9589 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9590 file, linenum, args[0]);
9591 goto out_err;
9592 }
9593
9594 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9595 if (err && *err != '\0') {
9596 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9597 file, linenum, err, args[0]);
9598 goto out_err;
9599 }
9600 cur_arg++;
9601#else
9602 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9603 goto out_err;
9604#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009605 } else if (!strcmp(args[0], "set-mark")) {
9606#ifdef SO_MARK
9607 char *err;
9608 rule->action = HTTP_RES_ACT_SET_MARK;
9609 cur_arg = 1;
9610
9611 if (!*args[cur_arg] ||
9612 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9613 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9614 file, linenum, args[0]);
9615 goto out_err;
9616 }
9617
9618 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9619 if (err && *err != '\0') {
9620 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9621 file, linenum, err, args[0]);
9622 goto out_err;
9623 }
9624 cur_arg++;
9625 global.last_checks |= LSTCHK_NETADM;
9626#else
9627 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9628 goto out_err;
9629#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009630 } else if (!strcmp(args[0], "set-log-level")) {
9631 rule->action = HTTP_RES_ACT_SET_LOGL;
9632 cur_arg = 1;
9633
9634 if (!*args[cur_arg] ||
9635 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9636 bad_log_level:
9637 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9638 file, linenum, args[0]);
9639 goto out_err;
9640 }
9641 if (strcmp(args[cur_arg], "silent") == 0)
9642 rule->arg.loglevel = -1;
9643 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9644 goto bad_log_level;
9645 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009646 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9647 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9648 cur_arg = 1;
9649
9650 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9651 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9652 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9653 file, linenum, args[0]);
9654 goto out_err;
9655 }
9656
9657 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9658 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9659 LIST_INIT(&rule->arg.hdr_add.fmt);
9660
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009661 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009662 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009663 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9664 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009665 free(proxy->conf.lfs_file);
9666 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9667 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009668 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009669 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009670 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009671 cur_arg = 1;
9672
9673 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009674 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9675 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009676 file, linenum, args[0]);
9677 goto out_err;
9678 }
9679
9680 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9681 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9682 LIST_INIT(&rule->arg.hdr_add.fmt);
9683
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009684 error = NULL;
9685 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9686 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9687 args[cur_arg + 1], error);
9688 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009689 goto out_err;
9690 }
9691
9692 proxy->conf.args.ctx = ARGC_HRQ;
9693 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9694 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9695 file, linenum);
9696
9697 free(proxy->conf.lfs_file);
9698 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9699 proxy->conf.lfs_line = proxy->conf.args.line;
9700 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009701 } else if (strcmp(args[0], "del-header") == 0) {
9702 rule->action = HTTP_RES_ACT_DEL_HDR;
9703 cur_arg = 1;
9704
9705 if (!*args[cur_arg] ||
9706 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9707 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9708 file, linenum, args[0]);
9709 goto out_err;
9710 }
9711
9712 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9713 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9714
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009715 proxy->conf.args.ctx = ARGC_HRS;
9716 free(proxy->conf.lfs_file);
9717 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9718 proxy->conf.lfs_line = proxy->conf.args.line;
9719 cur_arg += 1;
9720 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9721 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9722 rule->action = HTTP_RES_ACT_ADD_ACL;
9723 /*
9724 * '+ 8' for 'add-acl('
9725 * '- 9' for 'add-acl(' + trailing ')'
9726 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009727 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009728
9729 cur_arg = 1;
9730
9731 if (!*args[cur_arg] ||
9732 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9733 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9734 file, linenum, args[0]);
9735 goto out_err;
9736 }
9737
9738 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009739 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009740 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9741 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9742 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009743 free(proxy->conf.lfs_file);
9744 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9745 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009746
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009747 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009748 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9749 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9750 rule->action = HTTP_RES_ACT_DEL_ACL;
9751 /*
9752 * '+ 8' for 'del-acl('
9753 * '- 9' for 'del-acl(' + trailing ')'
9754 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009755 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009756
9757 cur_arg = 1;
9758
9759 if (!*args[cur_arg] ||
9760 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9761 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9762 file, linenum, args[0]);
9763 goto out_err;
9764 }
9765
9766 LIST_INIT(&rule->arg.map.key);
9767 proxy->conf.args.ctx = ARGC_HRS;
9768 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9769 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9770 file, linenum);
9771 free(proxy->conf.lfs_file);
9772 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9773 proxy->conf.lfs_line = proxy->conf.args.line;
9774 cur_arg += 1;
9775 } else if (strncmp(args[0], "del-map", 7) == 0) {
9776 /* http-response del-map(<reference (map name)>) <key pattern> */
9777 rule->action = HTTP_RES_ACT_DEL_MAP;
9778 /*
9779 * '+ 8' for 'del-map('
9780 * '- 9' for 'del-map(' + trailing ')'
9781 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009782 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009783
9784 cur_arg = 1;
9785
9786 if (!*args[cur_arg] ||
9787 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9788 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9789 file, linenum, args[0]);
9790 goto out_err;
9791 }
9792
9793 LIST_INIT(&rule->arg.map.key);
9794 proxy->conf.args.ctx = ARGC_HRS;
9795 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9796 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9797 file, linenum);
9798 free(proxy->conf.lfs_file);
9799 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9800 proxy->conf.lfs_line = proxy->conf.args.line;
9801 cur_arg += 1;
9802 } else if (strncmp(args[0], "set-map", 7) == 0) {
9803 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9804 rule->action = HTTP_RES_ACT_SET_MAP;
9805 /*
9806 * '+ 8' for 'set-map('
9807 * '- 9' for 'set-map(' + trailing ')'
9808 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009809 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009810
9811 cur_arg = 1;
9812
9813 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9814 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9815 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9816 file, linenum, args[0]);
9817 goto out_err;
9818 }
9819
9820 LIST_INIT(&rule->arg.map.key);
9821 LIST_INIT(&rule->arg.map.value);
9822
9823 proxy->conf.args.ctx = ARGC_HRS;
9824
9825 /* key pattern */
9826 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9827 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9828 file, linenum);
9829
9830 /* value pattern */
9831 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9832 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9833 file, linenum);
9834
9835 free(proxy->conf.lfs_file);
9836 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9837 proxy->conf.lfs_line = proxy->conf.args.line;
9838
9839 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009840 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9841 char *errmsg = NULL;
9842 cur_arg = 1;
9843 /* try in the module list */
9844 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9845 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9846 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9847 free(errmsg);
9848 goto out_err;
9849 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009850 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009851 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', but got '%s'%s.\n",
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009852 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9853 goto out_err;
9854 }
9855
9856 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9857 struct acl_cond *cond;
9858 char *errmsg = NULL;
9859
9860 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9861 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9862 file, linenum, args[0], errmsg);
9863 free(errmsg);
9864 goto out_err;
9865 }
9866 rule->cond = cond;
9867 }
9868 else if (*args[cur_arg]) {
9869 Alert("parsing [%s:%d]: 'http-response %s' expects"
9870 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9871 file, linenum, args[0], args[cur_arg]);
9872 goto out_err;
9873 }
9874
9875 return rule;
9876 out_err:
9877 free(rule);
9878 return NULL;
9879}
9880
Willy Tarreau4baae242012-12-27 12:00:31 +01009881/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009882 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9883 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009884 */
9885struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009886 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009887{
9888 struct redirect_rule *rule;
9889 int cur_arg;
9890 int type = REDIRECT_TYPE_NONE;
9891 int code = 302;
9892 const char *destination = NULL;
9893 const char *cookie = NULL;
9894 int cookie_set = 0;
9895 unsigned int flags = REDIRECT_FLAG_NONE;
9896 struct acl_cond *cond = NULL;
9897
9898 cur_arg = 0;
9899 while (*(args[cur_arg])) {
9900 if (strcmp(args[cur_arg], "location") == 0) {
9901 if (!*args[cur_arg + 1])
9902 goto missing_arg;
9903
9904 type = REDIRECT_TYPE_LOCATION;
9905 cur_arg++;
9906 destination = args[cur_arg];
9907 }
9908 else if (strcmp(args[cur_arg], "prefix") == 0) {
9909 if (!*args[cur_arg + 1])
9910 goto missing_arg;
9911
9912 type = REDIRECT_TYPE_PREFIX;
9913 cur_arg++;
9914 destination = args[cur_arg];
9915 }
9916 else if (strcmp(args[cur_arg], "scheme") == 0) {
9917 if (!*args[cur_arg + 1])
9918 goto missing_arg;
9919
9920 type = REDIRECT_TYPE_SCHEME;
9921 cur_arg++;
9922 destination = args[cur_arg];
9923 }
9924 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9925 if (!*args[cur_arg + 1])
9926 goto missing_arg;
9927
9928 cur_arg++;
9929 cookie = args[cur_arg];
9930 cookie_set = 1;
9931 }
9932 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9933 if (!*args[cur_arg + 1])
9934 goto missing_arg;
9935
9936 cur_arg++;
9937 cookie = args[cur_arg];
9938 cookie_set = 0;
9939 }
9940 else if (strcmp(args[cur_arg], "code") == 0) {
9941 if (!*args[cur_arg + 1])
9942 goto missing_arg;
9943
9944 cur_arg++;
9945 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009946 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009947 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009948 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009949 args[cur_arg - 1], args[cur_arg]);
9950 return NULL;
9951 }
9952 }
9953 else if (!strcmp(args[cur_arg],"drop-query")) {
9954 flags |= REDIRECT_FLAG_DROP_QS;
9955 }
9956 else if (!strcmp(args[cur_arg],"append-slash")) {
9957 flags |= REDIRECT_FLAG_APPEND_SLASH;
9958 }
9959 else if (strcmp(args[cur_arg], "if") == 0 ||
9960 strcmp(args[cur_arg], "unless") == 0) {
9961 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9962 if (!cond) {
9963 memprintf(errmsg, "error in condition: %s", *errmsg);
9964 return NULL;
9965 }
9966 break;
9967 }
9968 else {
9969 memprintf(errmsg,
9970 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9971 args[cur_arg]);
9972 return NULL;
9973 }
9974 cur_arg++;
9975 }
9976
9977 if (type == REDIRECT_TYPE_NONE) {
9978 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9979 return NULL;
9980 }
9981
9982 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9983 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009984 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009985
9986 if (!use_fmt) {
9987 /* old-style static redirect rule */
9988 rule->rdr_str = strdup(destination);
9989 rule->rdr_len = strlen(destination);
9990 }
9991 else {
9992 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009993
9994 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9995 * if prefix == "/", we don't want to add anything, otherwise it
9996 * makes it hard for the user to configure a self-redirection.
9997 */
Godbachd9722032014-12-18 15:44:58 +08009998 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009999 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +010010000 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +010010001 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10002 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010003 free(curproxy->conf.lfs_file);
10004 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10005 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010006 }
10007 }
10008
Willy Tarreau4baae242012-12-27 12:00:31 +010010009 if (cookie) {
10010 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10011 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10012 */
10013 rule->cookie_len = strlen(cookie);
10014 if (cookie_set) {
10015 rule->cookie_str = malloc(rule->cookie_len + 10);
10016 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10017 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10018 rule->cookie_len += 9;
10019 } else {
10020 rule->cookie_str = malloc(rule->cookie_len + 21);
10021 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10022 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10023 rule->cookie_len += 20;
10024 }
10025 }
10026 rule->type = type;
10027 rule->code = code;
10028 rule->flags = flags;
10029 LIST_INIT(&rule->list);
10030 return rule;
10031
10032 missing_arg:
10033 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10034 return NULL;
10035}
10036
Willy Tarreau8797c062007-05-07 00:55:35 +020010037/************************************************************************/
10038/* The code below is dedicated to ACL parsing and matching */
10039/************************************************************************/
10040
10041
Willy Tarreau14174bc2012-04-16 14:34:04 +020010042/* This function ensures that the prerequisites for an L7 fetch are ready,
10043 * which means that a request or response is ready. If some data is missing,
10044 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010045 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10046 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010047 *
10048 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010049 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10050 * decide whether or not an HTTP message is present ;
10051 * 0 if the requested data cannot be fetched or if it is certain that
10052 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010053 * 1 if an HTTP message is ready
10054 */
10055static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010056smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010057 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010058{
Willy Tarreau192252e2015-04-04 01:47:55 +020010059 struct http_txn *txn;
10060 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010061
Willy Tarreaueee5b512015-04-03 23:46:31 +020010062 /* Note: this function may only be used from places where
10063 * http_init_txn() has already been done, and implies that <s>,
10064 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10065 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010066 */
10067
Willy Tarreau192252e2015-04-04 01:47:55 +020010068 if (!s)
10069 return 0;
10070 txn = s->txn;
10071
10072 if (!txn)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010073 return 0;
Willy Tarreau192252e2015-04-04 01:47:55 +020010074 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010075
10076 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +020010077 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010078
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010079 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010080 /* If the buffer does not leave enough free space at the end,
10081 * we must first realign it.
10082 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010083 if (s->req.buf->p > s->req.buf->data &&
10084 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10085 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010086
Willy Tarreau14174bc2012-04-16 14:34:04 +020010087 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010088 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010089 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010090
10091 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010092 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010093 http_msg_analyzer(msg, &txn->hdr_idx);
10094
10095 /* Still no valid request ? */
10096 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010097 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010098 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010099 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010100 }
10101 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010102 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010103 return 0;
10104 }
10105
10106 /* OK we just got a valid HTTP request. We have some minor
10107 * preparation to perform so that further checks can rely
10108 * on HTTP tests.
10109 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010110
10111 /* If the request was parsed but was too large, we must absolutely
10112 * return an error so that it is not processed. At the moment this
10113 * cannot happen, but if the parsers are to change in the future,
10114 * we want this check to be maintained.
10115 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010116 if (unlikely(s->req.buf->i + s->req.buf->p >
10117 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010118 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +020010119 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010120 return 1;
10121 }
10122
Willy Tarreau9b28e032012-10-12 23:49:43 +020010123 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010124 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010125 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010126
Willy Tarreau506d0502013-07-06 13:29:24 +020010127 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10128 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010129 }
10130
Willy Tarreau506d0502013-07-06 13:29:24 +020010131 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010132 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010133 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010134
10135 /* otherwise everything's ready for the request */
10136 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010137 else {
10138 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010139 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10140 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010141 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010142 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010143 }
10144
10145 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +020010146 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010147 return 1;
10148}
Willy Tarreau8797c062007-05-07 00:55:35 +020010149
Willy Tarreau6c616e02014-06-25 16:56:41 +020010150/* Note: these functinos *do* modify the sample. Even in case of success, at
10151 * least the type and uint value are modified.
10152 */
Willy Tarreauc0239e02012-04-16 14:42:55 +020010153#define CHECK_HTTP_MESSAGE_FIRST() \
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010154 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 +020010155
Willy Tarreau24e32d82012-04-23 23:55:44 +020010156#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010157 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 +020010158
Willy Tarreau8797c062007-05-07 00:55:35 +020010159
10160/* 1. Check on METHOD
10161 * We use the pre-parsed method if it is known, and store its number as an
10162 * integer. If it is unknown, we use the pointer and the length.
10163 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010164static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010165{
10166 int len, meth;
10167
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010168 len = strlen(text);
10169 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010170
10171 pattern->val.i = meth;
10172 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010173 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010174 pattern->len = len;
10175 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010176 else {
10177 pattern->ptr.str = NULL;
10178 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010179 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010180 return 1;
10181}
10182
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010183/* This function fetches the method of current HTTP request and stores
10184 * it in the global pattern struct as a chunk. There are two possibilities :
10185 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10186 * in <len> and <ptr> is NULL ;
10187 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10188 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010189 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010190 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010191static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010192smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010193{
10194 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010195 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010196
Willy Tarreau24e32d82012-04-23 23:55:44 +020010197 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010198
Willy Tarreau8797c062007-05-07 00:55:35 +020010199 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010200 smp->type = SMP_T_METH;
10201 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010202 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010203 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10204 /* ensure the indexes are not affected */
10205 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010206 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010207 smp->data.meth.str.len = txn->req.sl.rq.m_l;
10208 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010209 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010210 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010211 return 1;
10212}
10213
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010214/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010215static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010216{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010217 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010218 struct pattern_list *lst;
10219 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010220
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010221 list_for_each_entry(lst, &expr->patterns, list) {
10222 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010223
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010224 /* well-known method */
10225 if (pattern->val.i != HTTP_METH_OTHER) {
10226 if (smp->data.meth.meth == pattern->val.i)
10227 return pattern;
10228 else
10229 continue;
10230 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010231
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010232 /* Other method, we must compare the strings */
10233 if (pattern->len != smp->data.meth.str.len)
10234 continue;
10235
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010236 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010237 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10238 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010239 return pattern;
10240 }
10241 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010242}
10243
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010244static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010245smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010246{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010247 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010248 char *ptr;
10249 int len;
10250
Willy Tarreauc0239e02012-04-16 14:42:55 +020010251 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010252
Willy Tarreau8797c062007-05-07 00:55:35 +020010253 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010254 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010255
10256 while ((len-- > 0) && (*ptr++ != '/'));
10257 if (len <= 0)
10258 return 0;
10259
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010260 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010261 smp->data.str.str = ptr;
10262 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010263
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010264 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010265 return 1;
10266}
10267
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010268static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010269smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010270{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010271 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010272 char *ptr;
10273 int len;
10274
Willy Tarreauc0239e02012-04-16 14:42:55 +020010275 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010276
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010277 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010278 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10279 return 0;
10280
Willy Tarreau8797c062007-05-07 00:55:35 +020010281 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010282 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010283
10284 while ((len-- > 0) && (*ptr++ != '/'));
10285 if (len <= 0)
10286 return 0;
10287
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010288 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010289 smp->data.str.str = ptr;
10290 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010291
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010292 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010293 return 1;
10294}
10295
10296/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010297static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010298smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010299{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010300 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010301 char *ptr;
10302 int len;
10303
Willy Tarreauc0239e02012-04-16 14:42:55 +020010304 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010305
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010306 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010307 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10308 return 0;
10309
Willy Tarreau8797c062007-05-07 00:55:35 +020010310 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010311 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010312
Willy Tarreauf853c462012-04-23 18:53:56 +020010313 smp->type = SMP_T_UINT;
10314 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010315 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010316 return 1;
10317}
10318
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010319/* returns the longest available part of the body. This requires that the body
10320 * has been waited for using http-buffer-request.
10321 */
10322static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010323smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010324{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010325 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010326 struct http_msg *msg;
10327 unsigned long len;
10328 unsigned long block1;
10329 char *body;
10330 struct chunk *temp;
10331
10332 CHECK_HTTP_MESSAGE_FIRST();
10333
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010334 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010335 msg = &txn->req;
10336 else
10337 msg = &txn->rsp;
10338
10339 len = http_body_bytes(msg);
10340 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10341
10342 block1 = len;
10343 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10344 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10345
10346 if (block1 == len) {
10347 /* buffer is not wrapped (or empty) */
10348 smp->type = SMP_T_BIN;
10349 smp->data.str.str = body;
10350 smp->data.str.len = len;
10351 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10352 }
10353 else {
10354 /* buffer is wrapped, we need to defragment it */
10355 temp = get_trash_chunk();
10356 memcpy(temp->str, body, block1);
10357 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
10358 smp->type = SMP_T_BIN;
10359 smp->data.str.str = temp->str;
10360 smp->data.str.len = len;
10361 smp->flags = SMP_F_VOL_TEST;
10362 }
10363 return 1;
10364}
10365
10366
10367/* returns the available length of the body. This requires that the body
10368 * has been waited for using http-buffer-request.
10369 */
10370static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010371smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010372{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010373 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010374 struct http_msg *msg;
10375
10376 CHECK_HTTP_MESSAGE_FIRST();
10377
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010378 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010379 msg = &txn->req;
10380 else
10381 msg = &txn->rsp;
10382
10383 smp->type = SMP_T_UINT;
10384 smp->data.uint = http_body_bytes(msg);
10385
10386 smp->flags = SMP_F_VOL_TEST;
10387 return 1;
10388}
10389
10390
10391/* returns the advertised length of the body, or the advertised size of the
10392 * chunks available in the buffer. This requires that the body has been waited
10393 * for using http-buffer-request.
10394 */
10395static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010396smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010397{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010398 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010399 struct http_msg *msg;
10400
10401 CHECK_HTTP_MESSAGE_FIRST();
10402
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010403 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010404 msg = &txn->req;
10405 else
10406 msg = &txn->rsp;
10407
10408 smp->type = SMP_T_UINT;
10409 smp->data.uint = msg->body_len;
10410
10411 smp->flags = SMP_F_VOL_TEST;
10412 return 1;
10413}
10414
10415
Willy Tarreau8797c062007-05-07 00:55:35 +020010416/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010417static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010418smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010419{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010420 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010421
Willy Tarreauc0239e02012-04-16 14:42:55 +020010422 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010423
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010424 txn = smp->strm->txn;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010425 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010426 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010427 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010428 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010429 return 1;
10430}
10431
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010432static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010433smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010434{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010435 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010436 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010437
Willy Tarreauc0239e02012-04-16 14:42:55 +020010438 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010439
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010440 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010441 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 +020010442 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010443 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010444
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010445 smp->type = SMP_T_IPV4;
10446 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010447 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010448 return 1;
10449}
10450
10451static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010452smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010453{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010454 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010455 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010456
Willy Tarreauc0239e02012-04-16 14:42:55 +020010457 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010458
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010459 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010460 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 +020010461 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10462 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010463
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010464 smp->type = SMP_T_UINT;
10465 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010466 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010467 return 1;
10468}
10469
Willy Tarreau185b5c42012-04-26 15:11:51 +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 Tarreau04ff9f12013-06-10 18:39:42 +020010474 * headers are considered from the first one. It does not stop on commas and
10475 * returns full lines instead (useful for User-Agent or Date for example).
10476 */
10477static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010478smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010479{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010480 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010481 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010482 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010483 int occ = 0;
10484 const char *name_str = NULL;
10485 int name_len = 0;
10486
10487 if (!ctx) {
10488 /* first call */
10489 ctx = &static_hdr_ctx;
10490 ctx->idx = 0;
10491 smp->ctx.a[0] = ctx;
10492 }
10493
10494 if (args) {
10495 if (args[0].type != ARGT_STR)
10496 return 0;
10497 name_str = args[0].data.str.str;
10498 name_len = args[0].data.str.len;
10499
10500 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10501 occ = args[1].data.uint;
10502 }
10503
10504 CHECK_HTTP_MESSAGE_FIRST();
10505
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010506 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010507 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 +020010508
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010509 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10510 /* search for header from the beginning */
10511 ctx->idx = 0;
10512
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010513 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010514 /* no explicit occurrence and single fetch => last header by default */
10515 occ = -1;
10516
10517 if (!occ)
10518 /* prepare to report multiple occurrences for ACL fetches */
10519 smp->flags |= SMP_F_NOT_LAST;
10520
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010521 smp->type = SMP_T_STR;
10522 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010523 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10524 return 1;
10525
10526 smp->flags &= ~SMP_F_NOT_LAST;
10527 return 0;
10528}
10529
10530/* 6. Check on HTTP header count. The number of occurrences is returned.
10531 * Accepts exactly 1 argument of type string. It does not stop on commas and
10532 * returns full lines instead (useful for User-Agent or Date for example).
10533 */
10534static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010535smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010536{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010537 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010538 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010539 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010540 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010541 const char *name = NULL;
10542 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010543
Willy Tarreau601a4d12015-04-01 19:16:09 +020010544 if (args && args->type == ARGT_STR) {
10545 name = args->data.str.str;
10546 len = args->data.str.len;
10547 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010548
10549 CHECK_HTTP_MESSAGE_FIRST();
10550
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010551 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010552 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 +020010553
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010554 ctx.idx = 0;
10555 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010556 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010557 cnt++;
10558
10559 smp->type = SMP_T_UINT;
10560 smp->data.uint = cnt;
10561 smp->flags = SMP_F_VOL_HDR;
10562 return 1;
10563}
10564
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010565static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010566smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010567{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010568 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010569 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010570 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010571 struct chunk *temp;
10572 char del = ',';
10573
10574 if (args && args->type == ARGT_STR)
10575 del = *args[0].data.str.str;
10576
10577 CHECK_HTTP_MESSAGE_FIRST();
10578
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010579 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010580 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 +020010581
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010582 temp = get_trash_chunk();
10583
10584 ctx.idx = 0;
10585 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10586 if (temp->len)
10587 temp->str[temp->len++] = del;
10588 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10589 temp->len += ctx.del;
10590 }
10591
10592 smp->type = SMP_T_STR;
10593 smp->data.str.str = temp->str;
10594 smp->data.str.len = temp->len;
10595 smp->flags = SMP_F_VOL_HDR;
10596 return 1;
10597}
10598
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010599/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10600 * Accepts an optional argument of type string containing the header field name,
10601 * and an optional argument of type signed or unsigned integer to request an
10602 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010603 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010604 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010605static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010606smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010607{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010608 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010609 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010610 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010611 int occ = 0;
10612 const char *name_str = NULL;
10613 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010614
Willy Tarreaua890d072013-04-02 12:01:06 +020010615 if (!ctx) {
10616 /* first call */
10617 ctx = &static_hdr_ctx;
10618 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010619 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010620 }
10621
Willy Tarreau185b5c42012-04-26 15:11:51 +020010622 if (args) {
10623 if (args[0].type != ARGT_STR)
10624 return 0;
10625 name_str = args[0].data.str.str;
10626 name_len = args[0].data.str.len;
10627
10628 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10629 occ = args[1].data.uint;
10630 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010631
Willy Tarreaue333ec92012-04-16 16:26:40 +020010632 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010633
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010634 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010635 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 +020010636
Willy Tarreau185b5c42012-04-26 15:11:51 +020010637 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010638 /* search for header from the beginning */
10639 ctx->idx = 0;
10640
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010641 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010642 /* no explicit occurrence and single fetch => last header by default */
10643 occ = -1;
10644
10645 if (!occ)
10646 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010647 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010648
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010649 smp->type = SMP_T_STR;
10650 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010651 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010652 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010653
Willy Tarreau37406352012-04-23 16:16:37 +020010654 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010655 return 0;
10656}
10657
Willy Tarreauc11416f2007-06-17 16:58:38 +020010658/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010659 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010660 */
10661static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010662smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010663{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010664 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010665 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010666 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010667 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010668 const char *name = NULL;
10669 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010670
Willy Tarreau601a4d12015-04-01 19:16:09 +020010671 if (args && args->type == ARGT_STR) {
10672 name = args->data.str.str;
10673 len = args->data.str.len;
10674 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010675
Willy Tarreaue333ec92012-04-16 16:26:40 +020010676 CHECK_HTTP_MESSAGE_FIRST();
10677
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010678 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010679 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 +020010680
Willy Tarreau33a7e692007-06-10 19:45:56 +020010681 ctx.idx = 0;
10682 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010683 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010684 cnt++;
10685
Willy Tarreauf853c462012-04-23 18:53:56 +020010686 smp->type = SMP_T_UINT;
10687 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010688 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010689 return 1;
10690}
10691
Willy Tarreau185b5c42012-04-26 15:11:51 +020010692/* Fetch an HTTP header's integer value. The integer value is returned. It
10693 * takes a mandatory argument of type string and an optional one of type int
10694 * to designate a specific occurrence. It returns an unsigned integer, which
10695 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010696 */
10697static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010698smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010699{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010700 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010701
Willy Tarreauf853c462012-04-23 18:53:56 +020010702 if (ret > 0) {
10703 smp->type = SMP_T_UINT;
10704 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10705 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010706
Willy Tarreaud53e2422012-04-16 17:21:11 +020010707 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010708}
10709
Cyril Bonté69fa9922012-10-25 00:01:06 +020010710/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10711 * and an optional one of type int to designate a specific occurrence.
10712 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010713 */
10714static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010715smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010716{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010717 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010718
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010719 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010720 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10721 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010722 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010723 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010724 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010725 if (smp->data.str.len < temp->size - 1) {
10726 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10727 temp->str[smp->data.str.len] = '\0';
10728 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10729 smp->type = SMP_T_IPV6;
10730 break;
10731 }
10732 }
10733 }
10734
Willy Tarreaud53e2422012-04-16 17:21:11 +020010735 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010736 if (!(smp->flags & SMP_F_NOT_LAST))
10737 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010738 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010739 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010740}
10741
Willy Tarreau737b0c12007-06-10 21:28:46 +020010742/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10743 * the first '/' after the possible hostname, and ends before the possible '?'.
10744 */
10745static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010746smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010747{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010748 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010749 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010750
Willy Tarreauc0239e02012-04-16 14:42:55 +020010751 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010752
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010753 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010754 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010755 ptr = http_get_path(txn);
10756 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010757 return 0;
10758
10759 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010760 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010761 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010762
10763 while (ptr < end && *ptr != '?')
10764 ptr++;
10765
Willy Tarreauf853c462012-04-23 18:53:56 +020010766 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010767 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010768 return 1;
10769}
10770
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010771/* This produces a concatenation of the first occurrence of the Host header
10772 * followed by the path component if it begins with a slash ('/'). This means
10773 * that '*' will not be added, resulting in exactly the first Host entry.
10774 * If no Host header is found, then the path is returned as-is. The returned
10775 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010776 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010777 */
10778static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010779smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010780{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010781 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010782 char *ptr, *end, *beg;
10783 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010784 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010785
10786 CHECK_HTTP_MESSAGE_FIRST();
10787
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010788 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010789 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010790 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010791 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010792
10793 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010794 temp = get_trash_chunk();
10795 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010796 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010797 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010798 smp->data.str.len = ctx.vlen;
10799
10800 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010801 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010802 beg = http_get_path(txn);
10803 if (!beg)
10804 beg = end;
10805
10806 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10807
10808 if (beg < ptr && *beg == '/') {
10809 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10810 smp->data.str.len += ptr - beg;
10811 }
10812
10813 smp->flags = SMP_F_VOL_1ST;
10814 return 1;
10815}
10816
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010817/* This produces a 32-bit hash of the concatenation of the first occurrence of
10818 * the Host header followed by the path component if it begins with a slash ('/').
10819 * This means that '*' will not be added, resulting in exactly the first Host
10820 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010821 * is hashed using the path hash followed by a full avalanche hash and provides a
10822 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010823 * high-traffic sites without having to store whole paths.
10824 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010825int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010826smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010827{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010828 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010829 struct hdr_ctx ctx;
10830 unsigned int hash = 0;
10831 char *ptr, *beg, *end;
10832 int len;
10833
10834 CHECK_HTTP_MESSAGE_FIRST();
10835
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010836 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010837 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010838 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010839 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10840 ptr = ctx.line + ctx.val;
10841 len = ctx.vlen;
10842 while (len--)
10843 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10844 }
10845
10846 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010847 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010848 beg = http_get_path(txn);
10849 if (!beg)
10850 beg = end;
10851
10852 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10853
10854 if (beg < ptr && *beg == '/') {
10855 while (beg < ptr)
10856 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10857 }
10858 hash = full_hash(hash);
10859
10860 smp->type = SMP_T_UINT;
10861 smp->data.uint = hash;
10862 smp->flags = SMP_F_VOL_1ST;
10863 return 1;
10864}
10865
Willy Tarreau4a550602012-12-09 14:53:32 +010010866/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010867 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10868 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10869 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010870 * that in environments where IPv6 is insignificant, truncating the output to
10871 * 8 bytes would still work.
10872 */
10873static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010874smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010875{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010876 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010877 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010878
10879 if (!cli_conn)
10880 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010881
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010882 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010883 return 0;
10884
Willy Tarreau47ca5452012-12-23 20:22:19 +010010885 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010886 *(unsigned int *)temp->str = htonl(smp->data.uint);
10887 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010888
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010889 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010890 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010891 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010892 temp->len += 4;
10893 break;
10894 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010895 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010896 temp->len += 16;
10897 break;
10898 default:
10899 return 0;
10900 }
10901
10902 smp->data.str = *temp;
10903 smp->type = SMP_T_BIN;
10904 return 1;
10905}
10906
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010907/* Extracts the query string, which comes after the question mark '?'. If no
10908 * question mark is found, nothing is returned. Otherwise it returns a sample
10909 * of type string carrying the whole query string.
10910 */
10911static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010912smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010913{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010914 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010915 char *ptr, *end;
10916
10917 CHECK_HTTP_MESSAGE_FIRST();
10918
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010919 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010920 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10921 end = ptr + txn->req.sl.rq.u_l;
10922
10923 /* look up the '?' */
10924 do {
10925 if (ptr == end)
10926 return 0;
10927 } while (*ptr++ != '?');
10928
10929 smp->type = SMP_T_STR;
10930 smp->data.str.str = ptr;
10931 smp->data.str.len = end - ptr;
10932 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10933 return 1;
10934}
10935
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010936static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010937smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010938{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010939 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10940 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10941 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010942
Willy Tarreau24e32d82012-04-23 23:55:44 +020010943 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010944
Willy Tarreauf853c462012-04-23 18:53:56 +020010945 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010946 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010947 return 1;
10948}
10949
Willy Tarreau7f18e522010-10-22 20:04:13 +020010950/* return a valid test if the current request is the first one on the connection */
10951static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010952smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010953{
Willy Tarreauf853c462012-04-23 18:53:56 +020010954 smp->type = SMP_T_BOOL;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010955 smp->data.uint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010956 return 1;
10957}
10958
Willy Tarreau34db1082012-04-19 17:16:54 +020010959/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010960static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010961smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010962{
10963
Willy Tarreau24e32d82012-04-23 23:55:44 +020010964 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010965 return 0;
10966
Willy Tarreauc0239e02012-04-16 14:42:55 +020010967 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010968
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010969 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010970 return 0;
10971
Willy Tarreauf853c462012-04-23 18:53:56 +020010972 smp->type = SMP_T_BOOL;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010973 smp->data.uint = check_user(args->data.usr, smp->strm->txn->auth.user,
10974 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010975 return 1;
10976}
Willy Tarreau8797c062007-05-07 00:55:35 +020010977
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010978/* Accepts exactly 1 argument of type userlist */
10979static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010980smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010981{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010982 if (!args || args->type != ARGT_USR)
10983 return 0;
10984
10985 CHECK_HTTP_MESSAGE_FIRST();
10986
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010987 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010988 return 0;
10989
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010990 /* if the user does not belong to the userlist or has a wrong password,
10991 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010992 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010993 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010994 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10995 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010996 return 0;
10997
10998 /* pat_match_auth() will need the user list */
10999 smp->ctx.a[0] = args->data.usr;
11000
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011001 smp->type = SMP_T_STR;
11002 smp->flags = SMP_F_CONST;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011003 smp->data.str.str = smp->strm->txn->auth.user;
11004 smp->data.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011005
11006 return 1;
11007}
11008
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011009/* Try to find the next occurrence of a cookie name in a cookie header value.
11010 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11011 * the cookie value is returned into *value and *value_l, and the function
11012 * returns a pointer to the next pointer to search from if the value was found.
11013 * Otherwise if the cookie was not found, NULL is returned and neither value
11014 * nor value_l are touched. The input <hdr> string should first point to the
11015 * header's value, and the <hdr_end> pointer must point to the first character
11016 * not part of the value. <list> must be non-zero if value may represent a list
11017 * of values (cookie headers). This makes it faster to abort parsing when no
11018 * list is expected.
11019 */
11020static char *
11021extract_cookie_value(char *hdr, const char *hdr_end,
11022 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011023 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011024{
11025 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11026 char *next;
11027
11028 /* we search at least a cookie name followed by an equal, and more
11029 * generally something like this :
11030 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11031 */
11032 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11033 /* Iterate through all cookies on this line */
11034
11035 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
11036 att_beg++;
11037
11038 /* find att_end : this is the first character after the last non
11039 * space before the equal. It may be equal to hdr_end.
11040 */
11041 equal = att_end = att_beg;
11042
11043 while (equal < hdr_end) {
11044 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11045 break;
11046 if (http_is_spht[(unsigned char)*equal++])
11047 continue;
11048 att_end = equal;
11049 }
11050
11051 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11052 * is between <att_beg> and <equal>, both may be identical.
11053 */
11054
11055 /* look for end of cookie if there is an equal sign */
11056 if (equal < hdr_end && *equal == '=') {
11057 /* look for the beginning of the value */
11058 val_beg = equal + 1;
11059 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
11060 val_beg++;
11061
11062 /* find the end of the value, respecting quotes */
11063 next = find_cookie_value_end(val_beg, hdr_end);
11064
11065 /* make val_end point to the first white space or delimitor after the value */
11066 val_end = next;
11067 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
11068 val_end--;
11069 } else {
11070 val_beg = val_end = next = equal;
11071 }
11072
11073 /* We have nothing to do with attributes beginning with '$'. However,
11074 * they will automatically be removed if a header before them is removed,
11075 * since they're supposed to be linked together.
11076 */
11077 if (*att_beg == '$')
11078 continue;
11079
11080 /* Ignore cookies with no equal sign */
11081 if (equal == next)
11082 continue;
11083
11084 /* Now we have the cookie name between att_beg and att_end, and
11085 * its value between val_beg and val_end.
11086 */
11087
11088 if (att_end - att_beg == cookie_name_l &&
11089 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11090 /* let's return this value and indicate where to go on from */
11091 *value = val_beg;
11092 *value_l = val_end - val_beg;
11093 return next + 1;
11094 }
11095
11096 /* Set-Cookie headers only have the name in the first attr=value part */
11097 if (!list)
11098 break;
11099 }
11100
11101 return NULL;
11102}
11103
William Lallemanda43ba4e2014-01-28 18:14:25 +010011104/* Fetch a captured HTTP request header. The index is the position of
11105 * the "capture" option in the configuration file
11106 */
11107static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011108smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011109{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011110 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011111 int idx;
11112
11113 if (!args || args->type != ARGT_UINT)
11114 return 0;
11115
11116 idx = args->data.uint;
11117
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011118 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 +010011119 return 0;
11120
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011121 smp->type = SMP_T_STR;
11122 smp->flags |= SMP_F_CONST;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011123 smp->data.str.str = smp->strm->req_cap[idx];
11124 smp->data.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011125
11126 return 1;
11127}
11128
11129/* Fetch a captured HTTP response header. The index is the position of
11130 * the "capture" option in the configuration file
11131 */
11132static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011133smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011134{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011135 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011136 int idx;
11137
11138 if (!args || args->type != ARGT_UINT)
11139 return 0;
11140
11141 idx = args->data.uint;
11142
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011143 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 +010011144 return 0;
11145
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011146 smp->type = SMP_T_STR;
11147 smp->flags |= SMP_F_CONST;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011148 smp->data.str.str = smp->strm->res_cap[idx];
11149 smp->data.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011150
11151 return 1;
11152}
11153
William Lallemand65ad6e12014-01-31 15:08:02 +010011154/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11155static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011156smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011157{
11158 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011159 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011160 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011161
Willy Tarreau15e91e12015-04-04 00:52:09 +020011162 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011163 return 0;
11164
William Lallemand96a77852014-02-05 00:30:02 +010011165 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011166
William Lallemand96a77852014-02-05 00:30:02 +010011167 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11168 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011169
William Lallemand96a77852014-02-05 00:30:02 +010011170 temp = get_trash_chunk();
11171 temp->str = txn->uri;
11172 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011173 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011174 smp->type = SMP_T_STR;
11175 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011176
11177 return 1;
11178
11179}
11180
11181/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11182static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011183smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011184{
11185 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011186 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011187 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011188
Willy Tarreau15e91e12015-04-04 00:52:09 +020011189 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011190 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011191
William Lallemand65ad6e12014-01-31 15:08:02 +010011192 ptr = txn->uri;
11193
11194 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11195 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011196
William Lallemand65ad6e12014-01-31 15:08:02 +010011197 if (!*ptr)
11198 return 0;
11199
11200 ptr++; /* skip the space */
11201
11202 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011203 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011204 if (!ptr)
11205 return 0;
11206 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11207 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011208
11209 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010011210 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011211 smp->type = SMP_T_STR;
11212 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011213
11214 return 1;
11215}
11216
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011217/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11218 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11219 */
11220static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011221smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011222{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011223 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011224
Willy Tarreau15e91e12015-04-04 00:52:09 +020011225 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011226 return 0;
11227
11228 if (txn->req.flags & HTTP_MSGF_VER_11)
11229 smp->data.str.str = "HTTP/1.1";
11230 else
11231 smp->data.str.str = "HTTP/1.0";
11232
11233 smp->data.str.len = 8;
11234 smp->type = SMP_T_STR;
11235 smp->flags = SMP_F_CONST;
11236 return 1;
11237
11238}
11239
11240/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11241 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11242 */
11243static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011244smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011245{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011246 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011247
Willy Tarreau15e91e12015-04-04 00:52:09 +020011248 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011249 return 0;
11250
11251 if (txn->rsp.flags & HTTP_MSGF_VER_11)
11252 smp->data.str.str = "HTTP/1.1";
11253 else
11254 smp->data.str.str = "HTTP/1.0";
11255
11256 smp->data.str.len = 8;
11257 smp->type = SMP_T_STR;
11258 smp->flags = SMP_F_CONST;
11259 return 1;
11260
11261}
11262
William Lallemand65ad6e12014-01-31 15:08:02 +010011263
Willy Tarreaue333ec92012-04-16 16:26:40 +020011264/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011265 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011266 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011267 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011268 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011269 * Accepts exactly 1 argument of type string. If the input options indicate
11270 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011271 * The returned sample is of type CSTR. Can be used to parse cookies in other
11272 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011273 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011274int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011275{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011276 struct http_txn *txn;
11277 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011278 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011279 const struct http_msg *msg;
11280 const char *hdr_name;
11281 int hdr_name_len;
11282 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011283 int occ = 0;
11284 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011285
Willy Tarreau24e32d82012-04-23 23:55:44 +020011286 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011287 return 0;
11288
Willy Tarreaua890d072013-04-02 12:01:06 +020011289 if (!ctx) {
11290 /* first call */
11291 ctx = &static_hdr_ctx;
11292 ctx->idx = 0;
11293 smp->ctx.a[2] = ctx;
11294 }
11295
Willy Tarreaue333ec92012-04-16 16:26:40 +020011296 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011297
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011298 txn = smp->strm->txn;
11299 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011300
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011301 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011302 msg = &txn->req;
11303 hdr_name = "Cookie";
11304 hdr_name_len = 6;
11305 } else {
11306 msg = &txn->rsp;
11307 hdr_name = "Set-Cookie";
11308 hdr_name_len = 10;
11309 }
11310
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011311 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011312 /* no explicit occurrence and single fetch => last cookie by default */
11313 occ = -1;
11314
11315 /* OK so basically here, either we want only one value and it's the
11316 * last one, or we want to iterate over all of them and we fetch the
11317 * next one.
11318 */
11319
Willy Tarreau9b28e032012-10-12 23:49:43 +020011320 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011321 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011322 /* search for the header from the beginning, we must first initialize
11323 * the search parameters.
11324 */
Willy Tarreau37406352012-04-23 16:16:37 +020011325 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011326 ctx->idx = 0;
11327 }
11328
Willy Tarreau28376d62012-04-26 21:26:10 +020011329 smp->flags |= SMP_F_VOL_HDR;
11330
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011331 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011332 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11333 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011334 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11335 goto out;
11336
Willy Tarreau24e32d82012-04-23 23:55:44 +020011337 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011338 continue;
11339
Willy Tarreau37406352012-04-23 16:16:37 +020011340 smp->ctx.a[0] = ctx->line + ctx->val;
11341 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011342 }
11343
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011344 smp->type = SMP_T_STR;
11345 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011346 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011347 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011348 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011349 &smp->data.str.str,
11350 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011351 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011352 found = 1;
11353 if (occ >= 0) {
11354 /* one value was returned into smp->data.str.{str,len} */
11355 smp->flags |= SMP_F_NOT_LAST;
11356 return 1;
11357 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011358 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011359 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011360 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011361 /* all cookie headers and values were scanned. If we're looking for the
11362 * last occurrence, we may return it now.
11363 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011364 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011365 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011366 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011367}
11368
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011369/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011370 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011371 * multiple cookies may be parsed on the same line. The returned sample is of
11372 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011373 */
11374static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011375smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011376{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011377 struct http_txn *txn;
11378 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011379 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011380 const struct http_msg *msg;
11381 const char *hdr_name;
11382 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011383 int cnt;
11384 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011385 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011386
Willy Tarreau24e32d82012-04-23 23:55:44 +020011387 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011388 return 0;
11389
Willy Tarreaue333ec92012-04-16 16:26:40 +020011390 CHECK_HTTP_MESSAGE_FIRST();
11391
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011392 txn = smp->strm->txn;
11393 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011394
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011395 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011396 msg = &txn->req;
11397 hdr_name = "Cookie";
11398 hdr_name_len = 6;
11399 } else {
11400 msg = &txn->rsp;
11401 hdr_name = "Set-Cookie";
11402 hdr_name_len = 10;
11403 }
11404
Willy Tarreau9b28e032012-10-12 23:49:43 +020011405 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011406 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011407 ctx.idx = 0;
11408 cnt = 0;
11409
11410 while (1) {
11411 /* Note: val_beg == NULL every time we need to fetch a new header */
11412 if (!val_beg) {
11413 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11414 break;
11415
Willy Tarreau24e32d82012-04-23 23:55:44 +020011416 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011417 continue;
11418
11419 val_beg = ctx.line + ctx.val;
11420 val_end = val_beg + ctx.vlen;
11421 }
11422
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011423 smp->type = SMP_T_STR;
11424 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011425 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011426 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011427 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011428 &smp->data.str.str,
11429 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011430 cnt++;
11431 }
11432 }
11433
Willy Tarreaub169eba2013-12-16 15:14:43 +010011434 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011435 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011436 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011437 return 1;
11438}
11439
Willy Tarreau51539362012-05-08 12:46:28 +020011440/* Fetch an cookie's integer value. The integer value is returned. It
11441 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11442 */
11443static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011444smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011445{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011446 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011447
11448 if (ret > 0) {
11449 smp->type = SMP_T_UINT;
11450 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11451 }
11452
11453 return ret;
11454}
11455
Willy Tarreau8797c062007-05-07 00:55:35 +020011456/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011457/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011458/************************************************************************/
11459
David Cournapeau16023ee2010-12-23 20:55:41 +090011460/*
11461 * Given a path string and its length, find the position of beginning of the
11462 * query string. Returns NULL if no query string is found in the path.
11463 *
11464 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11465 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011466 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011467 */
bedis4c75cca2012-10-05 08:38:24 +020011468static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011469{
11470 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011471
bedis4c75cca2012-10-05 08:38:24 +020011472 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011473 return p ? p + 1 : NULL;
11474}
11475
bedis4c75cca2012-10-05 08:38:24 +020011476static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011477{
bedis4c75cca2012-10-05 08:38:24 +020011478 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011479}
11480
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011481/* after increasing a pointer value, it can exceed the first buffer
11482 * size. This function transform the value of <ptr> according with
11483 * the expected position. <chunks> is an array of the one or two
11484 * avalaible chunks. The first value is the start of the first chunk,
11485 * the second value if the end+1 of the first chunks. The third value
11486 * is NULL or the start of the second chunk and the fourth value is
11487 * the end+1 of the second chunk. The function returns 1 if does a
11488 * wrap, else returns 0.
11489 */
11490static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11491{
11492 if (*ptr < chunks[1])
11493 return 0;
11494 if (!chunks[2])
11495 return 0;
11496 *ptr = chunks[2] + ( *ptr - chunks[1] );
11497 return 1;
11498}
11499
David Cournapeau16023ee2010-12-23 20:55:41 +090011500/*
11501 * Given a url parameter, find the starting position of the first occurence,
11502 * or NULL if the parameter is not found.
11503 *
11504 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11505 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011506 *
11507 * Warning:this function returns a pointer that can be point to the first chunk
11508 * or the second chunk. The caller must be check the position before using the
11509 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011510 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011511static const char *
11512find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011513 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011514 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011515{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011516 const char *pos, *last, *equal;
11517 const char **bufs = chunks;
11518 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011519
David Cournapeau16023ee2010-12-23 20:55:41 +090011520
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011521 pos = bufs[0];
11522 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011523 while (pos <= last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011524 /* Check the equal. */
11525 equal = pos + url_param_name_l;
11526 if (fix_pointer_if_wrap(chunks, &equal)) {
11527 if (equal >= chunks[3])
11528 return NULL;
11529 } else {
11530 if (equal >= chunks[1])
11531 return NULL;
11532 }
11533 if (*equal == '=') {
11534 if (pos + url_param_name_l > last) {
11535 /* process wrap case, we detect a wrap. In this case, the
11536 * comparison is performed in two parts.
11537 */
11538
11539 /* This is the end, we dont have any other chunk. */
11540 if (bufs != chunks || !bufs[2])
11541 return NULL;
11542
11543 /* Compute the length of each part of the comparison. */
11544 l1 = last - pos;
11545 l2 = url_param_name_l - l1;
11546
11547 /* The second buffer is too short to contain the compared string. */
11548 if (bufs[2] + l2 > bufs[3])
11549 return NULL;
11550
11551 if (memcmp(pos, url_param_name, l1) == 0 &&
11552 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11553 return pos;
11554
11555 /* Perform wrapping and jump the string who fail the comparison. */
11556 bufs += 2;
11557 pos = bufs[0] + l2;
11558 last = bufs[1];
11559
11560 } else {
11561 /* process a simple comparison. */
11562 if (memcmp(pos, url_param_name, url_param_name_l) == 0) {
11563 return pos; }
11564 pos += url_param_name_l + 1;
11565 if (fix_pointer_if_wrap(chunks, &pos))
11566 last = bufs[2];
11567 }
11568 }
11569
11570 while (1) {
11571 /* Look for the next delimiter. */
11572 while (pos <= last && !is_param_delimiter(*pos, delim))
11573 pos++;
11574 if (pos < last)
11575 break;
11576 /* process buffer wrapping. */
11577 if (bufs != chunks || !bufs[2])
11578 return NULL;
11579 bufs += 2;
11580 pos = bufs[0];
11581 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011582 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011583 pos++;
11584 }
11585 return NULL;
11586}
11587
11588/*
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011589 * Given a url parameter name and a query string, returns its value and size
11590 * into *value and *value_l respectively, and returns non-zero. An empty
11591 * url_param_name matches the first available parameter. If the parameter is
11592 * not found, zero is returned and value/value_l are not touched.
David Cournapeau16023ee2010-12-23 20:55:41 +090011593 */
11594static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011595find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011596 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011597 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011598{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011599 const char *arg_start, *qs_end;
11600 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011601
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011602 arg_start = chunks[0];
11603 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011604 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011605 /* Looks for an argument name. */
11606 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011607 url_param_name, url_param_name_l,
11608 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011609 /* Check for wrapping. */
11610 if (arg_start > qs_end)
11611 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011612 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011613 if (!arg_start)
11614 return 0;
11615
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011616 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011617 while (1) {
11618 /* looks for the first argument. */
11619 value_start = memchr(arg_start, '=', qs_end - arg_start);
11620 if (!value_start) {
11621
11622 /* Check for wrapping. */
11623 if (arg_start >= chunks[0] &&
11624 arg_start <= chunks[1] &&
11625 chunks[2]) {
11626 arg_start = chunks[2];
11627 qs_end = chunks[3];
11628 continue;
11629 }
11630 return 0;
11631 }
11632 break;
11633 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011634 value_start++;
11635 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011636 else {
11637 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011638 value_start = arg_start + url_param_name_l + 1;
11639
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011640 /* Check for pointer wrapping. */
11641 if (fix_pointer_if_wrap(chunks, &value_start)) {
11642 /* Update the end pointer. */
11643 qs_end = chunks[3];
11644
11645 /* Check for overflow. */
11646 if (value_start > qs_end)
11647 return 0;
11648 }
11649 }
11650
David Cournapeau16023ee2010-12-23 20:55:41 +090011651 value_end = value_start;
11652
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011653 while (1) {
11654 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11655 value_end++;
11656 if (value_end < qs_end)
11657 break;
11658 /* process buffer wrapping. */
11659 if (value_end >= chunks[0] &&
11660 value_end <= chunks[1] &&
11661 chunks[2]) {
11662 value_end = chunks[2];
11663 qs_end = chunks[3];
11664 continue;
11665 }
11666 break;
11667 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011668
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011669 *vstart = value_start;
11670 *vend = value_end;
Willy Tarreau00134332011-01-04 14:57:34 +010011671 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011672}
11673
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011674/* This scans a URL-encoded query string. It takes an optionally wrapping
11675 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11676 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11677 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011678 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011679static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011680smp_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 +090011681{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011682 const char *vstart, *vend;
11683 struct chunk *temp;
11684 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011685
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011686 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011687 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011688 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011689 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011690 return 0;
11691
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011692 /* Create sample. If the value is contiguous, return the pointer as CONST,
11693 * if the value is wrapped, copy-it in a buffer.
11694 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011695 smp->type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011696 if (chunks[2] &&
11697 vstart >= chunks[0] && vstart <= chunks[1] &&
11698 vend >= chunks[2] && vend <= chunks[3]) {
11699 /* Wrapped case. */
11700 temp = get_trash_chunk();
11701 memcpy(temp->str, vstart, chunks[1] - vstart);
11702 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
11703 smp->data.str.str = temp->str;
11704 smp->data.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
11705 } else {
11706 /* Contiguous case. */
11707 smp->data.str.str = (char *)vstart;
11708 smp->data.str.len = vend - vstart;
11709 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11710 }
11711
11712 /* Update context, check wrapping. */
11713 chunks[0] = vend;
11714 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11715 chunks[1] = chunks[3];
11716 chunks[2] = NULL;
11717 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011718
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011719 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011720 smp->flags |= SMP_F_NOT_LAST;
11721
David Cournapeau16023ee2010-12-23 20:55:41 +090011722 return 1;
11723}
11724
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011725/* This function iterates over each parameter of the query string. It uses
11726 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011727 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11728 * An optional parameter name is passed in args[0], otherwise any parameter is
11729 * considered. It supports an optional delimiter argument for the beginning of
11730 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011731 */
11732static int
11733smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11734{
11735 struct http_msg *msg;
11736 char delim = '?';
11737 const char *name;
11738 int name_len;
11739
Dragan Dosen26f77e52015-05-25 10:02:11 +020011740 if (!args ||
11741 (args[0].type && args[0].type != ARGT_STR) ||
11742 (args[1].type && args[1].type != ARGT_STR))
11743 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011744
Dragan Dosen26f77e52015-05-25 10:02:11 +020011745 name = "";
11746 name_len = 0;
11747 if (args->type == ARGT_STR) {
11748 name = args->data.str.str;
11749 name_len = args->data.str.len;
11750 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011751
Dragan Dosen26f77e52015-05-25 10:02:11 +020011752 if (args[1].type)
11753 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011754
Dragan Dosen26f77e52015-05-25 10:02:11 +020011755 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011756 CHECK_HTTP_MESSAGE_FIRST();
11757
11758 msg = &smp->strm->txn->req;
11759
11760 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11761 msg->sl.rq.u_l, delim);
11762 if (!smp->ctx.a[0])
11763 return 0;
11764
11765 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011766
11767 /* Assume that the context is filled with NULL pointer
11768 * before the first call.
11769 * smp->ctx.a[2] = NULL;
11770 * smp->ctx.a[3] = NULL;
11771 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011772 }
11773
11774 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11775}
11776
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011777/* This function iterates over each parameter of the body. This requires
11778 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011779 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11780 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11781 * optional second part if the body wraps at the end of the buffer. An optional
11782 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011783 */
11784static int
11785smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11786{
11787 struct http_txn *txn = smp->strm->txn;
11788 struct http_msg *msg;
11789 unsigned long len;
11790 unsigned long block1;
11791 char *body;
11792 const char *name;
11793 int name_len;
11794
11795 if (!args || (args[0].type && args[0].type != ARGT_STR))
11796 return 0;
11797
11798 name = "";
11799 name_len = 0;
11800 if (args[0].type == ARGT_STR) {
11801 name = args[0].data.str.str;
11802 name_len = args[0].data.str.len;
11803 }
11804
11805 if (!smp->ctx.a[0]) { // first call, find the query string
11806 CHECK_HTTP_MESSAGE_FIRST();
11807
11808 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11809 msg = &txn->req;
11810 else
11811 msg = &txn->rsp;
11812
11813 len = http_body_bytes(msg);
11814 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11815
11816 block1 = len;
11817 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11818 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11819
11820 if (block1 == len) {
11821 /* buffer is not wrapped (or empty) */
11822 smp->ctx.a[0] = body;
11823 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011824
11825 /* Assume that the context is filled with NULL pointer
11826 * before the first call.
11827 * smp->ctx.a[2] = NULL;
11828 * smp->ctx.a[3] = NULL;
11829 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011830 }
11831 else {
11832 /* buffer is wrapped, we need to defragment it */
11833 smp->ctx.a[0] = body;
11834 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011835 smp->ctx.a[2] = msg->chn->buf->data;
11836 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011837 }
11838 }
11839 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11840}
11841
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011842/* Return the signed integer value for the specified url parameter (see url_param
11843 * above).
11844 */
11845static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011846smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011847{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011848 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011849
11850 if (ret > 0) {
11851 smp->type = SMP_T_UINT;
11852 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11853 }
11854
11855 return ret;
11856}
11857
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011858/* This produces a 32-bit hash of the concatenation of the first occurrence of
11859 * the Host header followed by the path component if it begins with a slash ('/').
11860 * This means that '*' will not be added, resulting in exactly the first Host
11861 * entry. If no Host header is found, then the path is used. The resulting value
11862 * is hashed using the url hash followed by a full avalanche hash and provides a
11863 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11864 * high-traffic sites without having to store whole paths.
11865 * this differs from the base32 functions in that it includes the url parameters
11866 * as well as the path
11867 */
11868static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011869smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011870{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011871 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011872 struct hdr_ctx ctx;
11873 unsigned int hash = 0;
11874 char *ptr, *beg, *end;
11875 int len;
11876
11877 CHECK_HTTP_MESSAGE_FIRST();
11878
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011879 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011880 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011881 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011882 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11883 ptr = ctx.line + ctx.val;
11884 len = ctx.vlen;
11885 while (len--)
11886 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11887 }
11888
11889 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011890 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 +000011891 beg = http_get_path(txn);
11892 if (!beg)
11893 beg = end;
11894
11895 for (ptr = beg; ptr < end ; ptr++);
11896
11897 if (beg < ptr && *beg == '/') {
11898 while (beg < ptr)
11899 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11900 }
11901 hash = full_hash(hash);
11902
11903 smp->type = SMP_T_UINT;
11904 smp->data.uint = hash;
11905 smp->flags = SMP_F_VOL_1ST;
11906 return 1;
11907}
11908
11909/* This concatenates the source address with the 32-bit hash of the Host and
11910 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11911 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11912 * on the source address length. The URL hash is stored before the address so
11913 * that in environments where IPv6 is insignificant, truncating the output to
11914 * 8 bytes would still work.
11915 */
11916static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011917smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011918{
11919 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011920 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011921
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011922 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011923 return 0;
11924
11925 temp = get_trash_chunk();
11926 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11927 temp->len += sizeof(smp->data.uint);
11928
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011929 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011930 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011931 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011932 temp->len += 4;
11933 break;
11934 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011935 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011936 temp->len += 16;
11937 break;
11938 default:
11939 return 0;
11940 }
11941
11942 smp->data.str = *temp;
11943 smp->type = SMP_T_BIN;
11944 return 1;
11945}
11946
Willy Tarreau185b5c42012-04-26 15:11:51 +020011947/* This function is used to validate the arguments passed to any "hdr" fetch
11948 * keyword. These keywords support an optional positive or negative occurrence
11949 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11950 * is assumed that the types are already the correct ones. Returns 0 on error,
11951 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11952 * error message in case of error, that the caller is responsible for freeing.
11953 * The initial location must either be freeable or NULL.
11954 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011955int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011956{
11957 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011958 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011959 return 0;
11960 }
11961 return 1;
11962}
11963
Willy Tarreau276fae92013-07-25 14:36:01 +020011964/* takes an UINT value on input supposed to represent the time since EPOCH,
11965 * adds an optional offset found in args[0] and emits a string representing
11966 * the date in RFC-1123/5322 format.
11967 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011968static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011969{
11970 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11971 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11972 struct chunk *temp;
11973 struct tm *tm;
11974 time_t curr_date = smp->data.uint;
11975
11976 /* add offset */
11977 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11978 curr_date += args[0].data.sint;
11979
11980 tm = gmtime(&curr_date);
11981
11982 temp = get_trash_chunk();
11983 temp->len = snprintf(temp->str, temp->size - temp->len,
11984 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11985 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11986 tm->tm_hour, tm->tm_min, tm->tm_sec);
11987
11988 smp->data.str = *temp;
11989 smp->type = SMP_T_STR;
11990 return 1;
11991}
11992
Thierry FOURNIERad903512014-04-11 17:51:01 +020011993/* Match language range with language tag. RFC2616 14.4:
11994 *
11995 * A language-range matches a language-tag if it exactly equals
11996 * the tag, or if it exactly equals a prefix of the tag such
11997 * that the first tag character following the prefix is "-".
11998 *
11999 * Return 1 if the strings match, else return 0.
12000 */
12001static inline int language_range_match(const char *range, int range_len,
12002 const char *tag, int tag_len)
12003{
12004 const char *end = range + range_len;
12005 const char *tend = tag + tag_len;
12006 while (range < end) {
12007 if (*range == '-' && tag == tend)
12008 return 1;
12009 if (*range != *tag || tag == tend)
12010 return 0;
12011 range++;
12012 tag++;
12013 }
12014 /* Return true only if the last char of the tag is matched. */
12015 return tag == tend;
12016}
12017
12018/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012019static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012020{
12021 const char *al = smp->data.str.str;
12022 const char *end = al + smp->data.str.len;
12023 const char *token;
12024 int toklen;
12025 int qvalue;
12026 const char *str;
12027 const char *w;
12028 int best_q = 0;
12029
12030 /* Set the constant to the sample, because the output of the
12031 * function will be peek in the constant configuration string.
12032 */
12033 smp->flags |= SMP_F_CONST;
12034 smp->data.str.size = 0;
12035 smp->data.str.str = "";
12036 smp->data.str.len = 0;
12037
12038 /* Parse the accept language */
12039 while (1) {
12040
12041 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012042 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012043 al++;
12044 if (al >= end)
12045 break;
12046
12047 /* Start of the fisrt word. */
12048 token = al;
12049
12050 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012051 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012052 al++;
12053 if (al == token)
12054 goto expect_comma;
12055
12056 /* Length of the token. */
12057 toklen = al - token;
12058 qvalue = 1000;
12059
12060 /* Check if the token exists in the list. If the token not exists,
12061 * jump to the next token.
12062 */
12063 str = args[0].data.str.str;
12064 w = str;
12065 while (1) {
12066 if (*str == ';' || *str == '\0') {
12067 if (language_range_match(token, toklen, w, str-w))
12068 goto look_for_q;
12069 if (*str == '\0')
12070 goto expect_comma;
12071 w = str + 1;
12072 }
12073 str++;
12074 }
12075 goto expect_comma;
12076
12077look_for_q:
12078
12079 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012080 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012081 al++;
12082 if (al >= end)
12083 goto process_value;
12084
12085 /* If ',' is found, process the result */
12086 if (*al == ',')
12087 goto process_value;
12088
12089 /* If the character is different from ';', look
12090 * for the end of the header part in best effort.
12091 */
12092 if (*al != ';')
12093 goto expect_comma;
12094
12095 /* Assumes that the char is ';', now expect "q=". */
12096 al++;
12097
12098 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012099 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012100 al++;
12101 if (al >= end)
12102 goto process_value;
12103
12104 /* Expect 'q'. If no 'q', continue in best effort */
12105 if (*al != 'q')
12106 goto process_value;
12107 al++;
12108
12109 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012110 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012111 al++;
12112 if (al >= end)
12113 goto process_value;
12114
12115 /* Expect '='. If no '=', continue in best effort */
12116 if (*al != '=')
12117 goto process_value;
12118 al++;
12119
12120 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012121 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012122 al++;
12123 if (al >= end)
12124 goto process_value;
12125
12126 /* Parse the q value. */
12127 qvalue = parse_qvalue(al, &al);
12128
12129process_value:
12130
12131 /* If the new q value is the best q value, then store the associated
12132 * language in the response. If qvalue is the biggest value (1000),
12133 * break the process.
12134 */
12135 if (qvalue > best_q) {
12136 smp->data.str.str = (char *)w;
12137 smp->data.str.len = str - w;
12138 if (qvalue >= 1000)
12139 break;
12140 best_q = qvalue;
12141 }
12142
12143expect_comma:
12144
12145 /* Expect comma or end. If the end is detected, quit the loop. */
12146 while (al < end && *al != ',')
12147 al++;
12148 if (al >= end)
12149 break;
12150
12151 /* Comma is found, jump it and restart the analyzer. */
12152 al++;
12153 }
12154
12155 /* Set default value if required. */
12156 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
12157 smp->data.str.str = args[1].data.str.str;
12158 smp->data.str.len = args[1].data.str.len;
12159 }
12160
12161 /* Return true only if a matching language was found. */
12162 return smp->data.str.len != 0;
12163}
12164
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012165/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012166static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012167{
12168 /* If the constant flag is set or if not size is avalaible at
12169 * the end of the buffer, copy the string in other buffer
12170 * before decoding.
12171 */
12172 if (smp->flags & SMP_F_CONST || smp->data.str.size <= smp->data.str.len) {
12173 struct chunk *str = get_trash_chunk();
12174 memcpy(str->str, smp->data.str.str, smp->data.str.len);
12175 smp->data.str.str = str->str;
12176 smp->data.str.size = str->size;
12177 smp->flags &= ~SMP_F_CONST;
12178 }
12179
12180 /* Add final \0 required by url_decode(), and convert the input string. */
12181 smp->data.str.str[smp->data.str.len] = '\0';
12182 smp->data.str.len = url_decode(smp->data.str.str);
12183 return 1;
12184}
12185
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012186static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12187{
12188 struct proxy *fe = strm_fe(smp->strm);
12189 int idx, i;
12190 struct cap_hdr *hdr;
12191 int len;
12192
12193 if (!args || args->type != ARGT_UINT)
12194 return 0;
12195
12196 idx = args->data.uint;
12197
12198 /* Check the availibity of the capture id. */
12199 if (idx > fe->nb_req_cap - 1)
12200 return 0;
12201
12202 /* Look for the original configuration. */
12203 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12204 hdr != NULL && i != idx ;
12205 i--, hdr = hdr->next);
12206 if (!hdr)
12207 return 0;
12208
12209 /* check for the memory allocation */
12210 if (smp->strm->req_cap[hdr->index] == NULL)
12211 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12212 if (smp->strm->req_cap[hdr->index] == NULL)
12213 return 0;
12214
12215 /* Check length. */
12216 len = smp->data.str.len;
12217 if (len > hdr->len)
12218 len = hdr->len;
12219
12220 /* Capture input data. */
12221 memcpy(smp->strm->req_cap[idx], smp->data.str.str, len);
12222 smp->strm->req_cap[idx][len] = '\0';
12223
12224 return 1;
12225}
12226
12227static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12228{
12229 struct proxy *fe = strm_fe(smp->strm);
12230 int idx, i;
12231 struct cap_hdr *hdr;
12232 int len;
12233
12234 if (!args || args->type != ARGT_UINT)
12235 return 0;
12236
12237 idx = args->data.uint;
12238
12239 /* Check the availibity of the capture id. */
12240 if (idx > fe->nb_rsp_cap - 1)
12241 return 0;
12242
12243 /* Look for the original configuration. */
12244 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12245 hdr != NULL && i != idx ;
12246 i--, hdr = hdr->next);
12247 if (!hdr)
12248 return 0;
12249
12250 /* check for the memory allocation */
12251 if (smp->strm->res_cap[hdr->index] == NULL)
12252 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12253 if (smp->strm->res_cap[hdr->index] == NULL)
12254 return 0;
12255
12256 /* Check length. */
12257 len = smp->data.str.len;
12258 if (len > hdr->len)
12259 len = hdr->len;
12260
12261 /* Capture input data. */
12262 memcpy(smp->strm->res_cap[idx], smp->data.str.str, len);
12263 smp->strm->res_cap[idx][len] = '\0';
12264
12265 return 1;
12266}
12267
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012268/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012269 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012270 * the relevant part of the request line accordingly. Then it updates various
12271 * pointers to the next elements which were moved, and the total buffer length.
12272 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012273 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12274 * error, though this can be revisited when this code is finally exploited.
12275 *
12276 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12277 * query string and 3 to replace uri.
12278 *
12279 * In query string case, the mark question '?' must be set at the start of the
12280 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012281 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012282int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012283 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012284{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012285 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012286 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012287 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012288 int delta;
12289
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012290 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012291 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012292 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012293 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12294
12295 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012296 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012297 txn->req.sl.rq.m_l += delta;
12298 txn->req.sl.rq.u += delta;
12299 txn->req.sl.rq.v += delta;
12300 break;
12301
12302 case 1: // path
12303 cur_ptr = http_get_path(txn);
12304 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012305 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012306
12307 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012308 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 +010012309 cur_end++;
12310
12311 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012312 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012313 txn->req.sl.rq.u_l += delta;
12314 txn->req.sl.rq.v += delta;
12315 break;
12316
12317 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012318 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012319 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012320 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12321 while (cur_ptr < cur_end && *cur_ptr != '?')
12322 cur_ptr++;
12323
12324 /* skip the question mark or indicate that we must insert it
12325 * (but only if the format string is not empty then).
12326 */
12327 if (cur_ptr < cur_end)
12328 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012329 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012330 offset = 0;
12331
12332 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012333 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012334 txn->req.sl.rq.u_l += delta;
12335 txn->req.sl.rq.v += delta;
12336 break;
12337
12338 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012339 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012340 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12341
12342 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012343 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012344 txn->req.sl.rq.u_l += delta;
12345 txn->req.sl.rq.v += delta;
12346 break;
12347
12348 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012349 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012350 }
12351
12352 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012353 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012354 txn->req.sl.rq.l += delta;
12355 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012356 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012357 return 0;
12358}
12359
12360/* This function executes one of the set-{method,path,query,uri} actions. It
12361 * builds a string in the trash from the specified format string. It finds
12362 * the action to be performed in p[2], previously filled by function
12363 * parse_set_req_line(). The replacement action is excuted by the function
12364 * http_action_set_req_line_exec(). It always returns 1. If an error occurs
12365 * the action is canceled, but the rule processing continue.
12366 */
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012367int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012368{
12369 chunk_reset(&trash);
12370
12371 /* If we have to create a query string, prepare a '?'. */
12372 if (*(int *)&rule->arg.act.p[2] == 2)
12373 trash.str[trash.len++] = '?';
12374 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
12375
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012376 http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s);
Thierry FOURNIER01c30122015-03-14 14:14:47 +010012377 return 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012378}
12379
12380/* parse an http-request action among :
12381 * set-method
12382 * set-path
12383 * set-query
12384 * set-uri
12385 *
12386 * All of them accept a single argument of type string representing a log-format.
12387 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12388 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
12389 * It returns 0 on success, < 0 on error.
12390 */
12391int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
12392{
12393 int cur_arg = *orig_arg;
12394
12395 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
12396
12397 switch (args[0][4]) {
12398 case 'm' :
12399 *(int *)&rule->arg.act.p[2] = 0;
12400 rule->action_ptr = http_action_set_req_line;
12401 break;
12402 case 'p' :
12403 *(int *)&rule->arg.act.p[2] = 1;
12404 rule->action_ptr = http_action_set_req_line;
12405 break;
12406 case 'q' :
12407 *(int *)&rule->arg.act.p[2] = 2;
12408 rule->action_ptr = http_action_set_req_line;
12409 break;
12410 case 'u' :
12411 *(int *)&rule->arg.act.p[2] = 3;
12412 rule->action_ptr = http_action_set_req_line;
12413 break;
12414 default:
12415 memprintf(err, "internal error: unhandled action '%s'", args[0]);
12416 return -1;
12417 }
12418
12419 if (!*args[cur_arg] ||
12420 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12421 memprintf(err, "expects exactly 1 argument <format>");
12422 return -1;
12423 }
12424
12425 LIST_INIT((struct list *)&rule->arg.act.p[0]);
12426 proxy->conf.args.ctx = ARGC_HRQ;
12427 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
12428 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12429 proxy->conf.args.file, proxy->conf.args.line);
12430
12431 (*orig_arg)++;
12432 return 0;
12433}
12434
Willy Tarreaua9083d02015-05-08 15:27:59 +020012435/* This function executes the "capture" action. It executes a fetch expression,
12436 * turns the result into a string and puts it in a capture slot. It always
12437 * returns 1. If an error occurs the action is cancelled, but the rule
12438 * processing continues.
12439 */
12440int http_action_req_capture(struct http_req_rule *rule, struct proxy *px, struct stream *s)
12441{
12442 struct session *sess = s->sess;
12443 struct sample *key;
12444 struct sample_expr *expr = rule->arg.act.p[0];
12445 struct cap_hdr *h = rule->arg.act.p[1];
12446 char **cap = s->req_cap;
12447 int len;
12448
12449 key = sample_fetch_string(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, expr);
12450 if (!key)
12451 return 1;
12452
12453 if (cap[h->index] == NULL)
12454 cap[h->index] = pool_alloc2(h->pool);
12455
12456 if (cap[h->index] == NULL) /* no more capture memory */
12457 return 1;
12458
12459 len = key->data.str.len;
12460 if (len > h->len)
12461 len = h->len;
12462
12463 memcpy(cap[h->index], key->data.str.str, len);
12464 cap[h->index][len] = 0;
12465 return 1;
12466}
12467
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012468/* This function executes the "capture" action and store the result in a
12469 * capture slot if exists. It executes a fetch expression, turns the result
12470 * into a string and puts it in a capture slot. It always returns 1. If an
12471 * error occurs the action is cancelled, but the rule processing continues.
12472 */
12473int http_action_req_capture_by_id(struct http_req_rule *rule, struct proxy *px, struct stream *s)
12474{
12475 struct session *sess = s->sess;
12476 struct sample *key;
12477 struct sample_expr *expr = rule->arg.act.p[0];
12478 struct cap_hdr *h;
12479 int idx = (long)rule->arg.act.p[1];
12480 char **cap = s->req_cap;
12481 struct proxy *fe = strm_fe(s);
12482 int len;
12483 int i;
12484
12485 /* Look for the original configuration. */
12486 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
12487 h != NULL && i != idx ;
12488 i--, h = h->next);
12489 if (!h)
12490 return 1;
12491
12492 key = sample_fetch_string(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, expr);
12493 if (!key)
12494 return 1;
12495
12496 if (cap[h->index] == NULL)
12497 cap[h->index] = pool_alloc2(h->pool);
12498
12499 if (cap[h->index] == NULL) /* no more capture memory */
12500 return 1;
12501
12502 len = key->data.str.len;
12503 if (len > h->len)
12504 len = h->len;
12505
12506 memcpy(cap[h->index], key->data.str.str, len);
12507 cap[h->index][len] = 0;
12508 return 1;
12509}
12510
Willy Tarreaua9083d02015-05-08 15:27:59 +020012511/* parse an "http-request capture" action. It takes a single argument which is
12512 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012513 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Willy Tarreaua9083d02015-05-08 15:27:59 +020012514 * It returns 0 on success, < 0 on error.
12515 */
12516int parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
12517{
12518 struct sample_expr *expr;
12519 struct cap_hdr *hdr;
12520 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012521 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012522
12523 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12524 if (strcmp(args[cur_arg], "if") == 0 ||
12525 strcmp(args[cur_arg], "unless") == 0)
12526 break;
12527
12528 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012529 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Willy Tarreaua9083d02015-05-08 15:27:59 +020012530 return -1;
12531 }
12532
Willy Tarreaua9083d02015-05-08 15:27:59 +020012533 cur_arg = *orig_arg;
12534 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12535 if (!expr)
12536 return -1;
12537
12538 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12539 memprintf(err,
12540 "fetch method '%s' extracts information from '%s', none of which is available here",
12541 args[cur_arg-1], sample_src_names(expr->fetch->use));
12542 free(expr);
12543 return -1;
12544 }
12545
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012546 if (!args[cur_arg] || !*args[cur_arg]) {
12547 memprintf(err, "expects 'len or 'id'");
12548 free(expr);
12549 return -1;
12550 }
12551
Willy Tarreaua9083d02015-05-08 15:27:59 +020012552 if (strcmp(args[cur_arg], "len") == 0) {
12553 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012554
12555 if (!(px->cap & PR_CAP_FE)) {
12556 memprintf(err, "proxy '%s' has no frontend capability", px->id);
12557 return -1;
12558 }
12559
12560 proxy->conf.args.ctx = ARGC_CAP;
12561
Willy Tarreaua9083d02015-05-08 15:27:59 +020012562 if (!args[cur_arg]) {
12563 memprintf(err, "missing length value");
12564 free(expr);
12565 return -1;
12566 }
12567 /* we copy the table name for now, it will be resolved later */
12568 len = atoi(args[cur_arg]);
12569 if (len <= 0) {
12570 memprintf(err, "length must be > 0");
12571 free(expr);
12572 return -1;
12573 }
12574 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012575
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012576 if (!len) {
12577 memprintf(err, "a positive 'len' argument is mandatory");
12578 free(expr);
12579 return -1;
12580 }
12581
12582 hdr = calloc(sizeof(struct cap_hdr), 1);
12583 hdr->next = px->req_cap;
12584 hdr->name = NULL; /* not a header capture */
12585 hdr->namelen = 0;
12586 hdr->len = len;
12587 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12588 hdr->index = px->nb_req_cap++;
12589
12590 px->req_cap = hdr;
12591 px->to_log |= LW_REQHDR;
12592
12593 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
12594 rule->action_ptr = http_action_req_capture;
12595 rule->arg.act.p[0] = expr;
12596 rule->arg.act.p[1] = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012597 }
12598
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012599 else if (strcmp(args[cur_arg], "id") == 0) {
12600 int id;
12601 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012602
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012603 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012604
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012605 if (!args[cur_arg]) {
12606 memprintf(err, "missing id value");
12607 free(expr);
12608 return -1;
12609 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012610
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012611 id = strtol(args[cur_arg], &error, 10);
12612 if (*error != '\0') {
12613 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12614 free(expr);
12615 return -1;
12616 }
12617 cur_arg++;
12618
12619 proxy->conf.args.ctx = ARGC_CAP;
12620
12621 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
12622 rule->action_ptr = http_action_req_capture_by_id;
12623 rule->arg.act.p[0] = expr;
12624 rule->arg.act.p[1] = (void *)(long)id;
12625 }
12626
12627 else {
12628 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12629 free(expr);
12630 return -1;
12631 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012632
12633 *orig_arg = cur_arg;
12634 return 0;
12635}
12636
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012637/* This function executes the "capture" action and store the result in a
12638 * capture slot if exists. It executes a fetch expression, turns the result
12639 * into a string and puts it in a capture slot. It always returns 1. If an
12640 * error occurs the action is cancelled, but the rule processing continues.
12641 */
12642int http_action_res_capture_by_id(struct http_res_rule *rule, struct proxy *px, struct stream *s)
12643{
12644 struct session *sess = s->sess;
12645 struct sample *key;
12646 struct sample_expr *expr = rule->arg.act.p[0];
12647 struct cap_hdr *h;
12648 int idx = (long)rule->arg.act.p[1];
12649 char **cap = s->res_cap;
12650 struct proxy *fe = strm_fe(s);
12651 int len;
12652 int i;
12653
12654 /* Look for the original configuration. */
12655 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12656 h != NULL && i != idx ;
12657 i--, h = h->next);
12658 if (!h)
12659 return 1;
12660
12661 key = sample_fetch_string(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, expr);
12662 if (!key)
12663 return 1;
12664
12665 if (cap[h->index] == NULL)
12666 cap[h->index] = pool_alloc2(h->pool);
12667
12668 if (cap[h->index] == NULL) /* no more capture memory */
12669 return 1;
12670
12671 len = key->data.str.len;
12672 if (len > h->len)
12673 len = h->len;
12674
12675 memcpy(cap[h->index], key->data.str.str, len);
12676 cap[h->index][len] = 0;
12677 return 1;
12678}
12679
12680/* parse an "http-response capture" action. It takes a single argument which is
12681 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12682 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
12683 * It returns 0 on success, < 0 on error.
12684 */
12685int parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, struct http_res_rule *rule, char **err)
12686{
12687 struct sample_expr *expr;
12688 int cur_arg;
12689 int id;
12690 char *error;
12691
12692 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12693 if (strcmp(args[cur_arg], "if") == 0 ||
12694 strcmp(args[cur_arg], "unless") == 0)
12695 break;
12696
12697 if (cur_arg < *orig_arg + 3) {
12698 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
12699 return -1;
12700 }
12701
12702 cur_arg = *orig_arg;
12703 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12704 if (!expr)
12705 return -1;
12706
12707 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12708 memprintf(err,
12709 "fetch method '%s' extracts information from '%s', none of which is available here",
12710 args[cur_arg-1], sample_src_names(expr->fetch->use));
12711 free(expr);
12712 return -1;
12713 }
12714
12715 if (!args[cur_arg] || !*args[cur_arg]) {
12716 memprintf(err, "expects 'len or 'id'");
12717 free(expr);
12718 return -1;
12719 }
12720
12721 if (strcmp(args[cur_arg], "id") != 0) {
12722 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12723 free(expr);
12724 return -1;
12725 }
12726
12727 cur_arg++;
12728
12729 if (!args[cur_arg]) {
12730 memprintf(err, "missing id value");
12731 free(expr);
12732 return -1;
12733 }
12734
12735 id = strtol(args[cur_arg], &error, 10);
12736 if (*error != '\0') {
12737 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12738 free(expr);
12739 return -1;
12740 }
12741 cur_arg++;
12742
12743 LIST_INIT((struct list *)&rule->arg.act.p[0]);
12744 proxy->conf.args.ctx = ARGC_CAP;
12745
12746 rule->action = HTTP_RES_ACT_CUSTOM_CONT;
12747 rule->action_ptr = http_action_res_capture_by_id;
12748 rule->arg.act.p[0] = expr;
12749 rule->arg.act.p[1] = (void *)(long)id;
12750
12751 *orig_arg = cur_arg;
12752 return 0;
12753}
12754
William Lallemand73025dd2014-04-24 14:38:37 +020012755/*
12756 * Return the struct http_req_action_kw associated to a keyword.
12757 */
12758struct http_req_action_kw *action_http_req_custom(const char *kw)
12759{
12760 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
12761 struct http_req_action_kw_list *kw_list;
12762 int i;
12763
12764 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
12765 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
12766 if (!strcmp(kw, kw_list->kw[i].kw))
12767 return &kw_list->kw[i];
12768 }
12769 }
12770 }
12771 return NULL;
12772}
12773
12774/*
12775 * Return the struct http_res_action_kw associated to a keyword.
12776 */
12777struct http_res_action_kw *action_http_res_custom(const char *kw)
12778{
12779 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
12780 struct http_res_action_kw_list *kw_list;
12781 int i;
12782
12783 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
12784 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
12785 if (!strcmp(kw, kw_list->kw[i].kw))
12786 return &kw_list->kw[i];
12787 }
12788 }
12789 }
12790 return NULL;
12791}
12792
Willy Tarreau4a568972010-05-12 08:08:50 +020012793/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012794/* All supported ACL keywords must be declared here. */
12795/************************************************************************/
12796
12797/* Note: must not be declared <const> as its list will be overwritten.
12798 * Please take care of keeping this list alphabetically sorted.
12799 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012800static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012801 { "base", "base", PAT_MATCH_STR },
12802 { "base_beg", "base", PAT_MATCH_BEG },
12803 { "base_dir", "base", PAT_MATCH_DIR },
12804 { "base_dom", "base", PAT_MATCH_DOM },
12805 { "base_end", "base", PAT_MATCH_END },
12806 { "base_len", "base", PAT_MATCH_LEN },
12807 { "base_reg", "base", PAT_MATCH_REG },
12808 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012809
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012810 { "cook", "req.cook", PAT_MATCH_STR },
12811 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12812 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12813 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12814 { "cook_end", "req.cook", PAT_MATCH_END },
12815 { "cook_len", "req.cook", PAT_MATCH_LEN },
12816 { "cook_reg", "req.cook", PAT_MATCH_REG },
12817 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012818
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012819 { "hdr", "req.hdr", PAT_MATCH_STR },
12820 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12821 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12822 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12823 { "hdr_end", "req.hdr", PAT_MATCH_END },
12824 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12825 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12826 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012827
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012828 /* these two declarations uses strings with list storage (in place
12829 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12830 * and delete functions are relative to the list management. The parse
12831 * and match method are related to the corresponding fetch methods. This
12832 * is very particular ACL declaration mode.
12833 */
12834 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12835 { "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 +020012836
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012837 { "path", "path", PAT_MATCH_STR },
12838 { "path_beg", "path", PAT_MATCH_BEG },
12839 { "path_dir", "path", PAT_MATCH_DIR },
12840 { "path_dom", "path", PAT_MATCH_DOM },
12841 { "path_end", "path", PAT_MATCH_END },
12842 { "path_len", "path", PAT_MATCH_LEN },
12843 { "path_reg", "path", PAT_MATCH_REG },
12844 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012845
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012846 { "req_ver", "req.ver", PAT_MATCH_STR },
12847 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012848
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012849 { "scook", "res.cook", PAT_MATCH_STR },
12850 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12851 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12852 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12853 { "scook_end", "res.cook", PAT_MATCH_END },
12854 { "scook_len", "res.cook", PAT_MATCH_LEN },
12855 { "scook_reg", "res.cook", PAT_MATCH_REG },
12856 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012857
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012858 { "shdr", "res.hdr", PAT_MATCH_STR },
12859 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12860 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12861 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12862 { "shdr_end", "res.hdr", PAT_MATCH_END },
12863 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12864 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12865 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012866
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012867 { "url", "url", PAT_MATCH_STR },
12868 { "url_beg", "url", PAT_MATCH_BEG },
12869 { "url_dir", "url", PAT_MATCH_DIR },
12870 { "url_dom", "url", PAT_MATCH_DOM },
12871 { "url_end", "url", PAT_MATCH_END },
12872 { "url_len", "url", PAT_MATCH_LEN },
12873 { "url_reg", "url", PAT_MATCH_REG },
12874 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012875
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012876 { "urlp", "urlp", PAT_MATCH_STR },
12877 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12878 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12879 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12880 { "urlp_end", "urlp", PAT_MATCH_END },
12881 { "urlp_len", "urlp", PAT_MATCH_LEN },
12882 { "urlp_reg", "urlp", PAT_MATCH_REG },
12883 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012884
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012885 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012886}};
12887
12888/************************************************************************/
12889/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012890/************************************************************************/
12891/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012892static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012893 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012894 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12895 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12896
Willy Tarreau87b09662015-04-03 00:22:06 +020012897 /* capture are allocated and are permanent in the stream */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012898 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012899
12900 /* retrieve these captures from the HTTP logs */
12901 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12902 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12903 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12904
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012905 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012906 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012907
Willy Tarreau409bcde2013-01-08 00:31:00 +010012908 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12909 * are only here to match the ACL's name, are request-only and are used
12910 * for ACL compatibility only.
12911 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012912 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12913 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012914 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12915 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12916
12917 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12918 * only here to match the ACL's name, are request-only and are used for
12919 * ACL compatibility only.
12920 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012921 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012922 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12923 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
12924 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12925
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012926 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012927 { "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 +010012928 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012929 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012930 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012931 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012932
12933 /* HTTP protocol on the request path */
12934 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012935 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012936
12937 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012938 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12939 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012940
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012941 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12942 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12943 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012944 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012945
Willy Tarreau18ed2562013-01-14 15:56:36 +010012946 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012947 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12948 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012949
Willy Tarreau18ed2562013-01-14 15:56:36 +010012950 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012951 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012952 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12953 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12954
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012955 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012956 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012957 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012958 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12959 { "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 +010012960 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012961 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12962
12963 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012964 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012965 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12966 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12967
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012968 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012969 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012970 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012971 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12972 { "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 +010012973 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012974 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12975
Willy Tarreau409bcde2013-01-08 00:31:00 +010012976 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012977 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012978 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12979 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012980 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012981
12982 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012983 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012984 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12985 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
12986 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12987
12988 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012989 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012990 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12991 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012992 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
12993 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012994 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12995 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12996 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012997 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012998}};
12999
Willy Tarreau8797c062007-05-07 00:55:35 +020013000
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013001/************************************************************************/
13002/* All supported converter keywords must be declared here. */
13003/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013004/* Note: must not be declared <const> as its list will be overwritten */
13005static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020013006 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
13007 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013008 { "capture-req", smp_conv_req_capture, ARG1(1,UINT), NULL, SMP_T_STR, SMP_T_STR},
13009 { "capture-res", smp_conv_res_capture, ARG1(1,UINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013010 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013011 { NULL, NULL, 0, 0, 0 },
13012}};
13013
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013014
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013015/************************************************************************/
13016/* All supported http-request action keywords must be declared here. */
13017/************************************************************************/
13018struct http_req_action_kw_list http_req_actions = {
13019 .scope = "http",
13020 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013021 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013022 { "set-method", parse_set_req_line },
13023 { "set-path", parse_set_req_line },
13024 { "set-query", parse_set_req_line },
13025 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013026 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013027 }
13028};
13029
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013030struct http_res_action_kw_list http_res_actions = {
13031 .scope = "http",
13032 .kw = {
13033 { "capture", parse_http_res_capture },
13034 { NULL, NULL }
13035 }
13036};
13037
Willy Tarreau8797c062007-05-07 00:55:35 +020013038__attribute__((constructor))
13039static void __http_protocol_init(void)
13040{
13041 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013042 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013043 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013044 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013045 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013046}
13047
13048
Willy Tarreau58f10d72006-12-04 02:26:12 +010013049/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013050 * Local variables:
13051 * c-indent-level: 8
13052 * c-basic-offset: 8
13053 * End:
13054 */