blob: ce05a837c1f3545130b3c90b27f4f11b7a47ac25 [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 Tarreaue8e785b2009-12-26 15:34:26 +01002569 int use_close_only;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002570 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002571 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002573 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002574
Willy Tarreau87b09662015-04-03 00:22:06 +02002575 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 +01002576 now_ms, __FUNCTION__,
2577 s,
2578 req,
2579 req->rex, req->wex,
2580 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002581 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002582 req->analysers);
2583
Willy Tarreau52a0c602009-08-16 22:45:38 +02002584 /* we're speaking HTTP here, so let's speak HTTP to the client */
2585 s->srv_error = http_return_srv_error;
2586
Willy Tarreau83e3af02009-12-28 17:39:57 +01002587 /* There's a protected area at the end of the buffer for rewriting
2588 * purposes. We don't want to start to parse the request if the
2589 * protected area is affected, because we may have to move processed
2590 * data later, which is much more complicated.
2591 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002592 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002593 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002594 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002595 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002596 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002597 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002598 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002599 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002600 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002601 return 0;
2602 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002603 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2604 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2605 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002606 }
2607
Willy Tarreau065e8332010-01-08 00:30:20 +01002608 /* Note that we have the same problem with the response ; we
2609 * may want to send a redirect, error or anything which requires
2610 * some spare space. So we'll ensure that we have at least
2611 * maxrewrite bytes available in the response buffer before
2612 * processing that one. This will only affect pipelined
2613 * keep-alive requests.
2614 */
2615 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002616 unlikely(!channel_is_rewritable(&s->res) ||
2617 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2618 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2619 if (s->res.buf->o) {
2620 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002621 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002622 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002623 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002624 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2625 s->res.flags |= CF_WAKE_WRITE;
2626 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002627 return 0;
2628 }
2629 }
2630
Willy Tarreau9b28e032012-10-12 23:49:43 +02002631 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002632 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002633 }
2634
Willy Tarreau59234e92008-11-30 23:51:27 +01002635 /* 1: we might have to print this header in debug mode */
2636 if (unlikely((global.mode & MODE_DEBUG) &&
2637 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002638 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002639 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002640
Willy Tarreau9b28e032012-10-12 23:49:43 +02002641 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002642 /* this is a bit complex : in case of error on the request line,
2643 * we know that rq.l is still zero, so we display only the part
2644 * up to the end of the line (truncated by debug_hdr).
2645 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002646 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002647 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002648
Willy Tarreau59234e92008-11-30 23:51:27 +01002649 sol += hdr_idx_first_pos(&txn->hdr_idx);
2650 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002651
Willy Tarreau59234e92008-11-30 23:51:27 +01002652 while (cur_idx) {
2653 eol = sol + txn->hdr_idx.v[cur_idx].len;
2654 debug_hdr("clihdr", s, sol, eol);
2655 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2656 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002657 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002658 }
2659
Willy Tarreau58f10d72006-12-04 02:26:12 +01002660
Willy Tarreau59234e92008-11-30 23:51:27 +01002661 /*
2662 * Now we quickly check if we have found a full valid request.
2663 * If not so, we check the FD and buffer states before leaving.
2664 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002665 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002666 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002667 * on a keep-alive stream, if we encounter and error, close, t/o,
2668 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002669 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002670 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002671 * Last, we may increase some tracked counters' http request errors on
2672 * the cases that are deliberately the client's fault. For instance,
2673 * a timeout or connection reset is not counted as an error. However
2674 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002675 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002676
Willy Tarreau655dce92009-11-08 13:10:58 +01002677 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002678 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002679 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002680 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002681 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002682 stream_inc_http_req_ctr(s);
2683 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002684 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002686 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002687
Willy Tarreau59234e92008-11-30 23:51:27 +01002688 /* 1: Since we are in header mode, if there's no space
2689 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002690 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002691 * must terminate it now.
2692 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002693 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002694 /* FIXME: check if URI is set and return Status
2695 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002696 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002697 stream_inc_http_req_ctr(s);
2698 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002699 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002700 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002701 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002702 goto return_bad_req;
2703 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002704
Willy Tarreau59234e92008-11-30 23:51:27 +01002705 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002706 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002707 if (!(s->flags & SF_ERR_MASK))
2708 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002709
Willy Tarreaufcffa692010-01-10 14:21:19 +01002710 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002711 goto failed_keep_alive;
2712
Willy Tarreau59234e92008-11-30 23:51:27 +01002713 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002714 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002715 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002716 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002717 }
2718
Willy Tarreaudc979f22012-12-04 10:39:01 +01002719 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002720 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002721 msg->msg_state = HTTP_MSG_ERROR;
2722 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002723
Willy Tarreau87b09662015-04-03 00:22:06 +02002724 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002725 proxy_inc_fe_req_ctr(sess->fe);
2726 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002727 if (sess->listener->counters)
2728 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002729
Willy Tarreaue7dff022015-04-03 01:14:29 +02002730 if (!(s->flags & SF_FINST_MASK))
2731 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 return 0;
2733 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002734
Willy Tarreau59234e92008-11-30 23:51:27 +01002735 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002736 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002737 if (!(s->flags & SF_ERR_MASK))
2738 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002739
Willy Tarreaufcffa692010-01-10 14:21:19 +01002740 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002741 goto failed_keep_alive;
2742
Willy Tarreau59234e92008-11-30 23:51:27 +01002743 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002744 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002745 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002746 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002747 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002748 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002749 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002750 msg->msg_state = HTTP_MSG_ERROR;
2751 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002752
Willy Tarreau87b09662015-04-03 00:22:06 +02002753 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002754 proxy_inc_fe_req_ctr(sess->fe);
2755 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002756 if (sess->listener->counters)
2757 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002758
Willy Tarreaue7dff022015-04-03 01:14:29 +02002759 if (!(s->flags & SF_FINST_MASK))
2760 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002761 return 0;
2762 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002763
Willy Tarreau59234e92008-11-30 23:51:27 +01002764 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002765 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002766 if (!(s->flags & SF_ERR_MASK))
2767 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002768
Willy Tarreaufcffa692010-01-10 14:21:19 +01002769 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002770 goto failed_keep_alive;
2771
Willy Tarreau4076a152009-04-02 15:18:36 +02002772 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002773 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002774 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002775 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002776 msg->msg_state = HTTP_MSG_ERROR;
2777 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002778
Willy Tarreau87b09662015-04-03 00:22:06 +02002779 stream_inc_http_err_ctr(s);
2780 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002781 proxy_inc_fe_req_ctr(sess->fe);
2782 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002783 if (sess->listener->counters)
2784 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002785
Willy Tarreaue7dff022015-04-03 01:14:29 +02002786 if (!(s->flags & SF_FINST_MASK))
2787 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002788 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002789 }
2790
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002791 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002792 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002793 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002794#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002795 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2796 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002797 /* We need more data, we have to re-enable quick-ack in case we
2798 * previously disabled it, otherwise we might cause the client
2799 * to delay next data.
2800 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002801 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002802 }
2803#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002804
Willy Tarreaufcffa692010-01-10 14:21:19 +01002805 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2806 /* If the client starts to talk, let's fall back to
2807 * request timeout processing.
2808 */
2809 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002810 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002811 }
2812
Willy Tarreau59234e92008-11-30 23:51:27 +01002813 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002814 if (!tick_isset(req->analyse_exp)) {
2815 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2816 (txn->flags & TX_WAIT_NEXT_RQ) &&
2817 tick_isset(s->be->timeout.httpka))
2818 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2819 else
2820 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2821 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002822
Willy Tarreau59234e92008-11-30 23:51:27 +01002823 /* we're not ready yet */
2824 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002825
2826 failed_keep_alive:
2827 /* Here we process low-level errors for keep-alive requests. In
2828 * short, if the request is not the first one and it experiences
2829 * a timeout, read error or shutdown, we just silently close so
2830 * that the client can try again.
2831 */
2832 txn->status = 0;
2833 msg->msg_state = HTTP_MSG_RQBEFORE;
2834 req->analysers = 0;
2835 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002836 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002837 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002838 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002839 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002840 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002841
Willy Tarreaud787e662009-07-07 10:14:51 +02002842 /* OK now we have a complete HTTP request with indexed headers. Let's
2843 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002844 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002845 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002846 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002847 * byte after the last LF. msg->sov points to the first byte of data.
2848 * msg->eol cannot be trusted because it may have been left uninitialized
2849 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002850 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002851
Willy Tarreau87b09662015-04-03 00:22:06 +02002852 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002853 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002854
Willy Tarreaub16a5742010-01-10 14:46:16 +01002855 if (txn->flags & TX_WAIT_NEXT_RQ) {
2856 /* kill the pending keep-alive timeout */
2857 txn->flags &= ~TX_WAIT_NEXT_RQ;
2858 req->analyse_exp = TICK_ETERNITY;
2859 }
2860
2861
Willy Tarreaud787e662009-07-07 10:14:51 +02002862 /* Maybe we found in invalid header name while we were configured not
2863 * to block on that, so we have to capture it now.
2864 */
2865 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002866 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002867
Willy Tarreau59234e92008-11-30 23:51:27 +01002868 /*
2869 * 1: identify the method
2870 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002871 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002872
2873 /* we can make use of server redirect on GET and HEAD */
2874 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002875 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002876
Willy Tarreau59234e92008-11-30 23:51:27 +01002877 /*
2878 * 2: check if the URI matches the monitor_uri.
2879 * We have to do this for every request which gets in, because
2880 * the monitor-uri is defined by the frontend.
2881 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002882 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2883 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002884 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002885 sess->fe->monitor_uri,
2886 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002887 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002888 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002889 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002890 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002891
Willy Tarreaue7dff022015-04-03 01:14:29 +02002892 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002893 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002894
Willy Tarreau59234e92008-11-30 23:51:27 +01002895 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002896 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002897 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002898
Willy Tarreau59234e92008-11-30 23:51:27 +01002899 ret = acl_pass(ret);
2900 if (cond->pol == ACL_COND_UNLESS)
2901 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002902
Willy Tarreau59234e92008-11-30 23:51:27 +01002903 if (ret) {
2904 /* we fail this request, let's return 503 service unavail */
2905 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002906 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002907 if (!(s->flags & SF_ERR_MASK))
2908 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002909 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002910 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002911 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002912
Willy Tarreau59234e92008-11-30 23:51:27 +01002913 /* nothing to fail, let's reply normaly */
2914 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01002915 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002916 if (!(s->flags & SF_ERR_MASK))
2917 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002918 goto return_prx_cond;
2919 }
2920
2921 /*
2922 * 3: Maybe we have to copy the original REQURI for the logs ?
2923 * Note: we cannot log anymore if the request has been
2924 * classified as invalid.
2925 */
2926 if (unlikely(s->logs.logwait & LW_REQ)) {
2927 /* we have a complete HTTP request that we must log */
2928 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2929 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002930
Willy Tarreau59234e92008-11-30 23:51:27 +01002931 if (urilen >= REQURI_LEN)
2932 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002933 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002934 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002935
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002936 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002937 s->do_log(s);
2938 } else {
2939 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002940 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002941 }
Willy Tarreau06619262006-12-17 08:37:22 +01002942
Willy Tarreau59234e92008-11-30 23:51:27 +01002943 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002944 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002945 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002946
Willy Tarreau5b154472009-12-21 20:11:07 +01002947 /* ... and check if the request is HTTP/1.1 or above */
2948 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002949 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2950 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2951 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002952 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002953
2954 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002955 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 +01002956
Willy Tarreau88d349d2010-01-25 12:15:43 +01002957 /* if the frontend has "option http-use-proxy-header", we'll check if
2958 * we have what looks like a proxied connection instead of a connection,
2959 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2960 * Note that this is *not* RFC-compliant, however browsers and proxies
2961 * happen to do that despite being non-standard :-(
2962 * We consider that a request not beginning with either '/' or '*' is
2963 * a proxied connection, which covers both "scheme://location" and
2964 * CONNECT ip:port.
2965 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002966 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002967 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002968 txn->flags |= TX_USE_PX_CONN;
2969
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002970 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002971 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002972
Willy Tarreau59234e92008-11-30 23:51:27 +01002973 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002974 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002975 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002976 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002977
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002978 /* 6: determine the transfer-length.
2979 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2980 * the presence of a message-body in a REQUEST and its transfer length
2981 * must be determined that way (in order of precedence) :
2982 * 1. The presence of a message-body in a request is signaled by the
2983 * inclusion of a Content-Length or Transfer-Encoding header field
2984 * in the request's header fields. When a request message contains
2985 * both a message-body of non-zero length and a method that does
2986 * not define any semantics for that request message-body, then an
2987 * origin server SHOULD either ignore the message-body or respond
2988 * with an appropriate error message (e.g., 413). A proxy or
2989 * gateway, when presented the same request, SHOULD either forward
2990 * the request inbound with the message- body or ignore the
2991 * message-body when determining a response.
2992 *
2993 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2994 * and the "chunked" transfer-coding (Section 6.2) is used, the
2995 * transfer-length is defined by the use of this transfer-coding.
2996 * If a Transfer-Encoding header field is present and the "chunked"
2997 * transfer-coding is not present, the transfer-length is defined
2998 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002999 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003000 * 3. If a Content-Length header field is present, its decimal value in
3001 * OCTETs represents both the entity-length and the transfer-length.
3002 * If a message is received with both a Transfer-Encoding header
3003 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003004 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003005 * 4. By the server closing the connection. (Closing the connection
3006 * cannot be used to indicate the end of a request body, since that
3007 * would leave no possibility for the server to send back a response.)
3008 *
3009 * Whenever a transfer-coding is applied to a message-body, the set of
3010 * transfer-codings MUST include "chunked", unless the message indicates
3011 * it is terminated by closing the connection. When the "chunked"
3012 * transfer-coding is used, it MUST be the last transfer-coding applied
3013 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003014 */
3015
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003016 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003017 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003018 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003019 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003020 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003021 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003022 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3023 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003024 /* bad transfer-encoding (chunked followed by something else) */
3025 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003026 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003027 break;
3028 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003029 }
3030
Willy Tarreau1c913912015-04-30 10:57:51 +02003031 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003032 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003033 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3034 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3035 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3036 }
3037 else while (!use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003038 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003039 signed long long cl;
3040
Willy Tarreauad14f752011-09-02 20:33:27 +02003041 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003042 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003043 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003044 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003045
Willy Tarreauad14f752011-09-02 20:33:27 +02003046 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003047 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003048 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003049 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003050
Willy Tarreauad14f752011-09-02 20:33:27 +02003051 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003052 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003053 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003054 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003055
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003056 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003057 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003058 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003059 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003060
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003061 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003062 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003063 }
3064
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003065 /* bodyless requests have a known length */
3066 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003067 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003068
Willy Tarreau179085c2014-04-28 16:48:56 +02003069 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3070 * only change if both the request and the config reference something else.
3071 * Option httpclose by itself sets tunnel mode where headers are mangled.
3072 * However, if another mode is set, it will affect it (eg: server-close/
3073 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3074 * if FE and BE have the same settings (common). The method consists in
3075 * checking if options changed between the two calls (implying that either
3076 * one is non-null, or one of them is non-null and we are there for the first
3077 * time.
3078 */
3079 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003080 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003081 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003082
Willy Tarreaud787e662009-07-07 10:14:51 +02003083 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003084 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003085 req->analyse_exp = TICK_ETERNITY;
3086 return 1;
3087
3088 return_bad_req:
3089 /* We centralize bad requests processing here */
3090 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3091 /* we detected a parsing error. We want to archive this request
3092 * in the dedicated proxy area for later troubleshooting.
3093 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003094 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003095 }
3096
3097 txn->req.msg_state = HTTP_MSG_ERROR;
3098 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003099 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003100
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003101 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003102 if (sess->listener->counters)
3103 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003104
3105 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003106 if (!(s->flags & SF_ERR_MASK))
3107 s->flags |= SF_ERR_PRXCOND;
3108 if (!(s->flags & SF_FINST_MASK))
3109 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003110
3111 req->analysers = 0;
3112 req->analyse_exp = TICK_ETERNITY;
3113 return 0;
3114}
3115
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003116
Willy Tarreau347a35d2013-11-22 17:51:09 +01003117/* This function prepares an applet to handle the stats. It can deal with the
3118 * "100-continue" expectation, check that admin rules are met for POST requests,
3119 * and program a response message if something was unexpected. It cannot fail
3120 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003121 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003122 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003123 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003124 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003125int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003126{
3127 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003128 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003129 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003130 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003131 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003132 struct uri_auth *uri_auth = s->be->uri_auth;
3133 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003134 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003135
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003136 appctx = si_appctx(si);
3137 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3138 appctx->st1 = appctx->st2 = 0;
3139 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3140 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003141 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003142 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003143
3144 uri = msg->chn->buf->p + msg->sl.rq.u;
3145 lookup = uri + uri_auth->uri_len;
3146
3147 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3148 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003149 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003150 break;
3151 }
3152 }
3153
3154 if (uri_auth->refresh) {
3155 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3156 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003157 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003158 break;
3159 }
3160 }
3161 }
3162
3163 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3164 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003165 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003166 break;
3167 }
3168 }
3169
3170 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3171 if (memcmp(h, ";st=", 4) == 0) {
3172 int i;
3173 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003174 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003175 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3176 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003177 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003178 break;
3179 }
3180 }
3181 break;
3182 }
3183 }
3184
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003185 appctx->ctx.stats.scope_str = 0;
3186 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003187 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3188 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3189 int itx = 0;
3190 const char *h2;
3191 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3192 const char *err;
3193
3194 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3195 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003196 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003197 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3198 itx++;
3199 h++;
3200 }
3201
3202 if (itx > STAT_SCOPE_TXT_MAXLEN)
3203 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003204 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003205
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003206 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003207 memcpy(scope_txt, h2, itx);
3208 scope_txt[itx] = '\0';
3209 err = invalid_char(scope_txt);
3210 if (err) {
3211 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003212 appctx->ctx.stats.scope_str = 0;
3213 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003214 }
3215 break;
3216 }
3217 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003218
3219 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003220 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003221 int ret = 1;
3222
3223 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003224 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 +01003225 ret = acl_pass(ret);
3226 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3227 ret = !ret;
3228 }
3229
3230 if (ret) {
3231 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003232 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003233 break;
3234 }
3235 }
3236
3237 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003238 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003239 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003240 /* we'll need the request body, possibly after sending 100-continue */
3241 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003242 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003243 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003244 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003245 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3246 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003247 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003248 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003249 else {
3250 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003251 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003252 }
3253
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003254 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003255 return 1;
3256}
3257
Lukas Tribus67db8df2013-06-23 17:37:13 +02003258/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3259 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3260 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003261void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003262{
3263#ifdef IP_TOS
3264 if (from.ss_family == AF_INET)
3265 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3266#endif
3267#ifdef IPV6_TCLASS
3268 if (from.ss_family == AF_INET6) {
3269 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3270 /* v4-mapped addresses need IP_TOS */
3271 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3272 else
3273 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3274 }
3275#endif
3276}
3277
Willy Tarreau87b09662015-04-03 00:22:06 +02003278int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003279 const char* name, unsigned int name_len,
3280 const char *str, struct my_regex *re,
3281 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003282{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003283 struct hdr_ctx ctx;
3284 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003285 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003286 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3287 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003288 struct chunk *output = get_trash_chunk();
3289
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003290 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003291
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003292 /* Choose the header browsing function. */
3293 switch (action) {
3294 case HTTP_REQ_ACT_REPLACE_VAL:
3295 case HTTP_RES_ACT_REPLACE_VAL:
3296 http_find_hdr_func = http_find_header2;
3297 break;
3298 case HTTP_REQ_ACT_REPLACE_HDR:
3299 case HTTP_RES_ACT_REPLACE_HDR:
3300 http_find_hdr_func = http_find_full_header2;
3301 break;
3302 default: /* impossible */
3303 return -1;
3304 }
3305
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003306 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3307 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003308 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003309 char *val = ctx.line + ctx.val;
3310 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003311
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003312 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3313 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003314
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003315 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003316 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003317 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003318
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003319 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003320
3321 hdr->len += delta;
3322 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003323
3324 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003325 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003326 }
3327
3328 return 0;
3329}
3330
Willy Tarreau87b09662015-04-03 00:22:06 +02003331static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003332 const char* name, unsigned int name_len,
3333 struct list *fmt, struct my_regex *re,
3334 int action)
3335{
3336 struct chunk *replace = get_trash_chunk();
3337
3338 replace->len = build_logline(s, replace->str, replace->size, fmt);
3339 if (replace->len >= replace->size - 1)
3340 return -1;
3341
3342 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3343}
3344
Willy Tarreau87b09662015-04-03 00:22:06 +02003345/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003346 * transaction <txn>. Returns the verdict of the first rule that prevents
3347 * further processing of the request (auth, deny, ...), and defaults to
3348 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3349 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3350 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003351 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003352enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003353http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003354{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003355 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003356 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003357 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003358 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003359 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003360 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003361
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003362 /* If "the current_rule_list" match the executed rule list, we are in
3363 * resume condition. If a resume is needed it is always in the action
3364 * and never in the ACL or converters. In this case, we initialise the
3365 * current rule, and go to the action execution point.
3366 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003367 if (s->current_rule) {
3368 rule = s->current_rule;
3369 s->current_rule = NULL;
3370 if (s->current_rule_list == rules)
3371 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003372 }
3373 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003374
Willy Tarreauff011f22011-01-06 17:51:27 +01003375 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003376 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003377 continue;
3378
Willy Tarreau96257ec2012-12-27 10:46:37 +01003379 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003380 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003381 int ret;
3382
Willy Tarreau192252e2015-04-04 01:47:55 +02003383 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003384 ret = acl_pass(ret);
3385
Willy Tarreauff011f22011-01-06 17:51:27 +01003386 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003387 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003388
3389 if (!ret) /* condition not matched */
3390 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003391 }
3392
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003393resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003394 switch (rule->action) {
3395 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003396 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003397
3398 case HTTP_REQ_ACT_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003399 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003400 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003401
Willy Tarreauccbcc372012-12-27 12:37:57 +01003402 case HTTP_REQ_ACT_TARPIT:
3403 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003404 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003405 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003406
Willy Tarreau96257ec2012-12-27 10:46:37 +01003407 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003408 /* Auth might be performed on regular http-req rules as well as on stats */
3409 auth_realm = rule->arg.auth.realm;
3410 if (!auth_realm) {
3411 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3412 auth_realm = STATS_DEFAULT_REALM;
3413 else
3414 auth_realm = px->id;
3415 }
3416 /* send 401/407 depending on whether we use a proxy or not. We still
3417 * count one error, because normal browsing won't significantly
3418 * increase the counter but brute force attempts will.
3419 */
3420 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3421 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3422 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003423 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003424 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003425
Willy Tarreau81499eb2012-12-27 12:19:02 +01003426 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003427 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3428 return HTTP_RULE_RES_BADREQ;
3429 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003430
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003431 case HTTP_REQ_ACT_SET_NICE:
3432 s->task->nice = rule->arg.nice;
3433 break;
3434
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003435 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003436 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003437 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003438 break;
3439
Willy Tarreau51347ed2013-06-11 19:34:13 +02003440 case HTTP_REQ_ACT_SET_MARK:
3441#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003442 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003443 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003444#endif
3445 break;
3446
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003447 case HTTP_REQ_ACT_SET_LOGL:
3448 s->logs.level = rule->arg.loglevel;
3449 break;
3450
Sasha Pachev218f0642014-06-16 12:05:59 -06003451 case HTTP_REQ_ACT_REPLACE_HDR:
3452 case HTTP_REQ_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003453 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3454 rule->arg.hdr_add.name_len,
3455 &rule->arg.hdr_add.fmt,
3456 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003457 return HTTP_RULE_RES_BADREQ;
3458 break;
3459
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003460 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003461 ctx.idx = 0;
3462 /* remove all occurrences of the header */
3463 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3464 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3465 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003466 }
Willy Tarreau85603282015-01-21 20:39:27 +01003467 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003468
Willy Tarreau85603282015-01-21 20:39:27 +01003469 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003470 case HTTP_REQ_ACT_ADD_HDR:
3471 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3472 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3473 trash.len = rule->arg.hdr_add.name_len;
3474 trash.str[trash.len++] = ':';
3475 trash.str[trash.len++] = ' ';
3476 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 +01003477
3478 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3479 /* remove all occurrences of the header */
3480 ctx.idx = 0;
3481 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3482 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3483 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3484 }
3485 }
3486
Willy Tarreau96257ec2012-12-27 10:46:37 +01003487 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3488 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003489
3490 case HTTP_REQ_ACT_DEL_ACL:
3491 case HTTP_REQ_ACT_DEL_MAP: {
3492 struct pat_ref *ref;
3493 char *key;
3494 int len;
3495
3496 /* collect reference */
3497 ref = pat_ref_lookup(rule->arg.map.ref);
3498 if (!ref)
3499 continue;
3500
3501 /* collect key */
3502 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3503 key = trash.str;
3504 key[len] = '\0';
3505
3506 /* perform update */
3507 /* returned code: 1=ok, 0=ko */
3508 pat_ref_delete(ref, key);
3509
3510 break;
3511 }
3512
3513 case HTTP_REQ_ACT_ADD_ACL: {
3514 struct pat_ref *ref;
3515 char *key;
3516 struct chunk *trash_key;
3517 int len;
3518
3519 trash_key = get_trash_chunk();
3520
3521 /* collect reference */
3522 ref = pat_ref_lookup(rule->arg.map.ref);
3523 if (!ref)
3524 continue;
3525
3526 /* collect key */
3527 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3528 key = trash_key->str;
3529 key[len] = '\0';
3530
3531 /* perform update */
3532 /* add entry only if it does not already exist */
3533 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003534 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003535
3536 break;
3537 }
3538
3539 case HTTP_REQ_ACT_SET_MAP: {
3540 struct pat_ref *ref;
3541 char *key, *value;
3542 struct chunk *trash_key, *trash_value;
3543 int len;
3544
3545 trash_key = get_trash_chunk();
3546 trash_value = get_trash_chunk();
3547
3548 /* collect reference */
3549 ref = pat_ref_lookup(rule->arg.map.ref);
3550 if (!ref)
3551 continue;
3552
3553 /* collect key */
3554 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3555 key = trash_key->str;
3556 key[len] = '\0';
3557
3558 /* collect value */
3559 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3560 value = trash_value->str;
3561 value[len] = '\0';
3562
3563 /* perform update */
3564 if (pat_ref_find_elt(ref, key) != NULL)
3565 /* update entry if it exists */
3566 pat_ref_set(ref, key, value, NULL);
3567 else
3568 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003569 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003570
3571 break;
3572 }
William Lallemand73025dd2014-04-24 14:38:37 +02003573
3574 case HTTP_REQ_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003575 if (!rule->action_ptr(rule, px, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003576 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003577 return HTTP_RULE_RES_YIELD;
3578 }
William Lallemand73025dd2014-04-24 14:38:37 +02003579 break;
3580
3581 case HTTP_REQ_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003582 rule->action_ptr(rule, px, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003583 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003584
3585 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3586 /* Note: only the first valid tracking parameter of each
3587 * applies.
3588 */
3589
3590 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3591 struct stktable *t;
3592 struct stksess *ts;
3593 struct stktable_key *key;
3594 void *ptr;
3595
3596 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau192252e2015-04-04 01:47:55 +02003597 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 +02003598
3599 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003600 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003601
3602 /* let's count a new HTTP request as it's the first time we do it */
3603 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3604 if (ptr)
3605 stktable_data_cast(ptr, http_req_cnt)++;
3606
3607 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3608 if (ptr)
3609 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3610 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3611
3612 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003613 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003614 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3615 }
3616 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003617 }
3618 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003619
3620 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003621 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003622}
3623
Willy Tarreau71241ab2012-12-27 11:30:54 +01003624
Willy Tarreau87b09662015-04-03 00:22:06 +02003625/* Executes the http-response rules <rules> for stream <s>, proxy <px> and
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003626 * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD
3627 * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the
3628 * evaluation of next rule list. If *STOP is returned, the process must stop
3629 * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny
3630 * rule. If *YIELD is returned, the czller must call again the function with
3631 * the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003632 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003633static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003634http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003635{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003636 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003637 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003638 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003639 struct http_res_rule *rule;
3640 struct hdr_ctx ctx;
3641
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003642 /* If "the current_rule_list" match the executed rule list, we are in
3643 * resume condition. If a resume is needed it is always in the action
3644 * and never in the ACL or converters. In this case, we initialise the
3645 * current rule, and go to the action execution point.
3646 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003647 if (s->current_rule) {
3648 rule = s->current_rule;
3649 s->current_rule = NULL;
3650 if (s->current_rule_list == rules)
3651 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003652 }
3653 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003654
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003655 list_for_each_entry(rule, rules, list) {
3656 if (rule->action >= HTTP_RES_ACT_MAX)
3657 continue;
3658
3659 /* check optional condition */
3660 if (rule->cond) {
3661 int ret;
3662
Willy Tarreau192252e2015-04-04 01:47:55 +02003663 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003664 ret = acl_pass(ret);
3665
3666 if (rule->cond->pol == ACL_COND_UNLESS)
3667 ret = !ret;
3668
3669 if (!ret) /* condition not matched */
3670 continue;
3671 }
3672
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003673resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003674 switch (rule->action) {
3675 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003676 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003677
3678 case HTTP_RES_ACT_DENY:
3679 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003680 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003681
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003682 case HTTP_RES_ACT_SET_NICE:
3683 s->task->nice = rule->arg.nice;
3684 break;
3685
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003686 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003687 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003688 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003689 break;
3690
Willy Tarreau51347ed2013-06-11 19:34:13 +02003691 case HTTP_RES_ACT_SET_MARK:
3692#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003693 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003694 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003695#endif
3696 break;
3697
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003698 case HTTP_RES_ACT_SET_LOGL:
3699 s->logs.level = rule->arg.loglevel;
3700 break;
3701
Sasha Pachev218f0642014-06-16 12:05:59 -06003702 case HTTP_RES_ACT_REPLACE_HDR:
3703 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003704 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3705 rule->arg.hdr_add.name_len,
3706 &rule->arg.hdr_add.fmt,
3707 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003708 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003709 break;
3710
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003711 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003712 ctx.idx = 0;
3713 /* remove all occurrences of the header */
3714 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3715 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3716 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3717 }
Willy Tarreau85603282015-01-21 20:39:27 +01003718 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003719
Willy Tarreau85603282015-01-21 20:39:27 +01003720 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003721 case HTTP_RES_ACT_ADD_HDR:
3722 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3723 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3724 trash.len = rule->arg.hdr_add.name_len;
3725 trash.str[trash.len++] = ':';
3726 trash.str[trash.len++] = ' ';
3727 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 +01003728
3729 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3730 /* remove all occurrences of the header */
3731 ctx.idx = 0;
3732 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3733 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3734 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3735 }
3736 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003737 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3738 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003739
3740 case HTTP_RES_ACT_DEL_ACL:
3741 case HTTP_RES_ACT_DEL_MAP: {
3742 struct pat_ref *ref;
3743 char *key;
3744 int len;
3745
3746 /* collect reference */
3747 ref = pat_ref_lookup(rule->arg.map.ref);
3748 if (!ref)
3749 continue;
3750
3751 /* collect key */
3752 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3753 key = trash.str;
3754 key[len] = '\0';
3755
3756 /* perform update */
3757 /* returned code: 1=ok, 0=ko */
3758 pat_ref_delete(ref, key);
3759
3760 break;
3761 }
3762
3763 case HTTP_RES_ACT_ADD_ACL: {
3764 struct pat_ref *ref;
3765 char *key;
3766 struct chunk *trash_key;
3767 int len;
3768
3769 trash_key = get_trash_chunk();
3770
3771 /* collect reference */
3772 ref = pat_ref_lookup(rule->arg.map.ref);
3773 if (!ref)
3774 continue;
3775
3776 /* collect key */
3777 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3778 key = trash_key->str;
3779 key[len] = '\0';
3780
3781 /* perform update */
3782 /* check if the entry already exists */
3783 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003784 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003785
3786 break;
3787 }
3788
3789 case HTTP_RES_ACT_SET_MAP: {
3790 struct pat_ref *ref;
3791 char *key, *value;
3792 struct chunk *trash_key, *trash_value;
3793 int len;
3794
3795 trash_key = get_trash_chunk();
3796 trash_value = get_trash_chunk();
3797
3798 /* collect reference */
3799 ref = pat_ref_lookup(rule->arg.map.ref);
3800 if (!ref)
3801 continue;
3802
3803 /* collect key */
3804 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3805 key = trash_key->str;
3806 key[len] = '\0';
3807
3808 /* collect value */
3809 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3810 value = trash_value->str;
3811 value[len] = '\0';
3812
3813 /* perform update */
3814 if (pat_ref_find_elt(ref, key) != NULL)
3815 /* update entry if it exists */
3816 pat_ref_set(ref, key, value, NULL);
3817 else
3818 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003819 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003820
3821 break;
3822 }
William Lallemand73025dd2014-04-24 14:38:37 +02003823
3824 case HTTP_RES_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003825 if (!rule->action_ptr(rule, px, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003826 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003827 return HTTP_RULE_RES_YIELD;
3828 }
William Lallemand73025dd2014-04-24 14:38:37 +02003829 break;
3830
3831 case HTTP_RES_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003832 rule->action_ptr(rule, px, s);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003833 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003834 }
3835 }
3836
3837 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003838 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003839}
3840
3841
Willy Tarreau71241ab2012-12-27 11:30:54 +01003842/* Perform an HTTP redirect based on the information in <rule>. The function
3843 * returns non-zero on success, or zero in case of a, irrecoverable error such
3844 * as too large a request to build a valid response.
3845 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003846static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003847{
3848 struct http_msg *msg = &txn->req;
3849 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003850 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003851
3852 /* build redirect message */
3853 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003854 case 308:
3855 msg_fmt = HTTP_308;
3856 break;
3857 case 307:
3858 msg_fmt = HTTP_307;
3859 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003860 case 303:
3861 msg_fmt = HTTP_303;
3862 break;
3863 case 301:
3864 msg_fmt = HTTP_301;
3865 break;
3866 case 302:
3867 default:
3868 msg_fmt = HTTP_302;
3869 break;
3870 }
3871
3872 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3873 return 0;
3874
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003875 location = trash.str + trash.len;
3876
Willy Tarreau71241ab2012-12-27 11:30:54 +01003877 switch(rule->type) {
3878 case REDIRECT_TYPE_SCHEME: {
3879 const char *path;
3880 const char *host;
3881 struct hdr_ctx ctx;
3882 int pathlen;
3883 int hostlen;
3884
3885 host = "";
3886 hostlen = 0;
3887 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003888 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003889 host = ctx.line + ctx.val;
3890 hostlen = ctx.vlen;
3891 }
3892
3893 path = http_get_path(txn);
3894 /* build message using path */
3895 if (path) {
3896 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3897 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3898 int qs = 0;
3899 while (qs < pathlen) {
3900 if (path[qs] == '?') {
3901 pathlen = qs;
3902 break;
3903 }
3904 qs++;
3905 }
3906 }
3907 } else {
3908 path = "/";
3909 pathlen = 1;
3910 }
3911
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003912 if (rule->rdr_str) { /* this is an old "redirect" rule */
3913 /* check if we can add scheme + "://" + host + path */
3914 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3915 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003916
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003917 /* add scheme */
3918 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3919 trash.len += rule->rdr_len;
3920 }
3921 else {
3922 /* add scheme with executing log format */
3923 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003924
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003925 /* check if we can add scheme + "://" + host + path */
3926 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3927 return 0;
3928 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003929 /* add "://" */
3930 memcpy(trash.str + trash.len, "://", 3);
3931 trash.len += 3;
3932
3933 /* add host */
3934 memcpy(trash.str + trash.len, host, hostlen);
3935 trash.len += hostlen;
3936
3937 /* add path */
3938 memcpy(trash.str + trash.len, path, pathlen);
3939 trash.len += pathlen;
3940
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003941 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003942 if (trash.len && trash.str[trash.len - 1] != '/' &&
3943 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3944 if (trash.len > trash.size - 5)
3945 return 0;
3946 trash.str[trash.len] = '/';
3947 trash.len++;
3948 }
3949
3950 break;
3951 }
3952 case REDIRECT_TYPE_PREFIX: {
3953 const char *path;
3954 int pathlen;
3955
3956 path = http_get_path(txn);
3957 /* build message using path */
3958 if (path) {
3959 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3960 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3961 int qs = 0;
3962 while (qs < pathlen) {
3963 if (path[qs] == '?') {
3964 pathlen = qs;
3965 break;
3966 }
3967 qs++;
3968 }
3969 }
3970 } else {
3971 path = "/";
3972 pathlen = 1;
3973 }
3974
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003975 if (rule->rdr_str) { /* this is an old "redirect" rule */
3976 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3977 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003978
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003979 /* add prefix. Note that if prefix == "/", we don't want to
3980 * add anything, otherwise it makes it hard for the user to
3981 * configure a self-redirection.
3982 */
3983 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3984 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3985 trash.len += rule->rdr_len;
3986 }
3987 }
3988 else {
3989 /* add prefix with executing log format */
3990 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3991
3992 /* Check length */
3993 if (trash.len + pathlen > trash.size - 4)
3994 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003995 }
3996
3997 /* add path */
3998 memcpy(trash.str + trash.len, path, pathlen);
3999 trash.len += pathlen;
4000
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004001 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004002 if (trash.len && trash.str[trash.len - 1] != '/' &&
4003 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4004 if (trash.len > trash.size - 5)
4005 return 0;
4006 trash.str[trash.len] = '/';
4007 trash.len++;
4008 }
4009
4010 break;
4011 }
4012 case REDIRECT_TYPE_LOCATION:
4013 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004014 if (rule->rdr_str) { /* this is an old "redirect" rule */
4015 if (trash.len + rule->rdr_len > trash.size - 4)
4016 return 0;
4017
4018 /* add location */
4019 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4020 trash.len += rule->rdr_len;
4021 }
4022 else {
4023 /* add location with executing log format */
4024 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004025
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004026 /* Check left length */
4027 if (trash.len > trash.size - 4)
4028 return 0;
4029 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004030 break;
4031 }
4032
4033 if (rule->cookie_len) {
4034 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4035 trash.len += 14;
4036 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4037 trash.len += rule->cookie_len;
4038 memcpy(trash.str + trash.len, "\r\n", 2);
4039 trash.len += 2;
4040 }
4041
4042 /* add end of headers and the keep-alive/close status.
4043 * We may choose to set keep-alive if the Location begins
4044 * with a slash, because the client will come back to the
4045 * same server.
4046 */
4047 txn->status = rule->code;
4048 /* let's log the request time */
4049 s->logs.tv_request = now;
4050
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004051 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004052 (msg->flags & HTTP_MSGF_XFER_LEN) &&
4053 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
4054 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4055 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4056 /* keep-alive possible */
4057 if (!(msg->flags & HTTP_MSGF_VER_11)) {
4058 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4059 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4060 trash.len += 30;
4061 } else {
4062 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4063 trash.len += 24;
4064 }
4065 }
4066 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4067 trash.len += 4;
4068 bo_inject(txn->rsp.chn, trash.str, trash.len);
4069 /* "eat" the request */
4070 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02004071 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004072 msg->sov = 0;
4073 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004074 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004075 txn->req.msg_state = HTTP_MSG_CLOSED;
4076 txn->rsp.msg_state = HTTP_MSG_DONE;
4077 } else {
4078 /* keep-alive not possible */
4079 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4080 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4081 trash.len += 29;
4082 } else {
4083 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4084 trash.len += 23;
4085 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004086 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004087 txn->req.chn->analysers = 0;
4088 }
4089
Willy Tarreaue7dff022015-04-03 01:14:29 +02004090 if (!(s->flags & SF_ERR_MASK))
4091 s->flags |= SF_ERR_LOCAL;
4092 if (!(s->flags & SF_FINST_MASK))
4093 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004094
4095 return 1;
4096}
4097
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004098/* This stream analyser runs all HTTP request processing which is common to
4099 * frontends and backends, which means blocking ACLs, filters, connection-close,
4100 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004101 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004102 * either needs more data or wants to immediately abort the request (eg: deny,
4103 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004104 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004105int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004106{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004107 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004108 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004109 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004110 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004111 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004112 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004113
Willy Tarreau655dce92009-11-08 13:10:58 +01004114 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004115 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004116 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004117 }
4118
Willy Tarreau87b09662015-04-03 00:22:06 +02004119 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 +02004120 now_ms, __FUNCTION__,
4121 s,
4122 req,
4123 req->rex, req->wex,
4124 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004125 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004126 req->analysers);
4127
Willy Tarreau65410832014-04-28 21:25:43 +02004128 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004129 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004130
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004131 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004132 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004133 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004134
Willy Tarreau0b748332014-04-29 00:13:29 +02004135 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004136 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4137 goto return_prx_yield;
4138
Willy Tarreau0b748332014-04-29 00:13:29 +02004139 case HTTP_RULE_RES_CONT:
4140 case HTTP_RULE_RES_STOP: /* nothing to do */
4141 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004142
Willy Tarreau0b748332014-04-29 00:13:29 +02004143 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4144 if (txn->flags & TX_CLTARPIT)
4145 goto tarpit;
4146 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004147
Willy Tarreau0b748332014-04-29 00:13:29 +02004148 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4149 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004150
Willy Tarreau0b748332014-04-29 00:13:29 +02004151 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004152 goto done;
4153
Willy Tarreau0b748332014-04-29 00:13:29 +02004154 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4155 goto return_bad_req;
4156 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004157 }
4158
Willy Tarreau52542592014-04-28 18:33:26 +02004159 /* OK at this stage, we know that the request was accepted according to
4160 * the http-request rules, we can check for the stats. Note that the
4161 * URI is detected *before* the req* rules in order not to be affected
4162 * by a possible reqrep, while they are processed *after* so that a
4163 * reqdeny can still block them. This clearly needs to change in 1.6!
4164 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004165 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004166 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004167 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004168 txn->status = 500;
4169 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004170 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004171
Willy Tarreaue7dff022015-04-03 01:14:29 +02004172 if (!(s->flags & SF_ERR_MASK))
4173 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004174 goto return_prx_cond;
4175 }
4176
4177 /* parse the whole stats request and extract the relevant information */
4178 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004179 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004180 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004181
Willy Tarreau0b748332014-04-29 00:13:29 +02004182 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4183 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004184
Willy Tarreau0b748332014-04-29 00:13:29 +02004185 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4186 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004187 }
4188
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004189 /* evaluate the req* rules except reqadd */
4190 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004191 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004192 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004193
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004194 if (txn->flags & TX_CLDENY)
4195 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004196
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004197 if (txn->flags & TX_CLTARPIT)
4198 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004199 }
Willy Tarreau06619262006-12-17 08:37:22 +01004200
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004201 /* add request headers from the rule sets in the same order */
4202 list_for_each_entry(wl, &px->req_add, list) {
4203 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004204 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004205 ret = acl_pass(ret);
4206 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4207 ret = !ret;
4208 if (!ret)
4209 continue;
4210 }
4211
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004212 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004213 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004214 }
4215
Willy Tarreau52542592014-04-28 18:33:26 +02004216
4217 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004218 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004219 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004220 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4221 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004222
Willy Tarreaue7dff022015-04-03 01:14:29 +02004223 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4224 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4225 if (!(s->flags & SF_FINST_MASK))
4226 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004227
Willy Tarreau70730dd2014-04-24 18:06:27 +02004228 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004229 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004230 select_compression_request_header(s, req->buf);
4231
4232 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004233 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004234 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004235 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004236
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004237 /* check whether we have some ACLs set to redirect this request */
4238 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004239 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004240 int ret;
4241
Willy Tarreau192252e2015-04-04 01:47:55 +02004242 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004243 ret = acl_pass(ret);
4244 if (rule->cond->pol == ACL_COND_UNLESS)
4245 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004246 if (!ret)
4247 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004248 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004249 if (!http_apply_redirect_rule(rule, s, txn))
4250 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004251 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004252 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004253
Willy Tarreau2be39392010-01-03 17:24:51 +01004254 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4255 * If this happens, then the data will not come immediately, so we must
4256 * send all what we have without waiting. Note that due to the small gain
4257 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004258 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004259 * itself once used.
4260 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004261 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004262
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004263 done: /* done with this analyser, continue with next ones that the calling
4264 * points will have set, if any.
4265 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004266 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004267 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4268 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004269 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004270
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004271 tarpit:
4272 /* When a connection is tarpitted, we use the tarpit timeout,
4273 * which may be the same as the connect timeout if unspecified.
4274 * If unset, then set it to zero because we really want it to
4275 * eventually expire. We build the tarpit as an analyser.
4276 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004277 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004278
4279 /* wipe the request out so that we can drop the connection early
4280 * if the client closes first.
4281 */
4282 channel_dont_connect(req);
4283 req->analysers = 0; /* remove switching rules etc... */
4284 req->analysers |= AN_REQ_HTTP_TARPIT;
4285 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4286 if (!req->analyse_exp)
4287 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004288 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004289 sess->fe->fe_counters.denied_req++;
4290 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004291 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004292 if (sess->listener->counters)
4293 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004294 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004295
4296 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004297 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004298 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004299 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004300 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004301 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004302 sess->fe->fe_counters.denied_req++;
4303 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004304 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004305 if (sess->listener->counters)
4306 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004307 goto return_prx_cond;
4308
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004309 return_bad_req:
4310 /* We centralize bad requests processing here */
4311 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4312 /* we detected a parsing error. We want to archive this request
4313 * in the dedicated proxy area for later troubleshooting.
4314 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004315 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004316 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004317
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004318 txn->req.msg_state = HTTP_MSG_ERROR;
4319 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004320 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004321
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004322 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004323 if (sess->listener->counters)
4324 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004325
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004326 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004327 if (!(s->flags & SF_ERR_MASK))
4328 s->flags |= SF_ERR_PRXCOND;
4329 if (!(s->flags & SF_FINST_MASK))
4330 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004331
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004332 req->analysers = 0;
4333 req->analyse_exp = TICK_ETERNITY;
4334 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004335
4336 return_prx_yield:
4337 channel_dont_connect(req);
4338 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004339}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004340
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004341/* This function performs all the processing enabled for the current request.
4342 * It returns 1 if the processing can continue on next analysers, or zero if it
4343 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004344 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004345 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004346int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004347{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004348 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004349 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004350 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004351 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004352
Willy Tarreau655dce92009-11-08 13:10:58 +01004353 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004354 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004355 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004356 return 0;
4357 }
4358
Willy Tarreau87b09662015-04-03 00:22:06 +02004359 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 +02004360 now_ms, __FUNCTION__,
4361 s,
4362 req,
4363 req->rex, req->wex,
4364 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004365 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004366 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004367
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004368 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004369 select_compression_request_header(s, req->buf);
4370
Willy Tarreau59234e92008-11-30 23:51:27 +01004371 /*
4372 * Right now, we know that we have processed the entire headers
4373 * and that unwanted requests have been filtered out. We can do
4374 * whatever we want with the remaining request. Also, now we
4375 * may have separate values for ->fe, ->be.
4376 */
Willy Tarreau06619262006-12-17 08:37:22 +01004377
Willy Tarreau59234e92008-11-30 23:51:27 +01004378 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004379 * If HTTP PROXY is set we simply get remote server address parsing
4380 * incoming request. Note that this requires that a connection is
4381 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004382 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004383 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004384 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004385 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004386
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004387 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau350f4872014-11-28 14:42:25 +01004388 if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004389 txn->req.msg_state = HTTP_MSG_ERROR;
4390 txn->status = 500;
4391 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004392 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004393
Willy Tarreaue7dff022015-04-03 01:14:29 +02004394 if (!(s->flags & SF_ERR_MASK))
4395 s->flags |= SF_ERR_RESOURCE;
4396 if (!(s->flags & SF_FINST_MASK))
4397 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004398
4399 return 0;
4400 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004401
4402 path = http_get_path(txn);
4403 url2sa(req->buf->p + msg->sl.rq.u,
4404 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004405 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004406 /* if the path was found, we have to remove everything between
4407 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4408 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4409 * u_l characters by a single "/".
4410 */
4411 if (path) {
4412 char *cur_ptr = req->buf->p;
4413 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4414 int delta;
4415
4416 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4417 http_msg_move_end(&txn->req, delta);
4418 cur_end += delta;
4419 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4420 goto return_bad_req;
4421 }
4422 else {
4423 char *cur_ptr = req->buf->p;
4424 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4425 int delta;
4426
4427 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4428 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4429 http_msg_move_end(&txn->req, delta);
4430 cur_end += delta;
4431 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4432 goto return_bad_req;
4433 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004434 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004435
Willy Tarreau59234e92008-11-30 23:51:27 +01004436 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004437 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004438 * Note that doing so might move headers in the request, but
4439 * the fields will stay coherent and the URI will not move.
4440 * This should only be performed in the backend.
4441 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004442 if ((s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004443 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4444 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004445
Willy Tarreau59234e92008-11-30 23:51:27 +01004446 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004447 * 8: the appsession cookie was looked up very early in 1.2,
4448 * so let's do the same now.
4449 */
4450
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004451 /* It needs to look into the URI unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004452 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004453 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 +01004454 }
4455
William Lallemanda73203e2012-03-12 12:48:57 +01004456 /* add unique-id if "header-unique-id" is specified */
4457
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004458 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004459 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4460 goto return_bad_req;
4461 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004462 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004463 }
William Lallemanda73203e2012-03-12 12:48:57 +01004464
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004465 if (sess->fe->header_unique_id && s->unique_id) {
4466 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004467 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004468 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004469 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004470 goto return_bad_req;
4471 }
4472
Cyril Bontéb21570a2009-11-29 20:04:48 +01004473 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004474 * 9: add X-Forwarded-For if either the frontend or the backend
4475 * asks for it.
4476 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004477 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004478 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004479 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4480 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4481 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004482 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004483 /* The header is set to be added only if none is present
4484 * and we found it, so don't do anything.
4485 */
4486 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004487 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004488 /* Add an X-Forwarded-For header unless the source IP is
4489 * in the 'except' network range.
4490 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004491 if ((!sess->fe->except_mask.s_addr ||
4492 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4493 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004494 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004495 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004496 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004497 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004498 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004499 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004500
4501 /* Note: we rely on the backend to get the header name to be used for
4502 * x-forwarded-for, because the header is really meant for the backends.
4503 * However, if the backend did not specify any option, we have to rely
4504 * on the frontend's header name.
4505 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004506 if (s->be->fwdfor_hdr_len) {
4507 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004508 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004509 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004510 len = sess->fe->fwdfor_hdr_len;
4511 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004512 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004513 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 +01004514
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004515 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004516 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004517 }
4518 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004519 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004520 /* FIXME: for the sake of completeness, we should also support
4521 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004522 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004523 int len;
4524 char pn[INET6_ADDRSTRLEN];
4525 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004526 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004527 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004528
Willy Tarreau59234e92008-11-30 23:51:27 +01004529 /* Note: we rely on the backend to get the header name to be used for
4530 * x-forwarded-for, because the header is really meant for the backends.
4531 * However, if the backend did not specify any option, we have to rely
4532 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004533 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004534 if (s->be->fwdfor_hdr_len) {
4535 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004536 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004537 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004538 len = sess->fe->fwdfor_hdr_len;
4539 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004540 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004541 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004542
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004543 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004544 goto return_bad_req;
4545 }
4546 }
4547
4548 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004549 * 10: add X-Original-To if either the frontend or the backend
4550 * asks for it.
4551 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004552 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004553
4554 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004555 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004556 /* Add an X-Original-To header unless the destination IP is
4557 * in the 'except' network range.
4558 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004559 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004560
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004561 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004562 ((!sess->fe->except_mask_to.s_addr ||
4563 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4564 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004565 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004566 (((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 +02004567 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004568 int len;
4569 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004570 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004571
4572 /* Note: we rely on the backend to get the header name to be used for
4573 * x-original-to, because the header is really meant for the backends.
4574 * However, if the backend did not specify any option, we have to rely
4575 * on the frontend's header name.
4576 */
4577 if (s->be->orgto_hdr_len) {
4578 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004579 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004580 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004581 len = sess->fe->orgto_hdr_len;
4582 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004583 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004584 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 +02004585
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004586 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004587 goto return_bad_req;
4588 }
4589 }
4590 }
4591
Willy Tarreau50fc7772012-11-11 22:19:57 +01004592 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4593 * If an "Upgrade" token is found, the header is left untouched in order not to have
4594 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4595 * "Upgrade" is present in the Connection header.
4596 */
4597 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4598 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004599 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004600 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004601 unsigned int want_flags = 0;
4602
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004603 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004604 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004605 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004606 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004607 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004608 want_flags |= TX_CON_CLO_SET;
4609 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004610 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004611 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004612 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004613 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004614 want_flags |= TX_CON_KAL_SET;
4615 }
4616
4617 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004618 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004619 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004620
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004621
Willy Tarreau522d6c02009-12-06 18:49:18 +01004622 /* If we have no server assigned yet and we're balancing on url_param
4623 * with a POST request, we may be interested in checking the body for
4624 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004625 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004626 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004627 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004628 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004629 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004630 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004631 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004632
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004633 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004634 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004635#ifdef TCP_QUICKACK
4636 /* We expect some data from the client. Unless we know for sure
4637 * we already have a full request, we have to re-enable quick-ack
4638 * in case we previously disabled it, otherwise we might cause
4639 * the client to delay further data.
4640 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004641 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004642 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004643 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004644 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004645 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004646#endif
4647 }
Willy Tarreau03945942009-12-22 16:50:27 +01004648
Willy Tarreau59234e92008-11-30 23:51:27 +01004649 /*************************************************************
4650 * OK, that's finished for the headers. We have done what we *
4651 * could. Let's switch to the DATA state. *
4652 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004653 req->analyse_exp = TICK_ETERNITY;
4654 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004655
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004656 /* if the server closes the connection, we want to immediately react
4657 * and close the socket to save packets and syscalls.
4658 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004659 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004660 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004661
Willy Tarreau59234e92008-11-30 23:51:27 +01004662 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004663 /* OK let's go on with the BODY now */
4664 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004665
Willy Tarreau59234e92008-11-30 23:51:27 +01004666 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004667 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004668 /* we detected a parsing error. We want to archive this request
4669 * in the dedicated proxy area for later troubleshooting.
4670 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004671 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004672 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004673
Willy Tarreau59234e92008-11-30 23:51:27 +01004674 txn->req.msg_state = HTTP_MSG_ERROR;
4675 txn->status = 400;
4676 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004677 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004678
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004679 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004680 if (sess->listener->counters)
4681 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004682
Willy Tarreaue7dff022015-04-03 01:14:29 +02004683 if (!(s->flags & SF_ERR_MASK))
4684 s->flags |= SF_ERR_PRXCOND;
4685 if (!(s->flags & SF_FINST_MASK))
4686 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004687 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004688}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004689
Willy Tarreau60b85b02008-11-30 23:28:40 +01004690/* This function is an analyser which processes the HTTP tarpit. It always
4691 * returns zero, at the beginning because it prevents any other processing
4692 * from occurring, and at the end because it terminates the request.
4693 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004694int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004695{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004696 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004697
4698 /* This connection is being tarpitted. The CLIENT side has
4699 * already set the connect expiration date to the right
4700 * timeout. We just have to check that the client is still
4701 * there and that the timeout has not expired.
4702 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004703 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004704 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004705 !tick_is_expired(req->analyse_exp, now_ms))
4706 return 0;
4707
4708 /* We will set the queue timer to the time spent, just for
4709 * logging purposes. We fake a 500 server error, so that the
4710 * attacker will not suspect his connection has been tarpitted.
4711 * It will not cause trouble to the logs because we can exclude
4712 * the tarpitted connections by filtering on the 'PT' status flags.
4713 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004714 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4715
4716 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004717 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004718 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004719
4720 req->analysers = 0;
4721 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004722
Willy Tarreaue7dff022015-04-03 01:14:29 +02004723 if (!(s->flags & SF_ERR_MASK))
4724 s->flags |= SF_ERR_PRXCOND;
4725 if (!(s->flags & SF_FINST_MASK))
4726 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004727 return 0;
4728}
4729
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004730/* This function is an analyser which waits for the HTTP request body. It waits
4731 * for either the buffer to be full, or the full advertised contents to have
4732 * reached the buffer. It must only be called after the standard HTTP request
4733 * processing has occurred, because it expects the request to be parsed and will
4734 * look for the Expect header. It may send a 100-Continue interim response. It
4735 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4736 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4737 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004738 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004739int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004740{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004741 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004742 struct http_txn *txn = s->txn;
4743 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004744
4745 /* We have to parse the HTTP request body to find any required data.
4746 * "balance url_param check_post" should have been the only way to get
4747 * into this. We were brought here after HTTP header analysis, so all
4748 * related structures are ready.
4749 */
4750
Willy Tarreau890988f2014-04-10 11:59:33 +02004751 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4752 /* This is the first call */
4753 if (msg->msg_state < HTTP_MSG_BODY)
4754 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004755
Willy Tarreau890988f2014-04-10 11:59:33 +02004756 if (msg->msg_state < HTTP_MSG_100_SENT) {
4757 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4758 * send an HTTP/1.1 100 Continue intermediate response.
4759 */
4760 if (msg->flags & HTTP_MSGF_VER_11) {
4761 struct hdr_ctx ctx;
4762 ctx.idx = 0;
4763 /* Expect is allowed in 1.1, look for it */
4764 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4765 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004766 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004767 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004768 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004769 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004770 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004771
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004772 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004773 * req->buf->p still points to the beginning of the message. We
4774 * must save the body in msg->next because it survives buffer
4775 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004776 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004777 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004778
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004779 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004780 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4781 else
4782 msg->msg_state = HTTP_MSG_DATA;
4783 }
4784
Willy Tarreau890988f2014-04-10 11:59:33 +02004785 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4786 /* We're in content-length mode, we just have to wait for enough data. */
4787 if (req->buf->i - msg->sov < msg->body_len)
4788 goto missing_data;
4789
4790 /* OK we have everything we need now */
4791 goto http_end;
4792 }
4793
4794 /* OK here we're parsing a chunked-encoded message */
4795
Willy Tarreau522d6c02009-12-06 18:49:18 +01004796 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004797 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004798 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004799 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004800 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004801 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004802
Willy Tarreau115acb92009-12-26 13:56:06 +01004803 if (!ret)
4804 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004805 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004806 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004807 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004808 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004809 }
4810
Willy Tarreaud98cf932009-12-27 22:54:55 +01004811 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004812 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004813 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004814 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004815 if (msg->msg_state == HTTP_MSG_TRAILERS)
4816 goto http_end;
4817
Willy Tarreau226071e2014-04-10 11:55:45 +02004818 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004819 goto http_end;
4820
4821 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004822 /* we get here if we need to wait for more data. If the buffer is full,
4823 * we have the maximum we can expect.
4824 */
4825 if (buffer_full(req->buf, global.tune.maxrewrite))
4826 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004827
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004828 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004829 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004830 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004831
Willy Tarreaue7dff022015-04-03 01:14:29 +02004832 if (!(s->flags & SF_ERR_MASK))
4833 s->flags |= SF_ERR_CLITO;
4834 if (!(s->flags & SF_FINST_MASK))
4835 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004836 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004837 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004838
4839 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004840 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004841 /* Not enough data. We'll re-use the http-request
4842 * timeout here. Ideally, we should set the timeout
4843 * relative to the accept() date. We just set the
4844 * request timeout once at the beginning of the
4845 * request.
4846 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004847 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004848 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004849 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004850 return 0;
4851 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004852
4853 http_end:
4854 /* The situation will not evolve, so let's give up on the analysis. */
4855 s->logs.tv_request = now; /* update the request timer to reflect full request */
4856 req->analysers &= ~an_bit;
4857 req->analyse_exp = TICK_ETERNITY;
4858 return 1;
4859
4860 return_bad_req: /* let's centralize all bad requests */
4861 txn->req.msg_state = HTTP_MSG_ERROR;
4862 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004863 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004864
Willy Tarreaue7dff022015-04-03 01:14:29 +02004865 if (!(s->flags & SF_ERR_MASK))
4866 s->flags |= SF_ERR_PRXCOND;
4867 if (!(s->flags & SF_FINST_MASK))
4868 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004869
Willy Tarreau522d6c02009-12-06 18:49:18 +01004870 return_err_msg:
4871 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004872 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004873 if (sess->listener->counters)
4874 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004875 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004876}
4877
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004878/* send a server's name with an outgoing request over an established connection.
4879 * Note: this function is designed to be called once the request has been scheduled
4880 * for being forwarded. This is the reason why it rewinds the buffer before
4881 * proceeding.
4882 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004883int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004884
4885 struct hdr_ctx ctx;
4886
Mark Lamourinec2247f02012-01-04 13:02:01 -05004887 char *hdr_name = be->server_id_hdr_name;
4888 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004889 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004890 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004891 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004892
William Lallemandd9e90662012-01-30 17:27:17 +01004893 ctx.idx = 0;
4894
Willy Tarreau211cdec2014-04-17 20:18:08 +02004895 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004896 if (old_o) {
4897 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004898 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004899 txn->req.next += old_o;
4900 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004901 }
4902
Willy Tarreau9b28e032012-10-12 23:49:43 +02004903 old_i = chn->buf->i;
4904 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 -05004905 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004906 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004907 }
4908
4909 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004910 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004911 memcpy(hdr_val, hdr_name, hdr_name_len);
4912 hdr_val += hdr_name_len;
4913 *hdr_val++ = ':';
4914 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004915 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4916 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004917
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004918 if (old_o) {
4919 /* If this was a forwarded request, we must readjust the amount of
4920 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004921 * variations. Note that the current state is >= HTTP_MSG_BODY,
4922 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004923 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004924 old_o += chn->buf->i - old_i;
4925 b_adv(chn->buf, old_o);
4926 txn->req.next -= old_o;
4927 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004928 }
4929
Mark Lamourinec2247f02012-01-04 13:02:01 -05004930 return 0;
4931}
4932
Willy Tarreau610ecce2010-01-04 21:15:02 +01004933/* Terminate current transaction and prepare a new one. This is very tricky
4934 * right now but it works.
4935 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004936void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004937{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004938 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004939 struct proxy *fe = strm_fe(s);
Willy Tarreau068621e2013-12-23 15:11:25 +01004940
Willy Tarreau610ecce2010-01-04 21:15:02 +01004941 /* FIXME: We need a more portable way of releasing a backend's and a
4942 * server's connections. We need a safer way to reinitialize buffer
4943 * flags. We also need a more accurate method for computing per-request
4944 * data.
4945 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004946
Willy Tarreau4213a112013-12-15 10:25:42 +01004947 /* unless we're doing keep-alive, we want to quickly close the connection
4948 * to the server.
4949 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004950 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004951 !si_conn_ready(&s->si[1])) {
4952 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4953 si_shutr(&s->si[1]);
4954 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004955 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004956
Willy Tarreaue7dff022015-04-03 01:14:29 +02004957 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004958 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004959 if (unlikely(s->srv_conn))
4960 sess_change_server(s, NULL);
4961 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004962
4963 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004964 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004965
Willy Tarreaueee5b512015-04-03 23:46:31 +02004966 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004967 int n;
4968
Willy Tarreaueee5b512015-04-03 23:46:31 +02004969 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004970 if (n < 1 || n > 5)
4971 n = 0;
4972
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004973 if (fe->mode == PR_MODE_HTTP) {
4974 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02004975 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004976 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004977 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004978 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004979 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004980 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004981 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02004982 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004983 s->be->be_counters.p.http.comp_rsp++;
4984 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004985 }
4986
4987 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004988 s->logs.bytes_in -= s->req.buf->i;
4989 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004990
4991 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004992 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004993 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004994 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004995 s->do_log(s);
4996 }
4997
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004998 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004999 stream_stop_content_counters(s);
5000 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005001
Willy Tarreau610ecce2010-01-04 21:15:02 +01005002 s->logs.accept_date = date; /* user-visible date for logging */
5003 s->logs.tv_accept = now; /* corrected date for internal use */
5004 tv_zero(&s->logs.tv_request);
5005 s->logs.t_queue = -1;
5006 s->logs.t_connect = -1;
5007 s->logs.t_data = -1;
5008 s->logs.t_close = 0;
5009 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5010 s->logs.srv_queue_size = 0; /* we will get this number soon */
5011
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005012 s->logs.bytes_in = s->req.total = s->req.buf->i;
5013 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005014
5015 if (s->pend_pos)
5016 pendconn_free(s->pend_pos);
5017
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005018 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005019 if (s->flags & SF_CURR_SESS) {
5020 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005021 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005022 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005023 if (may_dequeue_tasks(objt_server(s->target), s->be))
5024 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005025 }
5026
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005027 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005028
Willy Tarreau4213a112013-12-15 10:25:42 +01005029 /* only release our endpoint if we don't intend to reuse the
5030 * connection.
5031 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005032 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005033 !si_conn_ready(&s->si[1])) {
5034 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005035 }
5036
Willy Tarreau350f4872014-11-28 14:42:25 +01005037 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5038 s->si[1].err_type = SI_ET_NONE;
5039 s->si[1].conn_retries = 0; /* used for logging too */
5040 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005041 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 +01005042 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);
5043 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 +02005044 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5045 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5046 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005047
Willy Tarreaueee5b512015-04-03 23:46:31 +02005048 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005049 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005050 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005051
5052 if (prev_status == 401 || prev_status == 407) {
5053 /* In HTTP keep-alive mode, if we receive a 401, we still have
5054 * a chance of being able to send the visitor again to the same
5055 * server over the same connection. This is required by some
5056 * broken protocols such as NTLM, and anyway whenever there is
5057 * an opportunity for sending the challenge to the proper place,
5058 * it's better to do it (at least it helps with debugging).
5059 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005060 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01005061 }
5062
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005063 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005064 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005065
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005066 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005067 s->req.flags |= CF_NEVER_WAIT;
5068 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005069 }
5070
Willy Tarreau610ecce2010-01-04 21:15:02 +01005071 /* if the request buffer is not empty, it means we're
5072 * about to process another request, so send pending
5073 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005074 * Just don't do this if the buffer is close to be full,
5075 * because the request will wait for it to flush a little
5076 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005077 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005078 if (s->req.buf->i) {
5079 if (s->res.buf->o &&
5080 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5081 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5082 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005083 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005084
5085 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005086 channel_auto_read(&s->req);
5087 channel_auto_close(&s->req);
5088 channel_auto_read(&s->res);
5089 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005090
Willy Tarreau27375622013-12-17 00:00:28 +01005091 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau350f4872014-11-28 14:42:25 +01005092 si_idle_conn(&s->si[1]);
Willy Tarreau27375622013-12-17 00:00:28 +01005093
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005094 s->req.analysers = strm_li(s)->analysers;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005095 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005096}
5097
5098
5099/* This function updates the request state machine according to the response
5100 * state machine and buffer flags. It returns 1 if it changes anything (flag
5101 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5102 * it is only used to find when a request/response couple is complete. Both
5103 * this function and its equivalent should loop until both return zero. It
5104 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5105 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005106int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005107{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005108 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005109 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005110 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005111 unsigned int old_state = txn->req.msg_state;
5112
Willy Tarreau610ecce2010-01-04 21:15:02 +01005113 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5114 return 0;
5115
5116 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005117 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005118 * We can shut the read side unless we want to abort_on_close,
5119 * or we have a POST request. The issue with POST requests is
5120 * that some browsers still send a CRLF after the request, and
5121 * this CRLF must be read so that it does not remain in the kernel
5122 * buffers, otherwise a close could cause an RST on some systems
5123 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005124 * Note that if we're using keep-alive on the client side, we'd
5125 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005126 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005127 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005128 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005129 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5130 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5131 !(s->be->options & PR_O_ABRT_CLOSE) &&
5132 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005133 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005134
Willy Tarreau40f151a2012-12-20 12:10:09 +01005135 /* if the server closes the connection, we want to immediately react
5136 * and close the socket to save packets and syscalls.
5137 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005138 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005139
Willy Tarreau610ecce2010-01-04 21:15:02 +01005140 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5141 goto wait_other_side;
5142
5143 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5144 /* The server has not finished to respond, so we
5145 * don't want to move in order not to upset it.
5146 */
5147 goto wait_other_side;
5148 }
5149
5150 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5151 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005152 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005153 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005154 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005155 goto wait_other_side;
5156 }
5157
5158 /* When we get here, it means that both the request and the
5159 * response have finished receiving. Depending on the connection
5160 * mode, we'll have to wait for the last bytes to leave in either
5161 * direction, and sometimes for a close to be effective.
5162 */
5163
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005164 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5165 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005166 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5167 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005168 }
5169 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5170 /* Option forceclose is set, or either side wants to close,
5171 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005172 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005173 * once both states are CLOSED.
5174 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005175 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5176 channel_shutr_now(chn);
5177 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005178 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005179 }
5180 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005181 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5182 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005183 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005184 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5185 channel_auto_read(chn);
5186 txn->req.msg_state = HTTP_MSG_TUNNEL;
5187 chn->flags |= CF_NEVER_WAIT;
5188 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005189 }
5190
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005191 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005192 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005193 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005194
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005195 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005196 txn->req.msg_state = HTTP_MSG_CLOSING;
5197 goto http_msg_closing;
5198 }
5199 else {
5200 txn->req.msg_state = HTTP_MSG_CLOSED;
5201 goto http_msg_closed;
5202 }
5203 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005204 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005205 }
5206
5207 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5208 http_msg_closing:
5209 /* nothing else to forward, just waiting for the output buffer
5210 * to be empty and for the shutw_now to take effect.
5211 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005212 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005213 txn->req.msg_state = HTTP_MSG_CLOSED;
5214 goto http_msg_closed;
5215 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005216 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005217 txn->req.msg_state = HTTP_MSG_ERROR;
5218 goto wait_other_side;
5219 }
5220 }
5221
5222 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5223 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005224 /* see above in MSG_DONE why we only do this in these states */
5225 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5226 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5227 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005228 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005229 goto wait_other_side;
5230 }
5231
5232 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005233 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005234}
5235
5236
5237/* This function updates the response state machine according to the request
5238 * state machine and buffer flags. It returns 1 if it changes anything (flag
5239 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5240 * it is only used to find when a request/response couple is complete. Both
5241 * this function and its equivalent should loop until both return zero. It
5242 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5243 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005244int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005245{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005246 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005247 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005248 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005249 unsigned int old_state = txn->rsp.msg_state;
5250
Willy Tarreau610ecce2010-01-04 21:15:02 +01005251 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5252 return 0;
5253
5254 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5255 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005256 * still monitor the server connection for a possible close
5257 * while the request is being uploaded, so we don't disable
5258 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005259 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005260 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005261
5262 if (txn->req.msg_state == HTTP_MSG_ERROR)
5263 goto wait_other_side;
5264
5265 if (txn->req.msg_state < HTTP_MSG_DONE) {
5266 /* The client seems to still be sending data, probably
5267 * because we got an error response during an upload.
5268 * We have the choice of either breaking the connection
5269 * or letting it pass through. Let's do the later.
5270 */
5271 goto wait_other_side;
5272 }
5273
5274 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5275 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005276 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005277 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005278 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005279 goto wait_other_side;
5280 }
5281
5282 /* When we get here, it means that both the request and the
5283 * response have finished receiving. Depending on the connection
5284 * mode, we'll have to wait for the last bytes to leave in either
5285 * direction, and sometimes for a close to be effective.
5286 */
5287
5288 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5289 /* Server-close mode : shut read and wait for the request
5290 * side to close its output buffer. The caller will detect
5291 * when we're in DONE and the other is in CLOSED and will
5292 * catch that for the final cleanup.
5293 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005294 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5295 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005296 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005297 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5298 /* Option forceclose is set, or either side wants to close,
5299 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005300 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005301 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005302 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005303 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5304 channel_shutr_now(chn);
5305 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005306 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005307 }
5308 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005309 /* The last possible modes are keep-alive and tunnel. Tunnel will
5310 * need to forward remaining data. Keep-alive will need to monitor
5311 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005312 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005313 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005314 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005315 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5316 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005317 }
5318
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005319 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005320 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005321 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005322 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5323 goto http_msg_closing;
5324 }
5325 else {
5326 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5327 goto http_msg_closed;
5328 }
5329 }
5330 goto wait_other_side;
5331 }
5332
5333 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5334 http_msg_closing:
5335 /* nothing else to forward, just waiting for the output buffer
5336 * to be empty and for the shutw_now to take effect.
5337 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005338 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005339 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5340 goto http_msg_closed;
5341 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005342 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005343 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005344 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005345 if (objt_server(s->target))
5346 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005347 goto wait_other_side;
5348 }
5349 }
5350
5351 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5352 http_msg_closed:
5353 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005354 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005355 channel_auto_close(chn);
5356 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005357 goto wait_other_side;
5358 }
5359
5360 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005361 /* We force the response to leave immediately if we're waiting for the
5362 * other side, since there is no pending shutdown to push it out.
5363 */
5364 if (!channel_is_empty(chn))
5365 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005366 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005367}
5368
5369
5370/* Resync the request and response state machines. Return 1 if either state
5371 * changes.
5372 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005373int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005374{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005375 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005376 int old_req_state = txn->req.msg_state;
5377 int old_res_state = txn->rsp.msg_state;
5378
Willy Tarreau610ecce2010-01-04 21:15:02 +01005379 http_sync_req_state(s);
5380 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005381 if (!http_sync_res_state(s))
5382 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005383 if (!http_sync_req_state(s))
5384 break;
5385 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005386
Willy Tarreau610ecce2010-01-04 21:15:02 +01005387 /* OK, both state machines agree on a compatible state.
5388 * There are a few cases we're interested in :
5389 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5390 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5391 * directions, so let's simply disable both analysers.
5392 * - HTTP_MSG_CLOSED on the response only means we must abort the
5393 * request.
5394 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5395 * with server-close mode means we've completed one request and we
5396 * must re-initialize the server connection.
5397 */
5398
5399 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5400 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5401 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5402 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005403 s->req.analysers = 0;
5404 channel_auto_close(&s->req);
5405 channel_auto_read(&s->req);
5406 s->res.analysers = 0;
5407 channel_auto_close(&s->res);
5408 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005409 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005410 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005411 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005412 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005413 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005414 s->res.analysers = 0;
5415 channel_auto_close(&s->res);
5416 channel_auto_read(&s->res);
5417 s->req.analysers = 0;
5418 channel_abort(&s->req);
5419 channel_auto_close(&s->req);
5420 channel_auto_read(&s->req);
5421 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005422 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005423 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5424 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005425 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005426 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5427 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5428 /* server-close/keep-alive: terminate this transaction,
5429 * possibly killing the server connection and reinitialize
5430 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005431 */
5432 http_end_txn_clean_session(s);
5433 }
5434
Willy Tarreau610ecce2010-01-04 21:15:02 +01005435 return txn->req.msg_state != old_req_state ||
5436 txn->rsp.msg_state != old_res_state;
5437}
5438
Willy Tarreaud98cf932009-12-27 22:54:55 +01005439/* This function is an analyser which forwards request body (including chunk
5440 * sizes if any). It is called as soon as we must forward, even if we forward
5441 * zero byte. The only situation where it must not be called is when we're in
5442 * tunnel mode and we want to forward till the close. It's used both to forward
5443 * remaining data and to resync after end of body. It expects the msg_state to
5444 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005445 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005446 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005447 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005448 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005449int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005450{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005451 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005452 struct http_txn *txn = s->txn;
5453 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005454
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005455 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5456 return 0;
5457
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005458 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005459 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005460 /* Output closed while we were sending data. We must abort and
5461 * wake the other side up.
5462 */
5463 msg->msg_state = HTTP_MSG_ERROR;
5464 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005465 return 1;
5466 }
5467
Willy Tarreaud98cf932009-12-27 22:54:55 +01005468 /* Note that we don't have to send 100-continue back because we don't
5469 * need the data to complete our job, and it's up to the server to
5470 * decide whether to return 100, 417 or anything else in return of
5471 * an "Expect: 100-continue" header.
5472 */
5473
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005474 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005475 /* we have msg->sov which points to the first byte of message
5476 * body, and req->buf.p still points to the beginning of the
5477 * message. We forward the headers now, as we don't need them
5478 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005479 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005480 b_adv(req->buf, msg->sov);
5481 msg->next -= msg->sov;
5482 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005483
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005484 /* The previous analysers guarantee that the state is somewhere
5485 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5486 * msg->next are always correct.
5487 */
5488 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5489 if (msg->flags & HTTP_MSGF_TE_CHNK)
5490 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5491 else
5492 msg->msg_state = HTTP_MSG_DATA;
5493 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005494 }
5495
Willy Tarreau7ba23542014-04-17 21:50:00 +02005496 /* Some post-connect processing might want us to refrain from starting to
5497 * forward data. Currently, the only reason for this is "balance url_param"
5498 * whichs need to parse/process the request after we've enabled forwarding.
5499 */
5500 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005501 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005502 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005503 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005504 goto missing_data;
5505 }
5506 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5507 }
5508
Willy Tarreau80a92c02014-03-12 10:41:13 +01005509 /* in most states, we should abort in case of early close */
5510 channel_auto_close(req);
5511
Willy Tarreauefdf0942014-04-24 20:08:57 +02005512 if (req->to_forward) {
5513 /* We can't process the buffer's contents yet */
5514 req->flags |= CF_WAKE_WRITE;
5515 goto missing_data;
5516 }
5517
Willy Tarreaud98cf932009-12-27 22:54:55 +01005518 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005519 if (msg->msg_state == HTTP_MSG_DATA) {
5520 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005521 /* we may have some pending data starting at req->buf->p */
5522 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005523 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005524 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005525 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005526 msg->next += msg->chunk_len;
5527 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005528
5529 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005530 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005531 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005532 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005533 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005534 }
5535 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005536 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005537 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005538 * TRAILERS state.
5539 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005540 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005541
Willy Tarreau54d23df2012-10-25 19:04:45 +02005542 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005543 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005544 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005545 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005546 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005547 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005548 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005549 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005550 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005551 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005552 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005553 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005554 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005555
5556 if (ret == 0)
5557 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005558 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005559 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005560 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005561 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005562 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005563 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005564 /* we're in MSG_CHUNK_SIZE now */
5565 }
5566 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005567 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005568
5569 if (ret == 0)
5570 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005571 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005572 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005573 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005574 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005575 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005576 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005577 /* we're in HTTP_MSG_DONE now */
5578 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005580 int old_state = msg->msg_state;
5581
Willy Tarreau610ecce2010-01-04 21:15:02 +01005582 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005583
5584 /* we may have some pending data starting at req->buf->p
5585 * such as last chunk of data or trailers.
5586 */
5587 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005588 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005589 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005590 msg->next = 0;
5591
Willy Tarreau4fe41902010-06-07 22:27:41 +02005592 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005593 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5594 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005595 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005596 if (http_resync_states(s)) {
5597 /* some state changes occurred, maybe the analyser
5598 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005599 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005600 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005601 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005602 /* request errors are most likely due to
5603 * the server aborting the transfer.
5604 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005605 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005606 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005607 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005608 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005609 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005610 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005611 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005612 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005613
5614 /* If "option abortonclose" is set on the backend, we
5615 * want to monitor the client's connection and forward
5616 * any shutdown notification to the server, which will
5617 * decide whether to close or to go on processing the
5618 * request.
5619 */
5620 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005621 channel_auto_read(req);
5622 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005623 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005624 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005625 /* POST requests may require to read extra CRLF
5626 * sent by broken browsers and which could cause
5627 * an RST to be sent upon close on some systems
5628 * (eg: Linux).
5629 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005630 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005631 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005632
Willy Tarreau610ecce2010-01-04 21:15:02 +01005633 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005634 }
5635 }
5636
Willy Tarreaud98cf932009-12-27 22:54:55 +01005637 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005638 /* we may have some pending data starting at req->buf->p */
5639 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005640 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005641 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5642
Willy Tarreaubed410e2014-04-22 08:19:34 +02005643 msg->next = 0;
5644 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5645
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005646 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005647 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005648 if (!(s->flags & SF_ERR_MASK))
5649 s->flags |= SF_ERR_CLICL;
5650 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005651 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005652 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005653 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005654 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005655 }
5656
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005657 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005658 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005659 if (objt_server(s->target))
5660 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005661
5662 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005663 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005664
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005665 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005666 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005667 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005668
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005669 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005670 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005671 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005672 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005673 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005674
Willy Tarreau5c620922011-05-11 19:56:11 +02005675 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005676 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005677 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005678 * modes are already handled by the stream sock layer. We must not do
5679 * this in content-length mode because it could present the MSG_MORE
5680 * flag with the last block of forwarded data, which would cause an
5681 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005682 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005683 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005684 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005685
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005686 return 0;
5687
Willy Tarreaud98cf932009-12-27 22:54:55 +01005688 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005689 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005690 if (sess->listener->counters)
5691 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005692
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005693 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005694 /* we may have some pending data starting at req->buf->p */
5695 b_adv(req->buf, msg->next);
5696 msg->next = 0;
5697
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005698 txn->req.msg_state = HTTP_MSG_ERROR;
5699 if (txn->status) {
5700 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005701 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005702 } else {
5703 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005704 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005705 }
5706 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005707 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005708
Willy Tarreaue7dff022015-04-03 01:14:29 +02005709 if (!(s->flags & SF_ERR_MASK))
5710 s->flags |= SF_ERR_PRXCOND;
5711 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005712 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005713 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005714 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005715 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005716 }
5717 return 0;
5718
5719 aborted_xfer:
5720 txn->req.msg_state = HTTP_MSG_ERROR;
5721 if (txn->status) {
5722 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005723 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005724 } else {
5725 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005726 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005727 }
5728 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005729 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005730
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005731 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005732 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005733 if (objt_server(s->target))
5734 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005735
Willy Tarreaue7dff022015-04-03 01:14:29 +02005736 if (!(s->flags & SF_ERR_MASK))
5737 s->flags |= SF_ERR_SRVCL;
5738 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005739 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005740 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005741 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005742 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005743 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005744 return 0;
5745}
5746
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005747/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5748 * processing can continue on next analysers, or zero if it either needs more
5749 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005750 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005751 * when it has nothing left to do, and may remove any analyser when it wants to
5752 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005753 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005754int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005755{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005756 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005757 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005758 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005759 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005760 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005761 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005762 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005763
Willy Tarreau87b09662015-04-03 00:22:06 +02005764 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 +02005765 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005766 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005767 rep,
5768 rep->rex, rep->wex,
5769 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005770 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005771 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005772
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005773 /*
5774 * Now parse the partial (or complete) lines.
5775 * We will check the response syntax, and also join multi-line
5776 * headers. An index of all the lines will be elaborated while
5777 * parsing.
5778 *
5779 * For the parsing, we use a 28 states FSM.
5780 *
5781 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005782 * rep->buf->p = beginning of response
5783 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5784 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005785 * msg->eol = end of current header or line (LF or CRLF)
5786 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005787 */
5788
Willy Tarreau628c40c2014-04-24 19:11:26 +02005789 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005790 /* There's a protected area at the end of the buffer for rewriting
5791 * purposes. We don't want to start to parse the request if the
5792 * protected area is affected, because we may have to move processed
5793 * data later, which is much more complicated.
5794 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005795 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005796 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005797 /* some data has still not left the buffer, wake us once that's done */
5798 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5799 goto abort_response;
5800 channel_dont_close(rep);
5801 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005802 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005803 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005804 }
5805
Willy Tarreau379357a2013-06-08 12:55:46 +02005806 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5807 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5808 buffer_slow_realign(rep->buf);
5809
Willy Tarreau9b28e032012-10-12 23:49:43 +02005810 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005811 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005812 }
5813
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005814 /* 1: we might have to print this header in debug mode */
5815 if (unlikely((global.mode & MODE_DEBUG) &&
5816 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005817 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005818 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005819
Willy Tarreau9b28e032012-10-12 23:49:43 +02005820 sol = rep->buf->p;
5821 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005822 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005823
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005824 sol += hdr_idx_first_pos(&txn->hdr_idx);
5825 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005826
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005827 while (cur_idx) {
5828 eol = sol + txn->hdr_idx.v[cur_idx].len;
5829 debug_hdr("srvhdr", s, sol, eol);
5830 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5831 cur_idx = txn->hdr_idx.v[cur_idx].next;
5832 }
5833 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005834
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005835 /*
5836 * Now we quickly check if we have found a full valid response.
5837 * If not so, we check the FD and buffer states before leaving.
5838 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005839 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005840 * responses are checked first.
5841 *
5842 * Depending on whether the client is still there or not, we
5843 * may send an error response back or not. Note that normally
5844 * we should only check for HTTP status there, and check I/O
5845 * errors somewhere else.
5846 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005847
Willy Tarreau655dce92009-11-08 13:10:58 +01005848 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005849 /* Invalid response */
5850 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5851 /* we detected a parsing error. We want to archive this response
5852 * in the dedicated proxy area for later troubleshooting.
5853 */
5854 hdr_response_bad:
5855 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005856 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005857
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005858 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005859 if (objt_server(s->target)) {
5860 objt_server(s->target)->counters.failed_resp++;
5861 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005862 }
Willy Tarreau64648412010-03-05 10:41:54 +01005863 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005864 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005865 rep->analysers = 0;
5866 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005867 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005868 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005869 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005870
Willy Tarreaue7dff022015-04-03 01:14:29 +02005871 if (!(s->flags & SF_ERR_MASK))
5872 s->flags |= SF_ERR_PRXCOND;
5873 if (!(s->flags & SF_FINST_MASK))
5874 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005875
5876 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005877 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005878
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005879 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005880 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005881 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005882 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005883 goto hdr_response_bad;
5884 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005885
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005886 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005887 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005888 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005889 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005890 else if (txn->flags & TX_NOT_FIRST)
5891 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005892
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005893 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005894 if (objt_server(s->target)) {
5895 objt_server(s->target)->counters.failed_resp++;
5896 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005897 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005898
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005899 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005900 rep->analysers = 0;
5901 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005902 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005903 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005904 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005905
Willy Tarreaue7dff022015-04-03 01:14:29 +02005906 if (!(s->flags & SF_ERR_MASK))
5907 s->flags |= SF_ERR_SRVCL;
5908 if (!(s->flags & SF_FINST_MASK))
5909 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005910 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005911 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005912
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005913 /* read timeout : return a 504 to the client. */
5914 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005915 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005916 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005917 else if (txn->flags & TX_NOT_FIRST)
5918 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005919
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005920 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005921 if (objt_server(s->target)) {
5922 objt_server(s->target)->counters.failed_resp++;
5923 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005924 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005925
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005926 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005927 rep->analysers = 0;
5928 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005929 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005930 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005931 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005932
Willy Tarreaue7dff022015-04-03 01:14:29 +02005933 if (!(s->flags & SF_ERR_MASK))
5934 s->flags |= SF_ERR_SRVTO;
5935 if (!(s->flags & SF_FINST_MASK))
5936 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005937 return 0;
5938 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005939
Willy Tarreauf003d372012-11-26 13:35:37 +01005940 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005941 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005942 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005943 s->be->be_counters.cli_aborts++;
5944 if (objt_server(s->target))
5945 objt_server(s->target)->counters.cli_aborts++;
5946
5947 rep->analysers = 0;
5948 channel_auto_close(rep);
5949
5950 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005951 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005952 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01005953
Willy Tarreaue7dff022015-04-03 01:14:29 +02005954 if (!(s->flags & SF_ERR_MASK))
5955 s->flags |= SF_ERR_CLICL;
5956 if (!(s->flags & SF_FINST_MASK))
5957 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005958
Willy Tarreau87b09662015-04-03 00:22:06 +02005959 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005960 return 0;
5961 }
5962
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005963 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005964 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005965 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005966 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005967 else if (txn->flags & TX_NOT_FIRST)
5968 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005969
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005970 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005971 if (objt_server(s->target)) {
5972 objt_server(s->target)->counters.failed_resp++;
5973 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005974 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005975
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005976 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005977 rep->analysers = 0;
5978 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005979 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005980 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005981 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005982
Willy Tarreaue7dff022015-04-03 01:14:29 +02005983 if (!(s->flags & SF_ERR_MASK))
5984 s->flags |= SF_ERR_SRVCL;
5985 if (!(s->flags & SF_FINST_MASK))
5986 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005987 return 0;
5988 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005989
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005990 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005991 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005992 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005993 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005994 else if (txn->flags & TX_NOT_FIRST)
5995 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005996
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005997 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005998 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005999 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006000
Willy Tarreaue7dff022015-04-03 01:14:29 +02006001 if (!(s->flags & SF_ERR_MASK))
6002 s->flags |= SF_ERR_CLICL;
6003 if (!(s->flags & SF_FINST_MASK))
6004 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006005
Willy Tarreau87b09662015-04-03 00:22:06 +02006006 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006007 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006008 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006009
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006010 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006011 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006012 return 0;
6013 }
6014
6015 /* More interesting part now : we know that we have a complete
6016 * response which at least looks like HTTP. We have an indicator
6017 * of each header's length, so we can parse them quickly.
6018 */
6019
6020 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006021 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006022
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006023 /*
6024 * 1: get the status code
6025 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006026 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006027 if (n < 1 || n > 5)
6028 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006029 /* when the client triggers a 4xx from the server, it's most often due
6030 * to a missing object or permission. These events should be tracked
6031 * because if they happen often, it may indicate a brute force or a
6032 * vulnerability scan.
6033 */
6034 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006035 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006036
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006037 if (objt_server(s->target))
6038 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006039
Willy Tarreau5b154472009-12-21 20:11:07 +01006040 /* check if the response is HTTP/1.1 or above */
6041 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006042 ((rep->buf->p[5] > '1') ||
6043 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006044 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006045
6046 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006047 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 +01006048
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006049 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006050 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006051
Willy Tarreau9b28e032012-10-12 23:49:43 +02006052 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006053
Willy Tarreau39650402010-03-15 19:44:39 +01006054 /* Adjust server's health based on status code. Note: status codes 501
6055 * and 505 are triggered on demand by client request, so we must not
6056 * count them as server failures.
6057 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006058 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006059 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006060 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006061 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006062 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006063 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006064
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006065 /*
6066 * 2: check for cacheability.
6067 */
6068
6069 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006070 case 100:
6071 /*
6072 * We may be facing a 100-continue response, in which case this
6073 * is not the right response, and we're waiting for the next one.
6074 * Let's allow this response to go to the client and wait for the
6075 * next one.
6076 */
6077 hdr_idx_init(&txn->hdr_idx);
6078 msg->next -= channel_forward(rep, msg->next);
6079 msg->msg_state = HTTP_MSG_RPBEFORE;
6080 txn->status = 0;
6081 s->logs.t_data = -1; /* was not a response yet */
6082 goto next_one;
6083
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006084 case 200:
6085 case 203:
6086 case 206:
6087 case 300:
6088 case 301:
6089 case 410:
6090 /* RFC2616 @13.4:
6091 * "A response received with a status code of
6092 * 200, 203, 206, 300, 301 or 410 MAY be stored
6093 * by a cache (...) unless a cache-control
6094 * directive prohibits caching."
6095 *
6096 * RFC2616 @9.5: POST method :
6097 * "Responses to this method are not cacheable,
6098 * unless the response includes appropriate
6099 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006100 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006101 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006102 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006103 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6104 break;
6105 default:
6106 break;
6107 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006108
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006109 /*
6110 * 3: we may need to capture headers
6111 */
6112 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006113 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006114 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006115 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006116
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006117 /* 4: determine the transfer-length.
6118 * According to RFC2616 #4.4, amended by the HTTPbis working group,
6119 * the presence of a message-body in a RESPONSE and its transfer length
6120 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006121 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006122 * All responses to the HEAD request method MUST NOT include a
6123 * message-body, even though the presence of entity-header fields
6124 * might lead one to believe they do. All 1xx (informational), 204
6125 * (No Content), and 304 (Not Modified) responses MUST NOT include a
6126 * message-body. All other responses do include a message-body,
6127 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006128 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006129 * 1. Any response which "MUST NOT" include a message-body (such as the
6130 * 1xx, 204 and 304 responses and any response to a HEAD request) is
6131 * always terminated by the first empty line after the header fields,
6132 * regardless of the entity-header fields present in the message.
6133 *
6134 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
6135 * the "chunked" transfer-coding (Section 6.2) is used, the
6136 * transfer-length is defined by the use of this transfer-coding.
6137 * If a Transfer-Encoding header field is present and the "chunked"
6138 * transfer-coding is not present, the transfer-length is defined by
6139 * the sender closing the connection.
6140 *
6141 * 3. If a Content-Length header field is present, its decimal value in
6142 * OCTETs represents both the entity-length and the transfer-length.
6143 * If a message is received with both a Transfer-Encoding header
6144 * field and a Content-Length header field, the latter MUST be ignored.
6145 *
6146 * 4. If the message uses the media type "multipart/byteranges", and
6147 * the transfer-length is not otherwise specified, then this self-
6148 * delimiting media type defines the transfer-length. This media
6149 * type MUST NOT be used unless the sender knows that the recipient
6150 * can parse it; the presence in a request of a Range header with
6151 * multiple byte-range specifiers from a 1.1 client implies that the
6152 * client can parse multipart/byteranges responses.
6153 *
6154 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006155 */
6156
6157 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006158 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006159 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006160 * FIXME: should we parse anyway and return an error on chunked encoding ?
6161 */
6162 if (txn->meth == HTTP_METH_HEAD ||
6163 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006164 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006165 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006166 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006167 goto skip_content_length;
6168 }
6169
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006170 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006171 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006172 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006173 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006174 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006175 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6176 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006177 /* bad transfer-encoding (chunked followed by something else) */
6178 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006179 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006180 break;
6181 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006182 }
6183
Willy Tarreau1c913912015-04-30 10:57:51 +02006184 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006185 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02006186 if (msg->flags & HTTP_MSGF_TE_CHNK) {
6187 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6188 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6189 }
6190 else while (!use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006191 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006192 signed long long cl;
6193
Willy Tarreauad14f752011-09-02 20:33:27 +02006194 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006195 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006196 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006197 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006198
Willy Tarreauad14f752011-09-02 20:33:27 +02006199 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006200 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006201 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006202 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006203
Willy Tarreauad14f752011-09-02 20:33:27 +02006204 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006205 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006206 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006207 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006208
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006209 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006210 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006211 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006212 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006213
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006214 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006215 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006216 }
6217
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006218 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006219 select_compression_response_header(s, rep->buf);
6220
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006221skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006222 /* Now we have to check if we need to modify the Connection header.
6223 * This is more difficult on the response than it is on the request,
6224 * because we can have two different HTTP versions and we don't know
6225 * how the client will interprete a response. For instance, let's say
6226 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6227 * HTTP/1.1 response without any header. Maybe it will bound itself to
6228 * HTTP/1.0 because it only knows about it, and will consider the lack
6229 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6230 * the lack of header as a keep-alive. Thus we will use two flags
6231 * indicating how a request MAY be understood by the client. In case
6232 * of multiple possibilities, we'll fix the header to be explicit. If
6233 * ambiguous cases such as both close and keepalive are seen, then we
6234 * will fall back to explicit close. Note that we won't take risks with
6235 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006236 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006237 */
6238
Willy Tarreaudc008c52010-02-01 16:20:08 +01006239 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6240 txn->status == 101)) {
6241 /* Either we've established an explicit tunnel, or we're
6242 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006243 * to understand the next protocols. We have to switch to tunnel
6244 * mode, so that we transfer the request and responses then let
6245 * this protocol pass unmodified. When we later implement specific
6246 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006247 * header which contains information about that protocol for
6248 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006249 */
6250 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6251 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006252 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6253 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006254 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006255 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006256 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006257
Willy Tarreau70dffda2014-01-30 03:07:23 +01006258 /* this situation happens when combining pretend-keepalive with httpclose. */
6259 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006260 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006261 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006262 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6263
Willy Tarreau60466522010-01-18 19:08:45 +01006264 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006265 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006266 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6267 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006268
Willy Tarreau60466522010-01-18 19:08:45 +01006269 /* now adjust header transformations depending on current state */
6270 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6271 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6272 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006273 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006274 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006275 }
Willy Tarreau60466522010-01-18 19:08:45 +01006276 else { /* SCL / KAL */
6277 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006278 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006279 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006280 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006281
Willy Tarreau60466522010-01-18 19:08:45 +01006282 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006283 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006284
Willy Tarreau60466522010-01-18 19:08:45 +01006285 /* Some keep-alive responses are converted to Server-close if
6286 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006287 */
Willy Tarreau60466522010-01-18 19:08:45 +01006288 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6289 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006290 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006291 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006292 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006293 }
6294
Willy Tarreau7959a552013-09-23 16:44:27 +02006295 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006296 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006297
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006298 /* end of job, return OK */
6299 rep->analysers &= ~an_bit;
6300 rep->analyse_exp = TICK_ETERNITY;
6301 channel_auto_close(rep);
6302 return 1;
6303
6304 abort_keep_alive:
6305 /* A keep-alive request to the server failed on a network error.
6306 * The client is required to retry. We need to close without returning
6307 * any other information so that the client retries.
6308 */
6309 txn->status = 0;
6310 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006311 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006312 channel_auto_close(rep);
6313 s->logs.logwait = 0;
6314 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006315 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006316 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006317 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006318 return 0;
6319}
6320
6321/* This function performs all the processing enabled for the current response.
6322 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006323 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006324 * other functions. It works like process_request (see indications above).
6325 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006326int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006327{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006328 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006329 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006330 struct http_msg *msg = &txn->rsp;
6331 struct proxy *cur_proxy;
6332 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006333 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006334
Willy Tarreau87b09662015-04-03 00:22:06 +02006335 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 +02006336 now_ms, __FUNCTION__,
6337 s,
6338 rep,
6339 rep->rex, rep->wex,
6340 rep->flags,
6341 rep->buf->i,
6342 rep->analysers);
6343
6344 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6345 return 0;
6346
Willy Tarreau70730dd2014-04-24 18:06:27 +02006347 /* The stats applet needs to adjust the Connection header but we don't
6348 * apply any filter there.
6349 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006350 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6351 rep->analysers &= ~an_bit;
6352 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006353 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006354 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006355
Willy Tarreau58975672014-04-24 21:13:57 +02006356 /*
6357 * We will have to evaluate the filters.
6358 * As opposed to version 1.2, now they will be evaluated in the
6359 * filters order and not in the header order. This means that
6360 * each filter has to be validated among all headers.
6361 *
6362 * Filters are tried with ->be first, then with ->fe if it is
6363 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006364 *
6365 * Maybe we are in resume condiion. In this case I choose the
6366 * "struct proxy" which contains the rule list matching the resume
6367 * pointer. If none of theses "struct proxy" match, I initialise
6368 * the process with the first one.
6369 *
6370 * In fact, I check only correspondance betwwen the current list
6371 * pointer and the ->fe rule list. If it doesn't match, I initialize
6372 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006373 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006374 if (s->current_rule_list == &sess->fe->http_res_rules)
6375 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006376 else
6377 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006378 while (1) {
6379 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006380
Willy Tarreau58975672014-04-24 21:13:57 +02006381 /* evaluate http-response rules */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006382 if (ret == HTTP_RULE_RES_CONT)
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006383 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006384
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006385 /* we need to be called again. */
6386 if (ret == HTTP_RULE_RES_YIELD) {
6387 channel_dont_close(rep);
6388 return 0;
6389 }
6390
Willy Tarreau58975672014-04-24 21:13:57 +02006391 /* try headers filters */
6392 if (rule_set->rsp_exp != NULL) {
6393 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6394 return_bad_resp:
6395 if (objt_server(s->target)) {
6396 objt_server(s->target)->counters.failed_resp++;
6397 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006398 }
Willy Tarreau58975672014-04-24 21:13:57 +02006399 s->be->be_counters.failed_resp++;
6400 return_srv_prx_502:
6401 rep->analysers = 0;
6402 txn->status = 502;
6403 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006404 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006405 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006406 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006407 if (!(s->flags & SF_ERR_MASK))
6408 s->flags |= SF_ERR_PRXCOND;
6409 if (!(s->flags & SF_FINST_MASK))
6410 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006411 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006412 }
Willy Tarreau58975672014-04-24 21:13:57 +02006413 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006414
Willy Tarreau58975672014-04-24 21:13:57 +02006415 /* has the response been denied ? */
6416 if (txn->flags & TX_SVDENY) {
6417 if (objt_server(s->target))
6418 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006419
Willy Tarreau58975672014-04-24 21:13:57 +02006420 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006421 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006422 if (sess->listener->counters)
6423 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006424
Willy Tarreau58975672014-04-24 21:13:57 +02006425 goto return_srv_prx_502;
6426 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006427
Willy Tarreau58975672014-04-24 21:13:57 +02006428 /* add response headers from the rule sets in the same order */
6429 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006430 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006431 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006432 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006433 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006434 ret = acl_pass(ret);
6435 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6436 ret = !ret;
6437 if (!ret)
6438 continue;
6439 }
6440 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6441 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006442 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006443
Willy Tarreau58975672014-04-24 21:13:57 +02006444 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006445 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006446 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006447 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006448 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006449
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006450 /* After this point, this anayzer can't return yield, so we can
6451 * remove the bit corresponding to this analyzer from the list.
6452 *
6453 * Note that the intermediate returns and goto found previously
6454 * reset the analyzers.
6455 */
6456 rep->analysers &= ~an_bit;
6457 rep->analyse_exp = TICK_ETERNITY;
6458
Willy Tarreau58975672014-04-24 21:13:57 +02006459 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006460 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006461 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006462
Willy Tarreau58975672014-04-24 21:13:57 +02006463 /*
6464 * Now check for a server cookie.
6465 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006466 if (s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name ||
Willy Tarreau58975672014-04-24 21:13:57 +02006467 (s->be->options & PR_O_CHK_CACHE))
6468 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006469
Willy Tarreau58975672014-04-24 21:13:57 +02006470 /*
6471 * Check for cache-control or pragma headers if required.
6472 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006473 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 +02006474 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006475
Willy Tarreau58975672014-04-24 21:13:57 +02006476 /*
6477 * Add server cookie in the response if needed
6478 */
6479 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6480 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006481 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006482 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6483 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6484 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6485 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6486 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006487 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006488 /* the server is known, it's not the one the client requested, or the
6489 * cookie's last seen date needs to be refreshed. We have to
6490 * insert a set-cookie here, except if we want to insert only on POST
6491 * requests and this one isn't. Note that servers which don't have cookies
6492 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006493 */
Willy Tarreau58975672014-04-24 21:13:57 +02006494 if (!objt_server(s->target)->cookie) {
6495 chunk_printf(&trash,
6496 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6497 s->be->cookie_name);
6498 }
6499 else {
6500 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006501
Willy Tarreau58975672014-04-24 21:13:57 +02006502 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6503 /* emit last_date, which is mandatory */
6504 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6505 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6506 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006507
Willy Tarreau58975672014-04-24 21:13:57 +02006508 if (s->be->cookie_maxlife) {
6509 /* emit first_date, which is either the original one or
6510 * the current date.
6511 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006512 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006513 s30tob64(txn->cookie_first_date ?
6514 txn->cookie_first_date >> 2 :
6515 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006516 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006517 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006518 }
Willy Tarreau58975672014-04-24 21:13:57 +02006519 chunk_appendf(&trash, "; path=/");
6520 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006521
Willy Tarreau58975672014-04-24 21:13:57 +02006522 if (s->be->cookie_domain)
6523 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006524
Willy Tarreau58975672014-04-24 21:13:57 +02006525 if (s->be->ck_opts & PR_CK_HTTPONLY)
6526 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006527
Willy Tarreau58975672014-04-24 21:13:57 +02006528 if (s->be->ck_opts & PR_CK_SECURE)
6529 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006530
Willy Tarreau58975672014-04-24 21:13:57 +02006531 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6532 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006533
Willy Tarreau58975672014-04-24 21:13:57 +02006534 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006535 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006536 /* the server did not change, only the date was updated */
6537 txn->flags |= TX_SCK_UPDATED;
6538 else
6539 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006540
Willy Tarreau58975672014-04-24 21:13:57 +02006541 /* Here, we will tell an eventual cache on the client side that we don't
6542 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6543 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6544 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006545 */
Willy Tarreau58975672014-04-24 21:13:57 +02006546 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006547
Willy Tarreau58975672014-04-24 21:13:57 +02006548 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006549
Willy Tarreau58975672014-04-24 21:13:57 +02006550 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6551 "Cache-control: private", 22) < 0))
6552 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006553 }
Willy Tarreau58975672014-04-24 21:13:57 +02006554 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006555
Willy Tarreau58975672014-04-24 21:13:57 +02006556 /*
6557 * Check if result will be cacheable with a cookie.
6558 * We'll block the response if security checks have caught
6559 * nasty things such as a cacheable cookie.
6560 */
6561 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6562 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6563 (s->be->options & PR_O_CHK_CACHE)) {
6564 /* we're in presence of a cacheable response containing
6565 * a set-cookie header. We'll block it as requested by
6566 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006567 */
Willy Tarreau58975672014-04-24 21:13:57 +02006568 if (objt_server(s->target))
6569 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006570
Willy Tarreau58975672014-04-24 21:13:57 +02006571 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006572 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006573 if (sess->listener->counters)
6574 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006575
Willy Tarreau58975672014-04-24 21:13:57 +02006576 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6577 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6578 send_log(s->be, LOG_ALERT,
6579 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6580 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6581 goto return_srv_prx_502;
6582 }
Willy Tarreau03945942009-12-22 16:50:27 +01006583
Willy Tarreau70730dd2014-04-24 18:06:27 +02006584 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006585 /*
6586 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6587 * If an "Upgrade" token is found, the header is left untouched in order
6588 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006589 * if anything but "Upgrade" is present in the Connection header. We don't
6590 * want to touch any 101 response either since it's switching to another
6591 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006592 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006593 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006594 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006595 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006596 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6597 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006598
Willy Tarreau58975672014-04-24 21:13:57 +02006599 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6600 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6601 /* we want a keep-alive response here. Keep-alive header
6602 * required if either side is not 1.1.
6603 */
6604 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6605 want_flags |= TX_CON_KAL_SET;
6606 }
6607 else {
6608 /* we want a close response here. Close header required if
6609 * the server is 1.1, regardless of the client.
6610 */
6611 if (msg->flags & HTTP_MSGF_VER_11)
6612 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006613 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006614
Willy Tarreau58975672014-04-24 21:13:57 +02006615 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6616 http_change_connection_header(txn, msg, want_flags);
6617 }
6618
6619 skip_header_mangling:
6620 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6621 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6622 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006623
Willy Tarreau58975672014-04-24 21:13:57 +02006624 /* if the user wants to log as soon as possible, without counting
6625 * bytes from the server, then this is the right moment. We have
6626 * to temporarily assign bytes_out to log what we currently have.
6627 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006628 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006629 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6630 s->logs.bytes_out = txn->rsp.eoh;
6631 s->do_log(s);
6632 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006633 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006634 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006635}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006636
Willy Tarreaud98cf932009-12-27 22:54:55 +01006637/* This function is an analyser which forwards response body (including chunk
6638 * sizes if any). It is called as soon as we must forward, even if we forward
6639 * zero byte. The only situation where it must not be called is when we're in
6640 * tunnel mode and we want to forward till the close. It's used both to forward
6641 * remaining data and to resync after end of body. It expects the msg_state to
6642 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006643 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006644 *
6645 * It is capable of compressing response data both in content-length mode and
6646 * in chunked mode. The state machines follows different flows depending on
6647 * whether content-length and chunked modes are used, since there are no
6648 * trailers in content-length :
6649 *
6650 * chk-mode cl-mode
6651 * ,----- BODY -----.
6652 * / \
6653 * V size > 0 V chk-mode
6654 * .--> SIZE -------------> DATA -------------> CRLF
6655 * | | size == 0 | last byte |
6656 * | v final crlf v inspected |
6657 * | TRAILERS -----------> DONE |
6658 * | |
6659 * `----------------------------------------------'
6660 *
6661 * Compression only happens in the DATA state, and must be flushed in final
6662 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6663 * is performed at once on final states for all bytes parsed, or when leaving
6664 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006665 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006666int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006667{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006668 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006669 struct http_txn *txn = s->txn;
6670 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006671 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006672 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006673 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006674
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006675 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6676 return 0;
6677
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006678 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006679 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006680 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006681 /* Output closed while we were sending data. We must abort and
6682 * wake the other side up.
6683 */
6684 msg->msg_state = HTTP_MSG_ERROR;
6685 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006686 return 1;
6687 }
6688
Willy Tarreau4fe41902010-06-07 22:27:41 +02006689 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006690 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006691
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006692 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006693 /* we have msg->sov which points to the first byte of message
6694 * body, and res->buf.p still points to the beginning of the
6695 * message. We forward the headers now, as we don't need them
6696 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006697 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006698 b_adv(res->buf, msg->sov);
6699 msg->next -= msg->sov;
6700 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006701
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006702 /* The previous analysers guarantee that the state is somewhere
6703 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6704 * msg->next are always correct.
6705 */
6706 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6707 if (msg->flags & HTTP_MSGF_TE_CHNK)
6708 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6709 else
6710 msg->msg_state = HTTP_MSG_DATA;
6711 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006712 }
6713
Willy Tarreauefdf0942014-04-24 20:08:57 +02006714 if (res->to_forward) {
6715 /* We can't process the buffer's contents yet */
6716 res->flags |= CF_WAKE_WRITE;
6717 goto missing_data;
6718 }
6719
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006720 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6721 /* We need a compression buffer in the DATA state to put the
6722 * output of compressed data, and in CRLF state to let the
6723 * TRAILERS state finish the job of removing the trailing CRLF.
6724 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006725 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006726 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006727 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006728 goto aborted_xfer; /* no memory */
6729 }
6730
6731 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006732 if (ret < 0) {
6733 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006734 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006735 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006736 compressing = 1;
6737 }
6738
Willy Tarreaud98cf932009-12-27 22:54:55 +01006739 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006740 switch (msg->msg_state - HTTP_MSG_DATA) {
6741 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006742 /* we may have some pending data starting at res->buf->p */
6743 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006744 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006745 if (ret < 0)
6746 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006747
Willy Tarreaud5a67832014-04-21 10:54:27 +02006748 if (msg->chunk_len) {
6749 /* input empty or output full */
6750 if (res->buf->i > msg->next)
6751 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006752 goto missing_data;
6753 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006754 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006755 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006756 if (msg->chunk_len > res->buf->i - msg->next) {
6757 /* output full */
6758 res->flags |= CF_WAKE_WRITE;
6759 goto missing_data;
6760 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006761 msg->next += msg->chunk_len;
6762 msg->chunk_len = 0;
6763 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006764
6765 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006766 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006767 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006768 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006769 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006770 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006771 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006772 /* fall through for HTTP_MSG_CHUNK_CRLF */
6773
6774 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6775 /* we want the CRLF after the data */
6776
6777 ret = http_skip_chunk_crlf(msg);
6778 if (ret == 0)
6779 goto missing_data;
6780 else if (ret < 0) {
6781 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006782 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006783 goto return_bad_res;
6784 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006785 /* we're in MSG_CHUNK_SIZE now, fall through */
6786
6787 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006788 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006789 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006790 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006791 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006792
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006793 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006794 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006795 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006796 else if (ret < 0) {
6797 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006798 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006799 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006800 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006801 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006802 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006803
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006804 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006805 if (unlikely(compressing)) {
6806 /* we need to flush output contents before syncing FSMs */
6807 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6808 compressing = 0;
6809 }
6810
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006811 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006812 if (ret == 0)
6813 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006814 else if (ret < 0) {
6815 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006816 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006817 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006818 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006819 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006820
6821 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006822 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006823 if (unlikely(compressing)) {
6824 /* we need to flush output contents before syncing FSMs */
6825 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6826 compressing = 0;
6827 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006828
Willy Tarreauc623c172014-04-18 09:53:50 +02006829 /* we may have some pending data starting at res->buf->p
6830 * such as a last chunk of data or trailers.
6831 */
6832 b_adv(res->buf, msg->next);
6833 msg->next = 0;
6834
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006835 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006836 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006837 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6838 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006839 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006840
Willy Tarreau610ecce2010-01-04 21:15:02 +01006841 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006842 /* some state changes occurred, maybe the analyser
6843 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006844 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006845 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006846 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006847 /* response errors are most likely due to
6848 * the client aborting the transfer.
6849 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006850 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006851 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006852 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006853 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006854 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006855 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006856 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006857 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006858 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006859 }
6860 }
6861
Willy Tarreaud98cf932009-12-27 22:54:55 +01006862 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006863 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006864 if (unlikely(compressing)) {
6865 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006866 compressing = 0;
6867 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006868
Willy Tarreauc623c172014-04-18 09:53:50 +02006869 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6870 b_adv(res->buf, msg->next);
6871 msg->next = 0;
6872 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6873 }
6874
Willy Tarreauf003d372012-11-26 13:35:37 +01006875 if (res->flags & CF_SHUTW)
6876 goto aborted_xfer;
6877
6878 /* stop waiting for data if the input is closed before the end. If the
6879 * client side was already closed, it means that the client has aborted,
6880 * so we don't want to count this as a server abort. Otherwise it's a
6881 * server abort.
6882 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006883 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006884 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006885 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006886 if (!(s->flags & SF_ERR_MASK))
6887 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006888 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006889 if (objt_server(s->target))
6890 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006891 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006892 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006893
Willy Tarreau40dba092010-03-04 18:14:51 +01006894 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006895 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006896 goto return_bad_res;
6897
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006898 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006899 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006900 * Similarly, with keep-alive on the client side, we don't want to forward a
6901 * close.
6902 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006903 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006904 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6905 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006906 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006907
Willy Tarreau5c620922011-05-11 19:56:11 +02006908 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006909 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006910 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006911 * modes are already handled by the stream sock layer. We must not do
6912 * this in content-length mode because it could present the MSG_MORE
6913 * flag with the last block of forwarded data, which would cause an
6914 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006915 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006916 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006917 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006918
Willy Tarreau87b09662015-04-03 00:22:06 +02006919 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006920 return 0;
6921
Willy Tarreau40dba092010-03-04 18:14:51 +01006922 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006923 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006924 if (objt_server(s->target))
6925 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006926
6927 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006928 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006929 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006930 compressing = 0;
6931 }
6932
Willy Tarreauc623c172014-04-18 09:53:50 +02006933 /* we may have some pending data starting at res->buf->p */
6934 if (s->comp_algo == NULL) {
6935 b_adv(res->buf, msg->next);
6936 msg->next = 0;
6937 }
6938
Willy Tarreaud98cf932009-12-27 22:54:55 +01006939 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006940 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006941 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006942 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006943 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006944 if (objt_server(s->target))
6945 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006946
Willy Tarreaue7dff022015-04-03 01:14:29 +02006947 if (!(s->flags & SF_ERR_MASK))
6948 s->flags |= SF_ERR_PRXCOND;
6949 if (!(s->flags & SF_FINST_MASK))
6950 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006951 return 0;
6952
6953 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006954 if (unlikely(compressing)) {
6955 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6956 compressing = 0;
6957 }
6958
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006959 txn->rsp.msg_state = HTTP_MSG_ERROR;
6960 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006961 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006962 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006963 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006964
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006965 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006966 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006967 if (objt_server(s->target))
6968 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006969
Willy Tarreaue7dff022015-04-03 01:14:29 +02006970 if (!(s->flags & SF_ERR_MASK))
6971 s->flags |= SF_ERR_CLICL;
6972 if (!(s->flags & SF_FINST_MASK))
6973 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006974 return 0;
6975}
6976
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006977/* Iterate the same filter through all request headers.
6978 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006979 * Since it can manage the switch to another backend, it updates the per-proxy
6980 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006981 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006982int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006983{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006984 char *cur_ptr, *cur_end, *cur_next;
6985 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006986 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006987 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006988 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006989
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006990 last_hdr = 0;
6991
Willy Tarreau9b28e032012-10-12 23:49:43 +02006992 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006993 old_idx = 0;
6994
6995 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006996 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006997 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006998 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006999 (exp->action == ACT_ALLOW ||
7000 exp->action == ACT_DENY ||
7001 exp->action == ACT_TARPIT))
7002 return 0;
7003
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007004 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007005 if (!cur_idx)
7006 break;
7007
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007008 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007009 cur_ptr = cur_next;
7010 cur_end = cur_ptr + cur_hdr->len;
7011 cur_next = cur_end + cur_hdr->cr + 1;
7012
7013 /* Now we have one header between cur_ptr and cur_end,
7014 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007015 */
7016
Willy Tarreau15a53a42015-01-21 13:39:42 +01007017 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007018 switch (exp->action) {
7019 case ACT_SETBE:
7020 /* It is not possible to jump a second time.
7021 * FIXME: should we return an HTTP/500 here so that
7022 * the admin knows there's a problem ?
7023 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007024 if (s->be != strm_fe(s))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007025 break;
7026
7027 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02007028 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007029 last_hdr = 1;
7030 break;
7031
7032 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007033 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007034 last_hdr = 1;
7035 break;
7036
7037 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007038 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007039 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007040 break;
7041
7042 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007043 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007044 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007045 break;
7046
7047 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007048 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7049 if (trash.len < 0)
7050 return -1;
7051
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007052 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007053 /* FIXME: if the user adds a newline in the replacement, the
7054 * index will not be recalculated for now, and the new line
7055 * will not be counted as a new header.
7056 */
7057
7058 cur_end += delta;
7059 cur_next += delta;
7060 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007061 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007062 break;
7063
7064 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007065 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007066 cur_next += delta;
7067
Willy Tarreaufa355d42009-11-29 18:12:29 +01007068 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007069 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7070 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007071 cur_hdr->len = 0;
7072 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007073 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007074 break;
7075
7076 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007077 }
7078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007079 /* keep the link from this header to next one in case of later
7080 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007082 old_idx = cur_idx;
7083 }
7084 return 0;
7085}
7086
7087
7088/* Apply the filter to the request line.
7089 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7090 * or -1 if a replacement resulted in an invalid request line.
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 Tarreau8d5d7f22007-01-21 19:16:41 +01007093 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007094int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007095{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007096 char *cur_ptr, *cur_end;
7097 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007098 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007099 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007100
Willy Tarreau3d300592007-03-18 18:34:41 +01007101 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007102 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007103 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007104 (exp->action == ACT_ALLOW ||
7105 exp->action == ACT_DENY ||
7106 exp->action == ACT_TARPIT))
7107 return 0;
7108 else if (exp->action == ACT_REMOVE)
7109 return 0;
7110
7111 done = 0;
7112
Willy Tarreau9b28e032012-10-12 23:49:43 +02007113 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007114 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007115
7116 /* Now we have the request line between cur_ptr and cur_end */
7117
Willy Tarreau15a53a42015-01-21 13:39:42 +01007118 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007119 switch (exp->action) {
7120 case ACT_SETBE:
7121 /* It is not possible to jump a second time.
7122 * FIXME: should we return an HTTP/500 here so that
7123 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007124 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007125 if (s->be != strm_fe(s))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007126 break;
7127
7128 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02007129 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007130 done = 1;
7131 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007132
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007133 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007134 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007135 done = 1;
7136 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007137
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007138 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007139 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007140 done = 1;
7141 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007142
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007143 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007144 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007145 done = 1;
7146 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007147
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007148 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007149 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7150 if (trash.len < 0)
7151 return -1;
7152
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007153 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007154 /* FIXME: if the user adds a newline in the replacement, the
7155 * index will not be recalculated for now, and the new line
7156 * will not be counted as a new header.
7157 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007158
Willy Tarreaufa355d42009-11-29 18:12:29 +01007159 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007160 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007161 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007162 HTTP_MSG_RQMETH,
7163 cur_ptr, cur_end + 1,
7164 NULL, NULL);
7165 if (unlikely(!cur_end))
7166 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007168 /* we have a full request and we know that we have either a CR
7169 * or an LF at <ptr>.
7170 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007171 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7172 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007173 /* there is no point trying this regex on headers */
7174 return 1;
7175 }
7176 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007177 return done;
7178}
Willy Tarreau97de6242006-12-27 17:18:38 +01007179
Willy Tarreau58f10d72006-12-04 02:26:12 +01007180
Willy Tarreau58f10d72006-12-04 02:26:12 +01007181
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007182/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007183 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007184 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185 * unparsable request. Since it can manage the switch to another backend, it
7186 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007187 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007188int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007189{
Willy Tarreau192252e2015-04-04 01:47:55 +02007190 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007191 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007192 struct hdr_exp *exp;
7193
7194 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007195 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007196
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007197 /*
7198 * The interleaving of transformations and verdicts
7199 * makes it difficult to decide to continue or stop
7200 * the evaluation.
7201 */
7202
Willy Tarreau6c123b12010-01-28 20:22:06 +01007203 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7204 break;
7205
Willy Tarreau3d300592007-03-18 18:34:41 +01007206 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007207 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007208 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007209 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007210
7211 /* if this filter had a condition, evaluate it now and skip to
7212 * next filter if the condition does not match.
7213 */
7214 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007215 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007216 ret = acl_pass(ret);
7217 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7218 ret = !ret;
7219
7220 if (!ret)
7221 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007222 }
7223
7224 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007225 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007226 if (unlikely(ret < 0))
7227 return -1;
7228
7229 if (likely(ret == 0)) {
7230 /* The filter did not match the request, it can be
7231 * iterated through all headers.
7232 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007233 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7234 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007235 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007236 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007237 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007238}
7239
7240
Willy Tarreaua15645d2007-03-18 16:22:39 +01007241
Willy Tarreau58f10d72006-12-04 02:26:12 +01007242/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007243 * Try to retrieve the server associated to the appsession.
Willy Tarreau87b09662015-04-03 00:22:06 +02007244 * If the server is found, it's assigned to the stream.
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007245 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007246void manage_client_side_appsession(struct stream *s, const char *buf, int len) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02007247 struct http_txn *txn = s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007248 appsess *asession = NULL;
7249 char *sessid_temp = NULL;
7250
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007251 if (len > s->be->appsession_len) {
7252 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007253 }
7254
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007255 if (s->be->options2 & PR_O2_AS_REQL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007256 /* request-learn option is enabled : store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007257 if (txn->sessid != NULL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007258 /* free previously allocated memory as we don't need the stream id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007259 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007260 }
7261
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007262 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007263 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007264 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007265 return;
7266 }
7267
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007268 memcpy(txn->sessid, buf, len);
7269 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007270 }
7271
7272 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7273 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007274 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007275 return;
7276 }
7277
Cyril Bontéb21570a2009-11-29 20:04:48 +01007278 memcpy(sessid_temp, buf, len);
7279 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007280
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007281 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007282 /* free previously allocated memory */
7283 pool_free2(apools.sessid, sessid_temp);
7284
7285 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007286 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7287 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007288 asession->request_count++;
7289
7290 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007291 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007292
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007293 while (srv) {
7294 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007295 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007296 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007297 (s->flags & SF_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007298 /* we found the server and it's usable */
7299 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007300 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007301 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007302 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007303
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007304 break;
7305 } else {
7306 txn->flags &= ~TX_CK_MASK;
7307 txn->flags |= TX_CK_DOWN;
7308 }
7309 }
7310 srv = srv->next;
7311 }
7312 }
7313 }
7314}
7315
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007316/* Find the end of a cookie value contained between <s> and <e>. It works the
7317 * same way as with headers above except that the semi-colon also ends a token.
7318 * See RFC2965 for more information. Note that it requires a valid header to
7319 * return a valid result.
7320 */
7321char *find_cookie_value_end(char *s, const char *e)
7322{
7323 int quoted, qdpair;
7324
7325 quoted = qdpair = 0;
7326 for (; s < e; s++) {
7327 if (qdpair) qdpair = 0;
7328 else if (quoted) {
7329 if (*s == '\\') qdpair = 1;
7330 else if (*s == '"') quoted = 0;
7331 }
7332 else if (*s == '"') quoted = 1;
7333 else if (*s == ',' || *s == ';') return s;
7334 }
7335 return s;
7336}
7337
7338/* Delete a value in a header between delimiters <from> and <next> in buffer
7339 * <buf>. The number of characters displaced is returned, and the pointer to
7340 * the first delimiter is updated if required. The function tries as much as
7341 * possible to respect the following principles :
7342 * - replace <from> delimiter by the <next> one unless <from> points to a
7343 * colon, in which case <next> is simply removed
7344 * - set exactly one space character after the new first delimiter, unless
7345 * there are not enough characters in the block being moved to do so.
7346 * - remove unneeded spaces before the previous delimiter and after the new
7347 * one.
7348 *
7349 * It is the caller's responsibility to ensure that :
7350 * - <from> points to a valid delimiter or the colon ;
7351 * - <next> points to a valid delimiter or the final CR/LF ;
7352 * - there are non-space chars before <from> ;
7353 * - there is a CR/LF at or after <next>.
7354 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007355int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007356{
7357 char *prev = *from;
7358
7359 if (*prev == ':') {
7360 /* We're removing the first value, preserve the colon and add a
7361 * space if possible.
7362 */
7363 if (!http_is_crlf[(unsigned char)*next])
7364 next++;
7365 prev++;
7366 if (prev < next)
7367 *prev++ = ' ';
7368
7369 while (http_is_spht[(unsigned char)*next])
7370 next++;
7371 } else {
7372 /* Remove useless spaces before the old delimiter. */
7373 while (http_is_spht[(unsigned char)*(prev-1)])
7374 prev--;
7375 *from = prev;
7376
7377 /* copy the delimiter and if possible a space if we're
7378 * not at the end of the line.
7379 */
7380 if (!http_is_crlf[(unsigned char)*next]) {
7381 *prev++ = *next++;
7382 if (prev + 1 < next)
7383 *prev++ = ' ';
7384 while (http_is_spht[(unsigned char)*next])
7385 next++;
7386 }
7387 }
7388 return buffer_replace2(buf, prev, next, NULL, 0);
7389}
7390
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007391/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007392 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007393 * desirable to call it only when needed. This code is quite complex because
7394 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7395 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007396 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007397void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007398{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007399 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007400 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007401 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007402 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007403 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7404 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007405
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007406 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007407 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007408 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007409
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007410 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007411 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007412 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007413
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007414 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007415 hdr_beg = hdr_next;
7416 hdr_end = hdr_beg + cur_hdr->len;
7417 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007418
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007419 /* We have one full header between hdr_beg and hdr_end, and the
7420 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007421 * "Cookie:" headers.
7422 */
7423
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007424 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007425 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007426 old_idx = cur_idx;
7427 continue;
7428 }
7429
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007430 del_from = NULL; /* nothing to be deleted */
7431 preserve_hdr = 0; /* assume we may kill the whole header */
7432
Willy Tarreau58f10d72006-12-04 02:26:12 +01007433 /* Now look for cookies. Conforming to RFC2109, we have to support
7434 * attributes whose name begin with a '$', and associate them with
7435 * the right cookie, if we want to delete this cookie.
7436 * So there are 3 cases for each cookie read :
7437 * 1) it's a special attribute, beginning with a '$' : ignore it.
7438 * 2) it's a server id cookie that we *MAY* want to delete : save
7439 * some pointers on it (last semi-colon, beginning of cookie...)
7440 * 3) it's an application cookie : we *MAY* have to delete a previous
7441 * "special" cookie.
7442 * At the end of loop, if a "special" cookie remains, we may have to
7443 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007444 * *MUST* delete it.
7445 *
7446 * Note: RFC2965 is unclear about the processing of spaces around
7447 * the equal sign in the ATTR=VALUE form. A careful inspection of
7448 * the RFC explicitly allows spaces before it, and not within the
7449 * tokens (attrs or values). An inspection of RFC2109 allows that
7450 * too but section 10.1.3 lets one think that spaces may be allowed
7451 * after the equal sign too, resulting in some (rare) buggy
7452 * implementations trying to do that. So let's do what servers do.
7453 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7454 * allowed quoted strings in values, with any possible character
7455 * after a backslash, including control chars and delimitors, which
7456 * causes parsing to become ambiguous. Browsers also allow spaces
7457 * within values even without quotes.
7458 *
7459 * We have to keep multiple pointers in order to support cookie
7460 * removal at the beginning, middle or end of header without
7461 * corrupting the header. All of these headers are valid :
7462 *
7463 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7464 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7465 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7466 * | | | | | | | | |
7467 * | | | | | | | | hdr_end <--+
7468 * | | | | | | | +--> next
7469 * | | | | | | +----> val_end
7470 * | | | | | +-----------> val_beg
7471 * | | | | +--------------> equal
7472 * | | | +----------------> att_end
7473 * | | +---------------------> att_beg
7474 * | +--------------------------> prev
7475 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007476 */
7477
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007478 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7479 /* Iterate through all cookies on this line */
7480
7481 /* find att_beg */
7482 att_beg = prev + 1;
7483 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7484 att_beg++;
7485
7486 /* find att_end : this is the first character after the last non
7487 * space before the equal. It may be equal to hdr_end.
7488 */
7489 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007490
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007491 while (equal < hdr_end) {
7492 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007493 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007494 if (http_is_spht[(unsigned char)*equal++])
7495 continue;
7496 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007497 }
7498
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007499 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7500 * is between <att_beg> and <equal>, both may be identical.
7501 */
7502
7503 /* look for end of cookie if there is an equal sign */
7504 if (equal < hdr_end && *equal == '=') {
7505 /* look for the beginning of the value */
7506 val_beg = equal + 1;
7507 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7508 val_beg++;
7509
7510 /* find the end of the value, respecting quotes */
7511 next = find_cookie_value_end(val_beg, hdr_end);
7512
7513 /* make val_end point to the first white space or delimitor after the value */
7514 val_end = next;
7515 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7516 val_end--;
7517 } else {
7518 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007519 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007520
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007521 /* We have nothing to do with attributes beginning with '$'. However,
7522 * they will automatically be removed if a header before them is removed,
7523 * since they're supposed to be linked together.
7524 */
7525 if (*att_beg == '$')
7526 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007527
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007528 /* Ignore cookies with no equal sign */
7529 if (equal == next) {
7530 /* This is not our cookie, so we must preserve it. But if we already
7531 * scheduled another cookie for removal, we cannot remove the
7532 * complete header, but we can remove the previous block itself.
7533 */
7534 preserve_hdr = 1;
7535 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007536 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007537 val_end += delta;
7538 next += delta;
7539 hdr_end += delta;
7540 hdr_next += delta;
7541 cur_hdr->len += delta;
7542 http_msg_move_end(&txn->req, delta);
7543 prev = del_from;
7544 del_from = NULL;
7545 }
7546 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007547 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007548
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007549 /* if there are spaces around the equal sign, we need to
7550 * strip them otherwise we'll get trouble for cookie captures,
7551 * or even for rewrites. Since this happens extremely rarely,
7552 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007553 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007554 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7555 int stripped_before = 0;
7556 int stripped_after = 0;
7557
7558 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007559 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007560 equal += stripped_before;
7561 val_beg += stripped_before;
7562 }
7563
7564 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007565 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007566 val_beg += stripped_after;
7567 stripped_before += stripped_after;
7568 }
7569
7570 val_end += stripped_before;
7571 next += stripped_before;
7572 hdr_end += stripped_before;
7573 hdr_next += stripped_before;
7574 cur_hdr->len += stripped_before;
7575 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007576 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007577 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007578
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007579 /* First, let's see if we want to capture this cookie. We check
7580 * that we don't already have a client side cookie, because we
7581 * can only capture one. Also as an optimisation, we ignore
7582 * cookies shorter than the declared name.
7583 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007584 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7585 (val_end - att_beg >= sess->fe->capture_namelen) &&
7586 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007587 int log_len = val_end - att_beg;
7588
7589 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7590 Alert("HTTP logging : out of memory.\n");
7591 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007592 if (log_len > sess->fe->capture_len)
7593 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007594 memcpy(txn->cli_cookie, att_beg, log_len);
7595 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007596 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007597 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007598
Willy Tarreaubca99692010-10-06 19:25:55 +02007599 /* Persistence cookies in passive, rewrite or insert mode have the
7600 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007601 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007602 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007603 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007604 * For cookies in prefix mode, the form is :
7605 *
7606 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007607 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007608 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7609 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7610 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007611 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007612
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007613 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7614 * have the server ID between val_beg and delim, and the original cookie between
7615 * delim+1 and val_end. Otherwise, delim==val_end :
7616 *
7617 * Cookie: NAME=SRV; # in all but prefix modes
7618 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7619 * | || || | |+-> next
7620 * | || || | +--> val_end
7621 * | || || +---------> delim
7622 * | || |+------------> val_beg
7623 * | || +-------------> att_end = equal
7624 * | |+-----------------> att_beg
7625 * | +------------------> prev
7626 * +-------------------------> hdr_beg
7627 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007628
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007629 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007630 for (delim = val_beg; delim < val_end; delim++)
7631 if (*delim == COOKIE_DELIM)
7632 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007633 } else {
7634 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007635 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007636 /* Now check if the cookie contains a date field, which would
7637 * appear after a vertical bar ('|') just after the server name
7638 * and before the delimiter.
7639 */
7640 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7641 if (vbar1) {
7642 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007643 * right is the last seen date. It is a base64 encoded
7644 * 30-bit value representing the UNIX date since the
7645 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007646 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007647 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007648 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007649 if (val_end - vbar1 >= 5) {
7650 val = b64tos30(vbar1);
7651 if (val > 0)
7652 txn->cookie_last_date = val << 2;
7653 }
7654 /* look for a second vertical bar */
7655 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7656 if (vbar1 && (val_end - vbar1 > 5)) {
7657 val = b64tos30(vbar1 + 1);
7658 if (val > 0)
7659 txn->cookie_first_date = val << 2;
7660 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007661 }
7662 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007663
Willy Tarreauf64d1412010-10-07 20:06:11 +02007664 /* if the cookie has an expiration date and the proxy wants to check
7665 * it, then we do that now. We first check if the cookie is too old,
7666 * then only if it has expired. We detect strict overflow because the
7667 * time resolution here is not great (4 seconds). Cookies with dates
7668 * in the future are ignored if their offset is beyond one day. This
7669 * allows an admin to fix timezone issues without expiring everyone
7670 * and at the same time avoids keeping unwanted side effects for too
7671 * long.
7672 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007673 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7674 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007675 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007676 txn->flags &= ~TX_CK_MASK;
7677 txn->flags |= TX_CK_OLD;
7678 delim = val_beg; // let's pretend we have not found the cookie
7679 txn->cookie_first_date = 0;
7680 txn->cookie_last_date = 0;
7681 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007682 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7683 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007684 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007685 txn->flags &= ~TX_CK_MASK;
7686 txn->flags |= TX_CK_EXPIRED;
7687 delim = val_beg; // let's pretend we have not found the cookie
7688 txn->cookie_first_date = 0;
7689 txn->cookie_last_date = 0;
7690 }
7691
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007692 /* Here, we'll look for the first running server which supports the cookie.
7693 * This allows to share a same cookie between several servers, for example
7694 * to dedicate backup servers to specific servers only.
7695 * However, to prevent clients from sticking to cookie-less backup server
7696 * when they have incidentely learned an empty cookie, we simply ignore
7697 * empty cookies and mark them as invalid.
7698 * The same behaviour is applied when persistence must be ignored.
7699 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007700 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007701 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007702
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007703 while (srv) {
7704 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7705 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007706 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007707 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007708 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007709 /* we found the server and we can use it */
7710 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007711 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007712 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007713 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007714 break;
7715 } else {
7716 /* we found a server, but it's down,
7717 * mark it as such and go on in case
7718 * another one is available.
7719 */
7720 txn->flags &= ~TX_CK_MASK;
7721 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007722 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007723 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007724 srv = srv->next;
7725 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007726
Willy Tarreauf64d1412010-10-07 20:06:11 +02007727 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007728 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007729 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007730 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007731 txn->flags |= TX_CK_UNUSED;
7732 else
7733 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007734 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007735
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007736 /* depending on the cookie mode, we may have to either :
7737 * - delete the complete cookie if we're in insert+indirect mode, so that
7738 * the server never sees it ;
7739 * - remove the server id from the cookie value, and tag the cookie as an
7740 * application cookie so that it does not get accidentely removed later,
7741 * if we're in cookie prefix mode
7742 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007743 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007744 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007745
Willy Tarreau9b28e032012-10-12 23:49:43 +02007746 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007747 val_end += delta;
7748 next += delta;
7749 hdr_end += delta;
7750 hdr_next += delta;
7751 cur_hdr->len += delta;
7752 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007753
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007754 del_from = NULL;
7755 preserve_hdr = 1; /* we want to keep this cookie */
7756 }
7757 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007758 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 del_from = prev;
7760 }
7761 } else {
7762 /* This is not our cookie, so we must preserve it. But if we already
7763 * scheduled another cookie for removal, we cannot remove the
7764 * complete header, but we can remove the previous block itself.
7765 */
7766 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007767
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007768 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007769 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007770 if (att_beg >= del_from)
7771 att_beg += delta;
7772 if (att_end >= del_from)
7773 att_end += delta;
7774 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007775 val_end += delta;
7776 next += delta;
7777 hdr_end += delta;
7778 hdr_next += delta;
7779 cur_hdr->len += delta;
7780 http_msg_move_end(&txn->req, delta);
7781 prev = del_from;
7782 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007783 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007784 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007785
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007786 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007787 if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007788 int cmp_len, value_len;
7789 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007790
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007791 if (s->be->options2 & PR_O2_AS_PFX) {
7792 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7793 value_begin = att_beg + s->be->appsession_name_len;
7794 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007795 } else {
7796 cmp_len = att_end - att_beg;
7797 value_begin = val_beg;
7798 value_len = val_end - val_beg;
7799 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007800
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007801 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007802 if (cmp_len == s->be->appsession_name_len &&
7803 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7804 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007805 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007806 }
7807
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007808 /* continue with next cookie on this header line */
7809 att_beg = next;
7810 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007811
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007812 /* There are no more cookies on this line.
7813 * We may still have one (or several) marked for deletion at the
7814 * end of the line. We must do this now in two ways :
7815 * - if some cookies must be preserved, we only delete from the
7816 * mark to the end of line ;
7817 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007818 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007819 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007820 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007821 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007822 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007823 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007824 cur_hdr->len += delta;
7825 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007826 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007827
7828 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007829 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7830 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007831 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007832 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007833 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007834 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007835 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007836 }
7837
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007838 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007839 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007840 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007841}
7842
7843
Willy Tarreaua15645d2007-03-18 16:22:39 +01007844/* Iterate the same filter through all response headers contained in <rtr>.
7845 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7846 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007847int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007848{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007849 char *cur_ptr, *cur_end, *cur_next;
7850 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007851 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007852 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007853 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007854
7855 last_hdr = 0;
7856
Willy Tarreau9b28e032012-10-12 23:49:43 +02007857 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007858 old_idx = 0;
7859
7860 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007861 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007862 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007863 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007864 (exp->action == ACT_ALLOW ||
7865 exp->action == ACT_DENY))
7866 return 0;
7867
7868 cur_idx = txn->hdr_idx.v[old_idx].next;
7869 if (!cur_idx)
7870 break;
7871
7872 cur_hdr = &txn->hdr_idx.v[cur_idx];
7873 cur_ptr = cur_next;
7874 cur_end = cur_ptr + cur_hdr->len;
7875 cur_next = cur_end + cur_hdr->cr + 1;
7876
7877 /* Now we have one header between cur_ptr and cur_end,
7878 * and the next header starts at cur_next.
7879 */
7880
Willy Tarreau15a53a42015-01-21 13:39:42 +01007881 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007882 switch (exp->action) {
7883 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007884 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007885 last_hdr = 1;
7886 break;
7887
7888 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007889 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007890 last_hdr = 1;
7891 break;
7892
7893 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007894 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7895 if (trash.len < 0)
7896 return -1;
7897
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007898 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007899 /* FIXME: if the user adds a newline in the replacement, the
7900 * index will not be recalculated for now, and the new line
7901 * will not be counted as a new header.
7902 */
7903
7904 cur_end += delta;
7905 cur_next += delta;
7906 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007907 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007908 break;
7909
7910 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007911 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007912 cur_next += delta;
7913
Willy Tarreaufa355d42009-11-29 18:12:29 +01007914 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007915 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7916 txn->hdr_idx.used--;
7917 cur_hdr->len = 0;
7918 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007919 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007920 break;
7921
7922 }
7923 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007924
7925 /* keep the link from this header to next one in case of later
7926 * removal of next header.
7927 */
7928 old_idx = cur_idx;
7929 }
7930 return 0;
7931}
7932
7933
7934/* Apply the filter to the status line in the response buffer <rtr>.
7935 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7936 * or -1 if a replacement resulted in an invalid status line.
7937 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007938int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007939{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007940 char *cur_ptr, *cur_end;
7941 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007942 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007943 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007944
7945
Willy Tarreau3d300592007-03-18 18:34:41 +01007946 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007947 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007948 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007949 (exp->action == ACT_ALLOW ||
7950 exp->action == ACT_DENY))
7951 return 0;
7952 else if (exp->action == ACT_REMOVE)
7953 return 0;
7954
7955 done = 0;
7956
Willy Tarreau9b28e032012-10-12 23:49:43 +02007957 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007958 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007959
7960 /* Now we have the status line between cur_ptr and cur_end */
7961
Willy Tarreau15a53a42015-01-21 13:39:42 +01007962 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007963 switch (exp->action) {
7964 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007965 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007966 done = 1;
7967 break;
7968
7969 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007970 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007971 done = 1;
7972 break;
7973
7974 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007975 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7976 if (trash.len < 0)
7977 return -1;
7978
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007979 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007980 /* FIXME: if the user adds a newline in the replacement, the
7981 * index will not be recalculated for now, and the new line
7982 * will not be counted as a new header.
7983 */
7984
Willy Tarreaufa355d42009-11-29 18:12:29 +01007985 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007986 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007987 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007988 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007989 cur_ptr, cur_end + 1,
7990 NULL, NULL);
7991 if (unlikely(!cur_end))
7992 return -1;
7993
7994 /* we have a full respnse and we know that we have either a CR
7995 * or an LF at <ptr>.
7996 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007997 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007998 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007999 /* there is no point trying this regex on headers */
8000 return 1;
8001 }
8002 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008003 return done;
8004}
8005
8006
8007
8008/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008009 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8011 * unparsable response.
8012 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008013int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008014{
Willy Tarreau192252e2015-04-04 01:47:55 +02008015 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008016 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008017 struct hdr_exp *exp;
8018
8019 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008020 int ret;
8021
8022 /*
8023 * The interleaving of transformations and verdicts
8024 * makes it difficult to decide to continue or stop
8025 * the evaluation.
8026 */
8027
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008028 if (txn->flags & TX_SVDENY)
8029 break;
8030
Willy Tarreau3d300592007-03-18 18:34:41 +01008031 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008032 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8033 exp->action == ACT_PASS)) {
8034 exp = exp->next;
8035 continue;
8036 }
8037
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008038 /* if this filter had a condition, evaluate it now and skip to
8039 * next filter if the condition does not match.
8040 */
8041 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008042 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008043 ret = acl_pass(ret);
8044 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8045 ret = !ret;
8046 if (!ret)
8047 continue;
8048 }
8049
Willy Tarreaua15645d2007-03-18 16:22:39 +01008050 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008051 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008052 if (unlikely(ret < 0))
8053 return -1;
8054
8055 if (likely(ret == 0)) {
8056 /* The filter did not match the response, it can be
8057 * iterated through all headers.
8058 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008059 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8060 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008061 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008062 }
8063 return 0;
8064}
8065
8066
Willy Tarreaua15645d2007-03-18 16:22:39 +01008067/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008068 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008069 * desirable to call it only when needed. This function is also used when we
8070 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008071 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008072void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008073{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008074 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008075 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008076 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008077 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008078 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008079 char *hdr_beg, *hdr_end, *hdr_next;
8080 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008081
Willy Tarreaua15645d2007-03-18 16:22:39 +01008082 /* Iterate through the headers.
8083 * we start with the start line.
8084 */
8085 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008086 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008087
8088 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8089 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008090 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008091
8092 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008093 hdr_beg = hdr_next;
8094 hdr_end = hdr_beg + cur_hdr->len;
8095 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008096
Willy Tarreau24581ba2010-08-31 22:39:35 +02008097 /* We have one full header between hdr_beg and hdr_end, and the
8098 * next header starts at hdr_next. We're only interested in
8099 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008100 */
8101
Willy Tarreau24581ba2010-08-31 22:39:35 +02008102 is_cookie2 = 0;
8103 prev = hdr_beg + 10;
8104 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008105 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008106 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8107 if (!val) {
8108 old_idx = cur_idx;
8109 continue;
8110 }
8111 is_cookie2 = 1;
8112 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008113 }
8114
Willy Tarreau24581ba2010-08-31 22:39:35 +02008115 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8116 * <prev> points to the colon.
8117 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008118 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008119
Willy Tarreau24581ba2010-08-31 22:39:35 +02008120 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8121 * check-cache is enabled) and we are not interested in checking
8122 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008123 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008124 if (s->be->cookie_name == NULL &&
8125 s->be->appsession_name == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008126 sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008127 return;
8128
Willy Tarreau24581ba2010-08-31 22:39:35 +02008129 /* OK so now we know we have to process this response cookie.
8130 * The format of the Set-Cookie header is slightly different
8131 * from the format of the Cookie header in that it does not
8132 * support the comma as a cookie delimiter (thus the header
8133 * cannot be folded) because the Expires attribute described in
8134 * the original Netscape's spec may contain an unquoted date
8135 * with a comma inside. We have to live with this because
8136 * many browsers don't support Max-Age and some browsers don't
8137 * support quoted strings. However the Set-Cookie2 header is
8138 * clean.
8139 *
8140 * We have to keep multiple pointers in order to support cookie
8141 * removal at the beginning, middle or end of header without
8142 * corrupting the header (in case of set-cookie2). A special
8143 * pointer, <scav> points to the beginning of the set-cookie-av
8144 * fields after the first semi-colon. The <next> pointer points
8145 * either to the end of line (set-cookie) or next unquoted comma
8146 * (set-cookie2). All of these headers are valid :
8147 *
8148 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8149 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8150 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8151 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8152 * | | | | | | | | | |
8153 * | | | | | | | | +-> next hdr_end <--+
8154 * | | | | | | | +------------> scav
8155 * | | | | | | +--------------> val_end
8156 * | | | | | +--------------------> val_beg
8157 * | | | | +----------------------> equal
8158 * | | | +------------------------> att_end
8159 * | | +----------------------------> att_beg
8160 * | +------------------------------> prev
8161 * +-----------------------------------------> hdr_beg
8162 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008163
Willy Tarreau24581ba2010-08-31 22:39:35 +02008164 for (; prev < hdr_end; prev = next) {
8165 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008166
Willy Tarreau24581ba2010-08-31 22:39:35 +02008167 /* find att_beg */
8168 att_beg = prev + 1;
8169 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8170 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008171
Willy Tarreau24581ba2010-08-31 22:39:35 +02008172 /* find att_end : this is the first character after the last non
8173 * space before the equal. It may be equal to hdr_end.
8174 */
8175 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008176
Willy Tarreau24581ba2010-08-31 22:39:35 +02008177 while (equal < hdr_end) {
8178 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8179 break;
8180 if (http_is_spht[(unsigned char)*equal++])
8181 continue;
8182 att_end = equal;
8183 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008184
Willy Tarreau24581ba2010-08-31 22:39:35 +02008185 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8186 * is between <att_beg> and <equal>, both may be identical.
8187 */
8188
8189 /* look for end of cookie if there is an equal sign */
8190 if (equal < hdr_end && *equal == '=') {
8191 /* look for the beginning of the value */
8192 val_beg = equal + 1;
8193 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8194 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008195
Willy Tarreau24581ba2010-08-31 22:39:35 +02008196 /* find the end of the value, respecting quotes */
8197 next = find_cookie_value_end(val_beg, hdr_end);
8198
8199 /* make val_end point to the first white space or delimitor after the value */
8200 val_end = next;
8201 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8202 val_end--;
8203 } else {
8204 /* <equal> points to next comma, semi-colon or EOL */
8205 val_beg = val_end = next = equal;
8206 }
8207
8208 if (next < hdr_end) {
8209 /* Set-Cookie2 supports multiple cookies, and <next> points to
8210 * a colon or semi-colon before the end. So skip all attr-value
8211 * pairs and look for the next comma. For Set-Cookie, since
8212 * commas are permitted in values, skip to the end.
8213 */
8214 if (is_cookie2)
8215 next = find_hdr_value_end(next, hdr_end);
8216 else
8217 next = hdr_end;
8218 }
8219
8220 /* Now everything is as on the diagram above */
8221
8222 /* Ignore cookies with no equal sign */
8223 if (equal == val_end)
8224 continue;
8225
8226 /* If there are spaces around the equal sign, we need to
8227 * strip them otherwise we'll get trouble for cookie captures,
8228 * or even for rewrites. Since this happens extremely rarely,
8229 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008230 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008231 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8232 int stripped_before = 0;
8233 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008234
Willy Tarreau24581ba2010-08-31 22:39:35 +02008235 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008236 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008237 equal += stripped_before;
8238 val_beg += stripped_before;
8239 }
8240
8241 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008242 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008243 val_beg += stripped_after;
8244 stripped_before += stripped_after;
8245 }
8246
8247 val_end += stripped_before;
8248 next += stripped_before;
8249 hdr_end += stripped_before;
8250 hdr_next += stripped_before;
8251 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008252 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008253 }
8254
8255 /* First, let's see if we want to capture this cookie. We check
8256 * that we don't already have a server side cookie, because we
8257 * can only capture one. Also as an optimisation, we ignore
8258 * cookies shorter than the declared name.
8259 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008260 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008261 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008262 (val_end - att_beg >= sess->fe->capture_namelen) &&
8263 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008264 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008265 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008266 Alert("HTTP logging : out of memory.\n");
8267 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008268 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008269 if (log_len > sess->fe->capture_len)
8270 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008271 memcpy(txn->srv_cookie, att_beg, log_len);
8272 txn->srv_cookie[log_len] = 0;
8273 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008274 }
8275
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008276 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008277 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008278 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008279 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8280 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008281 /* assume passive cookie by default */
8282 txn->flags &= ~TX_SCK_MASK;
8283 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008284
8285 /* If the cookie is in insert mode on a known server, we'll delete
8286 * this occurrence because we'll insert another one later.
8287 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008288 * a direct access.
8289 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008290 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008291 /* The "preserve" flag was set, we don't want to touch the
8292 * server's cookie.
8293 */
8294 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008295 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008296 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008297 /* this cookie must be deleted */
8298 if (*prev == ':' && next == hdr_end) {
8299 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008300 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008301 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8302 txn->hdr_idx.used--;
8303 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008304 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008305 hdr_next += delta;
8306 http_msg_move_end(&txn->rsp, delta);
8307 /* note: while both invalid now, <next> and <hdr_end>
8308 * are still equal, so the for() will stop as expected.
8309 */
8310 } else {
8311 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008312 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008313 next = prev;
8314 hdr_end += delta;
8315 hdr_next += delta;
8316 cur_hdr->len += delta;
8317 http_msg_move_end(&txn->rsp, delta);
8318 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008319 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008320 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008321 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008322 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008323 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008324 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008325 * with this server since we know it.
8326 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008327 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008328 next += delta;
8329 hdr_end += delta;
8330 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008331 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008332 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008333
Willy Tarreauf1348312010-10-07 15:54:11 +02008334 txn->flags &= ~TX_SCK_MASK;
8335 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008336 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008337 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008338 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008339 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008340 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008341 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008342 next += delta;
8343 hdr_end += delta;
8344 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008345 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008346 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008347
Willy Tarreau827aee92011-03-10 16:55:02 +01008348 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008349 txn->flags &= ~TX_SCK_MASK;
8350 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008351 }
8352 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008353 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008354 else if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008355 int cmp_len, value_len;
8356 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008357
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008358 if (s->be->options2 & PR_O2_AS_PFX) {
8359 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8360 value_begin = att_beg + s->be->appsession_name_len;
8361 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008362 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008363 cmp_len = att_end - att_beg;
8364 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008365 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008366 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008367
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008368 if ((cmp_len == s->be->appsession_name_len) &&
8369 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008370 /* free a possibly previously allocated memory */
8371 pool_free2(apools.sessid, txn->sessid);
8372
Willy Tarreau87b09662015-04-03 00:22:06 +02008373 /* Store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008374 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008375 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008376 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008377 return;
8378 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008379 memcpy(txn->sessid, value_begin, value_len);
8380 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008381 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008382 }
8383 /* that's done for this cookie, check the next one on the same
8384 * line when next != hdr_end (only if is_cookie2).
8385 */
8386 }
8387 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008388 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008389 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008390
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008391 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008392 appsess *asession = NULL;
8393 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008394 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008395 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008396 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008397 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8398 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008399 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008400 return;
8401 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008402 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8403
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008404 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8405 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008406 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8407 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008408 return;
8409 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008410 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8411 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008412
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008413 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008414 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008415 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008416 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8417 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008418 return;
8419 }
8420 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008421 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008422
8423 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008424 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008425 }
8426
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008427 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008428 asession->request_count++;
8429 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008430}
8431
8432
Willy Tarreaua15645d2007-03-18 16:22:39 +01008433/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008434 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008435 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008436void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008437{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008438 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008439 char *p1, *p2;
8440
8441 char *cur_ptr, *cur_end, *cur_next;
8442 int cur_idx;
8443
Willy Tarreau5df51872007-11-25 16:20:08 +01008444 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008445 return;
8446
8447 /* Iterate through the headers.
8448 * we start with the start line.
8449 */
8450 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008451 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008452
8453 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8454 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008455 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008456
8457 cur_hdr = &txn->hdr_idx.v[cur_idx];
8458 cur_ptr = cur_next;
8459 cur_end = cur_ptr + cur_hdr->len;
8460 cur_next = cur_end + cur_hdr->cr + 1;
8461
8462 /* We have one full header between cur_ptr and cur_end, and the
8463 * next header starts at cur_next. We're only interested in
8464 * "Cookie:" headers.
8465 */
8466
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008467 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8468 if (val) {
8469 if ((cur_end - (cur_ptr + val) >= 8) &&
8470 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8471 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8472 return;
8473 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008474 }
8475
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008476 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8477 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008478 continue;
8479
8480 /* OK, right now we know we have a cache-control header at cur_ptr */
8481
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008482 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008483
8484 if (p1 >= cur_end) /* no more info */
8485 continue;
8486
8487 /* p1 is at the beginning of the value */
8488 p2 = p1;
8489
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008490 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008491 p2++;
8492
8493 /* we have a complete value between p1 and p2 */
8494 if (p2 < cur_end && *p2 == '=') {
8495 /* we have something of the form no-cache="set-cookie" */
8496 if ((cur_end - p1 >= 21) &&
8497 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8498 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008499 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008500 continue;
8501 }
8502
8503 /* OK, so we know that either p2 points to the end of string or to a comma */
8504 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008505 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008506 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8507 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8508 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008509 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008510 return;
8511 }
8512
8513 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008514 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008515 continue;
8516 }
8517 }
8518}
8519
8520
Willy Tarreau58f10d72006-12-04 02:26:12 +01008521/*
8522 * Try to retrieve a known appsession in the URI, then the associated server.
Willy Tarreau87b09662015-04-03 00:22:06 +02008523 * If the server is found, it's assigned to the stream.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008524 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008525void get_srv_from_appsession(struct stream *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008526{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008527 char *end_params, *first_param, *cur_param, *next_param;
8528 char separator;
8529 int value_len;
8530
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008531 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008532
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008533 if (s->be->appsession_name == NULL ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02008534 (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 +01008535 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008536 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008537
Cyril Bontéb21570a2009-11-29 20:04:48 +01008538 first_param = NULL;
8539 switch (mode) {
8540 case PR_O2_AS_M_PP:
8541 first_param = memchr(begin, ';', len);
8542 break;
8543 case PR_O2_AS_M_QS:
8544 first_param = memchr(begin, '?', len);
8545 break;
8546 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008547
Cyril Bontéb21570a2009-11-29 20:04:48 +01008548 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008549 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008550 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008551
Cyril Bontéb21570a2009-11-29 20:04:48 +01008552 switch (mode) {
8553 case PR_O2_AS_M_PP:
8554 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8555 end_params = (char *) begin + len;
8556 }
8557 separator = ';';
8558 break;
8559 case PR_O2_AS_M_QS:
8560 end_params = (char *) begin + len;
8561 separator = '&';
8562 break;
8563 default:
8564 /* unknown mode, shouldn't happen */
8565 return;
8566 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008567
Cyril Bontéb21570a2009-11-29 20:04:48 +01008568 cur_param = next_param = end_params;
8569 while (cur_param > first_param) {
8570 cur_param--;
8571 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8572 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008573 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8574 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8575 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008576 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008577 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8578 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008579 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008580 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008581 }
8582 break;
8583 }
8584 next_param = cur_param;
8585 }
8586 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008587#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008588 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008589 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008590#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008591}
8592
Willy Tarreaub2513902006-12-17 14:52:38 +01008593/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008594 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008595 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008596 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008597 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008598 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008599 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008600 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008601 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008602int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008603{
8604 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008605 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008606 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008607
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008608 if (!uri_auth)
8609 return 0;
8610
Cyril Bonté70be45d2010-10-12 00:14:35 +02008611 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008612 return 0;
8613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008614 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008615 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008616 return 0;
8617
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008618 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008619 return 0;
8620
Willy Tarreaub2513902006-12-17 14:52:38 +01008621 return 1;
8622}
8623
Willy Tarreau4076a152009-04-02 15:18:36 +02008624/*
8625 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008626 * By default it tries to report the error position as msg->err_pos. However if
8627 * this one is not set, it will then report msg->next, which is the last known
8628 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008629 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008630 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008631void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008632 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008633 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008634{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008635 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008636 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008637 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008638
Willy Tarreau9b28e032012-10-12 23:49:43 +02008639 es->len = MIN(chn->buf->i, sizeof(es->buf));
8640 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008641 len1 = MIN(len1, es->len);
8642 len2 = es->len - len1; /* remaining data if buffer wraps */
8643
Willy Tarreau9b28e032012-10-12 23:49:43 +02008644 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008645 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008646 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008647
Willy Tarreau4076a152009-04-02 15:18:36 +02008648 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008649 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008650 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008651 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008652
Willy Tarreau4076a152009-04-02 15:18:36 +02008653 es->when = date; // user-visible date
8654 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008655 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008656 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008657 if (objt_conn(sess->origin))
8658 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008659 else
8660 memset(&es->src, 0, sizeof(es->src));
8661
Willy Tarreau078272e2010-12-12 12:46:33 +01008662 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008663 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008664 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008665 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008666 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008667 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008668 es->b_out = chn->buf->o;
8669 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008670 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008671 es->m_clen = msg->chunk_len;
8672 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008673}
Willy Tarreaub2513902006-12-17 14:52:38 +01008674
Willy Tarreau294c4732011-12-16 21:35:50 +01008675/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8676 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8677 * performed over the whole headers. Otherwise it must contain a valid header
8678 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8679 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8680 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8681 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008682 * -1. The value fetch stops at commas, so this function is suited for use with
8683 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008684 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008685 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008686unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008687 struct hdr_idx *idx, int occ,
8688 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008689{
Willy Tarreau294c4732011-12-16 21:35:50 +01008690 struct hdr_ctx local_ctx;
8691 char *ptr_hist[MAX_HDR_HISTORY];
8692 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008693 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008694 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008695
Willy Tarreau294c4732011-12-16 21:35:50 +01008696 if (!ctx) {
8697 local_ctx.idx = 0;
8698 ctx = &local_ctx;
8699 }
8700
Willy Tarreaubce70882009-09-07 11:51:47 +02008701 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008702 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008703 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008704 occ--;
8705 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008706 *vptr = ctx->line + ctx->val;
8707 *vlen = ctx->vlen;
8708 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008709 }
8710 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008711 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008712 }
8713
8714 /* negative occurrence, we scan all the list then walk back */
8715 if (-occ > MAX_HDR_HISTORY)
8716 return 0;
8717
Willy Tarreau294c4732011-12-16 21:35:50 +01008718 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008719 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008720 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8721 len_hist[hist_ptr] = ctx->vlen;
8722 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008723 hist_ptr = 0;
8724 found++;
8725 }
8726 if (-occ > found)
8727 return 0;
8728 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008729 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8730 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8731 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008732 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008733 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008734 if (hist_ptr >= MAX_HDR_HISTORY)
8735 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008736 *vptr = ptr_hist[hist_ptr];
8737 *vlen = len_hist[hist_ptr];
8738 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008739}
8740
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008741/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8742 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8743 * performed over the whole headers. Otherwise it must contain a valid header
8744 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8745 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8746 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8747 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8748 * -1. This function differs from http_get_hdr() in that it only returns full
8749 * line header values and does not stop at commas.
8750 * The return value is 0 if nothing was found, or non-zero otherwise.
8751 */
8752unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8753 struct hdr_idx *idx, int occ,
8754 struct hdr_ctx *ctx, char **vptr, int *vlen)
8755{
8756 struct hdr_ctx local_ctx;
8757 char *ptr_hist[MAX_HDR_HISTORY];
8758 int len_hist[MAX_HDR_HISTORY];
8759 unsigned int hist_ptr;
8760 int found;
8761
8762 if (!ctx) {
8763 local_ctx.idx = 0;
8764 ctx = &local_ctx;
8765 }
8766
8767 if (occ >= 0) {
8768 /* search from the beginning */
8769 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8770 occ--;
8771 if (occ <= 0) {
8772 *vptr = ctx->line + ctx->val;
8773 *vlen = ctx->vlen;
8774 return 1;
8775 }
8776 }
8777 return 0;
8778 }
8779
8780 /* negative occurrence, we scan all the list then walk back */
8781 if (-occ > MAX_HDR_HISTORY)
8782 return 0;
8783
8784 found = hist_ptr = 0;
8785 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8786 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8787 len_hist[hist_ptr] = ctx->vlen;
8788 if (++hist_ptr >= MAX_HDR_HISTORY)
8789 hist_ptr = 0;
8790 found++;
8791 }
8792 if (-occ > found)
8793 return 0;
8794 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8795 * find occurrence -occ, so we have to check [hist_ptr+occ].
8796 */
8797 hist_ptr += occ;
8798 if (hist_ptr >= MAX_HDR_HISTORY)
8799 hist_ptr -= MAX_HDR_HISTORY;
8800 *vptr = ptr_hist[hist_ptr];
8801 *vlen = len_hist[hist_ptr];
8802 return 1;
8803}
8804
Willy Tarreaubaaee002006-06-26 02:48:02 +02008805/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008806 * Print a debug line with a header. Always stop at the first CR or LF char,
8807 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8808 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008809 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008810void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008811{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008812 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008813 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008814
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008815 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008816 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008817 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008818 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 +02008819
8820 for (max = 0; start + max < end; max++)
8821 if (start[max] == '\r' || start[max] == '\n')
8822 break;
8823
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008824 UBOUND(max, trash.size - trash.len - 3);
8825 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8826 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008827 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008828}
8829
Willy Tarreaueee5b512015-04-03 23:46:31 +02008830
8831/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8832 * The hdr_idx is allocated as well. In case of allocation failure, everything
8833 * allocated is freed and NULL is returned. Otherwise the new transaction is
8834 * assigned to the stream and returned.
8835 */
8836struct http_txn *http_alloc_txn(struct stream *s)
8837{
8838 struct http_txn *txn = s->txn;
8839
8840 if (txn)
8841 return txn;
8842
8843 txn = pool_alloc2(pool2_http_txn);
8844 if (!txn)
8845 return txn;
8846
8847 txn->hdr_idx.size = global.tune.max_http_hdr;
8848 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8849 if (!txn->hdr_idx.v) {
8850 pool_free2(pool2_http_txn, txn);
8851 return NULL;
8852 }
8853
8854 s->txn = txn;
8855 return txn;
8856}
8857
Willy Tarreau0937bc42009-12-22 15:03:09 +01008858/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008859 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008860 * the required fields are properly allocated and that we only need to (re)init
8861 * them. This should be used before processing any new request.
8862 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008863void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008864{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008865 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008866 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008867
8868 txn->flags = 0;
8869 txn->status = -1;
8870
Willy Tarreauf64d1412010-10-07 20:06:11 +02008871 txn->cookie_first_date = 0;
8872 txn->cookie_last_date = 0;
8873
Willy Tarreaueee5b512015-04-03 23:46:31 +02008874 txn->sessid = NULL;
8875 txn->srv_cookie = NULL;
8876 txn->cli_cookie = NULL;
8877 txn->uri = NULL;
8878
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008879 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008880 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008881 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008882 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008883 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008884 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008885 txn->req.chunk_len = 0LL;
8886 txn->req.body_len = 0LL;
8887 txn->rsp.chunk_len = 0LL;
8888 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008889 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8890 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008891 txn->req.chn = &s->req;
8892 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008893
8894 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008895
8896 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8897 if (fe->options2 & PR_O2_REQBUG_OK)
8898 txn->req.err_pos = -1; /* let buggy requests pass */
8899
Willy Tarreau0937bc42009-12-22 15:03:09 +01008900 if (txn->hdr_idx.v)
8901 hdr_idx_init(&txn->hdr_idx);
8902}
8903
8904/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008905void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008906{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008907 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008908 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008909
Willy Tarreau75195602014-03-11 15:48:55 +01008910 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008911 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008912 s->comp_algo->end(&s->comp_ctx);
8913 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008914 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008915
Willy Tarreau0937bc42009-12-22 15:03:09 +01008916 /* these ones will have been dynamically allocated */
8917 pool_free2(pool2_requri, txn->uri);
8918 pool_free2(pool2_capture, txn->cli_cookie);
8919 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008920 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008921 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008922
William Lallemanda73203e2012-03-12 12:48:57 +01008923 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008924 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008925 txn->uri = NULL;
8926 txn->srv_cookie = NULL;
8927 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008928
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008929 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008930 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008931 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008932 pool_free2(h->pool, s->req_cap[h->index]);
8933 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008934 }
8935
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008936 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008937 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008938 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008939 pool_free2(h->pool, s->res_cap[h->index]);
8940 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008941 }
8942
Willy Tarreau0937bc42009-12-22 15:03:09 +01008943}
8944
8945/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008946void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008947{
8948 http_end_txn(s);
8949 http_init_txn(s);
8950
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008951 /* reinitialise the current rule list pointer to NULL. We are sure that
8952 * any rulelist match the NULL pointer.
8953 */
8954 s->current_rule_list = NULL;
8955
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008956 s->be = strm_fe(s);
8957 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008958 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008959 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008960 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008961 /* re-init store persistence */
8962 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008963 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008964
Willy Tarreau0937bc42009-12-22 15:03:09 +01008965 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008966
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008967 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008968
Willy Tarreau739cfba2010-01-25 23:11:14 +01008969 /* We must trim any excess data from the response buffer, because we
8970 * may have blocked an invalid response from a server that we don't
8971 * want to accidentely forward once we disable the analysers, nor do
8972 * we want those data to come along with next response. A typical
8973 * example of such data would be from a buggy server responding to
8974 * a HEAD with some data, or sending more than the advertised
8975 * content-length.
8976 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008977 if (unlikely(s->res.buf->i))
8978 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008979
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008980 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008981 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008982
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008983 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008984 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008985
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008986 s->req.rex = TICK_ETERNITY;
8987 s->req.wex = TICK_ETERNITY;
8988 s->req.analyse_exp = TICK_ETERNITY;
8989 s->res.rex = TICK_ETERNITY;
8990 s->res.wex = TICK_ETERNITY;
8991 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008992}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008993
Sasha Pachev218f0642014-06-16 12:05:59 -06008994void free_http_res_rules(struct list *r)
8995{
8996 struct http_res_rule *tr, *pr;
8997
8998 list_for_each_entry_safe(pr, tr, r, list) {
8999 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009000 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009001 free(pr);
9002 }
9003}
9004
9005void free_http_req_rules(struct list *r)
9006{
Willy Tarreauff011f22011-01-06 17:51:27 +01009007 struct http_req_rule *tr, *pr;
9008
9009 list_for_each_entry_safe(pr, tr, r, list) {
9010 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01009011 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009012 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009013
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009014 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009015 free(pr);
9016 }
9017}
9018
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009019/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01009020struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9021{
9022 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009023 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009024 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009025 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009026
9027 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
9028 if (!rule) {
9029 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009030 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009031 }
9032
CJ Ess108b1dd2015-04-07 12:03:37 -04009033 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009034 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01009035 rule->action = HTTP_REQ_ACT_ALLOW;
9036 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009037 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009038 int code;
9039 int hc;
9040
Willy Tarreauff011f22011-01-06 17:51:27 +01009041 rule->action = HTTP_REQ_ACT_DENY;
9042 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009043 if (strcmp(args[cur_arg], "deny_status") == 0) {
9044 cur_arg++;
9045 if (!args[cur_arg]) {
9046 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9047 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9048 goto out_err;
9049 }
9050
9051 code = atol(args[cur_arg]);
9052 cur_arg++;
9053 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9054 if (http_err_codes[hc] == code) {
9055 rule->deny_status = hc;
9056 break;
9057 }
9058 }
9059
9060 if (hc >= HTTP_ERR_SIZE) {
9061 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9062 file, linenum, code);
9063 }
9064 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009065 } else if (!strcmp(args[0], "tarpit")) {
9066 rule->action = HTTP_REQ_ACT_TARPIT;
9067 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009068 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01009069 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009070 cur_arg = 1;
9071
9072 while(*args[cur_arg]) {
9073 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009074 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009075 cur_arg+=2;
9076 continue;
9077 } else
9078 break;
9079 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009080 } else if (!strcmp(args[0], "set-nice")) {
9081 rule->action = HTTP_REQ_ACT_SET_NICE;
9082 cur_arg = 1;
9083
9084 if (!*args[cur_arg] ||
9085 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9086 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9087 file, linenum, args[0]);
9088 goto out_err;
9089 }
9090 rule->arg.nice = atoi(args[cur_arg]);
9091 if (rule->arg.nice < -1024)
9092 rule->arg.nice = -1024;
9093 else if (rule->arg.nice > 1024)
9094 rule->arg.nice = 1024;
9095 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009096 } else if (!strcmp(args[0], "set-tos")) {
9097#ifdef IP_TOS
9098 char *err;
9099 rule->action = HTTP_REQ_ACT_SET_TOS;
9100 cur_arg = 1;
9101
9102 if (!*args[cur_arg] ||
9103 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9104 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9105 file, linenum, args[0]);
9106 goto out_err;
9107 }
9108
9109 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9110 if (err && *err != '\0') {
9111 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9112 file, linenum, err, args[0]);
9113 goto out_err;
9114 }
9115 cur_arg++;
9116#else
9117 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9118 goto out_err;
9119#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009120 } else if (!strcmp(args[0], "set-mark")) {
9121#ifdef SO_MARK
9122 char *err;
9123 rule->action = HTTP_REQ_ACT_SET_MARK;
9124 cur_arg = 1;
9125
9126 if (!*args[cur_arg] ||
9127 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9128 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9129 file, linenum, args[0]);
9130 goto out_err;
9131 }
9132
9133 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9134 if (err && *err != '\0') {
9135 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9136 file, linenum, err, args[0]);
9137 goto out_err;
9138 }
9139 cur_arg++;
9140 global.last_checks |= LSTCHK_NETADM;
9141#else
9142 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9143 goto out_err;
9144#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009145 } else if (!strcmp(args[0], "set-log-level")) {
9146 rule->action = HTTP_REQ_ACT_SET_LOGL;
9147 cur_arg = 1;
9148
9149 if (!*args[cur_arg] ||
9150 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9151 bad_log_level:
9152 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9153 file, linenum, args[0]);
9154 goto out_err;
9155 }
9156 if (strcmp(args[cur_arg], "silent") == 0)
9157 rule->arg.loglevel = -1;
9158 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9159 goto bad_log_level;
9160 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009161 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9162 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9163 cur_arg = 1;
9164
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009165 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9166 (*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 +01009167 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9168 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009169 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009170 }
9171
9172 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9173 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9174 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009175
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009176 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009177 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009178 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9179 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009180 free(proxy->conf.lfs_file);
9181 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9182 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009183 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009184 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9185 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009186 cur_arg = 1;
9187
9188 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009189 (*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 -06009190 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9191 file, linenum, args[0]);
9192 goto out_err;
9193 }
9194
9195 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9196 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9197 LIST_INIT(&rule->arg.hdr_add.fmt);
9198
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009199 error = NULL;
9200 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9201 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9202 args[cur_arg + 1], error);
9203 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009204 goto out_err;
9205 }
9206
9207 proxy->conf.args.ctx = ARGC_HRQ;
9208 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9209 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9210 file, linenum);
9211
9212 free(proxy->conf.lfs_file);
9213 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9214 proxy->conf.lfs_line = proxy->conf.args.line;
9215 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009216 } else if (strcmp(args[0], "del-header") == 0) {
9217 rule->action = HTTP_REQ_ACT_DEL_HDR;
9218 cur_arg = 1;
9219
9220 if (!*args[cur_arg] ||
9221 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9222 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9223 file, linenum, args[0]);
9224 goto out_err;
9225 }
9226
9227 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9228 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9229
9230 proxy->conf.args.ctx = ARGC_HRQ;
9231 free(proxy->conf.lfs_file);
9232 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9233 proxy->conf.lfs_line = proxy->conf.args.line;
9234 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009235 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9236 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009237 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009238 struct sample_expr *expr;
9239 unsigned int where;
9240 char *err = NULL;
9241
9242 cur_arg = 1;
9243 proxy->conf.args.ctx = ARGC_TRK;
9244
9245 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9246 if (!expr) {
9247 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9248 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9249 free(err);
9250 goto out_err;
9251 }
9252
9253 where = 0;
9254 if (proxy->cap & PR_CAP_FE)
9255 where |= SMP_VAL_FE_HRQ_HDR;
9256 if (proxy->cap & PR_CAP_BE)
9257 where |= SMP_VAL_BE_HRQ_HDR;
9258
9259 if (!(expr->fetch->val & where)) {
9260 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9261 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9262 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9263 args[cur_arg-1], sample_src_names(expr->fetch->use));
9264 free(expr);
9265 goto out_err;
9266 }
9267
9268 if (strcmp(args[cur_arg], "table") == 0) {
9269 cur_arg++;
9270 if (!args[cur_arg]) {
9271 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9272 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9273 free(expr);
9274 goto out_err;
9275 }
9276 /* we copy the table name for now, it will be resolved later */
9277 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9278 cur_arg++;
9279 }
9280 rule->act_prm.trk_ctr.expr = expr;
9281 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009282 } else if (strcmp(args[0], "redirect") == 0) {
9283 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009284 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009285
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009286 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009287 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9288 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9289 goto out_err;
9290 }
9291
9292 /* this redirect rule might already contain a parsed condition which
9293 * we'll pass to the http-request rule.
9294 */
9295 rule->action = HTTP_REQ_ACT_REDIR;
9296 rule->arg.redir = redir;
9297 rule->cond = redir->cond;
9298 redir->cond = NULL;
9299 cur_arg = 2;
9300 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009301 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9302 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9303 rule->action = HTTP_REQ_ACT_ADD_ACL;
9304 /*
9305 * '+ 8' for 'add-acl('
9306 * '- 9' for 'add-acl(' + trailing ')'
9307 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009308 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009309
9310 cur_arg = 1;
9311
9312 if (!*args[cur_arg] ||
9313 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9314 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9315 file, linenum, args[0]);
9316 goto out_err;
9317 }
9318
9319 LIST_INIT(&rule->arg.map.key);
9320 proxy->conf.args.ctx = ARGC_HRQ;
9321 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9322 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9323 file, linenum);
9324 free(proxy->conf.lfs_file);
9325 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9326 proxy->conf.lfs_line = proxy->conf.args.line;
9327 cur_arg += 1;
9328 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9329 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9330 rule->action = HTTP_REQ_ACT_DEL_ACL;
9331 /*
9332 * '+ 8' for 'del-acl('
9333 * '- 9' for 'del-acl(' + trailing ')'
9334 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009335 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009336
9337 cur_arg = 1;
9338
9339 if (!*args[cur_arg] ||
9340 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9341 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9342 file, linenum, args[0]);
9343 goto out_err;
9344 }
9345
9346 LIST_INIT(&rule->arg.map.key);
9347 proxy->conf.args.ctx = ARGC_HRQ;
9348 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9349 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9350 file, linenum);
9351 free(proxy->conf.lfs_file);
9352 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9353 proxy->conf.lfs_line = proxy->conf.args.line;
9354 cur_arg += 1;
9355 } else if (strncmp(args[0], "del-map", 7) == 0) {
9356 /* http-request del-map(<reference (map name)>) <key pattern> */
9357 rule->action = HTTP_REQ_ACT_DEL_MAP;
9358 /*
9359 * '+ 8' for 'del-map('
9360 * '- 9' for 'del-map(' + trailing ')'
9361 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009362 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009363
9364 cur_arg = 1;
9365
9366 if (!*args[cur_arg] ||
9367 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9368 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9369 file, linenum, args[0]);
9370 goto out_err;
9371 }
9372
9373 LIST_INIT(&rule->arg.map.key);
9374 proxy->conf.args.ctx = ARGC_HRQ;
9375 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9376 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9377 file, linenum);
9378 free(proxy->conf.lfs_file);
9379 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9380 proxy->conf.lfs_line = proxy->conf.args.line;
9381 cur_arg += 1;
9382 } else if (strncmp(args[0], "set-map", 7) == 0) {
9383 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9384 rule->action = HTTP_REQ_ACT_SET_MAP;
9385 /*
9386 * '+ 8' for 'set-map('
9387 * '- 9' for 'set-map(' + trailing ')'
9388 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009389 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009390
9391 cur_arg = 1;
9392
9393 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9394 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9395 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9396 file, linenum, args[0]);
9397 goto out_err;
9398 }
9399
9400 LIST_INIT(&rule->arg.map.key);
9401 LIST_INIT(&rule->arg.map.value);
9402 proxy->conf.args.ctx = ARGC_HRQ;
9403
9404 /* key pattern */
9405 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9406 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9407 file, linenum);
9408
9409 /* value pattern */
9410 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9411 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9412 file, linenum);
9413 free(proxy->conf.lfs_file);
9414 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9415 proxy->conf.lfs_line = proxy->conf.args.line;
9416
9417 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009418 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9419 char *errmsg = NULL;
9420 cur_arg = 1;
9421 /* try in the module list */
9422 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9423 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9424 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9425 free(errmsg);
9426 goto out_err;
9427 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009428 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009429 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 +01009430 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009431 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009432 }
9433
9434 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9435 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009436 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009437
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009438 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9439 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9440 file, linenum, args[0], errmsg);
9441 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009442 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009443 }
9444 rule->cond = cond;
9445 }
9446 else if (*args[cur_arg]) {
9447 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9448 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9449 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009450 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009451 }
9452
9453 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009454 out_err:
9455 free(rule);
9456 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009457}
9458
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009459/* parse an "http-respose" rule */
9460struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9461{
9462 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009463 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009464 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009465 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009466
9467 rule = calloc(1, sizeof(*rule));
9468 if (!rule) {
9469 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9470 goto out_err;
9471 }
9472
9473 if (!strcmp(args[0], "allow")) {
9474 rule->action = HTTP_RES_ACT_ALLOW;
9475 cur_arg = 1;
9476 } else if (!strcmp(args[0], "deny")) {
9477 rule->action = HTTP_RES_ACT_DENY;
9478 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009479 } else if (!strcmp(args[0], "set-nice")) {
9480 rule->action = HTTP_RES_ACT_SET_NICE;
9481 cur_arg = 1;
9482
9483 if (!*args[cur_arg] ||
9484 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9485 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9486 file, linenum, args[0]);
9487 goto out_err;
9488 }
9489 rule->arg.nice = atoi(args[cur_arg]);
9490 if (rule->arg.nice < -1024)
9491 rule->arg.nice = -1024;
9492 else if (rule->arg.nice > 1024)
9493 rule->arg.nice = 1024;
9494 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009495 } else if (!strcmp(args[0], "set-tos")) {
9496#ifdef IP_TOS
9497 char *err;
9498 rule->action = HTTP_RES_ACT_SET_TOS;
9499 cur_arg = 1;
9500
9501 if (!*args[cur_arg] ||
9502 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9503 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9504 file, linenum, args[0]);
9505 goto out_err;
9506 }
9507
9508 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9509 if (err && *err != '\0') {
9510 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9511 file, linenum, err, args[0]);
9512 goto out_err;
9513 }
9514 cur_arg++;
9515#else
9516 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9517 goto out_err;
9518#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009519 } else if (!strcmp(args[0], "set-mark")) {
9520#ifdef SO_MARK
9521 char *err;
9522 rule->action = HTTP_RES_ACT_SET_MARK;
9523 cur_arg = 1;
9524
9525 if (!*args[cur_arg] ||
9526 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9527 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9528 file, linenum, args[0]);
9529 goto out_err;
9530 }
9531
9532 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9533 if (err && *err != '\0') {
9534 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9535 file, linenum, err, args[0]);
9536 goto out_err;
9537 }
9538 cur_arg++;
9539 global.last_checks |= LSTCHK_NETADM;
9540#else
9541 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9542 goto out_err;
9543#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009544 } else if (!strcmp(args[0], "set-log-level")) {
9545 rule->action = HTTP_RES_ACT_SET_LOGL;
9546 cur_arg = 1;
9547
9548 if (!*args[cur_arg] ||
9549 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9550 bad_log_level:
9551 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9552 file, linenum, args[0]);
9553 goto out_err;
9554 }
9555 if (strcmp(args[cur_arg], "silent") == 0)
9556 rule->arg.loglevel = -1;
9557 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9558 goto bad_log_level;
9559 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009560 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9561 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9562 cur_arg = 1;
9563
9564 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9565 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9566 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9567 file, linenum, args[0]);
9568 goto out_err;
9569 }
9570
9571 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9572 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9573 LIST_INIT(&rule->arg.hdr_add.fmt);
9574
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009575 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009576 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009577 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9578 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009579 free(proxy->conf.lfs_file);
9580 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9581 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009582 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009583 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009584 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009585 cur_arg = 1;
9586
9587 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009588 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9589 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009590 file, linenum, args[0]);
9591 goto out_err;
9592 }
9593
9594 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9595 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9596 LIST_INIT(&rule->arg.hdr_add.fmt);
9597
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009598 error = NULL;
9599 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9600 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9601 args[cur_arg + 1], error);
9602 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009603 goto out_err;
9604 }
9605
9606 proxy->conf.args.ctx = ARGC_HRQ;
9607 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9608 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9609 file, linenum);
9610
9611 free(proxy->conf.lfs_file);
9612 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9613 proxy->conf.lfs_line = proxy->conf.args.line;
9614 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009615 } else if (strcmp(args[0], "del-header") == 0) {
9616 rule->action = HTTP_RES_ACT_DEL_HDR;
9617 cur_arg = 1;
9618
9619 if (!*args[cur_arg] ||
9620 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9621 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9622 file, linenum, args[0]);
9623 goto out_err;
9624 }
9625
9626 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9627 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9628
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009629 proxy->conf.args.ctx = ARGC_HRS;
9630 free(proxy->conf.lfs_file);
9631 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9632 proxy->conf.lfs_line = proxy->conf.args.line;
9633 cur_arg += 1;
9634 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9635 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9636 rule->action = HTTP_RES_ACT_ADD_ACL;
9637 /*
9638 * '+ 8' for 'add-acl('
9639 * '- 9' for 'add-acl(' + trailing ')'
9640 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009641 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009642
9643 cur_arg = 1;
9644
9645 if (!*args[cur_arg] ||
9646 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9647 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9648 file, linenum, args[0]);
9649 goto out_err;
9650 }
9651
9652 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009653 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009654 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9655 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9656 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009657 free(proxy->conf.lfs_file);
9658 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9659 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009660
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009661 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009662 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9663 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9664 rule->action = HTTP_RES_ACT_DEL_ACL;
9665 /*
9666 * '+ 8' for 'del-acl('
9667 * '- 9' for 'del-acl(' + trailing ')'
9668 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009669 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009670
9671 cur_arg = 1;
9672
9673 if (!*args[cur_arg] ||
9674 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9675 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9676 file, linenum, args[0]);
9677 goto out_err;
9678 }
9679
9680 LIST_INIT(&rule->arg.map.key);
9681 proxy->conf.args.ctx = ARGC_HRS;
9682 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9683 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9684 file, linenum);
9685 free(proxy->conf.lfs_file);
9686 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9687 proxy->conf.lfs_line = proxy->conf.args.line;
9688 cur_arg += 1;
9689 } else if (strncmp(args[0], "del-map", 7) == 0) {
9690 /* http-response del-map(<reference (map name)>) <key pattern> */
9691 rule->action = HTTP_RES_ACT_DEL_MAP;
9692 /*
9693 * '+ 8' for 'del-map('
9694 * '- 9' for 'del-map(' + trailing ')'
9695 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009696 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009697
9698 cur_arg = 1;
9699
9700 if (!*args[cur_arg] ||
9701 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9702 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9703 file, linenum, args[0]);
9704 goto out_err;
9705 }
9706
9707 LIST_INIT(&rule->arg.map.key);
9708 proxy->conf.args.ctx = ARGC_HRS;
9709 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9710 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9711 file, linenum);
9712 free(proxy->conf.lfs_file);
9713 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9714 proxy->conf.lfs_line = proxy->conf.args.line;
9715 cur_arg += 1;
9716 } else if (strncmp(args[0], "set-map", 7) == 0) {
9717 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9718 rule->action = HTTP_RES_ACT_SET_MAP;
9719 /*
9720 * '+ 8' for 'set-map('
9721 * '- 9' for 'set-map(' + trailing ')'
9722 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009723 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009724
9725 cur_arg = 1;
9726
9727 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9728 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9729 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9730 file, linenum, args[0]);
9731 goto out_err;
9732 }
9733
9734 LIST_INIT(&rule->arg.map.key);
9735 LIST_INIT(&rule->arg.map.value);
9736
9737 proxy->conf.args.ctx = ARGC_HRS;
9738
9739 /* key pattern */
9740 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);
9743
9744 /* value pattern */
9745 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9746 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9747 file, linenum);
9748
9749 free(proxy->conf.lfs_file);
9750 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9751 proxy->conf.lfs_line = proxy->conf.args.line;
9752
9753 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009754 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9755 char *errmsg = NULL;
9756 cur_arg = 1;
9757 /* try in the module list */
9758 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9759 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9760 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9761 free(errmsg);
9762 goto out_err;
9763 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009764 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009765 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 +02009766 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9767 goto out_err;
9768 }
9769
9770 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9771 struct acl_cond *cond;
9772 char *errmsg = NULL;
9773
9774 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9775 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9776 file, linenum, args[0], errmsg);
9777 free(errmsg);
9778 goto out_err;
9779 }
9780 rule->cond = cond;
9781 }
9782 else if (*args[cur_arg]) {
9783 Alert("parsing [%s:%d]: 'http-response %s' expects"
9784 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9785 file, linenum, args[0], args[cur_arg]);
9786 goto out_err;
9787 }
9788
9789 return rule;
9790 out_err:
9791 free(rule);
9792 return NULL;
9793}
9794
Willy Tarreau4baae242012-12-27 12:00:31 +01009795/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009796 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9797 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009798 */
9799struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009800 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009801{
9802 struct redirect_rule *rule;
9803 int cur_arg;
9804 int type = REDIRECT_TYPE_NONE;
9805 int code = 302;
9806 const char *destination = NULL;
9807 const char *cookie = NULL;
9808 int cookie_set = 0;
9809 unsigned int flags = REDIRECT_FLAG_NONE;
9810 struct acl_cond *cond = NULL;
9811
9812 cur_arg = 0;
9813 while (*(args[cur_arg])) {
9814 if (strcmp(args[cur_arg], "location") == 0) {
9815 if (!*args[cur_arg + 1])
9816 goto missing_arg;
9817
9818 type = REDIRECT_TYPE_LOCATION;
9819 cur_arg++;
9820 destination = args[cur_arg];
9821 }
9822 else if (strcmp(args[cur_arg], "prefix") == 0) {
9823 if (!*args[cur_arg + 1])
9824 goto missing_arg;
9825
9826 type = REDIRECT_TYPE_PREFIX;
9827 cur_arg++;
9828 destination = args[cur_arg];
9829 }
9830 else if (strcmp(args[cur_arg], "scheme") == 0) {
9831 if (!*args[cur_arg + 1])
9832 goto missing_arg;
9833
9834 type = REDIRECT_TYPE_SCHEME;
9835 cur_arg++;
9836 destination = args[cur_arg];
9837 }
9838 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9839 if (!*args[cur_arg + 1])
9840 goto missing_arg;
9841
9842 cur_arg++;
9843 cookie = args[cur_arg];
9844 cookie_set = 1;
9845 }
9846 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9847 if (!*args[cur_arg + 1])
9848 goto missing_arg;
9849
9850 cur_arg++;
9851 cookie = args[cur_arg];
9852 cookie_set = 0;
9853 }
9854 else if (strcmp(args[cur_arg], "code") == 0) {
9855 if (!*args[cur_arg + 1])
9856 goto missing_arg;
9857
9858 cur_arg++;
9859 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009860 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009861 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009862 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009863 args[cur_arg - 1], args[cur_arg]);
9864 return NULL;
9865 }
9866 }
9867 else if (!strcmp(args[cur_arg],"drop-query")) {
9868 flags |= REDIRECT_FLAG_DROP_QS;
9869 }
9870 else if (!strcmp(args[cur_arg],"append-slash")) {
9871 flags |= REDIRECT_FLAG_APPEND_SLASH;
9872 }
9873 else if (strcmp(args[cur_arg], "if") == 0 ||
9874 strcmp(args[cur_arg], "unless") == 0) {
9875 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9876 if (!cond) {
9877 memprintf(errmsg, "error in condition: %s", *errmsg);
9878 return NULL;
9879 }
9880 break;
9881 }
9882 else {
9883 memprintf(errmsg,
9884 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9885 args[cur_arg]);
9886 return NULL;
9887 }
9888 cur_arg++;
9889 }
9890
9891 if (type == REDIRECT_TYPE_NONE) {
9892 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9893 return NULL;
9894 }
9895
9896 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9897 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009898 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009899
9900 if (!use_fmt) {
9901 /* old-style static redirect rule */
9902 rule->rdr_str = strdup(destination);
9903 rule->rdr_len = strlen(destination);
9904 }
9905 else {
9906 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009907
9908 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9909 * if prefix == "/", we don't want to add anything, otherwise it
9910 * makes it hard for the user to configure a self-redirection.
9911 */
Godbachd9722032014-12-18 15:44:58 +08009912 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009913 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009914 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009915 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9916 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009917 free(curproxy->conf.lfs_file);
9918 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9919 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009920 }
9921 }
9922
Willy Tarreau4baae242012-12-27 12:00:31 +01009923 if (cookie) {
9924 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9925 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9926 */
9927 rule->cookie_len = strlen(cookie);
9928 if (cookie_set) {
9929 rule->cookie_str = malloc(rule->cookie_len + 10);
9930 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9931 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9932 rule->cookie_len += 9;
9933 } else {
9934 rule->cookie_str = malloc(rule->cookie_len + 21);
9935 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9936 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9937 rule->cookie_len += 20;
9938 }
9939 }
9940 rule->type = type;
9941 rule->code = code;
9942 rule->flags = flags;
9943 LIST_INIT(&rule->list);
9944 return rule;
9945
9946 missing_arg:
9947 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9948 return NULL;
9949}
9950
Willy Tarreau8797c062007-05-07 00:55:35 +02009951/************************************************************************/
9952/* The code below is dedicated to ACL parsing and matching */
9953/************************************************************************/
9954
9955
Willy Tarreau14174bc2012-04-16 14:34:04 +02009956/* This function ensures that the prerequisites for an L7 fetch are ready,
9957 * which means that a request or response is ready. If some data is missing,
9958 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009959 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9960 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009961 *
9962 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009963 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9964 * decide whether or not an HTTP message is present ;
9965 * 0 if the requested data cannot be fetched or if it is certain that
9966 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009967 * 1 if an HTTP message is ready
9968 */
9969static int
Willy Tarreau15e91e12015-04-04 00:52:09 +02009970smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009971 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009972{
Willy Tarreau192252e2015-04-04 01:47:55 +02009973 struct http_txn *txn;
9974 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009975
Willy Tarreaueee5b512015-04-03 23:46:31 +02009976 /* Note: this function may only be used from places where
9977 * http_init_txn() has already been done, and implies that <s>,
9978 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
9979 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009980 */
9981
Willy Tarreau192252e2015-04-04 01:47:55 +02009982 if (!s)
9983 return 0;
9984 txn = s->txn;
9985
9986 if (!txn)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009987 return 0;
Willy Tarreau192252e2015-04-04 01:47:55 +02009988 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009989
9990 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009991 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009992
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009993 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009994 /* If the buffer does not leave enough free space at the end,
9995 * we must first realign it.
9996 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009997 if (s->req.buf->p > s->req.buf->data &&
9998 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9999 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010000
Willy Tarreau14174bc2012-04-16 14:34:04 +020010001 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010002 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010003 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010004
10005 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010006 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010007 http_msg_analyzer(msg, &txn->hdr_idx);
10008
10009 /* Still no valid request ? */
10010 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010011 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010012 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010013 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010014 }
10015 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010016 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010017 return 0;
10018 }
10019
10020 /* OK we just got a valid HTTP request. We have some minor
10021 * preparation to perform so that further checks can rely
10022 * on HTTP tests.
10023 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010024
10025 /* If the request was parsed but was too large, we must absolutely
10026 * return an error so that it is not processed. At the moment this
10027 * cannot happen, but if the parsers are to change in the future,
10028 * we want this check to be maintained.
10029 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010030 if (unlikely(s->req.buf->i + s->req.buf->p >
10031 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010032 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +020010033 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010034 return 1;
10035 }
10036
Willy Tarreau9b28e032012-10-12 23:49:43 +020010037 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010038 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010039 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010040
Willy Tarreau506d0502013-07-06 13:29:24 +020010041 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10042 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010043 }
10044
Willy Tarreau506d0502013-07-06 13:29:24 +020010045 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010046 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010047 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010048
10049 /* otherwise everything's ready for the request */
10050 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010051 else {
10052 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010053 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10054 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010055 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010056 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010057 }
10058
10059 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +020010060 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010061 return 1;
10062}
Willy Tarreau8797c062007-05-07 00:55:35 +020010063
Willy Tarreau6c616e02014-06-25 16:56:41 +020010064/* Note: these functinos *do* modify the sample. Even in case of success, at
10065 * least the type and uint value are modified.
10066 */
Willy Tarreauc0239e02012-04-16 14:42:55 +020010067#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau15e91e12015-04-04 00:52:09 +020010068 do { int r = smp_prefetch_http(px, strm, opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +020010069
Willy Tarreau24e32d82012-04-23 23:55:44 +020010070#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau15e91e12015-04-04 00:52:09 +020010071 do { int r = smp_prefetch_http(px, strm, opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +020010072
Willy Tarreau8797c062007-05-07 00:55:35 +020010073
10074/* 1. Check on METHOD
10075 * We use the pre-parsed method if it is known, and store its number as an
10076 * integer. If it is unknown, we use the pointer and the length.
10077 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010078static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010079{
10080 int len, meth;
10081
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010082 len = strlen(text);
10083 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010084
10085 pattern->val.i = meth;
10086 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010087 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010088 pattern->len = len;
10089 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010090 else {
10091 pattern->ptr.str = NULL;
10092 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010093 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010094 return 1;
10095}
10096
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010097/* This function fetches the method of current HTTP request and stores
10098 * it in the global pattern struct as a chunk. There are two possibilities :
10099 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10100 * in <len> and <ptr> is NULL ;
10101 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10102 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010103 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010104 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010105static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010106smp_fetch_meth(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010107 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010108{
10109 int meth;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010110 struct http_txn *txn = strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010111
Willy Tarreau24e32d82012-04-23 23:55:44 +020010112 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010113
Willy Tarreau8797c062007-05-07 00:55:35 +020010114 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010115 smp->type = SMP_T_METH;
10116 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010117 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010118 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10119 /* ensure the indexes are not affected */
10120 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010121 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010122 smp->data.meth.str.len = txn->req.sl.rq.m_l;
10123 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010124 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010125 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010126 return 1;
10127}
10128
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010129/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010130static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010131{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010132 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010133 struct pattern_list *lst;
10134 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010135
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010136 list_for_each_entry(lst, &expr->patterns, list) {
10137 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010138
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010139 /* well-known method */
10140 if (pattern->val.i != HTTP_METH_OTHER) {
10141 if (smp->data.meth.meth == pattern->val.i)
10142 return pattern;
10143 else
10144 continue;
10145 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010146
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010147 /* Other method, we must compare the strings */
10148 if (pattern->len != smp->data.meth.str.len)
10149 continue;
10150
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010151 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010152 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10153 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010154 return pattern;
10155 }
10156 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010157}
10158
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010159static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010160smp_fetch_rqver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010161 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010162{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010163 struct http_txn *txn = strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010164 char *ptr;
10165 int len;
10166
Willy Tarreauc0239e02012-04-16 14:42:55 +020010167 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010168
Willy Tarreau8797c062007-05-07 00:55:35 +020010169 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010170 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010171
10172 while ((len-- > 0) && (*ptr++ != '/'));
10173 if (len <= 0)
10174 return 0;
10175
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010176 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010177 smp->data.str.str = ptr;
10178 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010179
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010180 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010181 return 1;
10182}
10183
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010184static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010185smp_fetch_stver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010186 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010187{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010188 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010189 char *ptr;
10190 int len;
10191
Willy Tarreauc0239e02012-04-16 14:42:55 +020010192 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010193
Willy Tarreau15e91e12015-04-04 00:52:09 +020010194 txn = strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010195 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10196 return 0;
10197
Willy Tarreau8797c062007-05-07 00:55:35 +020010198 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010199 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010200
10201 while ((len-- > 0) && (*ptr++ != '/'));
10202 if (len <= 0)
10203 return 0;
10204
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010205 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010206 smp->data.str.str = ptr;
10207 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010208
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010209 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010210 return 1;
10211}
10212
10213/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010214static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010215smp_fetch_stcode(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010216 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010217{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010218 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010219 char *ptr;
10220 int len;
10221
Willy Tarreauc0239e02012-04-16 14:42:55 +020010222 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010223
Willy Tarreau15e91e12015-04-04 00:52:09 +020010224 txn = strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010225 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10226 return 0;
10227
Willy Tarreau8797c062007-05-07 00:55:35 +020010228 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010229 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010230
Willy Tarreauf853c462012-04-23 18:53:56 +020010231 smp->type = SMP_T_UINT;
10232 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010233 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010234 return 1;
10235}
10236
10237/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010238static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010239smp_fetch_url(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010240 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010241{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010242 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010243
Willy Tarreauc0239e02012-04-16 14:42:55 +020010244 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010245
Willy Tarreau15e91e12015-04-04 00:52:09 +020010246 txn = strm->txn;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010247 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010248 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010249 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010250 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010251 return 1;
10252}
10253
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010254static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010255smp_fetch_url_ip(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010256 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010257{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010258 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010259 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010260
Willy Tarreauc0239e02012-04-16 14:42:55 +020010261 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010262
Willy Tarreau15e91e12015-04-04 00:52:09 +020010263 txn = strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010264 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 +020010265 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010266 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010267
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010268 smp->type = SMP_T_IPV4;
10269 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010270 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010271 return 1;
10272}
10273
10274static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010275smp_fetch_url_port(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010276 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010277{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010278 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010279 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010280
Willy Tarreauc0239e02012-04-16 14:42:55 +020010281 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010282
Willy Tarreau15e91e12015-04-04 00:52:09 +020010283 txn = strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010284 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 +020010285 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10286 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010287
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010288 smp->type = SMP_T_UINT;
10289 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010290 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010291 return 1;
10292}
10293
Willy Tarreau185b5c42012-04-26 15:11:51 +020010294/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10295 * Accepts an optional argument of type string containing the header field name,
10296 * and an optional argument of type signed or unsigned integer to request an
10297 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010298 * headers are considered from the first one. It does not stop on commas and
10299 * returns full lines instead (useful for User-Agent or Date for example).
10300 */
10301static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010302smp_fetch_fhdr(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010303 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010304{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010305 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010306 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010307 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010308 int occ = 0;
10309 const char *name_str = NULL;
10310 int name_len = 0;
10311
10312 if (!ctx) {
10313 /* first call */
10314 ctx = &static_hdr_ctx;
10315 ctx->idx = 0;
10316 smp->ctx.a[0] = ctx;
10317 }
10318
10319 if (args) {
10320 if (args[0].type != ARGT_STR)
10321 return 0;
10322 name_str = args[0].data.str.str;
10323 name_len = args[0].data.str.len;
10324
10325 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10326 occ = args[1].data.uint;
10327 }
10328
10329 CHECK_HTTP_MESSAGE_FIRST();
10330
Willy Tarreau15e91e12015-04-04 00:52:09 +020010331 idx = &strm->txn->hdr_idx;
10332 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10333
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010334 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10335 /* search for header from the beginning */
10336 ctx->idx = 0;
10337
10338 if (!occ && !(opt & SMP_OPT_ITERATE))
10339 /* no explicit occurrence and single fetch => last header by default */
10340 occ = -1;
10341
10342 if (!occ)
10343 /* prepare to report multiple occurrences for ACL fetches */
10344 smp->flags |= SMP_F_NOT_LAST;
10345
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010346 smp->type = SMP_T_STR;
10347 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010348 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10349 return 1;
10350
10351 smp->flags &= ~SMP_F_NOT_LAST;
10352 return 0;
10353}
10354
10355/* 6. Check on HTTP header count. The number of occurrences is returned.
10356 * Accepts exactly 1 argument of type string. It does not stop on commas and
10357 * returns full lines instead (useful for User-Agent or Date for example).
10358 */
10359static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010360smp_fetch_fhdr_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010361 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010362{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010363 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010364 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010365 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010366 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010367 const char *name = NULL;
10368 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010369
Willy Tarreau601a4d12015-04-01 19:16:09 +020010370 if (args && args->type == ARGT_STR) {
10371 name = args->data.str.str;
10372 len = args->data.str.len;
10373 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010374
10375 CHECK_HTTP_MESSAGE_FIRST();
10376
Willy Tarreau15e91e12015-04-04 00:52:09 +020010377 idx = &strm->txn->hdr_idx;
10378 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10379
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010380 ctx.idx = 0;
10381 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010382 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010383 cnt++;
10384
10385 smp->type = SMP_T_UINT;
10386 smp->data.uint = cnt;
10387 smp->flags = SMP_F_VOL_HDR;
10388 return 1;
10389}
10390
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010391static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010392smp_fetch_hdr_names(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010393 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010394{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010395 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010396 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010397 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010398 struct chunk *temp;
10399 char del = ',';
10400
10401 if (args && args->type == ARGT_STR)
10402 del = *args[0].data.str.str;
10403
10404 CHECK_HTTP_MESSAGE_FIRST();
10405
Willy Tarreau15e91e12015-04-04 00:52:09 +020010406 idx = &strm->txn->hdr_idx;
10407 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10408
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010409 temp = get_trash_chunk();
10410
10411 ctx.idx = 0;
10412 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10413 if (temp->len)
10414 temp->str[temp->len++] = del;
10415 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10416 temp->len += ctx.del;
10417 }
10418
10419 smp->type = SMP_T_STR;
10420 smp->data.str.str = temp->str;
10421 smp->data.str.len = temp->len;
10422 smp->flags = SMP_F_VOL_HDR;
10423 return 1;
10424}
10425
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010426/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10427 * Accepts an optional argument of type string containing the header field name,
10428 * and an optional argument of type signed or unsigned integer to request an
10429 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010430 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010431 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010432static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010433smp_fetch_hdr(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010434 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010435{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010436 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010437 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010438 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010439 int occ = 0;
10440 const char *name_str = NULL;
10441 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010442
Willy Tarreaua890d072013-04-02 12:01:06 +020010443 if (!ctx) {
10444 /* first call */
10445 ctx = &static_hdr_ctx;
10446 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010447 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010448 }
10449
Willy Tarreau185b5c42012-04-26 15:11:51 +020010450 if (args) {
10451 if (args[0].type != ARGT_STR)
10452 return 0;
10453 name_str = args[0].data.str.str;
10454 name_len = args[0].data.str.len;
10455
10456 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10457 occ = args[1].data.uint;
10458 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010459
Willy Tarreaue333ec92012-04-16 16:26:40 +020010460 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010461
Willy Tarreau15e91e12015-04-04 00:52:09 +020010462 idx = &strm->txn->hdr_idx;
10463 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10464
Willy Tarreau185b5c42012-04-26 15:11:51 +020010465 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010466 /* search for header from the beginning */
10467 ctx->idx = 0;
10468
Willy Tarreau185b5c42012-04-26 15:11:51 +020010469 if (!occ && !(opt & SMP_OPT_ITERATE))
10470 /* no explicit occurrence and single fetch => last header by default */
10471 occ = -1;
10472
10473 if (!occ)
10474 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010475 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010476
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010477 smp->type = SMP_T_STR;
10478 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010479 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 +020010480 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010481
Willy Tarreau37406352012-04-23 16:16:37 +020010482 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010483 return 0;
10484}
10485
Willy Tarreauc11416f2007-06-17 16:58:38 +020010486/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010487 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010488 */
10489static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010490smp_fetch_hdr_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010491 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010492{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010493 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010494 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010495 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010496 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010497 const char *name = NULL;
10498 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010499
Willy Tarreau601a4d12015-04-01 19:16:09 +020010500 if (args && args->type == ARGT_STR) {
10501 name = args->data.str.str;
10502 len = args->data.str.len;
10503 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010504
Willy Tarreaue333ec92012-04-16 16:26:40 +020010505 CHECK_HTTP_MESSAGE_FIRST();
10506
Willy Tarreau15e91e12015-04-04 00:52:09 +020010507 idx = &strm->txn->hdr_idx;
10508 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10509
Willy Tarreau33a7e692007-06-10 19:45:56 +020010510 ctx.idx = 0;
10511 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010512 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010513 cnt++;
10514
Willy Tarreauf853c462012-04-23 18:53:56 +020010515 smp->type = SMP_T_UINT;
10516 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010517 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010518 return 1;
10519}
10520
Willy Tarreau185b5c42012-04-26 15:11:51 +020010521/* Fetch an HTTP header's integer value. The integer value is returned. It
10522 * takes a mandatory argument of type string and an optional one of type int
10523 * to designate a specific occurrence. It returns an unsigned integer, which
10524 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010525 */
10526static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010527smp_fetch_hdr_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010528 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010529{
Willy Tarreau192252e2015-04-04 01:47:55 +020010530 int ret = smp_fetch_hdr(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010531
Willy Tarreauf853c462012-04-23 18:53:56 +020010532 if (ret > 0) {
10533 smp->type = SMP_T_UINT;
10534 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10535 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010536
Willy Tarreaud53e2422012-04-16 17:21:11 +020010537 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010538}
10539
Cyril Bonté69fa9922012-10-25 00:01:06 +020010540/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10541 * and an optional one of type int to designate a specific occurrence.
10542 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010543 */
10544static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010545smp_fetch_hdr_ip(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010546 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010547{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010548 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010549
Willy Tarreau192252e2015-04-04 01:47:55 +020010550 while ((ret = smp_fetch_hdr(px, sess, strm, opt, args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010551 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10552 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010553 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010554 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010555 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010556 if (smp->data.str.len < temp->size - 1) {
10557 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10558 temp->str[smp->data.str.len] = '\0';
10559 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10560 smp->type = SMP_T_IPV6;
10561 break;
10562 }
10563 }
10564 }
10565
Willy Tarreaud53e2422012-04-16 17:21:11 +020010566 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010567 if (!(smp->flags & SMP_F_NOT_LAST))
10568 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010569 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010570 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010571}
10572
Willy Tarreau737b0c12007-06-10 21:28:46 +020010573/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10574 * the first '/' after the possible hostname, and ends before the possible '?'.
10575 */
10576static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010577smp_fetch_path(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010578 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010579{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010580 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010581 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010582
Willy Tarreauc0239e02012-04-16 14:42:55 +020010583 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010584
Willy Tarreau15e91e12015-04-04 00:52:09 +020010585 txn = strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010586 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010587 ptr = http_get_path(txn);
10588 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010589 return 0;
10590
10591 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010592 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010593 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010594
10595 while (ptr < end && *ptr != '?')
10596 ptr++;
10597
Willy Tarreauf853c462012-04-23 18:53:56 +020010598 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010599 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010600 return 1;
10601}
10602
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010603/* This produces a concatenation of the first occurrence of the Host header
10604 * followed by the path component if it begins with a slash ('/'). This means
10605 * that '*' will not be added, resulting in exactly the first Host entry.
10606 * If no Host header is found, then the path is returned as-is. The returned
10607 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010608 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010609 */
10610static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010611smp_fetch_base(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010612 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010613{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010614 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010615 char *ptr, *end, *beg;
10616 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010617 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010618
10619 CHECK_HTTP_MESSAGE_FIRST();
10620
Willy Tarreau15e91e12015-04-04 00:52:09 +020010621 txn = strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010622 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010623 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreau192252e2015-04-04 01:47:55 +020010624 return smp_fetch_path(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010625
10626 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010627 temp = get_trash_chunk();
10628 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010629 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010630 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010631 smp->data.str.len = ctx.vlen;
10632
10633 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010634 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010635 beg = http_get_path(txn);
10636 if (!beg)
10637 beg = end;
10638
10639 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10640
10641 if (beg < ptr && *beg == '/') {
10642 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10643 smp->data.str.len += ptr - beg;
10644 }
10645
10646 smp->flags = SMP_F_VOL_1ST;
10647 return 1;
10648}
10649
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010650/* This produces a 32-bit hash of the concatenation of the first occurrence of
10651 * the Host header followed by the path component if it begins with a slash ('/').
10652 * This means that '*' will not be added, resulting in exactly the first Host
10653 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010654 * is hashed using the path hash followed by a full avalanche hash and provides a
10655 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010656 * high-traffic sites without having to store whole paths.
10657 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010658int
Willy Tarreau192252e2015-04-04 01:47:55 +020010659smp_fetch_base32(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010660 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010661{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010662 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010663 struct hdr_ctx ctx;
10664 unsigned int hash = 0;
10665 char *ptr, *beg, *end;
10666 int len;
10667
10668 CHECK_HTTP_MESSAGE_FIRST();
10669
Willy Tarreau15e91e12015-04-04 00:52:09 +020010670 txn = strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010671 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010672 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010673 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10674 ptr = ctx.line + ctx.val;
10675 len = ctx.vlen;
10676 while (len--)
10677 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10678 }
10679
10680 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010681 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010682 beg = http_get_path(txn);
10683 if (!beg)
10684 beg = end;
10685
10686 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10687
10688 if (beg < ptr && *beg == '/') {
10689 while (beg < ptr)
10690 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10691 }
10692 hash = full_hash(hash);
10693
10694 smp->type = SMP_T_UINT;
10695 smp->data.uint = hash;
10696 smp->flags = SMP_F_VOL_1ST;
10697 return 1;
10698}
10699
Willy Tarreau4a550602012-12-09 14:53:32 +010010700/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010701 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10702 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10703 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010704 * that in environments where IPv6 is insignificant, truncating the output to
10705 * 8 bytes would still work.
10706 */
10707static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010708smp_fetch_base32_src(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010709 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010710{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010711 struct chunk *temp;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020010712 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010713
10714 if (!cli_conn)
10715 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010716
Willy Tarreau192252e2015-04-04 01:47:55 +020010717 if (!smp_fetch_base32(px, sess, strm, opt, args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010718 return 0;
10719
Willy Tarreau47ca5452012-12-23 20:22:19 +010010720 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010721 *(unsigned int *)temp->str = htonl(smp->data.uint);
10722 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010723
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010724 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010725 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010726 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010727 temp->len += 4;
10728 break;
10729 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010730 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010731 temp->len += 16;
10732 break;
10733 default:
10734 return 0;
10735 }
10736
10737 smp->data.str = *temp;
10738 smp->type = SMP_T_BIN;
10739 return 1;
10740}
10741
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010742/* Extracts the query string, which comes after the question mark '?'. If no
10743 * question mark is found, nothing is returned. Otherwise it returns a sample
10744 * of type string carrying the whole query string.
10745 */
10746static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010747smp_fetch_query(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010748 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010749{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010750 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010751 char *ptr, *end;
10752
10753 CHECK_HTTP_MESSAGE_FIRST();
10754
Willy Tarreau15e91e12015-04-04 00:52:09 +020010755 txn = strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010756 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10757 end = ptr + txn->req.sl.rq.u_l;
10758
10759 /* look up the '?' */
10760 do {
10761 if (ptr == end)
10762 return 0;
10763 } while (*ptr++ != '?');
10764
10765 smp->type = SMP_T_STR;
10766 smp->data.str.str = ptr;
10767 smp->data.str.len = end - ptr;
10768 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10769 return 1;
10770}
10771
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010772static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010773smp_fetch_proto_http(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010774 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010775{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010776 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10777 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10778 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010779
Willy Tarreau24e32d82012-04-23 23:55:44 +020010780 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010781
Willy Tarreauf853c462012-04-23 18:53:56 +020010782 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010783 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010784 return 1;
10785}
10786
Willy Tarreau7f18e522010-10-22 20:04:13 +020010787/* return a valid test if the current request is the first one on the connection */
10788static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010789smp_fetch_http_first_req(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010790 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010791{
Willy Tarreauf853c462012-04-23 18:53:56 +020010792 smp->type = SMP_T_BOOL;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010793 smp->data.uint = !(strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010794 return 1;
10795}
10796
Willy Tarreau34db1082012-04-19 17:16:54 +020010797/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010798static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010799smp_fetch_http_auth(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010800 const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010801{
10802
Willy Tarreau24e32d82012-04-23 23:55:44 +020010803 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010804 return 0;
10805
Willy Tarreauc0239e02012-04-16 14:42:55 +020010806 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010807
Willy Tarreau15e91e12015-04-04 00:52:09 +020010808 if (!get_http_auth(strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010809 return 0;
10810
Willy Tarreauf853c462012-04-23 18:53:56 +020010811 smp->type = SMP_T_BOOL;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010812 smp->data.uint = check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010813 return 1;
10814}
Willy Tarreau8797c062007-05-07 00:55:35 +020010815
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010816/* Accepts exactly 1 argument of type userlist */
10817static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010818smp_fetch_http_auth_grp(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010819 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010820{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010821 if (!args || args->type != ARGT_USR)
10822 return 0;
10823
10824 CHECK_HTTP_MESSAGE_FIRST();
10825
Willy Tarreau15e91e12015-04-04 00:52:09 +020010826 if (!get_http_auth(strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010827 return 0;
10828
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010829 /* if the user does not belong to the userlist or has a wrong password,
10830 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010831 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010832 */
Willy Tarreau15e91e12015-04-04 00:52:09 +020010833 if (!check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010834 return 0;
10835
10836 /* pat_match_auth() will need the user list */
10837 smp->ctx.a[0] = args->data.usr;
10838
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010839 smp->type = SMP_T_STR;
10840 smp->flags = SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010841 smp->data.str.str = strm->txn->auth.user;
10842 smp->data.str.len = strlen(strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010843
10844 return 1;
10845}
10846
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010847/* Try to find the next occurrence of a cookie name in a cookie header value.
10848 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10849 * the cookie value is returned into *value and *value_l, and the function
10850 * returns a pointer to the next pointer to search from if the value was found.
10851 * Otherwise if the cookie was not found, NULL is returned and neither value
10852 * nor value_l are touched. The input <hdr> string should first point to the
10853 * header's value, and the <hdr_end> pointer must point to the first character
10854 * not part of the value. <list> must be non-zero if value may represent a list
10855 * of values (cookie headers). This makes it faster to abort parsing when no
10856 * list is expected.
10857 */
10858static char *
10859extract_cookie_value(char *hdr, const char *hdr_end,
10860 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010861 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010862{
10863 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10864 char *next;
10865
10866 /* we search at least a cookie name followed by an equal, and more
10867 * generally something like this :
10868 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10869 */
10870 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10871 /* Iterate through all cookies on this line */
10872
10873 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10874 att_beg++;
10875
10876 /* find att_end : this is the first character after the last non
10877 * space before the equal. It may be equal to hdr_end.
10878 */
10879 equal = att_end = att_beg;
10880
10881 while (equal < hdr_end) {
10882 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10883 break;
10884 if (http_is_spht[(unsigned char)*equal++])
10885 continue;
10886 att_end = equal;
10887 }
10888
10889 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10890 * is between <att_beg> and <equal>, both may be identical.
10891 */
10892
10893 /* look for end of cookie if there is an equal sign */
10894 if (equal < hdr_end && *equal == '=') {
10895 /* look for the beginning of the value */
10896 val_beg = equal + 1;
10897 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10898 val_beg++;
10899
10900 /* find the end of the value, respecting quotes */
10901 next = find_cookie_value_end(val_beg, hdr_end);
10902
10903 /* make val_end point to the first white space or delimitor after the value */
10904 val_end = next;
10905 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10906 val_end--;
10907 } else {
10908 val_beg = val_end = next = equal;
10909 }
10910
10911 /* We have nothing to do with attributes beginning with '$'. However,
10912 * they will automatically be removed if a header before them is removed,
10913 * since they're supposed to be linked together.
10914 */
10915 if (*att_beg == '$')
10916 continue;
10917
10918 /* Ignore cookies with no equal sign */
10919 if (equal == next)
10920 continue;
10921
10922 /* Now we have the cookie name between att_beg and att_end, and
10923 * its value between val_beg and val_end.
10924 */
10925
10926 if (att_end - att_beg == cookie_name_l &&
10927 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10928 /* let's return this value and indicate where to go on from */
10929 *value = val_beg;
10930 *value_l = val_end - val_beg;
10931 return next + 1;
10932 }
10933
10934 /* Set-Cookie headers only have the name in the first attr=value part */
10935 if (!list)
10936 break;
10937 }
10938
10939 return NULL;
10940}
10941
William Lallemanda43ba4e2014-01-28 18:14:25 +010010942/* Fetch a captured HTTP request header. The index is the position of
10943 * the "capture" option in the configuration file
10944 */
10945static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010946smp_fetch_capture_header_req(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010947 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010948{
Willy Tarreaud0d8da92015-04-04 02:10:38 +020010949 struct proxy *fe = strm_fe(strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010950 int idx;
10951
10952 if (!args || args->type != ARGT_UINT)
10953 return 0;
10954
10955 idx = args->data.uint;
10956
Willy Tarreau15e91e12015-04-04 00:52:09 +020010957 if (idx > (fe->nb_req_cap - 1) || strm->req_cap == NULL || strm->req_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010958 return 0;
10959
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010960 smp->type = SMP_T_STR;
10961 smp->flags |= SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010962 smp->data.str.str = strm->req_cap[idx];
10963 smp->data.str.len = strlen(strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010964
10965 return 1;
10966}
10967
10968/* Fetch a captured HTTP response header. The index is the position of
10969 * the "capture" option in the configuration file
10970 */
10971static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010972smp_fetch_capture_header_res(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010973 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010974{
Willy Tarreaud0d8da92015-04-04 02:10:38 +020010975 struct proxy *fe = strm_fe(strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010976 int idx;
10977
10978 if (!args || args->type != ARGT_UINT)
10979 return 0;
10980
10981 idx = args->data.uint;
10982
Willy Tarreau15e91e12015-04-04 00:52:09 +020010983 if (idx > (fe->nb_rsp_cap - 1) || strm->res_cap == NULL || strm->res_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010984 return 0;
10985
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010986 smp->type = SMP_T_STR;
10987 smp->flags |= SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010988 smp->data.str.str = strm->res_cap[idx];
10989 smp->data.str.len = strlen(strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010990
10991 return 1;
10992}
10993
William Lallemand65ad6e12014-01-31 15:08:02 +010010994/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10995static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010996smp_fetch_capture_req_method(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010997 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010998{
10999 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011000 struct http_txn *txn = strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011001 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011002
Willy Tarreau15e91e12015-04-04 00:52:09 +020011003 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011004 return 0;
11005
William Lallemand96a77852014-02-05 00:30:02 +010011006 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011007
William Lallemand96a77852014-02-05 00:30:02 +010011008 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11009 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011010
William Lallemand96a77852014-02-05 00:30:02 +010011011 temp = get_trash_chunk();
11012 temp->str = txn->uri;
11013 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011014 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011015 smp->type = SMP_T_STR;
11016 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011017
11018 return 1;
11019
11020}
11021
11022/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11023static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011024smp_fetch_capture_req_uri(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011025 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011026{
11027 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011028 struct http_txn *txn = strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011029 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011030
Willy Tarreau15e91e12015-04-04 00:52:09 +020011031 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011032 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011033
William Lallemand65ad6e12014-01-31 15:08:02 +010011034 ptr = txn->uri;
11035
11036 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11037 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011038
William Lallemand65ad6e12014-01-31 15:08:02 +010011039 if (!*ptr)
11040 return 0;
11041
11042 ptr++; /* skip the space */
11043
11044 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011045 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011046 if (!ptr)
11047 return 0;
11048 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11049 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011050
11051 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010011052 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011053 smp->type = SMP_T_STR;
11054 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011055
11056 return 1;
11057}
11058
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011059/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11060 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11061 */
11062static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011063smp_fetch_capture_req_ver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011064 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011065{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011066 struct http_txn *txn = strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011067
Willy Tarreau15e91e12015-04-04 00:52:09 +020011068 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011069 return 0;
11070
11071 if (txn->req.flags & HTTP_MSGF_VER_11)
11072 smp->data.str.str = "HTTP/1.1";
11073 else
11074 smp->data.str.str = "HTTP/1.0";
11075
11076 smp->data.str.len = 8;
11077 smp->type = SMP_T_STR;
11078 smp->flags = SMP_F_CONST;
11079 return 1;
11080
11081}
11082
11083/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11084 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11085 */
11086static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011087smp_fetch_capture_res_ver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011088 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011089{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011090 struct http_txn *txn = strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011091
Willy Tarreau15e91e12015-04-04 00:52:09 +020011092 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011093 return 0;
11094
11095 if (txn->rsp.flags & HTTP_MSGF_VER_11)
11096 smp->data.str.str = "HTTP/1.1";
11097 else
11098 smp->data.str.str = "HTTP/1.0";
11099
11100 smp->data.str.len = 8;
11101 smp->type = SMP_T_STR;
11102 smp->flags = SMP_F_CONST;
11103 return 1;
11104
11105}
11106
William Lallemand65ad6e12014-01-31 15:08:02 +010011107
Willy Tarreaue333ec92012-04-16 16:26:40 +020011108/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011109 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011110 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011111 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011112 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011113 * Accepts exactly 1 argument of type string. If the input options indicate
11114 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011115 * The returned sample is of type CSTR. Can be used to parse cookies in other
11116 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011117 */
Willy Tarreau192252e2015-04-04 01:47:55 +020011118int smp_fetch_cookie(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011119 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011120{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011121 struct http_txn *txn;
11122 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011123 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011124 const struct http_msg *msg;
11125 const char *hdr_name;
11126 int hdr_name_len;
11127 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011128 int occ = 0;
11129 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011130
Willy Tarreau24e32d82012-04-23 23:55:44 +020011131 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011132 return 0;
11133
Willy Tarreaua890d072013-04-02 12:01:06 +020011134 if (!ctx) {
11135 /* first call */
11136 ctx = &static_hdr_ctx;
11137 ctx->idx = 0;
11138 smp->ctx.a[2] = ctx;
11139 }
11140
Willy Tarreaue333ec92012-04-16 16:26:40 +020011141 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011142
Willy Tarreau15e91e12015-04-04 00:52:09 +020011143 txn = strm->txn;
11144 idx = &strm->txn->hdr_idx;
11145
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011146 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011147 msg = &txn->req;
11148 hdr_name = "Cookie";
11149 hdr_name_len = 6;
11150 } else {
11151 msg = &txn->rsp;
11152 hdr_name = "Set-Cookie";
11153 hdr_name_len = 10;
11154 }
11155
Willy Tarreau28376d62012-04-26 21:26:10 +020011156 if (!occ && !(opt & SMP_OPT_ITERATE))
11157 /* no explicit occurrence and single fetch => last cookie by default */
11158 occ = -1;
11159
11160 /* OK so basically here, either we want only one value and it's the
11161 * last one, or we want to iterate over all of them and we fetch the
11162 * next one.
11163 */
11164
Willy Tarreau9b28e032012-10-12 23:49:43 +020011165 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011166 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011167 /* search for the header from the beginning, we must first initialize
11168 * the search parameters.
11169 */
Willy Tarreau37406352012-04-23 16:16:37 +020011170 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011171 ctx->idx = 0;
11172 }
11173
Willy Tarreau28376d62012-04-26 21:26:10 +020011174 smp->flags |= SMP_F_VOL_HDR;
11175
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011176 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011177 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11178 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011179 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11180 goto out;
11181
Willy Tarreau24e32d82012-04-23 23:55:44 +020011182 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011183 continue;
11184
Willy Tarreau37406352012-04-23 16:16:37 +020011185 smp->ctx.a[0] = ctx->line + ctx->val;
11186 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011187 }
11188
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011189 smp->type = SMP_T_STR;
11190 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011191 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011192 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011193 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011194 &smp->data.str.str,
11195 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011196 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011197 found = 1;
11198 if (occ >= 0) {
11199 /* one value was returned into smp->data.str.{str,len} */
11200 smp->flags |= SMP_F_NOT_LAST;
11201 return 1;
11202 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011203 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011204 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011205 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011206 /* all cookie headers and values were scanned. If we're looking for the
11207 * last occurrence, we may return it now.
11208 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011209 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011210 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011211 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011212}
11213
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011214/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011215 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011216 * multiple cookies may be parsed on the same line. The returned sample is of
11217 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011218 */
11219static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011220smp_fetch_cookie_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011221 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011222{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011223 struct http_txn *txn;
11224 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011225 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011226 const struct http_msg *msg;
11227 const char *hdr_name;
11228 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011229 int cnt;
11230 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011231 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011232
Willy Tarreau24e32d82012-04-23 23:55:44 +020011233 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011234 return 0;
11235
Willy Tarreaue333ec92012-04-16 16:26:40 +020011236 CHECK_HTTP_MESSAGE_FIRST();
11237
Willy Tarreau15e91e12015-04-04 00:52:09 +020011238 txn = strm->txn;
11239 idx = &strm->txn->hdr_idx;
11240
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011241 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011242 msg = &txn->req;
11243 hdr_name = "Cookie";
11244 hdr_name_len = 6;
11245 } else {
11246 msg = &txn->rsp;
11247 hdr_name = "Set-Cookie";
11248 hdr_name_len = 10;
11249 }
11250
Willy Tarreau9b28e032012-10-12 23:49:43 +020011251 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011252 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011253 ctx.idx = 0;
11254 cnt = 0;
11255
11256 while (1) {
11257 /* Note: val_beg == NULL every time we need to fetch a new header */
11258 if (!val_beg) {
11259 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11260 break;
11261
Willy Tarreau24e32d82012-04-23 23:55:44 +020011262 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011263 continue;
11264
11265 val_beg = ctx.line + ctx.val;
11266 val_end = val_beg + ctx.vlen;
11267 }
11268
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011269 smp->type = SMP_T_STR;
11270 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011271 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011272 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011273 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011274 &smp->data.str.str,
11275 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011276 cnt++;
11277 }
11278 }
11279
Willy Tarreaub169eba2013-12-16 15:14:43 +010011280 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011281 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011282 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011283 return 1;
11284}
11285
Willy Tarreau51539362012-05-08 12:46:28 +020011286/* Fetch an cookie's integer value. The integer value is returned. It
11287 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11288 */
11289static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011290smp_fetch_cookie_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011291 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011292{
Willy Tarreau192252e2015-04-04 01:47:55 +020011293 int ret = smp_fetch_cookie(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011294
11295 if (ret > 0) {
11296 smp->type = SMP_T_UINT;
11297 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11298 }
11299
11300 return ret;
11301}
11302
Willy Tarreau8797c062007-05-07 00:55:35 +020011303/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011304/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011305/************************************************************************/
11306
David Cournapeau16023ee2010-12-23 20:55:41 +090011307/*
11308 * Given a path string and its length, find the position of beginning of the
11309 * query string. Returns NULL if no query string is found in the path.
11310 *
11311 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11312 *
11313 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11314 */
bedis4c75cca2012-10-05 08:38:24 +020011315static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011316{
11317 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011318
bedis4c75cca2012-10-05 08:38:24 +020011319 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011320 return p ? p + 1 : NULL;
11321}
11322
bedis4c75cca2012-10-05 08:38:24 +020011323static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011324{
bedis4c75cca2012-10-05 08:38:24 +020011325 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011326}
11327
11328/*
11329 * Given a url parameter, find the starting position of the first occurence,
11330 * or NULL if the parameter is not found.
11331 *
11332 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11333 * the function will return query_string+8.
11334 */
11335static char*
11336find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011337 char* url_param_name, size_t url_param_name_l,
11338 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011339{
11340 char *pos, *last;
11341
11342 pos = query_string;
11343 last = query_string + query_string_l - url_param_name_l - 1;
11344
11345 while (pos <= last) {
11346 if (pos[url_param_name_l] == '=') {
11347 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11348 return pos;
11349 pos += url_param_name_l + 1;
11350 }
bedis4c75cca2012-10-05 08:38:24 +020011351 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011352 pos++;
11353 pos++;
11354 }
11355 return NULL;
11356}
11357
11358/*
11359 * Given a url parameter name, returns its value and size into *value and
11360 * *value_l respectively, and returns non-zero. If the parameter is not found,
11361 * zero is returned and value/value_l are not touched.
11362 */
11363static int
11364find_url_param_value(char* path, size_t path_l,
11365 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011366 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011367{
11368 char *query_string, *qs_end;
11369 char *arg_start;
11370 char *value_start, *value_end;
11371
bedis4c75cca2012-10-05 08:38:24 +020011372 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011373 if (!query_string)
11374 return 0;
11375
11376 qs_end = path + path_l;
11377 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011378 url_param_name, url_param_name_l,
11379 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011380 if (!arg_start)
11381 return 0;
11382
11383 value_start = arg_start + url_param_name_l + 1;
11384 value_end = value_start;
11385
bedis4c75cca2012-10-05 08:38:24 +020011386 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011387 value_end++;
11388
11389 *value = value_start;
11390 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011391 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011392}
11393
11394static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011395smp_fetch_url_param(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011396 const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011397{
bedis4c75cca2012-10-05 08:38:24 +020011398 char delim = '?';
Willy Tarreau15e91e12015-04-04 00:52:09 +020011399 struct http_msg *msg;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011400
bedis4c75cca2012-10-05 08:38:24 +020011401 if (!args || args[0].type != ARGT_STR ||
11402 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011403 return 0;
11404
11405 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011406
Willy Tarreau15e91e12015-04-04 00:52:09 +020011407 msg = &strm->txn->req;
11408
bedis4c75cca2012-10-05 08:38:24 +020011409 if (args[1].type)
11410 delim = *args[1].data.str.str;
11411
Willy Tarreau9b28e032012-10-12 23:49:43 +020011412 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011413 args->data.str.str, args->data.str.len,
11414 &smp->data.str.str, &smp->data.str.len,
11415 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011416 return 0;
11417
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011418 smp->type = SMP_T_STR;
11419 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011420 return 1;
11421}
11422
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011423/* Return the signed integer value for the specified url parameter (see url_param
11424 * above).
11425 */
11426static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011427smp_fetch_url_param_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011428 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011429{
Willy Tarreau192252e2015-04-04 01:47:55 +020011430 int ret = smp_fetch_url_param(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011431
11432 if (ret > 0) {
11433 smp->type = SMP_T_UINT;
11434 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11435 }
11436
11437 return ret;
11438}
11439
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011440/* This produces a 32-bit hash of the concatenation of the first occurrence of
11441 * the Host header followed by the path component if it begins with a slash ('/').
11442 * This means that '*' will not be added, resulting in exactly the first Host
11443 * entry. If no Host header is found, then the path is used. The resulting value
11444 * is hashed using the url hash followed by a full avalanche hash and provides a
11445 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11446 * high-traffic sites without having to store whole paths.
11447 * this differs from the base32 functions in that it includes the url parameters
11448 * as well as the path
11449 */
11450static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011451smp_fetch_url32(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011452 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011453{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011454 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011455 struct hdr_ctx ctx;
11456 unsigned int hash = 0;
11457 char *ptr, *beg, *end;
11458 int len;
11459
11460 CHECK_HTTP_MESSAGE_FIRST();
11461
Willy Tarreau15e91e12015-04-04 00:52:09 +020011462 txn = strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011463 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011464 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011465 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11466 ptr = ctx.line + ctx.val;
11467 len = ctx.vlen;
11468 while (len--)
11469 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11470 }
11471
11472 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011473 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 +000011474 beg = http_get_path(txn);
11475 if (!beg)
11476 beg = end;
11477
11478 for (ptr = beg; ptr < end ; ptr++);
11479
11480 if (beg < ptr && *beg == '/') {
11481 while (beg < ptr)
11482 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11483 }
11484 hash = full_hash(hash);
11485
11486 smp->type = SMP_T_UINT;
11487 smp->data.uint = hash;
11488 smp->flags = SMP_F_VOL_1ST;
11489 return 1;
11490}
11491
11492/* This concatenates the source address with the 32-bit hash of the Host and
11493 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11494 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11495 * on the source address length. The URL hash is stored before the address so
11496 * that in environments where IPv6 is insignificant, truncating the output to
11497 * 8 bytes would still work.
11498 */
11499static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011500smp_fetch_url32_src(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011501 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011502{
11503 struct chunk *temp;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020011504 struct connection *cli_conn = objt_conn(sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011505
Willy Tarreau192252e2015-04-04 01:47:55 +020011506 if (!smp_fetch_url32(px, sess, strm, opt, args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011507 return 0;
11508
11509 temp = get_trash_chunk();
11510 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11511 temp->len += sizeof(smp->data.uint);
11512
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011513 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011514 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011515 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011516 temp->len += 4;
11517 break;
11518 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011519 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011520 temp->len += 16;
11521 break;
11522 default:
11523 return 0;
11524 }
11525
11526 smp->data.str = *temp;
11527 smp->type = SMP_T_BIN;
11528 return 1;
11529}
11530
Willy Tarreau185b5c42012-04-26 15:11:51 +020011531/* This function is used to validate the arguments passed to any "hdr" fetch
11532 * keyword. These keywords support an optional positive or negative occurrence
11533 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11534 * is assumed that the types are already the correct ones. Returns 0 on error,
11535 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11536 * error message in case of error, that the caller is responsible for freeing.
11537 * The initial location must either be freeable or NULL.
11538 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011539int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011540{
11541 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011542 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011543 return 0;
11544 }
11545 return 1;
11546}
11547
Willy Tarreau276fae92013-07-25 14:36:01 +020011548/* takes an UINT value on input supposed to represent the time since EPOCH,
11549 * adds an optional offset found in args[0] and emits a string representing
11550 * the date in RFC-1123/5322 format.
11551 */
Willy Tarreau87b09662015-04-03 00:22:06 +020011552static int sample_conv_http_date(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011553 struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011554{
11555 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11556 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11557 struct chunk *temp;
11558 struct tm *tm;
11559 time_t curr_date = smp->data.uint;
11560
11561 /* add offset */
11562 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11563 curr_date += args[0].data.sint;
11564
11565 tm = gmtime(&curr_date);
11566
11567 temp = get_trash_chunk();
11568 temp->len = snprintf(temp->str, temp->size - temp->len,
11569 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11570 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11571 tm->tm_hour, tm->tm_min, tm->tm_sec);
11572
11573 smp->data.str = *temp;
11574 smp->type = SMP_T_STR;
11575 return 1;
11576}
11577
Thierry FOURNIERad903512014-04-11 17:51:01 +020011578/* Match language range with language tag. RFC2616 14.4:
11579 *
11580 * A language-range matches a language-tag if it exactly equals
11581 * the tag, or if it exactly equals a prefix of the tag such
11582 * that the first tag character following the prefix is "-".
11583 *
11584 * Return 1 if the strings match, else return 0.
11585 */
11586static inline int language_range_match(const char *range, int range_len,
11587 const char *tag, int tag_len)
11588{
11589 const char *end = range + range_len;
11590 const char *tend = tag + tag_len;
11591 while (range < end) {
11592 if (*range == '-' && tag == tend)
11593 return 1;
11594 if (*range != *tag || tag == tend)
11595 return 0;
11596 range++;
11597 tag++;
11598 }
11599 /* Return true only if the last char of the tag is matched. */
11600 return tag == tend;
11601}
11602
11603/* Arguments: The list of expected value, the number of parts returned and the separator */
Willy Tarreau87b09662015-04-03 00:22:06 +020011604static int sample_conv_q_prefered(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011605 struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011606{
11607 const char *al = smp->data.str.str;
11608 const char *end = al + smp->data.str.len;
11609 const char *token;
11610 int toklen;
11611 int qvalue;
11612 const char *str;
11613 const char *w;
11614 int best_q = 0;
11615
11616 /* Set the constant to the sample, because the output of the
11617 * function will be peek in the constant configuration string.
11618 */
11619 smp->flags |= SMP_F_CONST;
11620 smp->data.str.size = 0;
11621 smp->data.str.str = "";
11622 smp->data.str.len = 0;
11623
11624 /* Parse the accept language */
11625 while (1) {
11626
11627 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011628 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011629 al++;
11630 if (al >= end)
11631 break;
11632
11633 /* Start of the fisrt word. */
11634 token = al;
11635
11636 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011637 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011638 al++;
11639 if (al == token)
11640 goto expect_comma;
11641
11642 /* Length of the token. */
11643 toklen = al - token;
11644 qvalue = 1000;
11645
11646 /* Check if the token exists in the list. If the token not exists,
11647 * jump to the next token.
11648 */
11649 str = args[0].data.str.str;
11650 w = str;
11651 while (1) {
11652 if (*str == ';' || *str == '\0') {
11653 if (language_range_match(token, toklen, w, str-w))
11654 goto look_for_q;
11655 if (*str == '\0')
11656 goto expect_comma;
11657 w = str + 1;
11658 }
11659 str++;
11660 }
11661 goto expect_comma;
11662
11663look_for_q:
11664
11665 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011666 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011667 al++;
11668 if (al >= end)
11669 goto process_value;
11670
11671 /* If ',' is found, process the result */
11672 if (*al == ',')
11673 goto process_value;
11674
11675 /* If the character is different from ';', look
11676 * for the end of the header part in best effort.
11677 */
11678 if (*al != ';')
11679 goto expect_comma;
11680
11681 /* Assumes that the char is ';', now expect "q=". */
11682 al++;
11683
11684 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011685 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011686 al++;
11687 if (al >= end)
11688 goto process_value;
11689
11690 /* Expect 'q'. If no 'q', continue in best effort */
11691 if (*al != 'q')
11692 goto process_value;
11693 al++;
11694
11695 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011696 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011697 al++;
11698 if (al >= end)
11699 goto process_value;
11700
11701 /* Expect '='. If no '=', continue in best effort */
11702 if (*al != '=')
11703 goto process_value;
11704 al++;
11705
11706 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011707 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011708 al++;
11709 if (al >= end)
11710 goto process_value;
11711
11712 /* Parse the q value. */
11713 qvalue = parse_qvalue(al, &al);
11714
11715process_value:
11716
11717 /* If the new q value is the best q value, then store the associated
11718 * language in the response. If qvalue is the biggest value (1000),
11719 * break the process.
11720 */
11721 if (qvalue > best_q) {
11722 smp->data.str.str = (char *)w;
11723 smp->data.str.len = str - w;
11724 if (qvalue >= 1000)
11725 break;
11726 best_q = qvalue;
11727 }
11728
11729expect_comma:
11730
11731 /* Expect comma or end. If the end is detected, quit the loop. */
11732 while (al < end && *al != ',')
11733 al++;
11734 if (al >= end)
11735 break;
11736
11737 /* Comma is found, jump it and restart the analyzer. */
11738 al++;
11739 }
11740
11741 /* Set default value if required. */
11742 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11743 smp->data.str.str = args[1].data.str.str;
11744 smp->data.str.len = args[1].data.str.len;
11745 }
11746
11747 /* Return true only if a matching language was found. */
11748 return smp->data.str.len != 0;
11749}
11750
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011751/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011752 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011753 * the relevant part of the request line accordingly. Then it updates various
11754 * pointers to the next elements which were moved, and the total buffer length.
11755 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011756 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11757 * error, though this can be revisited when this code is finally exploited.
11758 *
11759 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11760 * query string and 3 to replace uri.
11761 *
11762 * In query string case, the mark question '?' must be set at the start of the
11763 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011764 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011765int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011766 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011767{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011768 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011769 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011770 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011771 int delta;
11772
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011773 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011774 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011775 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011776 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11777
11778 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011779 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011780 txn->req.sl.rq.m_l += delta;
11781 txn->req.sl.rq.u += delta;
11782 txn->req.sl.rq.v += delta;
11783 break;
11784
11785 case 1: // path
11786 cur_ptr = http_get_path(txn);
11787 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011788 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011789
11790 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011791 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 +010011792 cur_end++;
11793
11794 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011795 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011796 txn->req.sl.rq.u_l += delta;
11797 txn->req.sl.rq.v += delta;
11798 break;
11799
11800 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011801 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011802 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011803 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11804 while (cur_ptr < cur_end && *cur_ptr != '?')
11805 cur_ptr++;
11806
11807 /* skip the question mark or indicate that we must insert it
11808 * (but only if the format string is not empty then).
11809 */
11810 if (cur_ptr < cur_end)
11811 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011812 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011813 offset = 0;
11814
11815 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011816 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011817 txn->req.sl.rq.u_l += delta;
11818 txn->req.sl.rq.v += delta;
11819 break;
11820
11821 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011822 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011823 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11824
11825 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011826 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011827 txn->req.sl.rq.u_l += delta;
11828 txn->req.sl.rq.v += delta;
11829 break;
11830
11831 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011832 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011833 }
11834
11835 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011836 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011837 txn->req.sl.rq.l += delta;
11838 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011839 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011840 return 0;
11841}
11842
11843/* This function executes one of the set-{method,path,query,uri} actions. It
11844 * builds a string in the trash from the specified format string. It finds
11845 * the action to be performed in p[2], previously filled by function
11846 * parse_set_req_line(). The replacement action is excuted by the function
11847 * http_action_set_req_line_exec(). It always returns 1. If an error occurs
11848 * the action is canceled, but the rule processing continue.
11849 */
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011850int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011851{
11852 chunk_reset(&trash);
11853
11854 /* If we have to create a query string, prepare a '?'. */
11855 if (*(int *)&rule->arg.act.p[2] == 2)
11856 trash.str[trash.len++] = '?';
11857 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
11858
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011859 http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s);
Thierry FOURNIER01c30122015-03-14 14:14:47 +010011860 return 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011861}
11862
11863/* parse an http-request action among :
11864 * set-method
11865 * set-path
11866 * set-query
11867 * set-uri
11868 *
11869 * All of them accept a single argument of type string representing a log-format.
11870 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11871 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
11872 * It returns 0 on success, < 0 on error.
11873 */
11874int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
11875{
11876 int cur_arg = *orig_arg;
11877
11878 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
11879
11880 switch (args[0][4]) {
11881 case 'm' :
11882 *(int *)&rule->arg.act.p[2] = 0;
11883 rule->action_ptr = http_action_set_req_line;
11884 break;
11885 case 'p' :
11886 *(int *)&rule->arg.act.p[2] = 1;
11887 rule->action_ptr = http_action_set_req_line;
11888 break;
11889 case 'q' :
11890 *(int *)&rule->arg.act.p[2] = 2;
11891 rule->action_ptr = http_action_set_req_line;
11892 break;
11893 case 'u' :
11894 *(int *)&rule->arg.act.p[2] = 3;
11895 rule->action_ptr = http_action_set_req_line;
11896 break;
11897 default:
11898 memprintf(err, "internal error: unhandled action '%s'", args[0]);
11899 return -1;
11900 }
11901
11902 if (!*args[cur_arg] ||
11903 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11904 memprintf(err, "expects exactly 1 argument <format>");
11905 return -1;
11906 }
11907
11908 LIST_INIT((struct list *)&rule->arg.act.p[0]);
11909 proxy->conf.args.ctx = ARGC_HRQ;
11910 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
11911 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
11912 proxy->conf.args.file, proxy->conf.args.line);
11913
11914 (*orig_arg)++;
11915 return 0;
11916}
11917
William Lallemand73025dd2014-04-24 14:38:37 +020011918/*
11919 * Return the struct http_req_action_kw associated to a keyword.
11920 */
11921struct http_req_action_kw *action_http_req_custom(const char *kw)
11922{
11923 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11924 struct http_req_action_kw_list *kw_list;
11925 int i;
11926
11927 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11928 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11929 if (!strcmp(kw, kw_list->kw[i].kw))
11930 return &kw_list->kw[i];
11931 }
11932 }
11933 }
11934 return NULL;
11935}
11936
11937/*
11938 * Return the struct http_res_action_kw associated to a keyword.
11939 */
11940struct http_res_action_kw *action_http_res_custom(const char *kw)
11941{
11942 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11943 struct http_res_action_kw_list *kw_list;
11944 int i;
11945
11946 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11947 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11948 if (!strcmp(kw, kw_list->kw[i].kw))
11949 return &kw_list->kw[i];
11950 }
11951 }
11952 }
11953 return NULL;
11954}
11955
Willy Tarreau4a568972010-05-12 08:08:50 +020011956/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011957/* All supported ACL keywords must be declared here. */
11958/************************************************************************/
11959
11960/* Note: must not be declared <const> as its list will be overwritten.
11961 * Please take care of keeping this list alphabetically sorted.
11962 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011963static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011964 { "base", "base", PAT_MATCH_STR },
11965 { "base_beg", "base", PAT_MATCH_BEG },
11966 { "base_dir", "base", PAT_MATCH_DIR },
11967 { "base_dom", "base", PAT_MATCH_DOM },
11968 { "base_end", "base", PAT_MATCH_END },
11969 { "base_len", "base", PAT_MATCH_LEN },
11970 { "base_reg", "base", PAT_MATCH_REG },
11971 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011972
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011973 { "cook", "req.cook", PAT_MATCH_STR },
11974 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11975 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11976 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11977 { "cook_end", "req.cook", PAT_MATCH_END },
11978 { "cook_len", "req.cook", PAT_MATCH_LEN },
11979 { "cook_reg", "req.cook", PAT_MATCH_REG },
11980 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011981
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011982 { "hdr", "req.hdr", PAT_MATCH_STR },
11983 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11984 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11985 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11986 { "hdr_end", "req.hdr", PAT_MATCH_END },
11987 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11988 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11989 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011990
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011991 /* these two declarations uses strings with list storage (in place
11992 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11993 * and delete functions are relative to the list management. The parse
11994 * and match method are related to the corresponding fetch methods. This
11995 * is very particular ACL declaration mode.
11996 */
11997 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11998 { "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 +020011999
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012000 { "path", "path", PAT_MATCH_STR },
12001 { "path_beg", "path", PAT_MATCH_BEG },
12002 { "path_dir", "path", PAT_MATCH_DIR },
12003 { "path_dom", "path", PAT_MATCH_DOM },
12004 { "path_end", "path", PAT_MATCH_END },
12005 { "path_len", "path", PAT_MATCH_LEN },
12006 { "path_reg", "path", PAT_MATCH_REG },
12007 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012008
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012009 { "req_ver", "req.ver", PAT_MATCH_STR },
12010 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012011
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012012 { "scook", "res.cook", PAT_MATCH_STR },
12013 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12014 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12015 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12016 { "scook_end", "res.cook", PAT_MATCH_END },
12017 { "scook_len", "res.cook", PAT_MATCH_LEN },
12018 { "scook_reg", "res.cook", PAT_MATCH_REG },
12019 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012020
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012021 { "shdr", "res.hdr", PAT_MATCH_STR },
12022 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12023 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12024 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12025 { "shdr_end", "res.hdr", PAT_MATCH_END },
12026 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12027 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12028 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012029
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012030 { "url", "url", PAT_MATCH_STR },
12031 { "url_beg", "url", PAT_MATCH_BEG },
12032 { "url_dir", "url", PAT_MATCH_DIR },
12033 { "url_dom", "url", PAT_MATCH_DOM },
12034 { "url_end", "url", PAT_MATCH_END },
12035 { "url_len", "url", PAT_MATCH_LEN },
12036 { "url_reg", "url", PAT_MATCH_REG },
12037 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012038
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012039 { "urlp", "urlp", PAT_MATCH_STR },
12040 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12041 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12042 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12043 { "urlp_end", "urlp", PAT_MATCH_END },
12044 { "urlp_len", "urlp", PAT_MATCH_LEN },
12045 { "urlp_reg", "urlp", PAT_MATCH_REG },
12046 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012047
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012048 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012049}};
12050
12051/************************************************************************/
12052/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012053/************************************************************************/
12054/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012055static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012056 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012057 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12058 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12059
Willy Tarreau87b09662015-04-03 00:22:06 +020012060 /* capture are allocated and are permanent in the stream */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012061 { "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 +020012062
12063 /* retrieve these captures from the HTTP logs */
12064 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12065 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12066 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12067
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012068 { "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 +020012069 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012070
Willy Tarreau409bcde2013-01-08 00:31:00 +010012071 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12072 * are only here to match the ACL's name, are request-only and are used
12073 * for ACL compatibility only.
12074 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012075 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12076 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012077 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12078 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12079
12080 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12081 * only here to match the ACL's name, are request-only and are used for
12082 * ACL compatibility only.
12083 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012084 { "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 +010012085 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12086 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
12087 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12088
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012089 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012090 { "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 +010012091 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012092 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012093 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012094 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012095
12096 /* HTTP protocol on the request path */
12097 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012098 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012099
12100 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012101 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12102 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012103
12104 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012105 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12106 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012107
Willy Tarreau18ed2562013-01-14 15:56:36 +010012108 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012109 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012110 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12111 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12112
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012113 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012114 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012115 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012116 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12117 { "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 +010012118 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012119 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12120
12121 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012122 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012123 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12124 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12125
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012126 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012127 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012128 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012129 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12130 { "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 +010012131 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012132 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12133
Willy Tarreau409bcde2013-01-08 00:31:00 +010012134 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012135 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012136 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12137 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012138 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012139
12140 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012141 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012142 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12143 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
12144 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12145
12146 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012147 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012148 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12149 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012150 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
12151 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012152 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12153 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012154 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12155 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012156}};
12157
Willy Tarreau8797c062007-05-07 00:55:35 +020012158
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012159/************************************************************************/
12160/* All supported converter keywords must be declared here. */
12161/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012162/* Note: must not be declared <const> as its list will be overwritten */
12163static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020012164 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
12165 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012166 { NULL, NULL, 0, 0, 0 },
12167}};
12168
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012169/************************************************************************/
12170/* All supported http-request action keywords must be declared here. */
12171/************************************************************************/
12172struct http_req_action_kw_list http_req_actions = {
12173 .scope = "http",
12174 .kw = {
12175 { "set-method", parse_set_req_line },
12176 { "set-path", parse_set_req_line },
12177 { "set-query", parse_set_req_line },
12178 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012179 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012180 }
12181};
12182
Willy Tarreau8797c062007-05-07 00:55:35 +020012183__attribute__((constructor))
12184static void __http_protocol_init(void)
12185{
12186 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012187 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012188 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012189 http_req_keywords_register(&http_req_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012190}
12191
12192
Willy Tarreau58f10d72006-12-04 02:26:12 +010012193/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012194 * Local variables:
12195 * c-indent-level: 8
12196 * c-basic-offset: 8
12197 * End:
12198 */