blob: ad075a49f7663597f5ed2831ccade5e264107b87 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020064#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau522d6c02009-12-06 18:49:18 +010069const char HTTP_100[] =
70 "HTTP/1.1 100 Continue\r\n\r\n";
71
72const struct chunk http_100_chunk = {
73 .str = (char *)&HTTP_100,
74 .len = sizeof(HTTP_100)-1
75};
76
Willy Tarreaua9679ac2010-01-03 17:32:57 +010077/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040096
97/* same as 302 except that the browser MUST retry with the same method */
98const char *HTTP_307 =
99 "HTTP/1.1 307 Temporary Redirect\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Content-length: 0\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
104/* same as 301 except that the browser MUST retry with the same method */
105const char *HTTP_308 =
106 "HTTP/1.1 308 Permanent Redirect\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau844a7e72010-01-31 21:46:18 +0100120const char *HTTP_407_fmt =
121 "HTTP/1.0 407 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
124 "Content-Type: text/html\r\n"
125 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800127 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400134 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400136 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100137 [HTTP_ERR_500] = 500,
138 [HTTP_ERR_502] = 502,
139 [HTTP_ERR_503] = 503,
140 [HTTP_ERR_504] = 504,
141};
142
Willy Tarreau80587432006-12-24 17:47:20 +0100143static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200144 [HTTP_ERR_200] =
145 "HTTP/1.0 200 OK\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
151
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100153 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
159
160 [HTTP_ERR_403] =
161 "HTTP/1.0 403 Forbidden\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
167
CJ Ess108b1dd2015-04-07 12:03:37 -0400168 [HTTP_ERR_405] =
169 "HTTP/1.0 405 Method Not Allowed\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
175
Willy Tarreau0f772532006-12-23 20:51:41 +0100176 [HTTP_ERR_408] =
177 "HTTP/1.0 408 Request Time-out\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
183
CJ Ess108b1dd2015-04-07 12:03:37 -0400184 [HTTP_ERR_429] =
185 "HTTP/1.0 429 Too Many Requests\r\n"
186 "Cache-Control: no-cache\r\n"
187 "Connection: close\r\n"
188 "Content-Type: text/html\r\n"
189 "\r\n"
190 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
191
Willy Tarreau0f772532006-12-23 20:51:41 +0100192 [HTTP_ERR_500] =
193 "HTTP/1.0 500 Server Error\r\n"
194 "Cache-Control: no-cache\r\n"
195 "Connection: close\r\n"
196 "Content-Type: text/html\r\n"
197 "\r\n"
198 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
199
200 [HTTP_ERR_502] =
201 "HTTP/1.0 502 Bad Gateway\r\n"
202 "Cache-Control: no-cache\r\n"
203 "Connection: close\r\n"
204 "Content-Type: text/html\r\n"
205 "\r\n"
206 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
207
208 [HTTP_ERR_503] =
209 "HTTP/1.0 503 Service Unavailable\r\n"
210 "Cache-Control: no-cache\r\n"
211 "Connection: close\r\n"
212 "Content-Type: text/html\r\n"
213 "\r\n"
214 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
215
216 [HTTP_ERR_504] =
217 "HTTP/1.0 504 Gateway Time-out\r\n"
218 "Cache-Control: no-cache\r\n"
219 "Connection: close\r\n"
220 "Content-Type: text/html\r\n"
221 "\r\n"
222 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
223
224};
225
Cyril Bonté19979e12012-04-04 12:57:21 +0200226/* status codes available for the stats admin page (strictly 4 chars length) */
227const char *stat_status_codes[STAT_STATUS_SIZE] = {
228 [STAT_STATUS_DENY] = "DENY",
229 [STAT_STATUS_DONE] = "DONE",
230 [STAT_STATUS_ERRP] = "ERRP",
231 [STAT_STATUS_EXCD] = "EXCD",
232 [STAT_STATUS_NONE] = "NONE",
233 [STAT_STATUS_PART] = "PART",
234 [STAT_STATUS_UNKN] = "UNKN",
235};
236
237
William Lallemand73025dd2014-04-24 14:38:37 +0200238/* List head of all known action keywords for "http-request" */
239struct http_req_action_kw_list http_req_keywords = {
240 .list = LIST_HEAD_INIT(http_req_keywords.list)
241};
242
243/* List head of all known action keywords for "http-response" */
244struct http_res_action_kw_list http_res_keywords = {
245 .list = LIST_HEAD_INIT(http_res_keywords.list)
246};
247
Willy Tarreau80587432006-12-24 17:47:20 +0100248/* We must put the messages here since GCC cannot initialize consts depending
249 * on strlen().
250 */
251struct chunk http_err_chunks[HTTP_ERR_SIZE];
252
Willy Tarreaua890d072013-04-02 12:01:06 +0200253/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
254static struct hdr_ctx static_hdr_ctx;
255
Willy Tarreau42250582007-04-01 01:30:43 +0200256#define FD_SETS_ARE_BITFIELDS
257#ifdef FD_SETS_ARE_BITFIELDS
258/*
259 * This map is used with all the FD_* macros to check whether a particular bit
260 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
261 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
262 * byte should be encoded. Be careful to always pass bytes from 0 to 255
263 * exclusively to the macros.
264 */
265fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
266fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100267fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200268
269#else
270#error "Check if your OS uses bitfields for fd_sets"
271#endif
272
Willy Tarreau87b09662015-04-03 00:22:06 +0200273static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200274
Willy Tarreau80587432006-12-24 17:47:20 +0100275void init_proto_http()
276{
Willy Tarreau42250582007-04-01 01:30:43 +0200277 int i;
278 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100279 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200280
Willy Tarreau80587432006-12-24 17:47:20 +0100281 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
282 if (!http_err_msgs[msg]) {
283 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
284 abort();
285 }
286
287 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
288 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
289 }
Willy Tarreau42250582007-04-01 01:30:43 +0200290
291 /* initialize the log header encoding map : '{|}"#' should be encoded with
292 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
293 * URL encoding only requires '"', '#' to be encoded as well as non-
294 * printable characters above.
295 */
296 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
297 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100298 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200299 for (i = 0; i < 32; i++) {
300 FD_SET(i, hdr_encode_map);
301 FD_SET(i, url_encode_map);
302 }
303 for (i = 127; i < 256; i++) {
304 FD_SET(i, hdr_encode_map);
305 FD_SET(i, url_encode_map);
306 }
307
308 tmp = "\"#{|}";
309 while (*tmp) {
310 FD_SET(*tmp, hdr_encode_map);
311 tmp++;
312 }
313
314 tmp = "\"#";
315 while (*tmp) {
316 FD_SET(*tmp, url_encode_map);
317 tmp++;
318 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200319
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100320 /* initialize the http header encoding map. The draft httpbis define the
321 * header content as:
322 *
323 * HTTP-message = start-line
324 * *( header-field CRLF )
325 * CRLF
326 * [ message-body ]
327 * header-field = field-name ":" OWS field-value OWS
328 * field-value = *( field-content / obs-fold )
329 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
330 * obs-fold = CRLF 1*( SP / HTAB )
331 * field-vchar = VCHAR / obs-text
332 * VCHAR = %x21-7E
333 * obs-text = %x80-FF
334 *
335 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
336 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
337 * "obs-fold" is volontary forgotten because haproxy remove this.
338 */
339 memset(http_encode_map, 0, sizeof(http_encode_map));
340 for (i = 0x00; i <= 0x08; i++)
341 FD_SET(i, http_encode_map);
342 for (i = 0x0a; i <= 0x1f; i++)
343 FD_SET(i, http_encode_map);
344 FD_SET(0x7f, http_encode_map);
345
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200346 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200347 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200348 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100349 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100350}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351
Willy Tarreau53b6c742006-12-17 13:37:46 +0100352/*
353 * We have 26 list of methods (1 per first letter), each of which can have
354 * up to 3 entries (2 valid, 1 null).
355 */
356struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100357 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100358 int len;
359 const char text[8];
360};
361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100362const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100363 ['C' - 'A'] = {
364 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
365 },
366 ['D' - 'A'] = {
367 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
368 },
369 ['G' - 'A'] = {
370 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
371 },
372 ['H' - 'A'] = {
373 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
374 },
375 ['P' - 'A'] = {
376 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
377 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
378 },
379 ['T' - 'A'] = {
380 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
381 },
382 /* rest is empty like this :
383 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
384 */
385};
386
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100387const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
388 [HTTP_METH_NONE] = { "", 0 },
389 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
390 [HTTP_METH_GET] = { "GET", 3 },
391 [HTTP_METH_HEAD] = { "HEAD", 4 },
392 [HTTP_METH_POST] = { "POST", 4 },
393 [HTTP_METH_PUT] = { "PUT", 3 },
394 [HTTP_METH_DELETE] = { "DELETE", 6 },
395 [HTTP_METH_TRACE] = { "TRACE", 5 },
396 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
397};
398
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100399/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200400 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100401 * RFC2616 for those chars.
402 */
403
404const char http_is_spht[256] = {
405 [' '] = 1, ['\t'] = 1,
406};
407
408const char http_is_crlf[256] = {
409 ['\r'] = 1, ['\n'] = 1,
410};
411
412const char http_is_lws[256] = {
413 [' '] = 1, ['\t'] = 1,
414 ['\r'] = 1, ['\n'] = 1,
415};
416
417const char http_is_sep[256] = {
418 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
419 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
420 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
421 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
422 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
423};
424
425const char http_is_ctl[256] = {
426 [0 ... 31] = 1,
427 [127] = 1,
428};
429
430/*
431 * A token is any ASCII char that is neither a separator nor a CTL char.
432 * Do not overwrite values in assignment since gcc-2.95 will not handle
433 * them correctly. Instead, define every non-CTL char's status.
434 */
435const char http_is_token[256] = {
436 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
437 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
438 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
439 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
440 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
441 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
442 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
443 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
444 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
445 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
446 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
447 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
448 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
449 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
450 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
451 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
452 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
453 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
454 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
455 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
456 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
457 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
458 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
459 ['|'] = 1, ['}'] = 0, ['~'] = 1,
460};
461
462
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100463/*
464 * An http ver_token is any ASCII which can be found in an HTTP version,
465 * which includes 'H', 'T', 'P', '/', '.' and any digit.
466 */
467const char http_is_ver_token[256] = {
468 ['.'] = 1, ['/'] = 1,
469 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
470 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100471 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100472};
473
474
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100475/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100476 * Adds a header and its CRLF at the tail of the message's buffer, just before
477 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100478 * The header is also automatically added to the index <hdr_idx>, and the end
479 * of headers is automatically adjusted. The number of bytes added is returned
480 * on success, otherwise <0 is returned indicating an error.
481 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100482int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100483{
484 int bytes, len;
485
486 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200487 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100488 if (!bytes)
489 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100490 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100491 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
492}
493
494/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100495 * Adds a header and its CRLF at the tail of the message's buffer, just before
496 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100497 * the buffer is only opened and the space reserved, but nothing is copied.
498 * The header is also automatically added to the index <hdr_idx>, and the end
499 * of headers is automatically adjusted. The number of bytes added is returned
500 * on success, otherwise <0 is returned indicating an error.
501 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100502int http_header_add_tail2(struct http_msg *msg,
503 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100504{
505 int bytes;
506
Willy Tarreau9b28e032012-10-12 23:49:43 +0200507 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100508 if (!bytes)
509 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100510 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100511 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
512}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200513
514/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100515 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
516 * If so, returns the position of the first non-space character relative to
517 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
518 * to return a pointer to the place after the first space. Returns 0 if the
519 * header name does not match. Checks are case-insensitive.
520 */
521int http_header_match2(const char *hdr, const char *end,
522 const char *name, int len)
523{
524 const char *val;
525
526 if (hdr + len >= end)
527 return 0;
528 if (hdr[len] != ':')
529 return 0;
530 if (strncasecmp(hdr, name, len) != 0)
531 return 0;
532 val = hdr + len + 1;
533 while (val < end && HTTP_IS_SPHT(*val))
534 val++;
535 if ((val >= end) && (len + 2 <= end - hdr))
536 return len + 2; /* we may replace starting from second space */
537 return val - hdr;
538}
539
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200540/* Find the first or next occurrence of header <name> in message buffer <sol>
541 * using headers index <idx>, and return it in the <ctx> structure. This
542 * structure holds everything necessary to use the header and find next
543 * occurrence. If its <idx> member is 0, the header is searched from the
544 * beginning. Otherwise, the next occurrence is returned. The function returns
545 * 1 when it finds a value, and 0 when there is no more. It is very similar to
546 * http_find_header2() except that it is designed to work with full-line headers
547 * whose comma is not a delimiter but is part of the syntax. As a special case,
548 * if ctx->val is NULL when searching for a new values of a header, the current
549 * header is rescanned. This allows rescanning after a header deletion.
550 */
551int http_find_full_header2(const char *name, int len,
552 char *sol, struct hdr_idx *idx,
553 struct hdr_ctx *ctx)
554{
555 char *eol, *sov;
556 int cur_idx, old_idx;
557
558 cur_idx = ctx->idx;
559 if (cur_idx) {
560 /* We have previously returned a header, let's search another one */
561 sol = ctx->line;
562 eol = sol + idx->v[cur_idx].len;
563 goto next_hdr;
564 }
565
566 /* first request for this header */
567 sol += hdr_idx_first_pos(idx);
568 old_idx = 0;
569 cur_idx = hdr_idx_first_idx(idx);
570 while (cur_idx) {
571 eol = sol + idx->v[cur_idx].len;
572
573 if (len == 0) {
574 /* No argument was passed, we want any header.
575 * To achieve this, we simply build a fake request. */
576 while (sol + len < eol && sol[len] != ':')
577 len++;
578 name = sol;
579 }
580
581 if ((len < eol - sol) &&
582 (sol[len] == ':') &&
583 (strncasecmp(sol, name, len) == 0)) {
584 ctx->del = len;
585 sov = sol + len + 1;
586 while (sov < eol && http_is_lws[(unsigned char)*sov])
587 sov++;
588
589 ctx->line = sol;
590 ctx->prev = old_idx;
591 ctx->idx = cur_idx;
592 ctx->val = sov - sol;
593 ctx->tws = 0;
594 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
595 eol--;
596 ctx->tws++;
597 }
598 ctx->vlen = eol - sov;
599 return 1;
600 }
601 next_hdr:
602 sol = eol + idx->v[cur_idx].cr + 1;
603 old_idx = cur_idx;
604 cur_idx = idx->v[cur_idx].next;
605 }
606 return 0;
607}
608
Willy Tarreauc90dc232015-02-20 13:51:36 +0100609/* Find the first or next header field in message buffer <sol> using headers
610 * index <idx>, and return it in the <ctx> structure. This structure holds
611 * everything necessary to use the header and find next occurrence. If its
612 * <idx> member is 0, the first header is retrieved. Otherwise, the next
613 * occurrence is returned. The function returns 1 when it finds a value, and
614 * 0 when there is no more. It is equivalent to http_find_full_header2() with
615 * no header name.
616 */
617int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
618{
619 char *eol, *sov;
620 int cur_idx, old_idx;
621 int len;
622
623 cur_idx = ctx->idx;
624 if (cur_idx) {
625 /* We have previously returned a header, let's search another one */
626 sol = ctx->line;
627 eol = sol + idx->v[cur_idx].len;
628 goto next_hdr;
629 }
630
631 /* first request for this header */
632 sol += hdr_idx_first_pos(idx);
633 old_idx = 0;
634 cur_idx = hdr_idx_first_idx(idx);
635 while (cur_idx) {
636 eol = sol + idx->v[cur_idx].len;
637
638 len = 0;
639 while (1) {
640 if (len >= eol - sol)
641 goto next_hdr;
642 if (sol[len] == ':')
643 break;
644 len++;
645 }
646
647 ctx->del = len;
648 sov = sol + len + 1;
649 while (sov < eol && http_is_lws[(unsigned char)*sov])
650 sov++;
651
652 ctx->line = sol;
653 ctx->prev = old_idx;
654 ctx->idx = cur_idx;
655 ctx->val = sov - sol;
656 ctx->tws = 0;
657
658 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
659 eol--;
660 ctx->tws++;
661 }
662 ctx->vlen = eol - sov;
663 return 1;
664
665 next_hdr:
666 sol = eol + idx->v[cur_idx].cr + 1;
667 old_idx = cur_idx;
668 cur_idx = idx->v[cur_idx].next;
669 }
670 return 0;
671}
672
Willy Tarreau68085d82010-01-18 14:54:04 +0100673/* Find the end of the header value contained between <s> and <e>. See RFC2616,
674 * par 2.2 for more information. Note that it requires a valid header to return
675 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200676 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100677char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200678{
679 int quoted, qdpair;
680
681 quoted = qdpair = 0;
682 for (; s < e; s++) {
683 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200684 else if (quoted) {
685 if (*s == '\\') qdpair = 1;
686 else if (*s == '"') quoted = 0;
687 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200688 else if (*s == '"') quoted = 1;
689 else if (*s == ',') return s;
690 }
691 return s;
692}
693
694/* Find the first or next occurrence of header <name> in message buffer <sol>
695 * using headers index <idx>, and return it in the <ctx> structure. This
696 * structure holds everything necessary to use the header and find next
697 * occurrence. If its <idx> member is 0, the header is searched from the
698 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100699 * 1 when it finds a value, and 0 when there is no more. It is designed to work
700 * with headers defined as comma-separated lists. As a special case, if ctx->val
701 * is NULL when searching for a new values of a header, the current header is
702 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200703 */
704int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100705 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200706 struct hdr_ctx *ctx)
707{
Willy Tarreau68085d82010-01-18 14:54:04 +0100708 char *eol, *sov;
709 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200710
Willy Tarreau68085d82010-01-18 14:54:04 +0100711 cur_idx = ctx->idx;
712 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200713 /* We have previously returned a value, let's search
714 * another one on the same line.
715 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200716 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200717 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100718 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200719 eol = sol + idx->v[cur_idx].len;
720
721 if (sov >= eol)
722 /* no more values in this header */
723 goto next_hdr;
724
Willy Tarreau68085d82010-01-18 14:54:04 +0100725 /* values remaining for this header, skip the comma but save it
726 * for later use (eg: for header deletion).
727 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200728 sov++;
729 while (sov < eol && http_is_lws[(unsigned char)*sov])
730 sov++;
731
732 goto return_hdr;
733 }
734
735 /* first request for this header */
736 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100737 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200738 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200739 while (cur_idx) {
740 eol = sol + idx->v[cur_idx].len;
741
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200742 if (len == 0) {
743 /* No argument was passed, we want any header.
744 * To achieve this, we simply build a fake request. */
745 while (sol + len < eol && sol[len] != ':')
746 len++;
747 name = sol;
748 }
749
Willy Tarreau33a7e692007-06-10 19:45:56 +0200750 if ((len < eol - sol) &&
751 (sol[len] == ':') &&
752 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100753 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200754 sov = sol + len + 1;
755 while (sov < eol && http_is_lws[(unsigned char)*sov])
756 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100757
Willy Tarreau33a7e692007-06-10 19:45:56 +0200758 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100759 ctx->prev = old_idx;
760 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200761 ctx->idx = cur_idx;
762 ctx->val = sov - sol;
763
764 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200765 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200766 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200767 eol--;
768 ctx->tws++;
769 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200770 ctx->vlen = eol - sov;
771 return 1;
772 }
773 next_hdr:
774 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100775 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200776 cur_idx = idx->v[cur_idx].next;
777 }
778 return 0;
779}
780
781int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100782 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200783 struct hdr_ctx *ctx)
784{
785 return http_find_header2(name, strlen(name), sol, idx, ctx);
786}
787
Willy Tarreau68085d82010-01-18 14:54:04 +0100788/* Remove one value of a header. This only works on a <ctx> returned by one of
789 * the http_find_header functions. The value is removed, as well as surrounding
790 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100791 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100792 * message <msg>. The new index is returned. If it is zero, it means there is
793 * no more header, so any processing may stop. The ctx is always left in a form
794 * that can be handled by http_find_header2() to find next occurrence.
795 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100796int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100797{
798 int cur_idx = ctx->idx;
799 char *sol = ctx->line;
800 struct hdr_idx_elem *hdr;
801 int delta, skip_comma;
802
803 if (!cur_idx)
804 return 0;
805
806 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200807 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100808 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200809 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100810 http_msg_move_end(msg, delta);
811 idx->used--;
812 hdr->len = 0; /* unused entry */
813 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100814 if (idx->tail == ctx->idx)
815 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100816 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100817 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100818 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200819 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100820 return ctx->idx;
821 }
822
823 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200824 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
825 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100826 */
827
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200828 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200829 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200830 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100831 NULL, 0);
832 hdr->len += delta;
833 http_msg_move_end(msg, delta);
834 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200835 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100836 return ctx->idx;
837}
838
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100839/* This function handles a server error at the stream interface level. The
840 * stream interface is assumed to be already in a closed state. An optional
841 * message is copied into the input buffer, and an HTTP status code stored.
842 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100843 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200845static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100846 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100848 channel_auto_read(si_oc(si));
849 channel_abort(si_oc(si));
850 channel_auto_close(si_oc(si));
851 channel_erase(si_oc(si));
852 channel_auto_close(si_ic(si));
853 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100854 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200855 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100856 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200857 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200858 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200859 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200860 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200861 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200862}
863
Willy Tarreau87b09662015-04-03 00:22:06 +0200864/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100865 * and message.
866 */
867
Willy Tarreau87b09662015-04-03 00:22:06 +0200868struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100869{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200870 if (s->be->errmsg[msgnum].str)
871 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200872 else if (strm_fe(s)->errmsg[msgnum].str)
873 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100874 else
875 return &http_err_chunks[msgnum];
876}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200877
Willy Tarreau53b6c742006-12-17 13:37:46 +0100878/*
879 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
880 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
881 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100882enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100883{
884 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100885 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100886
887 m = ((unsigned)*str - 'A');
888
889 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100890 for (h = http_methods[m]; h->len > 0; h++) {
891 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100892 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100893 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100894 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100895 };
896 return HTTP_METH_OTHER;
897 }
898 return HTTP_METH_NONE;
899
900}
901
Willy Tarreau21d2af32008-02-14 20:25:24 +0100902/* Parse the URI from the given transaction (which is assumed to be in request
903 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
904 * It is returned otherwise.
905 */
906static char *
907http_get_path(struct http_txn *txn)
908{
909 char *ptr, *end;
910
Willy Tarreau9b28e032012-10-12 23:49:43 +0200911 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100912 end = ptr + txn->req.sl.rq.u_l;
913
914 if (ptr >= end)
915 return NULL;
916
917 /* RFC2616, par. 5.1.2 :
918 * Request-URI = "*" | absuri | abspath | authority
919 */
920
921 if (*ptr == '*')
922 return NULL;
923
924 if (isalpha((unsigned char)*ptr)) {
925 /* this is a scheme as described by RFC3986, par. 3.1 */
926 ptr++;
927 while (ptr < end &&
928 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
929 ptr++;
930 /* skip '://' */
931 if (ptr == end || *ptr++ != ':')
932 return NULL;
933 if (ptr == end || *ptr++ != '/')
934 return NULL;
935 if (ptr == end || *ptr++ != '/')
936 return NULL;
937 }
938 /* skip [user[:passwd]@]host[:[port]] */
939
940 while (ptr < end && *ptr != '/')
941 ptr++;
942
943 if (ptr == end)
944 return NULL;
945
946 /* OK, we got the '/' ! */
947 return ptr;
948}
949
William Lallemand65ad6e12014-01-31 15:08:02 +0100950/* Parse the URI from the given string and look for the "/" beginning the PATH.
951 * If not found, return NULL. It is returned otherwise.
952 */
953static char *
954http_get_path_from_string(char *str)
955{
956 char *ptr = str;
957
958 /* RFC2616, par. 5.1.2 :
959 * Request-URI = "*" | absuri | abspath | authority
960 */
961
962 if (*ptr == '*')
963 return NULL;
964
965 if (isalpha((unsigned char)*ptr)) {
966 /* this is a scheme as described by RFC3986, par. 3.1 */
967 ptr++;
968 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
969 ptr++;
970 /* skip '://' */
971 if (*ptr == '\0' || *ptr++ != ':')
972 return NULL;
973 if (*ptr == '\0' || *ptr++ != '/')
974 return NULL;
975 if (*ptr == '\0' || *ptr++ != '/')
976 return NULL;
977 }
978 /* skip [user[:passwd]@]host[:[port]] */
979
980 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
981 ptr++;
982
983 if (*ptr == '\0' || *ptr == ' ')
984 return NULL;
985
986 /* OK, we got the '/' ! */
987 return ptr;
988}
989
Willy Tarreau71241ab2012-12-27 11:30:54 +0100990/* Returns a 302 for a redirectable request that reaches a server working in
991 * in redirect mode. This may only be called just after the stream interface
992 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
993 * follow normal proxy processing. NOTE: this function is designed to support
994 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100995 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200996void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100997{
998 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100999 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001000 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001001 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001002
1003 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001004 trash.len = strlen(HTTP_302);
1005 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001007 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001008
Willy Tarreauefb453c2008-10-26 20:49:47 +01001009 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001010 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001011 return;
1012
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001013 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001014 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001015 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1016 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001017 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001018
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001019 /* 3: add the request URI. Since it was already forwarded, we need
1020 * to temporarily rewind the buffer.
1021 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001022 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001023 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001024
Willy Tarreauefb453c2008-10-26 20:49:47 +01001025 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001026 len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001027
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001028 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001029
Willy Tarreauefb453c2008-10-26 20:49:47 +01001030 if (!path)
1031 return;
1032
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001033 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001034 return;
1035
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001036 memcpy(trash.str + trash.len, path, len);
1037 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001038
1039 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001040 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1041 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001042 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001043 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1044 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001045 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001046
1047 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001048 si_shutr(si);
1049 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001050 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001051 si->state = SI_ST_CLO;
1052
1053 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001054 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001055
1056 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001057 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001058 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001059}
1060
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001061/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001062 * that the server side is closed. Note that err_type is actually a
1063 * bitmask, where almost only aborts may be cumulated with other
1064 * values. We consider that aborted operations are more important
1065 * than timeouts or errors due to the fact that nobody else in the
1066 * logs might explain incomplete retries. All others should avoid
1067 * being cumulated. It should normally not be possible to have multiple
1068 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001069 * Note that connection errors appearing on the second request of a keep-alive
1070 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001071 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001072void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001073{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001074 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001075
1076 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001077 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001078 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001079 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001080 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001081 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001082 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001083 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001084 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001085 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001087 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001088 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001089 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001090 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001091 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001092 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001093 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001094 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1095 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001096 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001097 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001098 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001099 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001100 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001101 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001102 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001103 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001104}
1105
Willy Tarreau42250582007-04-01 01:30:43 +02001106extern const char sess_term_cond[8];
1107extern const char sess_fin_state[8];
1108extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001109struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001110struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001111struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001112struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113
Willy Tarreau117f59e2007-03-04 18:17:17 +01001114/*
1115 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001116 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001117 */
1118void capture_headers(char *som, struct hdr_idx *idx,
1119 char **cap, struct cap_hdr *cap_hdr)
1120{
1121 char *eol, *sol, *col, *sov;
1122 int cur_idx;
1123 struct cap_hdr *h;
1124 int len;
1125
1126 sol = som + hdr_idx_first_pos(idx);
1127 cur_idx = hdr_idx_first_idx(idx);
1128
1129 while (cur_idx) {
1130 eol = sol + idx->v[cur_idx].len;
1131
1132 col = sol;
1133 while (col < eol && *col != ':')
1134 col++;
1135
1136 sov = col + 1;
1137 while (sov < eol && http_is_lws[(unsigned char)*sov])
1138 sov++;
1139
1140 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001141 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001142 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1143 if (cap[h->index] == NULL)
1144 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001145 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001146
1147 if (cap[h->index] == NULL) {
1148 Alert("HTTP capture : out of memory.\n");
1149 continue;
1150 }
1151
1152 len = eol - sov;
1153 if (len > h->len)
1154 len = h->len;
1155
1156 memcpy(cap[h->index], sov, len);
1157 cap[h->index][len]=0;
1158 }
1159 }
1160 sol = eol + idx->v[cur_idx].cr + 1;
1161 cur_idx = idx->v[cur_idx].next;
1162 }
1163}
1164
1165
Willy Tarreau42250582007-04-01 01:30:43 +02001166/* either we find an LF at <ptr> or we jump to <bad>.
1167 */
1168#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1169
1170/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1171 * otherwise to <http_msg_ood> with <state> set to <st>.
1172 */
1173#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1174 ptr++; \
1175 if (likely(ptr < end)) \
1176 goto good; \
1177 else { \
1178 state = (st); \
1179 goto http_msg_ood; \
1180 } \
1181 } while (0)
1182
1183
Willy Tarreaubaaee002006-06-26 02:48:02 +02001184/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001185 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001186 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1187 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1188 * will give undefined results.
1189 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1190 * and that msg->sol points to the beginning of the response.
1191 * If a complete line is found (which implies that at least one CR or LF is
1192 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1193 * returned indicating an incomplete line (which does not mean that parts have
1194 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1195 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1196 * upon next call.
1197 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001198 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001199 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1200 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001201 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001202 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001203const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001204 enum ht_state state, const char *ptr, const char *end,
1205 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001206{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001207 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001208
Willy Tarreau8973c702007-01-21 23:58:29 +01001209 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001210 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001211 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001212 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001213 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1214
1215 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001216 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001217 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1218 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001219 state = HTTP_MSG_ERROR;
1220 break;
1221
Willy Tarreau8973c702007-01-21 23:58:29 +01001222 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001223 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001224 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001225 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001226 goto http_msg_rpcode;
1227 }
1228 if (likely(HTTP_IS_SPHT(*ptr)))
1229 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1230 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001231 state = HTTP_MSG_ERROR;
1232 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001233
Willy Tarreau8973c702007-01-21 23:58:29 +01001234 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001235 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001236 if (likely(!HTTP_IS_LWS(*ptr)))
1237 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1238
1239 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001240 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001241 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1242 }
1243
1244 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001245 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001246 http_msg_rsp_reason:
1247 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001248 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001249 msg->sl.st.r_l = 0;
1250 goto http_msg_rpline_eol;
1251
Willy Tarreau8973c702007-01-21 23:58:29 +01001252 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001253 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001254 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001255 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001256 goto http_msg_rpreason;
1257 }
1258 if (likely(HTTP_IS_SPHT(*ptr)))
1259 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1260 /* so it's a CR/LF, so there is no reason phrase */
1261 goto http_msg_rsp_reason;
1262
Willy Tarreau8973c702007-01-21 23:58:29 +01001263 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001264 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001265 if (likely(!HTTP_IS_CRLF(*ptr)))
1266 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001267 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 http_msg_rpline_eol:
1269 /* We have seen the end of line. Note that we do not
1270 * necessarily have the \n yet, but at least we know that we
1271 * have EITHER \r OR \n, otherwise the response would not be
1272 * complete. We can then record the response length and return
1273 * to the caller which will be able to register it.
1274 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001275 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 return ptr;
1277
Willy Tarreau8973c702007-01-21 23:58:29 +01001278 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001279#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001280 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1281 exit(1);
1282#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001283 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 }
1285
1286 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001287 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001288 if (ret_state)
1289 *ret_state = state;
1290 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001291 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001292 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001293}
1294
Willy Tarreau8973c702007-01-21 23:58:29 +01001295/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001296 * This function parses a request line between <ptr> and <end>, starting with
1297 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1298 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1299 * will give undefined results.
1300 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1301 * and that msg->sol points to the beginning of the request.
1302 * If a complete line is found (which implies that at least one CR or LF is
1303 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1304 * returned indicating an incomplete line (which does not mean that parts have
1305 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1306 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1307 * upon next call.
1308 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001309 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1311 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001312 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001313 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001314const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001315 enum ht_state state, const char *ptr, const char *end,
1316 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001317{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001318 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001322 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001323 if (likely(HTTP_IS_TOKEN(*ptr)))
1324 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001327 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1329 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001330
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 if (likely(HTTP_IS_CRLF(*ptr))) {
1332 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001333 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001334 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001335 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001336 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001337 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001338 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001339 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 msg->sl.rq.v_l = 0;
1341 goto http_msg_rqline_eol;
1342 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001343 state = HTTP_MSG_ERROR;
1344 break;
1345
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001346 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001347 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001348 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001349 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 goto http_msg_rquri;
1351 }
1352 if (likely(HTTP_IS_SPHT(*ptr)))
1353 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1354 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1355 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001357 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001358 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001359 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001363 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1365 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001366
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001367 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001368 /* non-ASCII chars are forbidden unless option
1369 * accept-invalid-http-request is enabled in the frontend.
1370 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001371 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001372 if (msg->err_pos < -1)
1373 goto invalid_char;
1374 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001375 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001376 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1377 }
1378
1379 if (likely(HTTP_IS_CRLF(*ptr))) {
1380 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1381 goto http_msg_req09_uri_e;
1382 }
1383
1384 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001385 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001386 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001387 state = HTTP_MSG_ERROR;
1388 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001391 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001393 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 goto http_msg_rqver;
1395 }
1396 if (likely(HTTP_IS_SPHT(*ptr)))
1397 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1398 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1399 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001402 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001403 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001405
1406 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001407 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001408 http_msg_rqline_eol:
1409 /* We have seen the end of line. Note that we do not
1410 * necessarily have the \n yet, but at least we know that we
1411 * have EITHER \r OR \n, otherwise the request would not be
1412 * complete. We can then record the request length and return
1413 * to the caller which will be able to register it.
1414 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001415 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001416 return ptr;
1417 }
1418
1419 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001420 state = HTTP_MSG_ERROR;
1421 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001424#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1426 exit(1);
1427#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001428 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001430
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001432 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 if (ret_state)
1434 *ret_state = state;
1435 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001436 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001439
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001440/*
1441 * Returns the data from Authorization header. Function may be called more
1442 * than once so data is stored in txn->auth_data. When no header is found
1443 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001444 * searching again for something we are unable to find anyway. However, if
1445 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001446 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001447 */
1448
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001449/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1450 * set according to global.tune.bufsize.
1451 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001452char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001453
1454int
Willy Tarreau87b09662015-04-03 00:22:06 +02001455get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001456{
1457
Willy Tarreaueee5b512015-04-03 23:46:31 +02001458 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001459 struct chunk auth_method;
1460 struct hdr_ctx ctx;
1461 char *h, *p;
1462 int len;
1463
1464#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001465 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001466#endif
1467
1468 if (txn->auth.method == HTTP_AUTH_WRONG)
1469 return 0;
1470
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001471 txn->auth.method = HTTP_AUTH_WRONG;
1472
1473 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001474
1475 if (txn->flags & TX_USE_PX_CONN) {
1476 h = "Proxy-Authorization";
1477 len = strlen(h);
1478 } else {
1479 h = "Authorization";
1480 len = strlen(h);
1481 }
1482
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001483 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001484 return 0;
1485
1486 h = ctx.line + ctx.val;
1487
1488 p = memchr(h, ' ', ctx.vlen);
1489 if (!p || p == h)
1490 return 0;
1491
1492 chunk_initlen(&auth_method, h, 0, p-h);
1493 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1494
1495 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1496
1497 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001498 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001499
1500 if (len < 0)
1501 return 0;
1502
1503
1504 get_http_auth_buff[len] = '\0';
1505
1506 p = strchr(get_http_auth_buff, ':');
1507
1508 if (!p)
1509 return 0;
1510
1511 txn->auth.user = get_http_auth_buff;
1512 *p = '\0';
1513 txn->auth.pass = p+1;
1514
1515 txn->auth.method = HTTP_AUTH_BASIC;
1516 return 1;
1517 }
1518
1519 return 0;
1520}
1521
Willy Tarreau58f10d72006-12-04 02:26:12 +01001522
Willy Tarreau8973c702007-01-21 23:58:29 +01001523/*
1524 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001525 * depending on the initial msg->msg_state. The caller is responsible for
1526 * ensuring that the message does not wrap. The function can be preempted
1527 * everywhere when data are missing and recalled at the exact same location
1528 * with no information loss. The message may even be realigned between two
1529 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001530 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001531 * fields. Note that msg->sol will be initialized after completing the first
1532 * state, so that none of the msg pointers has to be initialized prior to the
1533 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001534 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001535void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536{
Willy Tarreau3770f232013-12-07 00:01:53 +01001537 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001539 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001540
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001541 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001542 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001543 ptr = buf->p + msg->next;
1544 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 if (unlikely(ptr >= end))
1547 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001548
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001550 /*
1551 * First, states that are specific to the response only.
1552 * We check them first so that request and headers are
1553 * closer to each other (accessed more often).
1554 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001555 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001556 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001557 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001558 /* we have a start of message, but we have to check
1559 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001560 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001561 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001562 if (unlikely(ptr != buf->p)) {
1563 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001564 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001565 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001566 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001567 }
Willy Tarreau26927362012-05-18 23:22:52 +02001568 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001569 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001570 hdr_idx_init(idx);
1571 state = HTTP_MSG_RPVER;
1572 goto http_msg_rpver;
1573 }
1574
1575 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1576 goto http_msg_invalid;
1577
1578 if (unlikely(*ptr == '\n'))
1579 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1580 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1581 /* stop here */
1582
Willy Tarreau8973c702007-01-21 23:58:29 +01001583 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001584 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001585 EXPECT_LF_HERE(ptr, http_msg_invalid);
1586 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1587 /* stop here */
1588
Willy Tarreau8973c702007-01-21 23:58:29 +01001589 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001590 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001591 case HTTP_MSG_RPVER_SP:
1592 case HTTP_MSG_RPCODE:
1593 case HTTP_MSG_RPCODE_SP:
1594 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001595 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001596 state, ptr, end,
1597 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001598 if (unlikely(!ptr))
1599 return;
1600
1601 /* we have a full response and we know that we have either a CR
1602 * or an LF at <ptr>.
1603 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001604 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1605
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001606 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001607 if (likely(*ptr == '\r'))
1608 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1609 goto http_msg_rpline_end;
1610
Willy Tarreau8973c702007-01-21 23:58:29 +01001611 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001612 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001613 /* msg->sol must point to the first of CR or LF. */
1614 EXPECT_LF_HERE(ptr, http_msg_invalid);
1615 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1616 /* stop here */
1617
1618 /*
1619 * Second, states that are specific to the request only
1620 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001622 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001624 /* we have a start of message, but we have to check
1625 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001626 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001627 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001628 if (likely(ptr != buf->p)) {
1629 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001630 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001631 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001632 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001633 }
Willy Tarreau26927362012-05-18 23:22:52 +02001634 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001635 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001636 state = HTTP_MSG_RQMETH;
1637 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1641 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 if (unlikely(*ptr == '\n'))
1644 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1645 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001646 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001648 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001649 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001650 EXPECT_LF_HERE(ptr, http_msg_invalid);
1651 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001652 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001655 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 case HTTP_MSG_RQMETH_SP:
1657 case HTTP_MSG_RQURI:
1658 case HTTP_MSG_RQURI_SP:
1659 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001660 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001661 state, ptr, end,
1662 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 if (unlikely(!ptr))
1664 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 /* we have a full request and we know that we have either a CR
1667 * or an LF at <ptr>.
1668 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001670
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001671 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001672 if (likely(*ptr == '\r'))
1673 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001675
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001677 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001678 /* check for HTTP/0.9 request : no version information available.
1679 * msg->sol must point to the first of CR or LF.
1680 */
1681 if (unlikely(msg->sl.rq.v_l == 0))
1682 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001683
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 EXPECT_LF_HERE(ptr, http_msg_invalid);
1685 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001686 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001687
Willy Tarreau8973c702007-01-21 23:58:29 +01001688 /*
1689 * Common states below
1690 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001691 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001692 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001693 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 if (likely(!HTTP_IS_CRLF(*ptr))) {
1695 goto http_msg_hdr_name;
1696 }
1697
1698 if (likely(*ptr == '\r'))
1699 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1700 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001701
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001702 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001703 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 /* assumes msg->sol points to the first char */
1705 if (likely(HTTP_IS_TOKEN(*ptr)))
1706 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001707
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001708 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001710
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001711 if (likely(msg->err_pos < -1) || *ptr == '\n')
1712 goto http_msg_invalid;
1713
1714 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001715 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001716
1717 /* and we still accept this non-token character */
1718 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001719
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001721 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001722 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 if (likely(HTTP_IS_SPHT(*ptr)))
1724 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001725
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001727 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001728
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 if (likely(!HTTP_IS_CRLF(*ptr))) {
1730 goto http_msg_hdr_val;
1731 }
1732
1733 if (likely(*ptr == '\r'))
1734 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1735 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001736
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001737 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001738 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 EXPECT_LF_HERE(ptr, http_msg_invalid);
1740 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001741
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001743 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 if (likely(HTTP_IS_SPHT(*ptr))) {
1745 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001746 for (; buf->p + msg->sov < ptr; msg->sov++)
1747 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 goto http_msg_hdr_l1_sp;
1749 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001750 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001751 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 goto http_msg_complete_header;
1753
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001755 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001756 /* assumes msg->sol points to the first char, and msg->sov
1757 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 */
1759 if (likely(!HTTP_IS_CRLF(*ptr)))
1760 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001761
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001762 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 /* Note: we could also copy eol into ->eoh so that we have the
1764 * real header end in case it ends with lots of LWS, but is this
1765 * really needed ?
1766 */
1767 if (likely(*ptr == '\r'))
1768 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1769 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001770
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001772 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 EXPECT_LF_HERE(ptr, http_msg_invalid);
1774 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001775
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001777 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1779 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001780 for (; buf->p + msg->eol < ptr; msg->eol++)
1781 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001782 goto http_msg_hdr_val;
1783 }
1784 http_msg_complete_header:
1785 /*
1786 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001787 * Assumes msg->sol points to the first char, msg->sov points
1788 * to the first character of the value and msg->eol to the
1789 * first CR or LF so we know how the line ends. We insert last
1790 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001792 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 idx, idx->tail) < 0))
1794 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001795
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001796 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 if (likely(!HTTP_IS_CRLF(*ptr))) {
1798 goto http_msg_hdr_name;
1799 }
1800
1801 if (likely(*ptr == '\r'))
1802 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1803 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001806 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001807 /* Assumes msg->sol points to the first of either CR or LF.
1808 * Sets ->sov and ->next to the total header length, ->eoh to
1809 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1810 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811 EXPECT_LF_HERE(ptr, http_msg_invalid);
1812 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001813 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001814 msg->eoh = msg->sol;
1815 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001816 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001817 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001818 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001819
1820 case HTTP_MSG_ERROR:
1821 /* this may only happen if we call http_msg_analyser() twice with an error */
1822 break;
1823
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001825#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1827 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001828#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001829 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 }
1831 http_msg_ood:
1832 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001833 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001834 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001836
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001837 http_msg_invalid:
1838 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001839 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001840 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001841 return;
1842}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001843
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001844/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1845 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1846 * nothing is done and 1 is returned.
1847 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001848static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001849{
1850 int delta;
1851 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001852 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001853
1854 if (msg->sl.rq.v_l != 0)
1855 return 1;
1856
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001857 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1858 if (txn->meth != HTTP_METH_GET)
1859 return 0;
1860
Willy Tarreau9b28e032012-10-12 23:49:43 +02001861 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001862 delta = 0;
1863
1864 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001865 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1866 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001867 }
1868 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001869 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001870 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001871 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001872 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001873 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001874 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001875 NULL, NULL);
1876 if (unlikely(!cur_end))
1877 return 0;
1878
1879 /* we have a full HTTP/1.0 request now and we know that
1880 * we have either a CR or an LF at <ptr>.
1881 */
1882 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1883 return 1;
1884}
1885
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001886/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001887 * and "keep-alive" values. If we already know that some headers may safely
1888 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001889 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1890 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001891 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001892 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1893 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1894 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001895 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001896 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001897void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001898{
Willy Tarreau5b154472009-12-21 20:11:07 +01001899 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001900 const char *hdr_val = "Connection";
1901 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001902
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001903 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001904 return;
1905
Willy Tarreau88d349d2010-01-25 12:15:43 +01001906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection";
1908 hdr_len = 16;
1909 }
1910
Willy Tarreau5b154472009-12-21 20:11:07 +01001911 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001912 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001913 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001914 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1915 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001916 if (to_del & 2)
1917 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001918 else
1919 txn->flags |= TX_CON_KAL_SET;
1920 }
1921 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1922 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001923 if (to_del & 1)
1924 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001925 else
1926 txn->flags |= TX_CON_CLO_SET;
1927 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001928 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1929 txn->flags |= TX_HDR_CONN_UPG;
1930 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001931 }
1932
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001933 txn->flags |= TX_HDR_CONN_PRS;
1934 return;
1935}
Willy Tarreau5b154472009-12-21 20:11:07 +01001936
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001937/* Apply desired changes on the Connection: header. Values may be removed and/or
1938 * added depending on the <wanted> flags, which are exclusively composed of
1939 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1940 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1941 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001942void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001943{
1944 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001945 const char *hdr_val = "Connection";
1946 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001947
1948 ctx.idx = 0;
1949
Willy Tarreau88d349d2010-01-25 12:15:43 +01001950
1951 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1952 hdr_val = "Proxy-Connection";
1953 hdr_len = 16;
1954 }
1955
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001956 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001957 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001958 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1959 if (wanted & TX_CON_KAL_SET)
1960 txn->flags |= TX_CON_KAL_SET;
1961 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001962 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001963 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001964 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1965 if (wanted & TX_CON_CLO_SET)
1966 txn->flags |= TX_CON_CLO_SET;
1967 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001968 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001969 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001970 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001971
1972 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1973 return;
1974
1975 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1976 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001977 hdr_val = "Connection: close";
1978 hdr_len = 17;
1979 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1980 hdr_val = "Proxy-Connection: close";
1981 hdr_len = 23;
1982 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001983 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001984 }
1985
1986 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1987 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001988 hdr_val = "Connection: keep-alive";
1989 hdr_len = 22;
1990 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1991 hdr_val = "Proxy-Connection: keep-alive";
1992 hdr_len = 28;
1993 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001994 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001995 }
1996 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001997}
1998
Willy Tarreauc24715e2014-04-17 15:21:20 +02001999/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2000 * the first byte of data after the chunk size, so that we know we can forward
2001 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2002 * the chunk size. That way it is always possible to differentiate between the
2003 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002004 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002005 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002006 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002007static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002008{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002009 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002010 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002011 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002012 const char *end = buf->data + buf->size;
2013 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002014 unsigned int chunk = 0;
2015
2016 /* The chunk size is in the following form, though we are only
2017 * interested in the size and CRLF :
2018 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2019 */
2020 while (1) {
2021 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002022 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002023 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002024 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002025 if (c < 0) /* not a hex digit anymore */
2026 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002027 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002028 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002029 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002030 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002031 chunk = (chunk << 4) + c;
2032 }
2033
Willy Tarreaud98cf932009-12-27 22:54:55 +01002034 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002035 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002036 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037
2038 while (http_is_spht[(unsigned char)*ptr]) {
2039 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002040 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002041 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002042 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002043 }
2044
Willy Tarreaud98cf932009-12-27 22:54:55 +01002045 /* Up to there, we know that at least one byte is present at *ptr. Check
2046 * for the end of chunk size.
2047 */
2048 while (1) {
2049 if (likely(HTTP_IS_CRLF(*ptr))) {
2050 /* we now have a CR or an LF at ptr */
2051 if (likely(*ptr == '\r')) {
2052 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002053 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002054 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002055 return 0;
2056 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002057
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002059 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002061 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002062 /* done */
2063 break;
2064 }
2065 else if (*ptr == ';') {
2066 /* chunk extension, ends at next CRLF */
2067 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002068 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002069 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002070 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002071
2072 while (!HTTP_IS_CRLF(*ptr)) {
2073 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002074 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002075 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002076 return 0;
2077 }
2078 /* we have a CRLF now, loop above */
2079 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002080 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002081 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002082 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002083 }
2084
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002086 * which may or may not be present. We save that into ->next,
2087 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002088 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002089 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002090 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002091 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002092 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002093 msg->chunk_len = chunk;
2094 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002096 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002097 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002098 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002099 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002100}
2101
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002102/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002103 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002104 * the trailers is found, it is automatically scheduled to be forwarded,
2105 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2106 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002107 * except maybe msg->next if it could parse some lines, and returns zero.
2108 * If a parse error is encountered, the function returns < 0 and does not
2109 * change anything except maybe msg->next. Note that the message must
2110 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002111 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002112 * forwarding, and that msg->next exactly matches the length of trailers
2113 * already parsed and not forwarded. It is also important to note that this
2114 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002115 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002116static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002117{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002118 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002119
Willy Tarreaua458b672012-03-05 11:17:50 +01002120 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002121 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002122 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002123 const char *ptr = b_ptr(buf, msg->next);
2124 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002125 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002126
2127 /* scan current line and stop at LF or CRLF */
2128 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002129 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002130 return 0;
2131
2132 if (*ptr == '\n') {
2133 if (!p1)
2134 p1 = ptr;
2135 p2 = ptr;
2136 break;
2137 }
2138
2139 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002140 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002141 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002142 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002143 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002144 p1 = ptr;
2145 }
2146
2147 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002148 if (ptr >= buf->data + buf->size)
2149 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002150 }
2151
2152 /* after LF; point to beginning of next line */
2153 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002154 if (p2 >= buf->data + buf->size)
2155 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002156
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002157 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002158 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002159 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002160
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002161 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002162 /* LF/CRLF at beginning of line => end of trailers at p2.
2163 * Everything was scheduled for forwarding, there's nothing
2164 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002165 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002166 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002167 msg->msg_state = HTTP_MSG_DONE;
2168 return 1;
2169 }
2170 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002171 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002172 }
2173}
2174
Willy Tarreauc24715e2014-04-17 15:21:20 +02002175/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2176 * or a possible LF alone at the end of a chunk. It automatically adjusts
2177 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002178 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002179 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2180 * not enough data are available, the function does not change anything and
2181 * returns zero. If a parse error is encountered, the function returns < 0 and
2182 * does not change anything. Note: this function is designed to parse wrapped
2183 * CRLF at the end of the buffer.
2184 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002185static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002186{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002187 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002188 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002189 int bytes;
2190
2191 /* NB: we'll check data availabilty at the end. It's not a
2192 * problem because whatever we match first will be checked
2193 * against the correct length.
2194 */
2195 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002196 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 if (*ptr == '\r') {
2198 bytes++;
2199 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002200 if (ptr >= buf->data + buf->size)
2201 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002202 }
2203
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002204 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002205 return 0;
2206
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002207 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002208 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002209 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002210 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002211
2212 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002213 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002214 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002215 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002216 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002217 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2218 return 1;
2219}
Willy Tarreau5b154472009-12-21 20:11:07 +01002220
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002221/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2222 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2223 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2224 * Unparsable qvalues return 1000 as "q=1.000".
2225 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002226int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002227{
2228 int q = 1000;
2229
Willy Tarreau506c69a2014-07-08 00:59:48 +02002230 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002231 goto out;
2232 q = (*qvalue++ - '0') * 1000;
2233
2234 if (*qvalue++ != '.')
2235 goto out;
2236
Willy Tarreau506c69a2014-07-08 00:59:48 +02002237 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002238 goto out;
2239 q += (*qvalue++ - '0') * 100;
2240
Willy Tarreau506c69a2014-07-08 00:59:48 +02002241 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002242 goto out;
2243 q += (*qvalue++ - '0') * 10;
2244
Willy Tarreau506c69a2014-07-08 00:59:48 +02002245 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002246 goto out;
2247 q += (*qvalue++ - '0') * 1;
2248 out:
2249 if (q > 1000)
2250 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002251 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002252 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002253 return q;
2254}
William Lallemand82fe75c2012-10-23 10:25:10 +02002255
2256/*
2257 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002258 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002259int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002260{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002261 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002262 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002263 struct hdr_ctx ctx;
2264 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002265 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002266
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002267 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2268 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002269 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2270 */
2271 ctx.idx = 0;
2272 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2273 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002274 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2275 (ctx.vlen < 31 ||
2276 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2277 ctx.line[ctx.val + 30] < '6' ||
2278 (ctx.line[ctx.val + 30] == '6' &&
2279 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2280 s->comp_algo = NULL;
2281 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002282 }
2283
William Lallemand82fe75c2012-10-23 10:25:10 +02002284 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002285 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002286 int best_q = 0;
2287
William Lallemand82fe75c2012-10-23 10:25:10 +02002288 ctx.idx = 0;
2289 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002290 const char *qval;
2291 int q;
2292 int toklen;
2293
2294 /* try to isolate the token from the optional q-value */
2295 toklen = 0;
2296 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2297 toklen++;
2298
2299 qval = ctx.line + ctx.val + toklen;
2300 while (1) {
2301 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2302 qval++;
2303
2304 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2305 qval = NULL;
2306 break;
2307 }
2308 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002309
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002310 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2311 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002312
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002313 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2314 qval = NULL;
2315 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002316 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002317 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2318 break;
2319
2320 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2321 qval++;
2322 }
2323
2324 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002325 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002326
2327 if (q <= best_q)
2328 continue;
2329
2330 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2331 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002332 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002333 s->comp_algo = comp_algo;
2334 best_q = q;
2335 break;
2336 }
2337 }
2338 }
2339 }
2340
2341 /* remove all occurrences of the header when "compression offload" is set */
2342 if (s->comp_algo) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002343 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002344 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2345 ctx.idx = 0;
2346 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2347 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002348 }
2349 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002350 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002351 }
2352
2353 /* identity is implicit does not require headers */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002354 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002355 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002356 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002357 s->comp_algo = comp_algo;
2358 return 1;
2359 }
2360 }
2361 }
2362
2363 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002364 return 0;
2365}
2366
2367/*
2368 * Selects a comression algorithm depending of the server response.
2369 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002370int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002371{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002372 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002373 struct http_msg *msg = &txn->rsp;
2374 struct hdr_ctx ctx;
2375 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002376
2377 /* no common compression algorithm was found in request header */
2378 if (s->comp_algo == NULL)
2379 goto fail;
2380
2381 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002382 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002383 goto fail;
2384
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002385 /* compress 200,201,202,203 responses only */
2386 if ((txn->status != 200) &&
2387 (txn->status != 201) &&
2388 (txn->status != 202) &&
2389 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002390 goto fail;
2391
William Lallemand82fe75c2012-10-23 10:25:10 +02002392 /* Content-Length is null */
2393 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2394 goto fail;
2395
2396 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002397 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002398 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2399 goto fail;
2400
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002401 /* no compression when Cache-Control: no-transform is present in the message */
2402 ctx.idx = 0;
2403 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2404 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2405 goto fail;
2406 }
2407
William Lallemand82fe75c2012-10-23 10:25:10 +02002408 comp_type = NULL;
2409
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002410 /* we don't want to compress multipart content-types, nor content-types that are
2411 * not listed in the "compression type" directive if any. If no content-type was
2412 * found but configuration requires one, we don't compress either. Backend has
2413 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002414 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002415 ctx.idx = 0;
2416 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2417 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2418 goto fail;
2419
2420 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002421 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002422 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002423 if (ctx.vlen >= comp_type->name_len &&
2424 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002425 /* this Content-Type should be compressed */
2426 break;
2427 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002428 /* this Content-Type should not be compressed */
2429 if (comp_type == NULL)
2430 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002431 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002432 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002433 else { /* no content-type header */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002434 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002435 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002436 }
2437
William Lallemandd85f9172012-11-09 17:05:39 +01002438 /* limit compression rate */
2439 if (global.comp_rate_lim > 0)
2440 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2441 goto fail;
2442
William Lallemand072a2bf2012-11-20 17:01:01 +01002443 /* limit cpu usage */
2444 if (idle_pct < compress_min_idle)
2445 goto fail;
2446
William Lallemand4c49fae2012-11-07 15:00:23 +01002447 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002448 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002449 goto fail;
2450
Willy Tarreaue7dff022015-04-03 01:14:29 +02002451 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002452
William Lallemand82fe75c2012-10-23 10:25:10 +02002453 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002454 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002455 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2456 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2457
2458 /* add Transfer-Encoding header */
2459 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2460 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2461
2462 /*
2463 * Add Content-Encoding header when it's not identity encoding.
2464 * RFC 2616 : Identity encoding: This content-coding is used only in the
2465 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2466 * header.
2467 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002468 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002469 trash.len = 18;
2470 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002471 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2472 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002473 trash.str[trash.len] = '\0';
2474 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002475 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002476 return 1;
2477
2478fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002479 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002480 return 0;
2481}
2482
Willy Tarreau87b09662015-04-03 00:22:06 +02002483void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002484{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002485 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002486 int tmp = TX_CON_WANT_KAL;
2487
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002488 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2489 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002490 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2491 tmp = TX_CON_WANT_TUN;
2492
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002493 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002494 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2495 tmp = TX_CON_WANT_TUN;
2496 }
2497
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002498 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002499 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2500 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002501 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002502 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2503 tmp = TX_CON_WANT_CLO;
2504 else
2505 tmp = TX_CON_WANT_SCL;
2506 }
2507
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002508 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002509 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2510 tmp = TX_CON_WANT_CLO;
2511
2512 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2513 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2514
2515 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2516 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2517 /* parse the Connection header and possibly clean it */
2518 int to_del = 0;
2519 if ((msg->flags & HTTP_MSGF_VER_11) ||
2520 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002521 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002522 to_del |= 2; /* remove "keep-alive" */
2523 if (!(msg->flags & HTTP_MSGF_VER_11))
2524 to_del |= 1; /* remove "close" */
2525 http_parse_connection_header(txn, msg, to_del);
2526 }
2527
2528 /* check if client or config asks for explicit close in KAL/SCL */
2529 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2530 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2531 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2532 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2533 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002534 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002535 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2536}
William Lallemand82fe75c2012-10-23 10:25:10 +02002537
Willy Tarreaud787e662009-07-07 10:14:51 +02002538/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2539 * processing can continue on next analysers, or zero if it either needs more
2540 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002541 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002542 * when it has nothing left to do, and may remove any analyser when it wants to
2543 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002544 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002545int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002546{
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 /*
2548 * We will parse the partial (or complete) lines.
2549 * We will check the request syntax, and also join multi-line
2550 * headers. An index of all the lines will be elaborated while
2551 * parsing.
2552 *
2553 * For the parsing, we use a 28 states FSM.
2554 *
2555 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002556 * req->buf->p = beginning of request
2557 * req->buf->p + msg->eoh = end of processed headers / start of current one
2558 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002559 * msg->eol = end of current header or line (LF or CRLF)
2560 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002561 *
2562 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002563 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002564 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2565 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002566 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002567
Willy Tarreau59234e92008-11-30 23:51:27 +01002568 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002569 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002570 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002571 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002572 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002573
Willy Tarreau87b09662015-04-03 00:22:06 +02002574 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002575 now_ms, __FUNCTION__,
2576 s,
2577 req,
2578 req->rex, req->wex,
2579 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002580 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002581 req->analysers);
2582
Willy Tarreau52a0c602009-08-16 22:45:38 +02002583 /* we're speaking HTTP here, so let's speak HTTP to the client */
2584 s->srv_error = http_return_srv_error;
2585
Willy Tarreau83e3af02009-12-28 17:39:57 +01002586 /* There's a protected area at the end of the buffer for rewriting
2587 * purposes. We don't want to start to parse the request if the
2588 * protected area is affected, because we may have to move processed
2589 * data later, which is much more complicated.
2590 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002591 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002592 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002593 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002594 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002595 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002596 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002597 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002598 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002599 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002600 return 0;
2601 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002602 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2603 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2604 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002605 }
2606
Willy Tarreau065e8332010-01-08 00:30:20 +01002607 /* Note that we have the same problem with the response ; we
2608 * may want to send a redirect, error or anything which requires
2609 * some spare space. So we'll ensure that we have at least
2610 * maxrewrite bytes available in the response buffer before
2611 * processing that one. This will only affect pipelined
2612 * keep-alive requests.
2613 */
2614 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002615 unlikely(!channel_is_rewritable(&s->res) ||
2616 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2617 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2618 if (s->res.buf->o) {
2619 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002620 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002621 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002622 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002623 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2624 s->res.flags |= CF_WAKE_WRITE;
2625 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002626 return 0;
2627 }
2628 }
2629
Willy Tarreau9b28e032012-10-12 23:49:43 +02002630 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002631 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002632 }
2633
Willy Tarreau59234e92008-11-30 23:51:27 +01002634 /* 1: we might have to print this header in debug mode */
2635 if (unlikely((global.mode & MODE_DEBUG) &&
2636 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002637 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002639
Willy Tarreau9b28e032012-10-12 23:49:43 +02002640 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002641 /* this is a bit complex : in case of error on the request line,
2642 * we know that rq.l is still zero, so we display only the part
2643 * up to the end of the line (truncated by debug_hdr).
2644 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002645 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002646 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002647
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 sol += hdr_idx_first_pos(&txn->hdr_idx);
2649 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002650
Willy Tarreau59234e92008-11-30 23:51:27 +01002651 while (cur_idx) {
2652 eol = sol + txn->hdr_idx.v[cur_idx].len;
2653 debug_hdr("clihdr", s, sol, eol);
2654 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2655 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002656 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 }
2658
Willy Tarreau58f10d72006-12-04 02:26:12 +01002659
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 /*
2661 * Now we quickly check if we have found a full valid request.
2662 * If not so, we check the FD and buffer states before leaving.
2663 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002664 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002665 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002666 * on a keep-alive stream, if we encounter and error, close, t/o,
2667 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002668 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002669 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002670 * Last, we may increase some tracked counters' http request errors on
2671 * the cases that are deliberately the client's fault. For instance,
2672 * a timeout or connection reset is not counted as an error. However
2673 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002674 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002675
Willy Tarreau655dce92009-11-08 13:10:58 +01002676 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002677 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002678 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002679 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002680 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002681 stream_inc_http_req_ctr(s);
2682 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002683 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002685 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002686
Willy Tarreau59234e92008-11-30 23:51:27 +01002687 /* 1: Since we are in header mode, if there's no space
2688 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002689 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 * must terminate it now.
2691 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002692 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 /* FIXME: check if URI is set and return Status
2694 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002695 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002696 stream_inc_http_req_ctr(s);
2697 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002698 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002699 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002700 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002701 goto return_bad_req;
2702 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002703
Willy Tarreau59234e92008-11-30 23:51:27 +01002704 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002705 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002706 if (!(s->flags & SF_ERR_MASK))
2707 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002708
Willy Tarreaufcffa692010-01-10 14:21:19 +01002709 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002710 goto failed_keep_alive;
2711
Willy Tarreau59234e92008-11-30 23:51:27 +01002712 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002713 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002714 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002715 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002716 }
2717
Willy Tarreaudc979f22012-12-04 10:39:01 +01002718 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002719 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002720 msg->msg_state = HTTP_MSG_ERROR;
2721 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002722
Willy Tarreau87b09662015-04-03 00:22:06 +02002723 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002724 proxy_inc_fe_req_ctr(sess->fe);
2725 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002726 if (sess->listener->counters)
2727 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002728
Willy Tarreaue7dff022015-04-03 01:14:29 +02002729 if (!(s->flags & SF_FINST_MASK))
2730 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002731 return 0;
2732 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002733
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002735 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002736 if (!(s->flags & SF_ERR_MASK))
2737 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002738
Willy Tarreaufcffa692010-01-10 14:21:19 +01002739 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002740 goto failed_keep_alive;
2741
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002743 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002744 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002745 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002746 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002747 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002748 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 msg->msg_state = HTTP_MSG_ERROR;
2750 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002751
Willy Tarreau87b09662015-04-03 00:22:06 +02002752 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002753 proxy_inc_fe_req_ctr(sess->fe);
2754 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002755 if (sess->listener->counters)
2756 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002757
Willy Tarreaue7dff022015-04-03 01:14:29 +02002758 if (!(s->flags & SF_FINST_MASK))
2759 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002760 return 0;
2761 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002762
Willy Tarreau59234e92008-11-30 23:51:27 +01002763 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002764 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002765 if (!(s->flags & SF_ERR_MASK))
2766 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002767
Willy Tarreaufcffa692010-01-10 14:21:19 +01002768 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002769 goto failed_keep_alive;
2770
Willy Tarreau4076a152009-04-02 15:18:36 +02002771 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002772 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002773 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002774 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 msg->msg_state = HTTP_MSG_ERROR;
2776 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002777
Willy Tarreau87b09662015-04-03 00:22:06 +02002778 stream_inc_http_err_ctr(s);
2779 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002780 proxy_inc_fe_req_ctr(sess->fe);
2781 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002782 if (sess->listener->counters)
2783 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002784
Willy Tarreaue7dff022015-04-03 01:14:29 +02002785 if (!(s->flags & SF_FINST_MASK))
2786 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002787 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002788 }
2789
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002790 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002791 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002792 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002793#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002794 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2795 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002796 /* We need more data, we have to re-enable quick-ack in case we
2797 * previously disabled it, otherwise we might cause the client
2798 * to delay next data.
2799 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002800 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002801 }
2802#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002803
Willy Tarreaufcffa692010-01-10 14:21:19 +01002804 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2805 /* If the client starts to talk, let's fall back to
2806 * request timeout processing.
2807 */
2808 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002809 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002810 }
2811
Willy Tarreau59234e92008-11-30 23:51:27 +01002812 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002813 if (!tick_isset(req->analyse_exp)) {
2814 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2815 (txn->flags & TX_WAIT_NEXT_RQ) &&
2816 tick_isset(s->be->timeout.httpka))
2817 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2818 else
2819 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2820 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002821
Willy Tarreau59234e92008-11-30 23:51:27 +01002822 /* we're not ready yet */
2823 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002824
2825 failed_keep_alive:
2826 /* Here we process low-level errors for keep-alive requests. In
2827 * short, if the request is not the first one and it experiences
2828 * a timeout, read error or shutdown, we just silently close so
2829 * that the client can try again.
2830 */
2831 txn->status = 0;
2832 msg->msg_state = HTTP_MSG_RQBEFORE;
2833 req->analysers = 0;
2834 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002835 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002836 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002837 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002838 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002839 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002840
Willy Tarreaud787e662009-07-07 10:14:51 +02002841 /* OK now we have a complete HTTP request with indexed headers. Let's
2842 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002843 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002844 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002845 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002846 * byte after the last LF. msg->sov points to the first byte of data.
2847 * msg->eol cannot be trusted because it may have been left uninitialized
2848 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002849 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002850
Willy Tarreau87b09662015-04-03 00:22:06 +02002851 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002852 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002853
Willy Tarreaub16a5742010-01-10 14:46:16 +01002854 if (txn->flags & TX_WAIT_NEXT_RQ) {
2855 /* kill the pending keep-alive timeout */
2856 txn->flags &= ~TX_WAIT_NEXT_RQ;
2857 req->analyse_exp = TICK_ETERNITY;
2858 }
2859
2860
Willy Tarreaud787e662009-07-07 10:14:51 +02002861 /* Maybe we found in invalid header name while we were configured not
2862 * to block on that, so we have to capture it now.
2863 */
2864 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002865 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002866
Willy Tarreau59234e92008-11-30 23:51:27 +01002867 /*
2868 * 1: identify the method
2869 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002870 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002871
2872 /* we can make use of server redirect on GET and HEAD */
2873 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002874 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002875
Willy Tarreau59234e92008-11-30 23:51:27 +01002876 /*
2877 * 2: check if the URI matches the monitor_uri.
2878 * We have to do this for every request which gets in, because
2879 * the monitor-uri is defined by the frontend.
2880 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002881 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2882 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002883 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002884 sess->fe->monitor_uri,
2885 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002886 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002887 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002888 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002889 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002890
Willy Tarreaue7dff022015-04-03 01:14:29 +02002891 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002892 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002893
Willy Tarreau59234e92008-11-30 23:51:27 +01002894 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002895 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002896 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002897
Willy Tarreau59234e92008-11-30 23:51:27 +01002898 ret = acl_pass(ret);
2899 if (cond->pol == ACL_COND_UNLESS)
2900 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002901
Willy Tarreau59234e92008-11-30 23:51:27 +01002902 if (ret) {
2903 /* we fail this request, let's return 503 service unavail */
2904 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002905 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002906 if (!(s->flags & SF_ERR_MASK))
2907 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002908 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002909 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002910 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002911
Willy Tarreau59234e92008-11-30 23:51:27 +01002912 /* nothing to fail, let's reply normaly */
2913 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01002914 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002915 if (!(s->flags & SF_ERR_MASK))
2916 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002917 goto return_prx_cond;
2918 }
2919
2920 /*
2921 * 3: Maybe we have to copy the original REQURI for the logs ?
2922 * Note: we cannot log anymore if the request has been
2923 * classified as invalid.
2924 */
2925 if (unlikely(s->logs.logwait & LW_REQ)) {
2926 /* we have a complete HTTP request that we must log */
2927 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2928 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002929
Willy Tarreau59234e92008-11-30 23:51:27 +01002930 if (urilen >= REQURI_LEN)
2931 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002932 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002933 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002934
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002935 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002936 s->do_log(s);
2937 } else {
2938 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002939 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002940 }
Willy Tarreau06619262006-12-17 08:37:22 +01002941
Willy Tarreau59234e92008-11-30 23:51:27 +01002942 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002943 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002944 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002945
Willy Tarreau5b154472009-12-21 20:11:07 +01002946 /* ... and check if the request is HTTP/1.1 or above */
2947 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002948 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2949 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2950 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002951 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002952
2953 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002954 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 +01002955
Willy Tarreau88d349d2010-01-25 12:15:43 +01002956 /* if the frontend has "option http-use-proxy-header", we'll check if
2957 * we have what looks like a proxied connection instead of a connection,
2958 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2959 * Note that this is *not* RFC-compliant, however browsers and proxies
2960 * happen to do that despite being non-standard :-(
2961 * We consider that a request not beginning with either '/' or '*' is
2962 * a proxied connection, which covers both "scheme://location" and
2963 * CONNECT ip:port.
2964 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002965 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002966 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002967 txn->flags |= TX_USE_PX_CONN;
2968
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002969 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002970 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002971
Willy Tarreau59234e92008-11-30 23:51:27 +01002972 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002973 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002974 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002975 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002976
Willy Tarreau557f1992015-05-01 10:05:17 +02002977 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2978 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002979 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002980 * The length of a message body is determined by one of the following
2981 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002982 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002983 * 1. Any response to a HEAD request and any response with a 1xx
2984 * (Informational), 204 (No Content), or 304 (Not Modified) status
2985 * code is always terminated by the first empty line after the
2986 * header fields, regardless of the header fields present in the
2987 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002988 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002989 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2990 * the connection will become a tunnel immediately after the empty
2991 * line that concludes the header fields. A client MUST ignore any
2992 * Content-Length or Transfer-Encoding header fields received in
2993 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002994 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002995 * 3. If a Transfer-Encoding header field is present and the chunked
2996 * transfer coding (Section 4.1) is the final encoding, the message
2997 * body length is determined by reading and decoding the chunked
2998 * data until the transfer coding indicates the data is complete.
2999 *
3000 * If a Transfer-Encoding header field is present in a response and
3001 * the chunked transfer coding is not the final encoding, the
3002 * message body length is determined by reading the connection until
3003 * it is closed by the server. If a Transfer-Encoding header field
3004 * is present in a request and the chunked transfer coding is not
3005 * the final encoding, the message body length cannot be determined
3006 * reliably; the server MUST respond with the 400 (Bad Request)
3007 * status code and then close the connection.
3008 *
3009 * If a message is received with both a Transfer-Encoding and a
3010 * Content-Length header field, the Transfer-Encoding overrides the
3011 * Content-Length. Such a message might indicate an attempt to
3012 * perform request smuggling (Section 9.5) or response splitting
3013 * (Section 9.4) and ought to be handled as an error. A sender MUST
3014 * remove the received Content-Length field prior to forwarding such
3015 * a message downstream.
3016 *
3017 * 4. If a message is received without Transfer-Encoding and with
3018 * either multiple Content-Length header fields having differing
3019 * field-values or a single Content-Length header field having an
3020 * invalid value, then the message framing is invalid and the
3021 * recipient MUST treat it as an unrecoverable error. If this is a
3022 * request message, the server MUST respond with a 400 (Bad Request)
3023 * status code and then close the connection. If this is a response
3024 * message received by a proxy, the proxy MUST close the connection
3025 * to the server, discard the received response, and send a 502 (Bad
3026 * Gateway) response to the client. If this is a response message
3027 * received by a user agent, the user agent MUST close the
3028 * connection to the server and discard the received response.
3029 *
3030 * 5. If a valid Content-Length header field is present without
3031 * Transfer-Encoding, its decimal value defines the expected message
3032 * body length in octets. If the sender closes the connection or
3033 * the recipient times out before the indicated number of octets are
3034 * received, the recipient MUST consider the message to be
3035 * incomplete and close the connection.
3036 *
3037 * 6. If this is a request message and none of the above are true, then
3038 * the message body length is zero (no message body is present).
3039 *
3040 * 7. Otherwise, this is a response message without a declared message
3041 * body length, so the message body length is determined by the
3042 * number of octets received prior to the server closing the
3043 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003044 */
3045
Willy Tarreau32b47f42009-10-18 20:55:02 +02003046 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003047 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003048 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003049 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003050 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3051 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003052 /* chunked not last, return badreq */
3053 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003054 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003055 }
3056
Willy Tarreau1c913912015-04-30 10:57:51 +02003057 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003058 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003059 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3060 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3061 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3062 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003063 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003064 signed long long cl;
3065
Willy Tarreauad14f752011-09-02 20:33:27 +02003066 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003067 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003068 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003069 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003070
Willy Tarreauad14f752011-09-02 20:33:27 +02003071 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003072 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003073 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003074 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003075
Willy Tarreauad14f752011-09-02 20:33:27 +02003076 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003077 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003078 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003079 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003080
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003081 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003082 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003083 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003084 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003085
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003086 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003087 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003088 }
3089
Willy Tarreau34dfc602015-05-01 10:09:49 +02003090 /* even bodyless requests have a known length */
3091 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003092
Willy Tarreau179085c2014-04-28 16:48:56 +02003093 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3094 * only change if both the request and the config reference something else.
3095 * Option httpclose by itself sets tunnel mode where headers are mangled.
3096 * However, if another mode is set, it will affect it (eg: server-close/
3097 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3098 * if FE and BE have the same settings (common). The method consists in
3099 * checking if options changed between the two calls (implying that either
3100 * one is non-null, or one of them is non-null and we are there for the first
3101 * time.
3102 */
3103 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003104 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003105 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003106
Willy Tarreaud787e662009-07-07 10:14:51 +02003107 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003108 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003109 req->analyse_exp = TICK_ETERNITY;
3110 return 1;
3111
3112 return_bad_req:
3113 /* We centralize bad requests processing here */
3114 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3115 /* we detected a parsing error. We want to archive this request
3116 * in the dedicated proxy area for later troubleshooting.
3117 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003118 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003119 }
3120
3121 txn->req.msg_state = HTTP_MSG_ERROR;
3122 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003123 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003124
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003125 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003126 if (sess->listener->counters)
3127 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003128
3129 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003130 if (!(s->flags & SF_ERR_MASK))
3131 s->flags |= SF_ERR_PRXCOND;
3132 if (!(s->flags & SF_FINST_MASK))
3133 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003134
3135 req->analysers = 0;
3136 req->analyse_exp = TICK_ETERNITY;
3137 return 0;
3138}
3139
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003140
Willy Tarreau347a35d2013-11-22 17:51:09 +01003141/* This function prepares an applet to handle the stats. It can deal with the
3142 * "100-continue" expectation, check that admin rules are met for POST requests,
3143 * and program a response message if something was unexpected. It cannot fail
3144 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003145 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003146 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003147 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003148 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003149int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003150{
3151 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003152 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003153 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003154 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003155 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003156 struct uri_auth *uri_auth = s->be->uri_auth;
3157 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003158 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003159
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003160 appctx = si_appctx(si);
3161 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3162 appctx->st1 = appctx->st2 = 0;
3163 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3164 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003165 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003166 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003167
3168 uri = msg->chn->buf->p + msg->sl.rq.u;
3169 lookup = uri + uri_auth->uri_len;
3170
3171 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3172 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003173 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003174 break;
3175 }
3176 }
3177
3178 if (uri_auth->refresh) {
3179 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3180 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003181 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003182 break;
3183 }
3184 }
3185 }
3186
3187 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3188 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003189 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003190 break;
3191 }
3192 }
3193
3194 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3195 if (memcmp(h, ";st=", 4) == 0) {
3196 int i;
3197 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003198 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003199 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3200 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003201 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003202 break;
3203 }
3204 }
3205 break;
3206 }
3207 }
3208
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003209 appctx->ctx.stats.scope_str = 0;
3210 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003211 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3212 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3213 int itx = 0;
3214 const char *h2;
3215 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3216 const char *err;
3217
3218 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3219 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003220 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003221 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3222 itx++;
3223 h++;
3224 }
3225
3226 if (itx > STAT_SCOPE_TXT_MAXLEN)
3227 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003228 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003229
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003230 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003231 memcpy(scope_txt, h2, itx);
3232 scope_txt[itx] = '\0';
3233 err = invalid_char(scope_txt);
3234 if (err) {
3235 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003236 appctx->ctx.stats.scope_str = 0;
3237 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003238 }
3239 break;
3240 }
3241 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003242
3243 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003244 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003245 int ret = 1;
3246
3247 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003248 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 +01003249 ret = acl_pass(ret);
3250 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3251 ret = !ret;
3252 }
3253
3254 if (ret) {
3255 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003256 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003257 break;
3258 }
3259 }
3260
3261 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003262 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003263 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003264 /* we'll need the request body, possibly after sending 100-continue */
3265 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003266 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003267 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003268 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003269 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3270 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003271 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003272 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003273 else {
3274 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003275 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003276 }
3277
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003278 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003279 return 1;
3280}
3281
Lukas Tribus67db8df2013-06-23 17:37:13 +02003282/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3283 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3284 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003285void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003286{
3287#ifdef IP_TOS
3288 if (from.ss_family == AF_INET)
3289 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3290#endif
3291#ifdef IPV6_TCLASS
3292 if (from.ss_family == AF_INET6) {
3293 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3294 /* v4-mapped addresses need IP_TOS */
3295 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3296 else
3297 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3298 }
3299#endif
3300}
3301
Willy Tarreau87b09662015-04-03 00:22:06 +02003302int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003303 const char* name, unsigned int name_len,
3304 const char *str, struct my_regex *re,
3305 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003306{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003307 struct hdr_ctx ctx;
3308 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003309 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003310 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3311 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003312 struct chunk *output = get_trash_chunk();
3313
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003314 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003315
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003316 /* Choose the header browsing function. */
3317 switch (action) {
3318 case HTTP_REQ_ACT_REPLACE_VAL:
3319 case HTTP_RES_ACT_REPLACE_VAL:
3320 http_find_hdr_func = http_find_header2;
3321 break;
3322 case HTTP_REQ_ACT_REPLACE_HDR:
3323 case HTTP_RES_ACT_REPLACE_HDR:
3324 http_find_hdr_func = http_find_full_header2;
3325 break;
3326 default: /* impossible */
3327 return -1;
3328 }
3329
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003330 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3331 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003332 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003333 char *val = ctx.line + ctx.val;
3334 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003335
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003336 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3337 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003338
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003339 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003340 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003341 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003342
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003343 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003344
3345 hdr->len += delta;
3346 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003347
3348 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003349 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003350 }
3351
3352 return 0;
3353}
3354
Willy Tarreau87b09662015-04-03 00:22:06 +02003355static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003356 const char* name, unsigned int name_len,
3357 struct list *fmt, struct my_regex *re,
3358 int action)
3359{
3360 struct chunk *replace = get_trash_chunk();
3361
3362 replace->len = build_logline(s, replace->str, replace->size, fmt);
3363 if (replace->len >= replace->size - 1)
3364 return -1;
3365
3366 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3367}
3368
Willy Tarreau87b09662015-04-03 00:22:06 +02003369/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003370 * transaction <txn>. Returns the verdict of the first rule that prevents
3371 * further processing of the request (auth, deny, ...), and defaults to
3372 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3373 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3374 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003375 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003376enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003377http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003378{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003379 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003380 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003381 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003382 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003383 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003384 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003385
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003386 /* If "the current_rule_list" match the executed rule list, we are in
3387 * resume condition. If a resume is needed it is always in the action
3388 * and never in the ACL or converters. In this case, we initialise the
3389 * current rule, and go to the action execution point.
3390 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003391 if (s->current_rule) {
3392 rule = s->current_rule;
3393 s->current_rule = NULL;
3394 if (s->current_rule_list == rules)
3395 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003396 }
3397 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003398
Willy Tarreauff011f22011-01-06 17:51:27 +01003399 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003400 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003401 continue;
3402
Willy Tarreau96257ec2012-12-27 10:46:37 +01003403 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003404 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003405 int ret;
3406
Willy Tarreau192252e2015-04-04 01:47:55 +02003407 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003408 ret = acl_pass(ret);
3409
Willy Tarreauff011f22011-01-06 17:51:27 +01003410 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003411 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003412
3413 if (!ret) /* condition not matched */
3414 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003415 }
3416
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003417resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003418 switch (rule->action) {
3419 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003420 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003421
3422 case HTTP_REQ_ACT_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003423 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003424 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003425
Willy Tarreauccbcc372012-12-27 12:37:57 +01003426 case HTTP_REQ_ACT_TARPIT:
3427 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003428 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003429 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003430
Willy Tarreau96257ec2012-12-27 10:46:37 +01003431 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003432 /* Auth might be performed on regular http-req rules as well as on stats */
3433 auth_realm = rule->arg.auth.realm;
3434 if (!auth_realm) {
3435 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3436 auth_realm = STATS_DEFAULT_REALM;
3437 else
3438 auth_realm = px->id;
3439 }
3440 /* send 401/407 depending on whether we use a proxy or not. We still
3441 * count one error, because normal browsing won't significantly
3442 * increase the counter but brute force attempts will.
3443 */
3444 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3445 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3446 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003447 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003448 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003449
Willy Tarreau81499eb2012-12-27 12:19:02 +01003450 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003451 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3452 return HTTP_RULE_RES_BADREQ;
3453 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003454
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003455 case HTTP_REQ_ACT_SET_NICE:
3456 s->task->nice = rule->arg.nice;
3457 break;
3458
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003459 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003460 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003461 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003462 break;
3463
Willy Tarreau51347ed2013-06-11 19:34:13 +02003464 case HTTP_REQ_ACT_SET_MARK:
3465#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003466 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003467 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003468#endif
3469 break;
3470
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003471 case HTTP_REQ_ACT_SET_LOGL:
3472 s->logs.level = rule->arg.loglevel;
3473 break;
3474
Sasha Pachev218f0642014-06-16 12:05:59 -06003475 case HTTP_REQ_ACT_REPLACE_HDR:
3476 case HTTP_REQ_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003477 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3478 rule->arg.hdr_add.name_len,
3479 &rule->arg.hdr_add.fmt,
3480 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003481 return HTTP_RULE_RES_BADREQ;
3482 break;
3483
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003484 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003485 ctx.idx = 0;
3486 /* remove all occurrences of the header */
3487 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3488 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3489 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003490 }
Willy Tarreau85603282015-01-21 20:39:27 +01003491 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003492
Willy Tarreau85603282015-01-21 20:39:27 +01003493 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003494 case HTTP_REQ_ACT_ADD_HDR:
3495 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3496 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3497 trash.len = rule->arg.hdr_add.name_len;
3498 trash.str[trash.len++] = ':';
3499 trash.str[trash.len++] = ' ';
3500 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 +01003501
3502 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3503 /* remove all occurrences of the header */
3504 ctx.idx = 0;
3505 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3506 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3507 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3508 }
3509 }
3510
Willy Tarreau96257ec2012-12-27 10:46:37 +01003511 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3512 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003513
3514 case HTTP_REQ_ACT_DEL_ACL:
3515 case HTTP_REQ_ACT_DEL_MAP: {
3516 struct pat_ref *ref;
3517 char *key;
3518 int len;
3519
3520 /* collect reference */
3521 ref = pat_ref_lookup(rule->arg.map.ref);
3522 if (!ref)
3523 continue;
3524
3525 /* collect key */
3526 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3527 key = trash.str;
3528 key[len] = '\0';
3529
3530 /* perform update */
3531 /* returned code: 1=ok, 0=ko */
3532 pat_ref_delete(ref, key);
3533
3534 break;
3535 }
3536
3537 case HTTP_REQ_ACT_ADD_ACL: {
3538 struct pat_ref *ref;
3539 char *key;
3540 struct chunk *trash_key;
3541 int len;
3542
3543 trash_key = get_trash_chunk();
3544
3545 /* collect reference */
3546 ref = pat_ref_lookup(rule->arg.map.ref);
3547 if (!ref)
3548 continue;
3549
3550 /* collect key */
3551 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3552 key = trash_key->str;
3553 key[len] = '\0';
3554
3555 /* perform update */
3556 /* add entry only if it does not already exist */
3557 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003558 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003559
3560 break;
3561 }
3562
3563 case HTTP_REQ_ACT_SET_MAP: {
3564 struct pat_ref *ref;
3565 char *key, *value;
3566 struct chunk *trash_key, *trash_value;
3567 int len;
3568
3569 trash_key = get_trash_chunk();
3570 trash_value = get_trash_chunk();
3571
3572 /* collect reference */
3573 ref = pat_ref_lookup(rule->arg.map.ref);
3574 if (!ref)
3575 continue;
3576
3577 /* collect key */
3578 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3579 key = trash_key->str;
3580 key[len] = '\0';
3581
3582 /* collect value */
3583 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3584 value = trash_value->str;
3585 value[len] = '\0';
3586
3587 /* perform update */
3588 if (pat_ref_find_elt(ref, key) != NULL)
3589 /* update entry if it exists */
3590 pat_ref_set(ref, key, value, NULL);
3591 else
3592 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003593 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003594
3595 break;
3596 }
William Lallemand73025dd2014-04-24 14:38:37 +02003597
3598 case HTTP_REQ_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003599 if (!rule->action_ptr(rule, px, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003600 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003601 return HTTP_RULE_RES_YIELD;
3602 }
William Lallemand73025dd2014-04-24 14:38:37 +02003603 break;
3604
3605 case HTTP_REQ_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003606 rule->action_ptr(rule, px, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003607 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003608
3609 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3610 /* Note: only the first valid tracking parameter of each
3611 * applies.
3612 */
3613
3614 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3615 struct stktable *t;
3616 struct stksess *ts;
3617 struct stktable_key *key;
3618 void *ptr;
3619
3620 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau192252e2015-04-04 01:47:55 +02003621 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 +02003622
3623 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003624 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003625
3626 /* let's count a new HTTP request as it's the first time we do it */
3627 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3628 if (ptr)
3629 stktable_data_cast(ptr, http_req_cnt)++;
3630
3631 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3632 if (ptr)
3633 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3634 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3635
3636 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003637 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003638 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3639 }
3640 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003641 }
3642 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003643
3644 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003645 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003646}
3647
Willy Tarreau71241ab2012-12-27 11:30:54 +01003648
Willy Tarreau87b09662015-04-03 00:22:06 +02003649/* Executes the http-response rules <rules> for stream <s>, proxy <px> and
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003650 * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD
3651 * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the
3652 * evaluation of next rule list. If *STOP is returned, the process must stop
3653 * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny
3654 * rule. If *YIELD is returned, the czller must call again the function with
3655 * the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003656 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003657static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003658http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003659{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003660 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003661 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003662 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003663 struct http_res_rule *rule;
3664 struct hdr_ctx ctx;
3665
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003666 /* If "the current_rule_list" match the executed rule list, we are in
3667 * resume condition. If a resume is needed it is always in the action
3668 * and never in the ACL or converters. In this case, we initialise the
3669 * current rule, and go to the action execution point.
3670 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003671 if (s->current_rule) {
3672 rule = s->current_rule;
3673 s->current_rule = NULL;
3674 if (s->current_rule_list == rules)
3675 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003676 }
3677 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003678
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003679 list_for_each_entry(rule, rules, list) {
3680 if (rule->action >= HTTP_RES_ACT_MAX)
3681 continue;
3682
3683 /* check optional condition */
3684 if (rule->cond) {
3685 int ret;
3686
Willy Tarreau192252e2015-04-04 01:47:55 +02003687 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003688 ret = acl_pass(ret);
3689
3690 if (rule->cond->pol == ACL_COND_UNLESS)
3691 ret = !ret;
3692
3693 if (!ret) /* condition not matched */
3694 continue;
3695 }
3696
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003697resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003698 switch (rule->action) {
3699 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003700 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003701
3702 case HTTP_RES_ACT_DENY:
3703 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003704 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003705
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003706 case HTTP_RES_ACT_SET_NICE:
3707 s->task->nice = rule->arg.nice;
3708 break;
3709
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003710 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003711 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003712 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003713 break;
3714
Willy Tarreau51347ed2013-06-11 19:34:13 +02003715 case HTTP_RES_ACT_SET_MARK:
3716#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003717 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003718 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003719#endif
3720 break;
3721
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003722 case HTTP_RES_ACT_SET_LOGL:
3723 s->logs.level = rule->arg.loglevel;
3724 break;
3725
Sasha Pachev218f0642014-06-16 12:05:59 -06003726 case HTTP_RES_ACT_REPLACE_HDR:
3727 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003728 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3729 rule->arg.hdr_add.name_len,
3730 &rule->arg.hdr_add.fmt,
3731 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003732 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003733 break;
3734
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003735 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003736 ctx.idx = 0;
3737 /* remove all occurrences of the header */
3738 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3739 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3740 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3741 }
Willy Tarreau85603282015-01-21 20:39:27 +01003742 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003743
Willy Tarreau85603282015-01-21 20:39:27 +01003744 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003745 case HTTP_RES_ACT_ADD_HDR:
3746 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3747 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3748 trash.len = rule->arg.hdr_add.name_len;
3749 trash.str[trash.len++] = ':';
3750 trash.str[trash.len++] = ' ';
3751 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 +01003752
3753 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3754 /* remove all occurrences of the header */
3755 ctx.idx = 0;
3756 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3757 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3758 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3759 }
3760 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003761 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3762 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003763
3764 case HTTP_RES_ACT_DEL_ACL:
3765 case HTTP_RES_ACT_DEL_MAP: {
3766 struct pat_ref *ref;
3767 char *key;
3768 int len;
3769
3770 /* collect reference */
3771 ref = pat_ref_lookup(rule->arg.map.ref);
3772 if (!ref)
3773 continue;
3774
3775 /* collect key */
3776 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3777 key = trash.str;
3778 key[len] = '\0';
3779
3780 /* perform update */
3781 /* returned code: 1=ok, 0=ko */
3782 pat_ref_delete(ref, key);
3783
3784 break;
3785 }
3786
3787 case HTTP_RES_ACT_ADD_ACL: {
3788 struct pat_ref *ref;
3789 char *key;
3790 struct chunk *trash_key;
3791 int len;
3792
3793 trash_key = get_trash_chunk();
3794
3795 /* collect reference */
3796 ref = pat_ref_lookup(rule->arg.map.ref);
3797 if (!ref)
3798 continue;
3799
3800 /* collect key */
3801 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3802 key = trash_key->str;
3803 key[len] = '\0';
3804
3805 /* perform update */
3806 /* check if the entry already exists */
3807 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003808 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003809
3810 break;
3811 }
3812
3813 case HTTP_RES_ACT_SET_MAP: {
3814 struct pat_ref *ref;
3815 char *key, *value;
3816 struct chunk *trash_key, *trash_value;
3817 int len;
3818
3819 trash_key = get_trash_chunk();
3820 trash_value = get_trash_chunk();
3821
3822 /* collect reference */
3823 ref = pat_ref_lookup(rule->arg.map.ref);
3824 if (!ref)
3825 continue;
3826
3827 /* collect key */
3828 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3829 key = trash_key->str;
3830 key[len] = '\0';
3831
3832 /* collect value */
3833 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3834 value = trash_value->str;
3835 value[len] = '\0';
3836
3837 /* perform update */
3838 if (pat_ref_find_elt(ref, key) != NULL)
3839 /* update entry if it exists */
3840 pat_ref_set(ref, key, value, NULL);
3841 else
3842 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003843 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003844
3845 break;
3846 }
William Lallemand73025dd2014-04-24 14:38:37 +02003847
3848 case HTTP_RES_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003849 if (!rule->action_ptr(rule, px, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003850 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003851 return HTTP_RULE_RES_YIELD;
3852 }
William Lallemand73025dd2014-04-24 14:38:37 +02003853 break;
3854
3855 case HTTP_RES_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003856 rule->action_ptr(rule, px, s);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003857 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003858 }
3859 }
3860
3861 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003862 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003863}
3864
3865
Willy Tarreau71241ab2012-12-27 11:30:54 +01003866/* Perform an HTTP redirect based on the information in <rule>. The function
3867 * returns non-zero on success, or zero in case of a, irrecoverable error such
3868 * as too large a request to build a valid response.
3869 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003870static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003871{
3872 struct http_msg *msg = &txn->req;
3873 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003874 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003875
3876 /* build redirect message */
3877 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003878 case 308:
3879 msg_fmt = HTTP_308;
3880 break;
3881 case 307:
3882 msg_fmt = HTTP_307;
3883 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003884 case 303:
3885 msg_fmt = HTTP_303;
3886 break;
3887 case 301:
3888 msg_fmt = HTTP_301;
3889 break;
3890 case 302:
3891 default:
3892 msg_fmt = HTTP_302;
3893 break;
3894 }
3895
3896 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3897 return 0;
3898
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003899 location = trash.str + trash.len;
3900
Willy Tarreau71241ab2012-12-27 11:30:54 +01003901 switch(rule->type) {
3902 case REDIRECT_TYPE_SCHEME: {
3903 const char *path;
3904 const char *host;
3905 struct hdr_ctx ctx;
3906 int pathlen;
3907 int hostlen;
3908
3909 host = "";
3910 hostlen = 0;
3911 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003912 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003913 host = ctx.line + ctx.val;
3914 hostlen = ctx.vlen;
3915 }
3916
3917 path = http_get_path(txn);
3918 /* build message using path */
3919 if (path) {
3920 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3921 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3922 int qs = 0;
3923 while (qs < pathlen) {
3924 if (path[qs] == '?') {
3925 pathlen = qs;
3926 break;
3927 }
3928 qs++;
3929 }
3930 }
3931 } else {
3932 path = "/";
3933 pathlen = 1;
3934 }
3935
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003936 if (rule->rdr_str) { /* this is an old "redirect" rule */
3937 /* check if we can add scheme + "://" + host + path */
3938 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3939 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003940
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003941 /* add scheme */
3942 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3943 trash.len += rule->rdr_len;
3944 }
3945 else {
3946 /* add scheme with executing log format */
3947 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003948
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003949 /* check if we can add scheme + "://" + host + path */
3950 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3951 return 0;
3952 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003953 /* add "://" */
3954 memcpy(trash.str + trash.len, "://", 3);
3955 trash.len += 3;
3956
3957 /* add host */
3958 memcpy(trash.str + trash.len, host, hostlen);
3959 trash.len += hostlen;
3960
3961 /* add path */
3962 memcpy(trash.str + trash.len, path, pathlen);
3963 trash.len += pathlen;
3964
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003965 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003966 if (trash.len && trash.str[trash.len - 1] != '/' &&
3967 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3968 if (trash.len > trash.size - 5)
3969 return 0;
3970 trash.str[trash.len] = '/';
3971 trash.len++;
3972 }
3973
3974 break;
3975 }
3976 case REDIRECT_TYPE_PREFIX: {
3977 const char *path;
3978 int pathlen;
3979
3980 path = http_get_path(txn);
3981 /* build message using path */
3982 if (path) {
3983 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3984 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3985 int qs = 0;
3986 while (qs < pathlen) {
3987 if (path[qs] == '?') {
3988 pathlen = qs;
3989 break;
3990 }
3991 qs++;
3992 }
3993 }
3994 } else {
3995 path = "/";
3996 pathlen = 1;
3997 }
3998
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003999 if (rule->rdr_str) { /* this is an old "redirect" rule */
4000 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4001 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004002
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004003 /* add prefix. Note that if prefix == "/", we don't want to
4004 * add anything, otherwise it makes it hard for the user to
4005 * configure a self-redirection.
4006 */
4007 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4008 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4009 trash.len += rule->rdr_len;
4010 }
4011 }
4012 else {
4013 /* add prefix with executing log format */
4014 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4015
4016 /* Check length */
4017 if (trash.len + pathlen > trash.size - 4)
4018 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004019 }
4020
4021 /* add path */
4022 memcpy(trash.str + trash.len, path, pathlen);
4023 trash.len += pathlen;
4024
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004025 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004026 if (trash.len && trash.str[trash.len - 1] != '/' &&
4027 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4028 if (trash.len > trash.size - 5)
4029 return 0;
4030 trash.str[trash.len] = '/';
4031 trash.len++;
4032 }
4033
4034 break;
4035 }
4036 case REDIRECT_TYPE_LOCATION:
4037 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004038 if (rule->rdr_str) { /* this is an old "redirect" rule */
4039 if (trash.len + rule->rdr_len > trash.size - 4)
4040 return 0;
4041
4042 /* add location */
4043 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4044 trash.len += rule->rdr_len;
4045 }
4046 else {
4047 /* add location with executing log format */
4048 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004049
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004050 /* Check left length */
4051 if (trash.len > trash.size - 4)
4052 return 0;
4053 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004054 break;
4055 }
4056
4057 if (rule->cookie_len) {
4058 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4059 trash.len += 14;
4060 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4061 trash.len += rule->cookie_len;
4062 memcpy(trash.str + trash.len, "\r\n", 2);
4063 trash.len += 2;
4064 }
4065
4066 /* add end of headers and the keep-alive/close status.
4067 * We may choose to set keep-alive if the Location begins
4068 * with a slash, because the client will come back to the
4069 * same server.
4070 */
4071 txn->status = rule->code;
4072 /* let's log the request time */
4073 s->logs.tv_request = now;
4074
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004075 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004076 (msg->flags & HTTP_MSGF_XFER_LEN) &&
4077 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
4078 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4079 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4080 /* keep-alive possible */
4081 if (!(msg->flags & HTTP_MSGF_VER_11)) {
4082 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4083 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4084 trash.len += 30;
4085 } else {
4086 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4087 trash.len += 24;
4088 }
4089 }
4090 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4091 trash.len += 4;
4092 bo_inject(txn->rsp.chn, trash.str, trash.len);
4093 /* "eat" the request */
4094 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02004095 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004096 msg->sov = 0;
4097 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004098 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004099 txn->req.msg_state = HTTP_MSG_CLOSED;
4100 txn->rsp.msg_state = HTTP_MSG_DONE;
4101 } else {
4102 /* keep-alive not possible */
4103 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4104 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4105 trash.len += 29;
4106 } else {
4107 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4108 trash.len += 23;
4109 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004110 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004111 txn->req.chn->analysers = 0;
4112 }
4113
Willy Tarreaue7dff022015-04-03 01:14:29 +02004114 if (!(s->flags & SF_ERR_MASK))
4115 s->flags |= SF_ERR_LOCAL;
4116 if (!(s->flags & SF_FINST_MASK))
4117 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004118
4119 return 1;
4120}
4121
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004122/* This stream analyser runs all HTTP request processing which is common to
4123 * frontends and backends, which means blocking ACLs, filters, connection-close,
4124 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004125 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004126 * either needs more data or wants to immediately abort the request (eg: deny,
4127 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004128 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004129int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004130{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004131 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004132 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004133 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004134 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004135 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004136 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004137
Willy Tarreau655dce92009-11-08 13:10:58 +01004138 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004139 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004140 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004141 }
4142
Willy Tarreau87b09662015-04-03 00:22:06 +02004143 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 +02004144 now_ms, __FUNCTION__,
4145 s,
4146 req,
4147 req->rex, req->wex,
4148 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004149 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004150 req->analysers);
4151
Willy Tarreau65410832014-04-28 21:25:43 +02004152 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004153 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004154
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004155 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004156 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004157 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004158
Willy Tarreau0b748332014-04-29 00:13:29 +02004159 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004160 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4161 goto return_prx_yield;
4162
Willy Tarreau0b748332014-04-29 00:13:29 +02004163 case HTTP_RULE_RES_CONT:
4164 case HTTP_RULE_RES_STOP: /* nothing to do */
4165 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004166
Willy Tarreau0b748332014-04-29 00:13:29 +02004167 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4168 if (txn->flags & TX_CLTARPIT)
4169 goto tarpit;
4170 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004171
Willy Tarreau0b748332014-04-29 00:13:29 +02004172 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4173 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004174
Willy Tarreau0b748332014-04-29 00:13:29 +02004175 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004176 goto done;
4177
Willy Tarreau0b748332014-04-29 00:13:29 +02004178 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4179 goto return_bad_req;
4180 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004181 }
4182
Willy Tarreau52542592014-04-28 18:33:26 +02004183 /* OK at this stage, we know that the request was accepted according to
4184 * the http-request rules, we can check for the stats. Note that the
4185 * URI is detected *before* the req* rules in order not to be affected
4186 * by a possible reqrep, while they are processed *after* so that a
4187 * reqdeny can still block them. This clearly needs to change in 1.6!
4188 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004189 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004190 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004191 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004192 txn->status = 500;
4193 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004194 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004195
Willy Tarreaue7dff022015-04-03 01:14:29 +02004196 if (!(s->flags & SF_ERR_MASK))
4197 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004198 goto return_prx_cond;
4199 }
4200
4201 /* parse the whole stats request and extract the relevant information */
4202 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004203 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004204 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004205
Willy Tarreau0b748332014-04-29 00:13:29 +02004206 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4207 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004208
Willy Tarreau0b748332014-04-29 00:13:29 +02004209 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4210 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004211 }
4212
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004213 /* evaluate the req* rules except reqadd */
4214 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004215 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004216 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004217
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004218 if (txn->flags & TX_CLDENY)
4219 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004220
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004221 if (txn->flags & TX_CLTARPIT)
4222 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004223 }
Willy Tarreau06619262006-12-17 08:37:22 +01004224
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004225 /* add request headers from the rule sets in the same order */
4226 list_for_each_entry(wl, &px->req_add, list) {
4227 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004228 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004229 ret = acl_pass(ret);
4230 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4231 ret = !ret;
4232 if (!ret)
4233 continue;
4234 }
4235
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004236 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004237 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004238 }
4239
Willy Tarreau52542592014-04-28 18:33:26 +02004240
4241 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004242 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004243 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004244 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4245 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004246
Willy Tarreaue7dff022015-04-03 01:14:29 +02004247 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4248 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4249 if (!(s->flags & SF_FINST_MASK))
4250 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004251
Willy Tarreau70730dd2014-04-24 18:06:27 +02004252 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004253 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004254 select_compression_request_header(s, req->buf);
4255
4256 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004257 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004258 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004259 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004260
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004261 /* check whether we have some ACLs set to redirect this request */
4262 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004263 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004264 int ret;
4265
Willy Tarreau192252e2015-04-04 01:47:55 +02004266 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004267 ret = acl_pass(ret);
4268 if (rule->cond->pol == ACL_COND_UNLESS)
4269 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004270 if (!ret)
4271 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004272 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004273 if (!http_apply_redirect_rule(rule, s, txn))
4274 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004275 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004276 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004277
Willy Tarreau2be39392010-01-03 17:24:51 +01004278 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4279 * If this happens, then the data will not come immediately, so we must
4280 * send all what we have without waiting. Note that due to the small gain
4281 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004282 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004283 * itself once used.
4284 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004285 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004286
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004287 done: /* done with this analyser, continue with next ones that the calling
4288 * points will have set, if any.
4289 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004290 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004291 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4292 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004293 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004294
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004295 tarpit:
4296 /* When a connection is tarpitted, we use the tarpit timeout,
4297 * which may be the same as the connect timeout if unspecified.
4298 * If unset, then set it to zero because we really want it to
4299 * eventually expire. We build the tarpit as an analyser.
4300 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004301 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004302
4303 /* wipe the request out so that we can drop the connection early
4304 * if the client closes first.
4305 */
4306 channel_dont_connect(req);
4307 req->analysers = 0; /* remove switching rules etc... */
4308 req->analysers |= AN_REQ_HTTP_TARPIT;
4309 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4310 if (!req->analyse_exp)
4311 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004312 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004313 sess->fe->fe_counters.denied_req++;
4314 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004315 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004316 if (sess->listener->counters)
4317 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004318 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004319
4320 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004321 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004322 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004323 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004324 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004325 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004326 sess->fe->fe_counters.denied_req++;
4327 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004328 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004329 if (sess->listener->counters)
4330 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004331 goto return_prx_cond;
4332
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004333 return_bad_req:
4334 /* We centralize bad requests processing here */
4335 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4336 /* we detected a parsing error. We want to archive this request
4337 * in the dedicated proxy area for later troubleshooting.
4338 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004339 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004340 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004341
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004342 txn->req.msg_state = HTTP_MSG_ERROR;
4343 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004344 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004345
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004346 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004347 if (sess->listener->counters)
4348 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004349
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004350 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004351 if (!(s->flags & SF_ERR_MASK))
4352 s->flags |= SF_ERR_PRXCOND;
4353 if (!(s->flags & SF_FINST_MASK))
4354 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004355
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004356 req->analysers = 0;
4357 req->analyse_exp = TICK_ETERNITY;
4358 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004359
4360 return_prx_yield:
4361 channel_dont_connect(req);
4362 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004363}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004364
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004365/* This function performs all the processing enabled for the current request.
4366 * It returns 1 if the processing can continue on next analysers, or zero if it
4367 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004368 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004369 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004370int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004371{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004372 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004373 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004374 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004375 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004376
Willy Tarreau655dce92009-11-08 13:10:58 +01004377 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004378 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004379 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004380 return 0;
4381 }
4382
Willy Tarreau87b09662015-04-03 00:22:06 +02004383 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 +02004384 now_ms, __FUNCTION__,
4385 s,
4386 req,
4387 req->rex, req->wex,
4388 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004389 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004390 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004391
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004392 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004393 select_compression_request_header(s, req->buf);
4394
Willy Tarreau59234e92008-11-30 23:51:27 +01004395 /*
4396 * Right now, we know that we have processed the entire headers
4397 * and that unwanted requests have been filtered out. We can do
4398 * whatever we want with the remaining request. Also, now we
4399 * may have separate values for ->fe, ->be.
4400 */
Willy Tarreau06619262006-12-17 08:37:22 +01004401
Willy Tarreau59234e92008-11-30 23:51:27 +01004402 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004403 * If HTTP PROXY is set we simply get remote server address parsing
4404 * incoming request. Note that this requires that a connection is
4405 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004406 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004407 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004408 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004409 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004410
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004411 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau350f4872014-11-28 14:42:25 +01004412 if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004413 txn->req.msg_state = HTTP_MSG_ERROR;
4414 txn->status = 500;
4415 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004416 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004417
Willy Tarreaue7dff022015-04-03 01:14:29 +02004418 if (!(s->flags & SF_ERR_MASK))
4419 s->flags |= SF_ERR_RESOURCE;
4420 if (!(s->flags & SF_FINST_MASK))
4421 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004422
4423 return 0;
4424 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004425
4426 path = http_get_path(txn);
4427 url2sa(req->buf->p + msg->sl.rq.u,
4428 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004429 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004430 /* if the path was found, we have to remove everything between
4431 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4432 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4433 * u_l characters by a single "/".
4434 */
4435 if (path) {
4436 char *cur_ptr = req->buf->p;
4437 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4438 int delta;
4439
4440 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4441 http_msg_move_end(&txn->req, delta);
4442 cur_end += delta;
4443 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4444 goto return_bad_req;
4445 }
4446 else {
4447 char *cur_ptr = req->buf->p;
4448 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4449 int delta;
4450
4451 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4452 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4453 http_msg_move_end(&txn->req, delta);
4454 cur_end += delta;
4455 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4456 goto return_bad_req;
4457 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004458 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004459
Willy Tarreau59234e92008-11-30 23:51:27 +01004460 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004461 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004462 * Note that doing so might move headers in the request, but
4463 * the fields will stay coherent and the URI will not move.
4464 * This should only be performed in the backend.
4465 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004466 if ((s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004467 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4468 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004469
Willy Tarreau59234e92008-11-30 23:51:27 +01004470 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004471 * 8: the appsession cookie was looked up very early in 1.2,
4472 * so let's do the same now.
4473 */
4474
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004475 /* It needs to look into the URI unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004476 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004477 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 +01004478 }
4479
William Lallemanda73203e2012-03-12 12:48:57 +01004480 /* add unique-id if "header-unique-id" is specified */
4481
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004482 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004483 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4484 goto return_bad_req;
4485 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004486 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004487 }
William Lallemanda73203e2012-03-12 12:48:57 +01004488
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004489 if (sess->fe->header_unique_id && s->unique_id) {
4490 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004491 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004492 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004493 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004494 goto return_bad_req;
4495 }
4496
Cyril Bontéb21570a2009-11-29 20:04:48 +01004497 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004498 * 9: add X-Forwarded-For if either the frontend or the backend
4499 * asks for it.
4500 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004501 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004502 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004503 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4504 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4505 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004506 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004507 /* The header is set to be added only if none is present
4508 * and we found it, so don't do anything.
4509 */
4510 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004511 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004512 /* Add an X-Forwarded-For header unless the source IP is
4513 * in the 'except' network range.
4514 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004515 if ((!sess->fe->except_mask.s_addr ||
4516 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4517 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004518 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004519 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004520 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004521 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004522 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004523 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004524
4525 /* Note: we rely on the backend to get the header name to be used for
4526 * x-forwarded-for, because the header is really meant for the backends.
4527 * However, if the backend did not specify any option, we have to rely
4528 * on the frontend's header name.
4529 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004530 if (s->be->fwdfor_hdr_len) {
4531 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004532 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004533 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004534 len = sess->fe->fwdfor_hdr_len;
4535 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004536 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004537 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 +01004538
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004539 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004540 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004541 }
4542 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004543 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004544 /* FIXME: for the sake of completeness, we should also support
4545 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004546 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004547 int len;
4548 char pn[INET6_ADDRSTRLEN];
4549 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004550 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004551 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004552
Willy Tarreau59234e92008-11-30 23:51:27 +01004553 /* Note: we rely on the backend to get the header name to be used for
4554 * x-forwarded-for, because the header is really meant for the backends.
4555 * However, if the backend did not specify any option, we have to rely
4556 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004557 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004558 if (s->be->fwdfor_hdr_len) {
4559 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004560 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004561 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004562 len = sess->fe->fwdfor_hdr_len;
4563 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004564 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004565 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004566
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004567 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004568 goto return_bad_req;
4569 }
4570 }
4571
4572 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004573 * 10: add X-Original-To if either the frontend or the backend
4574 * asks for it.
4575 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004576 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004577
4578 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004579 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004580 /* Add an X-Original-To header unless the destination IP is
4581 * in the 'except' network range.
4582 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004583 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004584
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004585 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004586 ((!sess->fe->except_mask_to.s_addr ||
4587 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4588 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004589 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004590 (((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 +02004591 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004592 int len;
4593 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004594 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004595
4596 /* Note: we rely on the backend to get the header name to be used for
4597 * x-original-to, because the header is really meant for the backends.
4598 * However, if the backend did not specify any option, we have to rely
4599 * on the frontend's header name.
4600 */
4601 if (s->be->orgto_hdr_len) {
4602 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004603 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004604 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004605 len = sess->fe->orgto_hdr_len;
4606 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004607 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004608 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 +02004609
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004610 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004611 goto return_bad_req;
4612 }
4613 }
4614 }
4615
Willy Tarreau50fc7772012-11-11 22:19:57 +01004616 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4617 * If an "Upgrade" token is found, the header is left untouched in order not to have
4618 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4619 * "Upgrade" is present in the Connection header.
4620 */
4621 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4622 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004623 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004624 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004625 unsigned int want_flags = 0;
4626
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004627 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004628 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004629 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004630 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004631 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004632 want_flags |= TX_CON_CLO_SET;
4633 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004634 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004635 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004636 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004637 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004638 want_flags |= TX_CON_KAL_SET;
4639 }
4640
4641 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004642 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004643 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004644
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004645
Willy Tarreau522d6c02009-12-06 18:49:18 +01004646 /* If we have no server assigned yet and we're balancing on url_param
4647 * with a POST request, we may be interested in checking the body for
4648 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004649 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004650 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004651 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004652 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004653 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004654 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004655 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004656
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004657 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004658 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004659#ifdef TCP_QUICKACK
4660 /* We expect some data from the client. Unless we know for sure
4661 * we already have a full request, we have to re-enable quick-ack
4662 * in case we previously disabled it, otherwise we might cause
4663 * the client to delay further data.
4664 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004665 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004666 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004667 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004668 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004669 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004670#endif
4671 }
Willy Tarreau03945942009-12-22 16:50:27 +01004672
Willy Tarreau59234e92008-11-30 23:51:27 +01004673 /*************************************************************
4674 * OK, that's finished for the headers. We have done what we *
4675 * could. Let's switch to the DATA state. *
4676 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004677 req->analyse_exp = TICK_ETERNITY;
4678 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004679
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004680 /* if the server closes the connection, we want to immediately react
4681 * and close the socket to save packets and syscalls.
4682 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004683 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004684 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004685
Willy Tarreau59234e92008-11-30 23:51:27 +01004686 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004687 /* OK let's go on with the BODY now */
4688 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004689
Willy Tarreau59234e92008-11-30 23:51:27 +01004690 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004691 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004692 /* we detected a parsing error. We want to archive this request
4693 * in the dedicated proxy area for later troubleshooting.
4694 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004695 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004696 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004697
Willy Tarreau59234e92008-11-30 23:51:27 +01004698 txn->req.msg_state = HTTP_MSG_ERROR;
4699 txn->status = 400;
4700 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004701 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004702
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004703 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004704 if (sess->listener->counters)
4705 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004706
Willy Tarreaue7dff022015-04-03 01:14:29 +02004707 if (!(s->flags & SF_ERR_MASK))
4708 s->flags |= SF_ERR_PRXCOND;
4709 if (!(s->flags & SF_FINST_MASK))
4710 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004711 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004712}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004713
Willy Tarreau60b85b02008-11-30 23:28:40 +01004714/* This function is an analyser which processes the HTTP tarpit. It always
4715 * returns zero, at the beginning because it prevents any other processing
4716 * from occurring, and at the end because it terminates the request.
4717 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004718int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004719{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004720 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004721
4722 /* This connection is being tarpitted. The CLIENT side has
4723 * already set the connect expiration date to the right
4724 * timeout. We just have to check that the client is still
4725 * there and that the timeout has not expired.
4726 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004727 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004728 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004729 !tick_is_expired(req->analyse_exp, now_ms))
4730 return 0;
4731
4732 /* We will set the queue timer to the time spent, just for
4733 * logging purposes. We fake a 500 server error, so that the
4734 * attacker will not suspect his connection has been tarpitted.
4735 * It will not cause trouble to the logs because we can exclude
4736 * the tarpitted connections by filtering on the 'PT' status flags.
4737 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004738 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4739
4740 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004741 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004742 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004743
4744 req->analysers = 0;
4745 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004746
Willy Tarreaue7dff022015-04-03 01:14:29 +02004747 if (!(s->flags & SF_ERR_MASK))
4748 s->flags |= SF_ERR_PRXCOND;
4749 if (!(s->flags & SF_FINST_MASK))
4750 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004751 return 0;
4752}
4753
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004754/* This function is an analyser which waits for the HTTP request body. It waits
4755 * for either the buffer to be full, or the full advertised contents to have
4756 * reached the buffer. It must only be called after the standard HTTP request
4757 * processing has occurred, because it expects the request to be parsed and will
4758 * look for the Expect header. It may send a 100-Continue interim response. It
4759 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4760 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4761 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004762 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004763int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004764{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004765 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004766 struct http_txn *txn = s->txn;
4767 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004768
4769 /* We have to parse the HTTP request body to find any required data.
4770 * "balance url_param check_post" should have been the only way to get
4771 * into this. We were brought here after HTTP header analysis, so all
4772 * related structures are ready.
4773 */
4774
Willy Tarreau890988f2014-04-10 11:59:33 +02004775 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4776 /* This is the first call */
4777 if (msg->msg_state < HTTP_MSG_BODY)
4778 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004779
Willy Tarreau890988f2014-04-10 11:59:33 +02004780 if (msg->msg_state < HTTP_MSG_100_SENT) {
4781 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4782 * send an HTTP/1.1 100 Continue intermediate response.
4783 */
4784 if (msg->flags & HTTP_MSGF_VER_11) {
4785 struct hdr_ctx ctx;
4786 ctx.idx = 0;
4787 /* Expect is allowed in 1.1, look for it */
4788 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4789 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004790 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004791 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004792 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004793 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004794 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004795
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004796 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004797 * req->buf->p still points to the beginning of the message. We
4798 * must save the body in msg->next because it survives buffer
4799 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004800 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004801 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004802
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004803 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004804 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4805 else
4806 msg->msg_state = HTTP_MSG_DATA;
4807 }
4808
Willy Tarreau890988f2014-04-10 11:59:33 +02004809 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4810 /* We're in content-length mode, we just have to wait for enough data. */
4811 if (req->buf->i - msg->sov < msg->body_len)
4812 goto missing_data;
4813
4814 /* OK we have everything we need now */
4815 goto http_end;
4816 }
4817
4818 /* OK here we're parsing a chunked-encoded message */
4819
Willy Tarreau522d6c02009-12-06 18:49:18 +01004820 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004821 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004822 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004823 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004824 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004825 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004826
Willy Tarreau115acb92009-12-26 13:56:06 +01004827 if (!ret)
4828 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004829 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004830 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004831 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004832 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004833 }
4834
Willy Tarreaud98cf932009-12-27 22:54:55 +01004835 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004836 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004837 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004838 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004839 if (msg->msg_state == HTTP_MSG_TRAILERS)
4840 goto http_end;
4841
Willy Tarreau226071e2014-04-10 11:55:45 +02004842 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004843 goto http_end;
4844
4845 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004846 /* we get here if we need to wait for more data. If the buffer is full,
4847 * we have the maximum we can expect.
4848 */
4849 if (buffer_full(req->buf, global.tune.maxrewrite))
4850 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004851
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004852 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004853 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004854 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004855
Willy Tarreaue7dff022015-04-03 01:14:29 +02004856 if (!(s->flags & SF_ERR_MASK))
4857 s->flags |= SF_ERR_CLITO;
4858 if (!(s->flags & SF_FINST_MASK))
4859 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004860 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004861 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004862
4863 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004864 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004865 /* Not enough data. We'll re-use the http-request
4866 * timeout here. Ideally, we should set the timeout
4867 * relative to the accept() date. We just set the
4868 * request timeout once at the beginning of the
4869 * request.
4870 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004871 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004872 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004873 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004874 return 0;
4875 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004876
4877 http_end:
4878 /* The situation will not evolve, so let's give up on the analysis. */
4879 s->logs.tv_request = now; /* update the request timer to reflect full request */
4880 req->analysers &= ~an_bit;
4881 req->analyse_exp = TICK_ETERNITY;
4882 return 1;
4883
4884 return_bad_req: /* let's centralize all bad requests */
4885 txn->req.msg_state = HTTP_MSG_ERROR;
4886 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004887 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004888
Willy Tarreaue7dff022015-04-03 01:14:29 +02004889 if (!(s->flags & SF_ERR_MASK))
4890 s->flags |= SF_ERR_PRXCOND;
4891 if (!(s->flags & SF_FINST_MASK))
4892 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004893
Willy Tarreau522d6c02009-12-06 18:49:18 +01004894 return_err_msg:
4895 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004896 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004897 if (sess->listener->counters)
4898 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004899 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004900}
4901
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004902/* send a server's name with an outgoing request over an established connection.
4903 * Note: this function is designed to be called once the request has been scheduled
4904 * for being forwarded. This is the reason why it rewinds the buffer before
4905 * proceeding.
4906 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004907int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004908
4909 struct hdr_ctx ctx;
4910
Mark Lamourinec2247f02012-01-04 13:02:01 -05004911 char *hdr_name = be->server_id_hdr_name;
4912 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004913 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004914 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004915 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004916
William Lallemandd9e90662012-01-30 17:27:17 +01004917 ctx.idx = 0;
4918
Willy Tarreau211cdec2014-04-17 20:18:08 +02004919 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004920 if (old_o) {
4921 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004922 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004923 txn->req.next += old_o;
4924 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004925 }
4926
Willy Tarreau9b28e032012-10-12 23:49:43 +02004927 old_i = chn->buf->i;
4928 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 -05004929 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004930 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004931 }
4932
4933 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004934 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004935 memcpy(hdr_val, hdr_name, hdr_name_len);
4936 hdr_val += hdr_name_len;
4937 *hdr_val++ = ':';
4938 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004939 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4940 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004941
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004942 if (old_o) {
4943 /* If this was a forwarded request, we must readjust the amount of
4944 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004945 * variations. Note that the current state is >= HTTP_MSG_BODY,
4946 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004947 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004948 old_o += chn->buf->i - old_i;
4949 b_adv(chn->buf, old_o);
4950 txn->req.next -= old_o;
4951 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004952 }
4953
Mark Lamourinec2247f02012-01-04 13:02:01 -05004954 return 0;
4955}
4956
Willy Tarreau610ecce2010-01-04 21:15:02 +01004957/* Terminate current transaction and prepare a new one. This is very tricky
4958 * right now but it works.
4959 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004960void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004961{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004962 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004963 struct proxy *fe = strm_fe(s);
Willy Tarreau068621e2013-12-23 15:11:25 +01004964
Willy Tarreau610ecce2010-01-04 21:15:02 +01004965 /* FIXME: We need a more portable way of releasing a backend's and a
4966 * server's connections. We need a safer way to reinitialize buffer
4967 * flags. We also need a more accurate method for computing per-request
4968 * data.
4969 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004970
Willy Tarreau4213a112013-12-15 10:25:42 +01004971 /* unless we're doing keep-alive, we want to quickly close the connection
4972 * to the server.
4973 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004974 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004975 !si_conn_ready(&s->si[1])) {
4976 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4977 si_shutr(&s->si[1]);
4978 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004979 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004980
Willy Tarreaue7dff022015-04-03 01:14:29 +02004981 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004982 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004983 if (unlikely(s->srv_conn))
4984 sess_change_server(s, NULL);
4985 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004986
4987 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004988 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004989
Willy Tarreaueee5b512015-04-03 23:46:31 +02004990 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004991 int n;
4992
Willy Tarreaueee5b512015-04-03 23:46:31 +02004993 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004994 if (n < 1 || n > 5)
4995 n = 0;
4996
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004997 if (fe->mode == PR_MODE_HTTP) {
4998 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02004999 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005000 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005001 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005002 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005003 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005004 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005005 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005006 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005007 s->be->be_counters.p.http.comp_rsp++;
5008 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005009 }
5010
5011 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005012 s->logs.bytes_in -= s->req.buf->i;
5013 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005014
5015 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005016 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005017 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005018 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005019 s->do_log(s);
5020 }
5021
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005022 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005023 stream_stop_content_counters(s);
5024 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005025
Willy Tarreau610ecce2010-01-04 21:15:02 +01005026 s->logs.accept_date = date; /* user-visible date for logging */
5027 s->logs.tv_accept = now; /* corrected date for internal use */
5028 tv_zero(&s->logs.tv_request);
5029 s->logs.t_queue = -1;
5030 s->logs.t_connect = -1;
5031 s->logs.t_data = -1;
5032 s->logs.t_close = 0;
5033 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5034 s->logs.srv_queue_size = 0; /* we will get this number soon */
5035
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005036 s->logs.bytes_in = s->req.total = s->req.buf->i;
5037 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005038
5039 if (s->pend_pos)
5040 pendconn_free(s->pend_pos);
5041
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005042 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005043 if (s->flags & SF_CURR_SESS) {
5044 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005045 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005046 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005047 if (may_dequeue_tasks(objt_server(s->target), s->be))
5048 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005049 }
5050
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005051 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005052
Willy Tarreau4213a112013-12-15 10:25:42 +01005053 /* only release our endpoint if we don't intend to reuse the
5054 * connection.
5055 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005056 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005057 !si_conn_ready(&s->si[1])) {
5058 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005059 }
5060
Willy Tarreau350f4872014-11-28 14:42:25 +01005061 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5062 s->si[1].err_type = SI_ET_NONE;
5063 s->si[1].conn_retries = 0; /* used for logging too */
5064 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005065 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 +01005066 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);
5067 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 +02005068 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5069 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5070 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005071
Willy Tarreaueee5b512015-04-03 23:46:31 +02005072 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005073 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005074 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005075
5076 if (prev_status == 401 || prev_status == 407) {
5077 /* In HTTP keep-alive mode, if we receive a 401, we still have
5078 * a chance of being able to send the visitor again to the same
5079 * server over the same connection. This is required by some
5080 * broken protocols such as NTLM, and anyway whenever there is
5081 * an opportunity for sending the challenge to the proper place,
5082 * it's better to do it (at least it helps with debugging).
5083 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005084 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01005085 }
5086
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005087 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005088 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005089
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005090 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005091 s->req.flags |= CF_NEVER_WAIT;
5092 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005093 }
5094
Willy Tarreau610ecce2010-01-04 21:15:02 +01005095 /* if the request buffer is not empty, it means we're
5096 * about to process another request, so send pending
5097 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005098 * Just don't do this if the buffer is close to be full,
5099 * because the request will wait for it to flush a little
5100 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005101 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005102 if (s->req.buf->i) {
5103 if (s->res.buf->o &&
5104 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5105 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5106 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005107 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005108
5109 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005110 channel_auto_read(&s->req);
5111 channel_auto_close(&s->req);
5112 channel_auto_read(&s->res);
5113 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005114
Willy Tarreau27375622013-12-17 00:00:28 +01005115 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau350f4872014-11-28 14:42:25 +01005116 si_idle_conn(&s->si[1]);
Willy Tarreau27375622013-12-17 00:00:28 +01005117
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005118 s->req.analysers = strm_li(s)->analysers;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005119 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005120}
5121
5122
5123/* This function updates the request state machine according to the response
5124 * state machine and buffer flags. It returns 1 if it changes anything (flag
5125 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5126 * it is only used to find when a request/response couple is complete. Both
5127 * this function and its equivalent should loop until both return zero. It
5128 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5129 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005130int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005131{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005132 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005133 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005134 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005135 unsigned int old_state = txn->req.msg_state;
5136
Willy Tarreau610ecce2010-01-04 21:15:02 +01005137 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5138 return 0;
5139
5140 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005141 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005142 * We can shut the read side unless we want to abort_on_close,
5143 * or we have a POST request. The issue with POST requests is
5144 * that some browsers still send a CRLF after the request, and
5145 * this CRLF must be read so that it does not remain in the kernel
5146 * buffers, otherwise a close could cause an RST on some systems
5147 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005148 * Note that if we're using keep-alive on the client side, we'd
5149 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005150 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005151 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005152 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005153 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5154 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5155 !(s->be->options & PR_O_ABRT_CLOSE) &&
5156 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005157 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005158
Willy Tarreau40f151a2012-12-20 12:10:09 +01005159 /* if the server closes the connection, we want to immediately react
5160 * and close the socket to save packets and syscalls.
5161 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005162 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005163
Willy Tarreau610ecce2010-01-04 21:15:02 +01005164 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5165 goto wait_other_side;
5166
5167 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5168 /* The server has not finished to respond, so we
5169 * don't want to move in order not to upset it.
5170 */
5171 goto wait_other_side;
5172 }
5173
5174 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5175 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005176 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005177 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005178 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005179 goto wait_other_side;
5180 }
5181
5182 /* When we get here, it means that both the request and the
5183 * response have finished receiving. Depending on the connection
5184 * mode, we'll have to wait for the last bytes to leave in either
5185 * direction, and sometimes for a close to be effective.
5186 */
5187
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005188 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5189 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005190 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5191 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005192 }
5193 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5194 /* Option forceclose is set, or either side wants to close,
5195 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005196 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005197 * once both states are CLOSED.
5198 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005199 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5200 channel_shutr_now(chn);
5201 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005202 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005203 }
5204 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005205 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5206 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005207 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005208 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5209 channel_auto_read(chn);
5210 txn->req.msg_state = HTTP_MSG_TUNNEL;
5211 chn->flags |= CF_NEVER_WAIT;
5212 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005213 }
5214
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005215 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005216 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005217 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005218
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005219 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005220 txn->req.msg_state = HTTP_MSG_CLOSING;
5221 goto http_msg_closing;
5222 }
5223 else {
5224 txn->req.msg_state = HTTP_MSG_CLOSED;
5225 goto http_msg_closed;
5226 }
5227 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005228 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005229 }
5230
5231 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5232 http_msg_closing:
5233 /* nothing else to forward, just waiting for the output buffer
5234 * to be empty and for the shutw_now to take effect.
5235 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005236 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005237 txn->req.msg_state = HTTP_MSG_CLOSED;
5238 goto http_msg_closed;
5239 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005240 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005241 txn->req.msg_state = HTTP_MSG_ERROR;
5242 goto wait_other_side;
5243 }
5244 }
5245
5246 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5247 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005248 /* see above in MSG_DONE why we only do this in these states */
5249 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5250 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5251 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005252 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005253 goto wait_other_side;
5254 }
5255
5256 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005257 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005258}
5259
5260
5261/* This function updates the response state machine according to the request
5262 * state machine and buffer flags. It returns 1 if it changes anything (flag
5263 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5264 * it is only used to find when a request/response couple is complete. Both
5265 * this function and its equivalent should loop until both return zero. It
5266 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5267 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005268int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005269{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005270 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005271 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005272 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005273 unsigned int old_state = txn->rsp.msg_state;
5274
Willy Tarreau610ecce2010-01-04 21:15:02 +01005275 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5276 return 0;
5277
5278 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5279 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005280 * still monitor the server connection for a possible close
5281 * while the request is being uploaded, so we don't disable
5282 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005283 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005284 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005285
5286 if (txn->req.msg_state == HTTP_MSG_ERROR)
5287 goto wait_other_side;
5288
5289 if (txn->req.msg_state < HTTP_MSG_DONE) {
5290 /* The client seems to still be sending data, probably
5291 * because we got an error response during an upload.
5292 * We have the choice of either breaking the connection
5293 * or letting it pass through. Let's do the later.
5294 */
5295 goto wait_other_side;
5296 }
5297
5298 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5299 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005300 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005301 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005302 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005303 goto wait_other_side;
5304 }
5305
5306 /* When we get here, it means that both the request and the
5307 * response have finished receiving. Depending on the connection
5308 * mode, we'll have to wait for the last bytes to leave in either
5309 * direction, and sometimes for a close to be effective.
5310 */
5311
5312 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5313 /* Server-close mode : shut read and wait for the request
5314 * side to close its output buffer. The caller will detect
5315 * when we're in DONE and the other is in CLOSED and will
5316 * catch that for the final cleanup.
5317 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005318 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5319 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005320 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005321 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5322 /* Option forceclose is set, or either side wants to close,
5323 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005324 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005325 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005326 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005327 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5328 channel_shutr_now(chn);
5329 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005330 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005331 }
5332 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005333 /* The last possible modes are keep-alive and tunnel. Tunnel will
5334 * need to forward remaining data. Keep-alive will need to monitor
5335 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005336 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005337 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005338 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005339 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5340 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005341 }
5342
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005343 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005344 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005345 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005346 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5347 goto http_msg_closing;
5348 }
5349 else {
5350 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5351 goto http_msg_closed;
5352 }
5353 }
5354 goto wait_other_side;
5355 }
5356
5357 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5358 http_msg_closing:
5359 /* nothing else to forward, just waiting for the output buffer
5360 * to be empty and for the shutw_now to take effect.
5361 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005362 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005363 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5364 goto http_msg_closed;
5365 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005366 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005367 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005368 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005369 if (objt_server(s->target))
5370 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005371 goto wait_other_side;
5372 }
5373 }
5374
5375 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5376 http_msg_closed:
5377 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005378 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005379 channel_auto_close(chn);
5380 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005381 goto wait_other_side;
5382 }
5383
5384 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005385 /* We force the response to leave immediately if we're waiting for the
5386 * other side, since there is no pending shutdown to push it out.
5387 */
5388 if (!channel_is_empty(chn))
5389 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005390 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005391}
5392
5393
5394/* Resync the request and response state machines. Return 1 if either state
5395 * changes.
5396 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005397int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005398{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005399 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005400 int old_req_state = txn->req.msg_state;
5401 int old_res_state = txn->rsp.msg_state;
5402
Willy Tarreau610ecce2010-01-04 21:15:02 +01005403 http_sync_req_state(s);
5404 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005405 if (!http_sync_res_state(s))
5406 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005407 if (!http_sync_req_state(s))
5408 break;
5409 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005410
Willy Tarreau610ecce2010-01-04 21:15:02 +01005411 /* OK, both state machines agree on a compatible state.
5412 * There are a few cases we're interested in :
5413 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5414 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5415 * directions, so let's simply disable both analysers.
5416 * - HTTP_MSG_CLOSED on the response only means we must abort the
5417 * request.
5418 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5419 * with server-close mode means we've completed one request and we
5420 * must re-initialize the server connection.
5421 */
5422
5423 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5424 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5425 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5426 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005427 s->req.analysers = 0;
5428 channel_auto_close(&s->req);
5429 channel_auto_read(&s->req);
5430 s->res.analysers = 0;
5431 channel_auto_close(&s->res);
5432 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005433 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005434 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005435 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005436 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005437 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005438 s->res.analysers = 0;
5439 channel_auto_close(&s->res);
5440 channel_auto_read(&s->res);
5441 s->req.analysers = 0;
5442 channel_abort(&s->req);
5443 channel_auto_close(&s->req);
5444 channel_auto_read(&s->req);
5445 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005446 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005447 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5448 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005449 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005450 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5451 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5452 /* server-close/keep-alive: terminate this transaction,
5453 * possibly killing the server connection and reinitialize
5454 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005455 */
5456 http_end_txn_clean_session(s);
5457 }
5458
Willy Tarreau610ecce2010-01-04 21:15:02 +01005459 return txn->req.msg_state != old_req_state ||
5460 txn->rsp.msg_state != old_res_state;
5461}
5462
Willy Tarreaud98cf932009-12-27 22:54:55 +01005463/* This function is an analyser which forwards request body (including chunk
5464 * sizes if any). It is called as soon as we must forward, even if we forward
5465 * zero byte. The only situation where it must not be called is when we're in
5466 * tunnel mode and we want to forward till the close. It's used both to forward
5467 * remaining data and to resync after end of body. It expects the msg_state to
5468 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005469 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005470 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005471 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005472 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005473int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005474{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005475 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005476 struct http_txn *txn = s->txn;
5477 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005478
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005479 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5480 return 0;
5481
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005482 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005483 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005484 /* Output closed while we were sending data. We must abort and
5485 * wake the other side up.
5486 */
5487 msg->msg_state = HTTP_MSG_ERROR;
5488 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005489 return 1;
5490 }
5491
Willy Tarreaud98cf932009-12-27 22:54:55 +01005492 /* Note that we don't have to send 100-continue back because we don't
5493 * need the data to complete our job, and it's up to the server to
5494 * decide whether to return 100, 417 or anything else in return of
5495 * an "Expect: 100-continue" header.
5496 */
5497
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005498 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005499 /* we have msg->sov which points to the first byte of message
5500 * body, and req->buf.p still points to the beginning of the
5501 * message. We forward the headers now, as we don't need them
5502 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005503 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005504 b_adv(req->buf, msg->sov);
5505 msg->next -= msg->sov;
5506 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005507
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005508 /* The previous analysers guarantee that the state is somewhere
5509 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5510 * msg->next are always correct.
5511 */
5512 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5513 if (msg->flags & HTTP_MSGF_TE_CHNK)
5514 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5515 else
5516 msg->msg_state = HTTP_MSG_DATA;
5517 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005518 }
5519
Willy Tarreau7ba23542014-04-17 21:50:00 +02005520 /* Some post-connect processing might want us to refrain from starting to
5521 * forward data. Currently, the only reason for this is "balance url_param"
5522 * whichs need to parse/process the request after we've enabled forwarding.
5523 */
5524 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005525 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005526 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005527 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005528 goto missing_data;
5529 }
5530 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5531 }
5532
Willy Tarreau80a92c02014-03-12 10:41:13 +01005533 /* in most states, we should abort in case of early close */
5534 channel_auto_close(req);
5535
Willy Tarreauefdf0942014-04-24 20:08:57 +02005536 if (req->to_forward) {
5537 /* We can't process the buffer's contents yet */
5538 req->flags |= CF_WAKE_WRITE;
5539 goto missing_data;
5540 }
5541
Willy Tarreaud98cf932009-12-27 22:54:55 +01005542 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005543 if (msg->msg_state == HTTP_MSG_DATA) {
5544 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005545 /* we may have some pending data starting at req->buf->p */
5546 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005547 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005548 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005549 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005550 msg->next += msg->chunk_len;
5551 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005552
5553 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005554 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005555 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005556 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005557 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005558 }
5559 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005560 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005561 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005562 * TRAILERS state.
5563 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005564 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005565
Willy Tarreau54d23df2012-10-25 19:04:45 +02005566 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005567 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005568 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005569 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005570 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005571 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005572 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005573 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005574 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005575 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005576 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005577 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005578 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005579
5580 if (ret == 0)
5581 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005582 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005583 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005584 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005585 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005586 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005587 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005588 /* we're in MSG_CHUNK_SIZE now */
5589 }
5590 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005591 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005592
5593 if (ret == 0)
5594 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005595 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005596 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005597 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005598 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005599 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005600 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005601 /* we're in HTTP_MSG_DONE now */
5602 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005603 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005604 int old_state = msg->msg_state;
5605
Willy Tarreau610ecce2010-01-04 21:15:02 +01005606 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005607
5608 /* we may have some pending data starting at req->buf->p
5609 * such as last chunk of data or trailers.
5610 */
5611 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005612 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005613 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005614 msg->next = 0;
5615
Willy Tarreau4fe41902010-06-07 22:27:41 +02005616 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005617 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5618 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005619 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005620 if (http_resync_states(s)) {
5621 /* some state changes occurred, maybe the analyser
5622 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005623 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005624 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005625 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005626 /* request errors are most likely due to
5627 * the server aborting the transfer.
5628 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005629 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005630 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005631 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005632 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005633 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005634 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005635 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005636 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005637
5638 /* If "option abortonclose" is set on the backend, we
5639 * want to monitor the client's connection and forward
5640 * any shutdown notification to the server, which will
5641 * decide whether to close or to go on processing the
5642 * request.
5643 */
5644 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005645 channel_auto_read(req);
5646 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005647 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005648 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005649 /* POST requests may require to read extra CRLF
5650 * sent by broken browsers and which could cause
5651 * an RST to be sent upon close on some systems
5652 * (eg: Linux).
5653 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005654 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005655 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005656
Willy Tarreau610ecce2010-01-04 21:15:02 +01005657 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005658 }
5659 }
5660
Willy Tarreaud98cf932009-12-27 22:54:55 +01005661 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005662 /* we may have some pending data starting at req->buf->p */
5663 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005664 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005665 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5666
Willy Tarreaubed410e2014-04-22 08:19:34 +02005667 msg->next = 0;
5668 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5669
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005670 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005671 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005672 if (!(s->flags & SF_ERR_MASK))
5673 s->flags |= SF_ERR_CLICL;
5674 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005675 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005676 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005677 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005678 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005679 }
5680
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005681 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005682 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005683 if (objt_server(s->target))
5684 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005685
5686 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005687 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005688
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005689 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005690 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005691 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005692
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005693 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005694 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005695 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005696 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005697 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005698
Willy Tarreau5c620922011-05-11 19:56:11 +02005699 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005700 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005701 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005702 * modes are already handled by the stream sock layer. We must not do
5703 * this in content-length mode because it could present the MSG_MORE
5704 * flag with the last block of forwarded data, which would cause an
5705 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005706 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005707 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005708 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005709
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005710 return 0;
5711
Willy Tarreaud98cf932009-12-27 22:54:55 +01005712 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005713 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005714 if (sess->listener->counters)
5715 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005716
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005717 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005718 /* we may have some pending data starting at req->buf->p */
5719 b_adv(req->buf, msg->next);
5720 msg->next = 0;
5721
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005722 txn->req.msg_state = HTTP_MSG_ERROR;
5723 if (txn->status) {
5724 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005725 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005726 } else {
5727 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005728 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005729 }
5730 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005731 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005732
Willy Tarreaue7dff022015-04-03 01:14:29 +02005733 if (!(s->flags & SF_ERR_MASK))
5734 s->flags |= SF_ERR_PRXCOND;
5735 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005736 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005737 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005738 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005739 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005740 }
5741 return 0;
5742
5743 aborted_xfer:
5744 txn->req.msg_state = HTTP_MSG_ERROR;
5745 if (txn->status) {
5746 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005747 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005748 } else {
5749 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005750 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005751 }
5752 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005753 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005754
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005755 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005756 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005757 if (objt_server(s->target))
5758 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005759
Willy Tarreaue7dff022015-04-03 01:14:29 +02005760 if (!(s->flags & SF_ERR_MASK))
5761 s->flags |= SF_ERR_SRVCL;
5762 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005763 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005764 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005765 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005766 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005767 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005768 return 0;
5769}
5770
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005771/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5772 * processing can continue on next analysers, or zero if it either needs more
5773 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005774 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005775 * when it has nothing left to do, and may remove any analyser when it wants to
5776 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005777 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005778int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005779{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005780 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005781 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005782 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005783 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005784 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005785 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005786 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005787
Willy Tarreau87b09662015-04-03 00:22:06 +02005788 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 +02005789 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005790 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005791 rep,
5792 rep->rex, rep->wex,
5793 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005794 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005795 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005796
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005797 /*
5798 * Now parse the partial (or complete) lines.
5799 * We will check the response syntax, and also join multi-line
5800 * headers. An index of all the lines will be elaborated while
5801 * parsing.
5802 *
5803 * For the parsing, we use a 28 states FSM.
5804 *
5805 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005806 * rep->buf->p = beginning of response
5807 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5808 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005809 * msg->eol = end of current header or line (LF or CRLF)
5810 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005811 */
5812
Willy Tarreau628c40c2014-04-24 19:11:26 +02005813 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005814 /* There's a protected area at the end of the buffer for rewriting
5815 * purposes. We don't want to start to parse the request if the
5816 * protected area is affected, because we may have to move processed
5817 * data later, which is much more complicated.
5818 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005819 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005820 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005821 /* some data has still not left the buffer, wake us once that's done */
5822 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5823 goto abort_response;
5824 channel_dont_close(rep);
5825 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005826 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005827 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005828 }
5829
Willy Tarreau379357a2013-06-08 12:55:46 +02005830 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5831 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5832 buffer_slow_realign(rep->buf);
5833
Willy Tarreau9b28e032012-10-12 23:49:43 +02005834 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005835 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005836 }
5837
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005838 /* 1: we might have to print this header in debug mode */
5839 if (unlikely((global.mode & MODE_DEBUG) &&
5840 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005841 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005842 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005843
Willy Tarreau9b28e032012-10-12 23:49:43 +02005844 sol = rep->buf->p;
5845 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005846 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005847
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005848 sol += hdr_idx_first_pos(&txn->hdr_idx);
5849 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005850
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005851 while (cur_idx) {
5852 eol = sol + txn->hdr_idx.v[cur_idx].len;
5853 debug_hdr("srvhdr", s, sol, eol);
5854 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5855 cur_idx = txn->hdr_idx.v[cur_idx].next;
5856 }
5857 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005858
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005859 /*
5860 * Now we quickly check if we have found a full valid response.
5861 * If not so, we check the FD and buffer states before leaving.
5862 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005863 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005864 * responses are checked first.
5865 *
5866 * Depending on whether the client is still there or not, we
5867 * may send an error response back or not. Note that normally
5868 * we should only check for HTTP status there, and check I/O
5869 * errors somewhere else.
5870 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005871
Willy Tarreau655dce92009-11-08 13:10:58 +01005872 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005873 /* Invalid response */
5874 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5875 /* we detected a parsing error. We want to archive this response
5876 * in the dedicated proxy area for later troubleshooting.
5877 */
5878 hdr_response_bad:
5879 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005880 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005881
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005882 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005883 if (objt_server(s->target)) {
5884 objt_server(s->target)->counters.failed_resp++;
5885 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005886 }
Willy Tarreau64648412010-03-05 10:41:54 +01005887 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005888 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005889 rep->analysers = 0;
5890 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005891 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005892 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005893 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005894
Willy Tarreaue7dff022015-04-03 01:14:29 +02005895 if (!(s->flags & SF_ERR_MASK))
5896 s->flags |= SF_ERR_PRXCOND;
5897 if (!(s->flags & SF_FINST_MASK))
5898 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005899
5900 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005901 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005902
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005903 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005904 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005905 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005906 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005907 goto hdr_response_bad;
5908 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005909
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005910 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005911 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005912 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005913 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005914 else if (txn->flags & TX_NOT_FIRST)
5915 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005916
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005917 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005918 if (objt_server(s->target)) {
5919 objt_server(s->target)->counters.failed_resp++;
5920 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005921 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005922
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005923 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005924 rep->analysers = 0;
5925 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005926 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005927 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005928 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005929
Willy Tarreaue7dff022015-04-03 01:14:29 +02005930 if (!(s->flags & SF_ERR_MASK))
5931 s->flags |= SF_ERR_SRVCL;
5932 if (!(s->flags & SF_FINST_MASK))
5933 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005934 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005935 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005936
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005937 /* read timeout : return a 504 to the client. */
5938 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005939 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005940 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005941 else if (txn->flags & TX_NOT_FIRST)
5942 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005943
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005944 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005945 if (objt_server(s->target)) {
5946 objt_server(s->target)->counters.failed_resp++;
5947 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005948 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005949
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005950 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005951 rep->analysers = 0;
5952 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005953 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005954 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005955 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005956
Willy Tarreaue7dff022015-04-03 01:14:29 +02005957 if (!(s->flags & SF_ERR_MASK))
5958 s->flags |= SF_ERR_SRVTO;
5959 if (!(s->flags & SF_FINST_MASK))
5960 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005961 return 0;
5962 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005963
Willy Tarreauf003d372012-11-26 13:35:37 +01005964 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005965 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005966 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005967 s->be->be_counters.cli_aborts++;
5968 if (objt_server(s->target))
5969 objt_server(s->target)->counters.cli_aborts++;
5970
5971 rep->analysers = 0;
5972 channel_auto_close(rep);
5973
5974 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005975 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005976 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01005977
Willy Tarreaue7dff022015-04-03 01:14:29 +02005978 if (!(s->flags & SF_ERR_MASK))
5979 s->flags |= SF_ERR_CLICL;
5980 if (!(s->flags & SF_FINST_MASK))
5981 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005982
Willy Tarreau87b09662015-04-03 00:22:06 +02005983 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005984 return 0;
5985 }
5986
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005987 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005988 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005989 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005990 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005991 else if (txn->flags & TX_NOT_FIRST)
5992 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005993
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005994 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005995 if (objt_server(s->target)) {
5996 objt_server(s->target)->counters.failed_resp++;
5997 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005998 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005999
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006000 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006001 rep->analysers = 0;
6002 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006003 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006004 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006005 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006006
Willy Tarreaue7dff022015-04-03 01:14:29 +02006007 if (!(s->flags & SF_ERR_MASK))
6008 s->flags |= SF_ERR_SRVCL;
6009 if (!(s->flags & SF_FINST_MASK))
6010 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006011 return 0;
6012 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006013
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006014 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006015 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006016 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006017 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006018 else if (txn->flags & TX_NOT_FIRST)
6019 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006020
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006021 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006022 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006023 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006024
Willy Tarreaue7dff022015-04-03 01:14:29 +02006025 if (!(s->flags & SF_ERR_MASK))
6026 s->flags |= SF_ERR_CLICL;
6027 if (!(s->flags & SF_FINST_MASK))
6028 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006029
Willy Tarreau87b09662015-04-03 00:22:06 +02006030 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006031 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006032 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006033
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006034 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006035 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006036 return 0;
6037 }
6038
6039 /* More interesting part now : we know that we have a complete
6040 * response which at least looks like HTTP. We have an indicator
6041 * of each header's length, so we can parse them quickly.
6042 */
6043
6044 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006045 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006046
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006047 /*
6048 * 1: get the status code
6049 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006050 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006051 if (n < 1 || n > 5)
6052 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006053 /* when the client triggers a 4xx from the server, it's most often due
6054 * to a missing object or permission. These events should be tracked
6055 * because if they happen often, it may indicate a brute force or a
6056 * vulnerability scan.
6057 */
6058 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006059 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006060
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006061 if (objt_server(s->target))
6062 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006063
Willy Tarreau5b154472009-12-21 20:11:07 +01006064 /* check if the response is HTTP/1.1 or above */
6065 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006066 ((rep->buf->p[5] > '1') ||
6067 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006068 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006069
6070 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006071 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 +01006072
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006073 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006074 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006075
Willy Tarreau9b28e032012-10-12 23:49:43 +02006076 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006077
Willy Tarreau39650402010-03-15 19:44:39 +01006078 /* Adjust server's health based on status code. Note: status codes 501
6079 * and 505 are triggered on demand by client request, so we must not
6080 * count them as server failures.
6081 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006082 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006083 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006084 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006085 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006086 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006087 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006088
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006089 /*
6090 * 2: check for cacheability.
6091 */
6092
6093 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006094 case 100:
6095 /*
6096 * We may be facing a 100-continue response, in which case this
6097 * is not the right response, and we're waiting for the next one.
6098 * Let's allow this response to go to the client and wait for the
6099 * next one.
6100 */
6101 hdr_idx_init(&txn->hdr_idx);
6102 msg->next -= channel_forward(rep, msg->next);
6103 msg->msg_state = HTTP_MSG_RPBEFORE;
6104 txn->status = 0;
6105 s->logs.t_data = -1; /* was not a response yet */
6106 goto next_one;
6107
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006108 case 200:
6109 case 203:
6110 case 206:
6111 case 300:
6112 case 301:
6113 case 410:
6114 /* RFC2616 @13.4:
6115 * "A response received with a status code of
6116 * 200, 203, 206, 300, 301 or 410 MAY be stored
6117 * by a cache (...) unless a cache-control
6118 * directive prohibits caching."
6119 *
6120 * RFC2616 @9.5: POST method :
6121 * "Responses to this method are not cacheable,
6122 * unless the response includes appropriate
6123 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006124 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006125 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006126 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006127 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6128 break;
6129 default:
6130 break;
6131 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006132
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006133 /*
6134 * 3: we may need to capture headers
6135 */
6136 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006137 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006138 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006139 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006140
Willy Tarreau557f1992015-05-01 10:05:17 +02006141 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6142 * by RFC7230#3.3.3 :
6143 *
6144 * The length of a message body is determined by one of the following
6145 * (in order of precedence):
6146 *
6147 * 1. Any response to a HEAD request and any response with a 1xx
6148 * (Informational), 204 (No Content), or 304 (Not Modified) status
6149 * code is always terminated by the first empty line after the
6150 * header fields, regardless of the header fields present in the
6151 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006152 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006153 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6154 * the connection will become a tunnel immediately after the empty
6155 * line that concludes the header fields. A client MUST ignore any
6156 * Content-Length or Transfer-Encoding header fields received in
6157 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006158 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006159 * 3. If a Transfer-Encoding header field is present and the chunked
6160 * transfer coding (Section 4.1) is the final encoding, the message
6161 * body length is determined by reading and decoding the chunked
6162 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006163 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006164 * If a Transfer-Encoding header field is present in a response and
6165 * the chunked transfer coding is not the final encoding, the
6166 * message body length is determined by reading the connection until
6167 * it is closed by the server. If a Transfer-Encoding header field
6168 * is present in a request and the chunked transfer coding is not
6169 * the final encoding, the message body length cannot be determined
6170 * reliably; the server MUST respond with the 400 (Bad Request)
6171 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006172 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006173 * If a message is received with both a Transfer-Encoding and a
6174 * Content-Length header field, the Transfer-Encoding overrides the
6175 * Content-Length. Such a message might indicate an attempt to
6176 * perform request smuggling (Section 9.5) or response splitting
6177 * (Section 9.4) and ought to be handled as an error. A sender MUST
6178 * remove the received Content-Length field prior to forwarding such
6179 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006180 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006181 * 4. If a message is received without Transfer-Encoding and with
6182 * either multiple Content-Length header fields having differing
6183 * field-values or a single Content-Length header field having an
6184 * invalid value, then the message framing is invalid and the
6185 * recipient MUST treat it as an unrecoverable error. If this is a
6186 * request message, the server MUST respond with a 400 (Bad Request)
6187 * status code and then close the connection. If this is a response
6188 * message received by a proxy, the proxy MUST close the connection
6189 * to the server, discard the received response, and send a 502 (Bad
6190 * Gateway) response to the client. If this is a response message
6191 * received by a user agent, the user agent MUST close the
6192 * connection to the server and discard the received response.
6193 *
6194 * 5. If a valid Content-Length header field is present without
6195 * Transfer-Encoding, its decimal value defines the expected message
6196 * body length in octets. If the sender closes the connection or
6197 * the recipient times out before the indicated number of octets are
6198 * received, the recipient MUST consider the message to be
6199 * incomplete and close the connection.
6200 *
6201 * 6. If this is a request message and none of the above are true, then
6202 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006203 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006204 * 7. Otherwise, this is a response message without a declared message
6205 * body length, so the message body length is determined by the
6206 * number of octets received prior to the server closing the
6207 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006208 */
6209
6210 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006211 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006212 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006213 * FIXME: should we parse anyway and return an error on chunked encoding ?
6214 */
6215 if (txn->meth == HTTP_METH_HEAD ||
6216 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006217 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006218 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006219 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006220 goto skip_content_length;
6221 }
6222
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006223 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006224 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006225 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006226 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006227 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6228 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006229 /* bad transfer-encoding (chunked followed by something else) */
6230 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006231 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006232 break;
6233 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006234 }
6235
Willy Tarreau1c913912015-04-30 10:57:51 +02006236 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006237 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02006238 if (msg->flags & HTTP_MSGF_TE_CHNK) {
6239 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6240 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6241 }
6242 else while (!use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006243 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006244 signed long long cl;
6245
Willy Tarreauad14f752011-09-02 20:33:27 +02006246 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006247 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006248 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006249 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006250
Willy Tarreauad14f752011-09-02 20:33:27 +02006251 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006252 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006253 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006254 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006255
Willy Tarreauad14f752011-09-02 20:33:27 +02006256 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006257 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006258 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006259 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006260
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006261 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006262 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006263 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006264 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006265
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006266 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006267 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006268 }
6269
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006270 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006271 select_compression_response_header(s, rep->buf);
6272
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006273skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006274 /* Now we have to check if we need to modify the Connection header.
6275 * This is more difficult on the response than it is on the request,
6276 * because we can have two different HTTP versions and we don't know
6277 * how the client will interprete a response. For instance, let's say
6278 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6279 * HTTP/1.1 response without any header. Maybe it will bound itself to
6280 * HTTP/1.0 because it only knows about it, and will consider the lack
6281 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6282 * the lack of header as a keep-alive. Thus we will use two flags
6283 * indicating how a request MAY be understood by the client. In case
6284 * of multiple possibilities, we'll fix the header to be explicit. If
6285 * ambiguous cases such as both close and keepalive are seen, then we
6286 * will fall back to explicit close. Note that we won't take risks with
6287 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006288 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006289 */
6290
Willy Tarreaudc008c52010-02-01 16:20:08 +01006291 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6292 txn->status == 101)) {
6293 /* Either we've established an explicit tunnel, or we're
6294 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006295 * to understand the next protocols. We have to switch to tunnel
6296 * mode, so that we transfer the request and responses then let
6297 * this protocol pass unmodified. When we later implement specific
6298 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006299 * header which contains information about that protocol for
6300 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006301 */
6302 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6303 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006304 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6305 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006306 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006307 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006308 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006309
Willy Tarreau70dffda2014-01-30 03:07:23 +01006310 /* this situation happens when combining pretend-keepalive with httpclose. */
6311 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006312 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006313 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006314 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6315
Willy Tarreau60466522010-01-18 19:08:45 +01006316 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006317 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006318 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6319 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006320
Willy Tarreau60466522010-01-18 19:08:45 +01006321 /* now adjust header transformations depending on current state */
6322 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6323 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6324 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006325 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006326 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006327 }
Willy Tarreau60466522010-01-18 19:08:45 +01006328 else { /* SCL / KAL */
6329 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006330 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006331 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006332 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006333
Willy Tarreau60466522010-01-18 19:08:45 +01006334 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006335 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006336
Willy Tarreau60466522010-01-18 19:08:45 +01006337 /* Some keep-alive responses are converted to Server-close if
6338 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006339 */
Willy Tarreau60466522010-01-18 19:08:45 +01006340 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6341 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006342 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006343 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006344 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006345 }
6346
Willy Tarreau7959a552013-09-23 16:44:27 +02006347 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006348 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006349
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006350 /* end of job, return OK */
6351 rep->analysers &= ~an_bit;
6352 rep->analyse_exp = TICK_ETERNITY;
6353 channel_auto_close(rep);
6354 return 1;
6355
6356 abort_keep_alive:
6357 /* A keep-alive request to the server failed on a network error.
6358 * The client is required to retry. We need to close without returning
6359 * any other information so that the client retries.
6360 */
6361 txn->status = 0;
6362 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006363 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006364 channel_auto_close(rep);
6365 s->logs.logwait = 0;
6366 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006367 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006368 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006369 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006370 return 0;
6371}
6372
6373/* This function performs all the processing enabled for the current response.
6374 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006375 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006376 * other functions. It works like process_request (see indications above).
6377 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006378int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006379{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006380 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006381 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006382 struct http_msg *msg = &txn->rsp;
6383 struct proxy *cur_proxy;
6384 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006385 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006386
Willy Tarreau87b09662015-04-03 00:22:06 +02006387 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 +02006388 now_ms, __FUNCTION__,
6389 s,
6390 rep,
6391 rep->rex, rep->wex,
6392 rep->flags,
6393 rep->buf->i,
6394 rep->analysers);
6395
6396 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6397 return 0;
6398
Willy Tarreau70730dd2014-04-24 18:06:27 +02006399 /* The stats applet needs to adjust the Connection header but we don't
6400 * apply any filter there.
6401 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006402 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6403 rep->analysers &= ~an_bit;
6404 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006405 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006406 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006407
Willy Tarreau58975672014-04-24 21:13:57 +02006408 /*
6409 * We will have to evaluate the filters.
6410 * As opposed to version 1.2, now they will be evaluated in the
6411 * filters order and not in the header order. This means that
6412 * each filter has to be validated among all headers.
6413 *
6414 * Filters are tried with ->be first, then with ->fe if it is
6415 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006416 *
6417 * Maybe we are in resume condiion. In this case I choose the
6418 * "struct proxy" which contains the rule list matching the resume
6419 * pointer. If none of theses "struct proxy" match, I initialise
6420 * the process with the first one.
6421 *
6422 * In fact, I check only correspondance betwwen the current list
6423 * pointer and the ->fe rule list. If it doesn't match, I initialize
6424 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006425 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006426 if (s->current_rule_list == &sess->fe->http_res_rules)
6427 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006428 else
6429 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006430 while (1) {
6431 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006432
Willy Tarreau58975672014-04-24 21:13:57 +02006433 /* evaluate http-response rules */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006434 if (ret == HTTP_RULE_RES_CONT)
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006435 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006436
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006437 /* we need to be called again. */
6438 if (ret == HTTP_RULE_RES_YIELD) {
6439 channel_dont_close(rep);
6440 return 0;
6441 }
6442
Willy Tarreau58975672014-04-24 21:13:57 +02006443 /* try headers filters */
6444 if (rule_set->rsp_exp != NULL) {
6445 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6446 return_bad_resp:
6447 if (objt_server(s->target)) {
6448 objt_server(s->target)->counters.failed_resp++;
6449 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006450 }
Willy Tarreau58975672014-04-24 21:13:57 +02006451 s->be->be_counters.failed_resp++;
6452 return_srv_prx_502:
6453 rep->analysers = 0;
6454 txn->status = 502;
6455 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006456 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006457 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006458 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006459 if (!(s->flags & SF_ERR_MASK))
6460 s->flags |= SF_ERR_PRXCOND;
6461 if (!(s->flags & SF_FINST_MASK))
6462 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006463 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006464 }
Willy Tarreau58975672014-04-24 21:13:57 +02006465 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006466
Willy Tarreau58975672014-04-24 21:13:57 +02006467 /* has the response been denied ? */
6468 if (txn->flags & TX_SVDENY) {
6469 if (objt_server(s->target))
6470 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006471
Willy Tarreau58975672014-04-24 21:13:57 +02006472 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006473 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006474 if (sess->listener->counters)
6475 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006476
Willy Tarreau58975672014-04-24 21:13:57 +02006477 goto return_srv_prx_502;
6478 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006479
Willy Tarreau58975672014-04-24 21:13:57 +02006480 /* add response headers from the rule sets in the same order */
6481 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006482 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006483 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006484 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006485 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006486 ret = acl_pass(ret);
6487 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6488 ret = !ret;
6489 if (!ret)
6490 continue;
6491 }
6492 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6493 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006494 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006495
Willy Tarreau58975672014-04-24 21:13:57 +02006496 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006497 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006498 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006499 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006500 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006501
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006502 /* After this point, this anayzer can't return yield, so we can
6503 * remove the bit corresponding to this analyzer from the list.
6504 *
6505 * Note that the intermediate returns and goto found previously
6506 * reset the analyzers.
6507 */
6508 rep->analysers &= ~an_bit;
6509 rep->analyse_exp = TICK_ETERNITY;
6510
Willy Tarreau58975672014-04-24 21:13:57 +02006511 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006512 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006513 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006514
Willy Tarreau58975672014-04-24 21:13:57 +02006515 /*
6516 * Now check for a server cookie.
6517 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006518 if (s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name ||
Willy Tarreau58975672014-04-24 21:13:57 +02006519 (s->be->options & PR_O_CHK_CACHE))
6520 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006521
Willy Tarreau58975672014-04-24 21:13:57 +02006522 /*
6523 * Check for cache-control or pragma headers if required.
6524 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006525 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 +02006526 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006527
Willy Tarreau58975672014-04-24 21:13:57 +02006528 /*
6529 * Add server cookie in the response if needed
6530 */
6531 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6532 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006533 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006534 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6535 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6536 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6537 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6538 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006539 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006540 /* the server is known, it's not the one the client requested, or the
6541 * cookie's last seen date needs to be refreshed. We have to
6542 * insert a set-cookie here, except if we want to insert only on POST
6543 * requests and this one isn't. Note that servers which don't have cookies
6544 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006545 */
Willy Tarreau58975672014-04-24 21:13:57 +02006546 if (!objt_server(s->target)->cookie) {
6547 chunk_printf(&trash,
6548 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6549 s->be->cookie_name);
6550 }
6551 else {
6552 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006553
Willy Tarreau58975672014-04-24 21:13:57 +02006554 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6555 /* emit last_date, which is mandatory */
6556 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6557 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6558 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006559
Willy Tarreau58975672014-04-24 21:13:57 +02006560 if (s->be->cookie_maxlife) {
6561 /* emit first_date, which is either the original one or
6562 * the current date.
6563 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006564 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006565 s30tob64(txn->cookie_first_date ?
6566 txn->cookie_first_date >> 2 :
6567 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006568 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006569 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006570 }
Willy Tarreau58975672014-04-24 21:13:57 +02006571 chunk_appendf(&trash, "; path=/");
6572 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006573
Willy Tarreau58975672014-04-24 21:13:57 +02006574 if (s->be->cookie_domain)
6575 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006576
Willy Tarreau58975672014-04-24 21:13:57 +02006577 if (s->be->ck_opts & PR_CK_HTTPONLY)
6578 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006579
Willy Tarreau58975672014-04-24 21:13:57 +02006580 if (s->be->ck_opts & PR_CK_SECURE)
6581 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006582
Willy Tarreau58975672014-04-24 21:13:57 +02006583 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6584 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006585
Willy Tarreau58975672014-04-24 21:13:57 +02006586 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006587 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006588 /* the server did not change, only the date was updated */
6589 txn->flags |= TX_SCK_UPDATED;
6590 else
6591 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006592
Willy Tarreau58975672014-04-24 21:13:57 +02006593 /* Here, we will tell an eventual cache on the client side that we don't
6594 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6595 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6596 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006597 */
Willy Tarreau58975672014-04-24 21:13:57 +02006598 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006599
Willy Tarreau58975672014-04-24 21:13:57 +02006600 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006601
Willy Tarreau58975672014-04-24 21:13:57 +02006602 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6603 "Cache-control: private", 22) < 0))
6604 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006605 }
Willy Tarreau58975672014-04-24 21:13:57 +02006606 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006607
Willy Tarreau58975672014-04-24 21:13:57 +02006608 /*
6609 * Check if result will be cacheable with a cookie.
6610 * We'll block the response if security checks have caught
6611 * nasty things such as a cacheable cookie.
6612 */
6613 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6614 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6615 (s->be->options & PR_O_CHK_CACHE)) {
6616 /* we're in presence of a cacheable response containing
6617 * a set-cookie header. We'll block it as requested by
6618 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006619 */
Willy Tarreau58975672014-04-24 21:13:57 +02006620 if (objt_server(s->target))
6621 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006622
Willy Tarreau58975672014-04-24 21:13:57 +02006623 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006624 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006625 if (sess->listener->counters)
6626 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006627
Willy Tarreau58975672014-04-24 21:13:57 +02006628 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6629 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6630 send_log(s->be, LOG_ALERT,
6631 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6632 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6633 goto return_srv_prx_502;
6634 }
Willy Tarreau03945942009-12-22 16:50:27 +01006635
Willy Tarreau70730dd2014-04-24 18:06:27 +02006636 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006637 /*
6638 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6639 * If an "Upgrade" token is found, the header is left untouched in order
6640 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006641 * if anything but "Upgrade" is present in the Connection header. We don't
6642 * want to touch any 101 response either since it's switching to another
6643 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006644 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006645 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006646 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006647 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006648 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6649 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006650
Willy Tarreau58975672014-04-24 21:13:57 +02006651 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6652 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6653 /* we want a keep-alive response here. Keep-alive header
6654 * required if either side is not 1.1.
6655 */
6656 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6657 want_flags |= TX_CON_KAL_SET;
6658 }
6659 else {
6660 /* we want a close response here. Close header required if
6661 * the server is 1.1, regardless of the client.
6662 */
6663 if (msg->flags & HTTP_MSGF_VER_11)
6664 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006665 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006666
Willy Tarreau58975672014-04-24 21:13:57 +02006667 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6668 http_change_connection_header(txn, msg, want_flags);
6669 }
6670
6671 skip_header_mangling:
6672 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6673 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6674 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006675
Willy Tarreau58975672014-04-24 21:13:57 +02006676 /* if the user wants to log as soon as possible, without counting
6677 * bytes from the server, then this is the right moment. We have
6678 * to temporarily assign bytes_out to log what we currently have.
6679 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006680 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006681 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6682 s->logs.bytes_out = txn->rsp.eoh;
6683 s->do_log(s);
6684 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006685 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006686 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006687}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006688
Willy Tarreaud98cf932009-12-27 22:54:55 +01006689/* This function is an analyser which forwards response body (including chunk
6690 * sizes if any). It is called as soon as we must forward, even if we forward
6691 * zero byte. The only situation where it must not be called is when we're in
6692 * tunnel mode and we want to forward till the close. It's used both to forward
6693 * remaining data and to resync after end of body. It expects the msg_state to
6694 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006695 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006696 *
6697 * It is capable of compressing response data both in content-length mode and
6698 * in chunked mode. The state machines follows different flows depending on
6699 * whether content-length and chunked modes are used, since there are no
6700 * trailers in content-length :
6701 *
6702 * chk-mode cl-mode
6703 * ,----- BODY -----.
6704 * / \
6705 * V size > 0 V chk-mode
6706 * .--> SIZE -------------> DATA -------------> CRLF
6707 * | | size == 0 | last byte |
6708 * | v final crlf v inspected |
6709 * | TRAILERS -----------> DONE |
6710 * | |
6711 * `----------------------------------------------'
6712 *
6713 * Compression only happens in the DATA state, and must be flushed in final
6714 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6715 * is performed at once on final states for all bytes parsed, or when leaving
6716 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006717 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006718int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006719{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006720 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006721 struct http_txn *txn = s->txn;
6722 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006723 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006724 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006725 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006726
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006727 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6728 return 0;
6729
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006730 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006731 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006732 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006733 /* Output closed while we were sending data. We must abort and
6734 * wake the other side up.
6735 */
6736 msg->msg_state = HTTP_MSG_ERROR;
6737 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006738 return 1;
6739 }
6740
Willy Tarreau4fe41902010-06-07 22:27:41 +02006741 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006742 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006743
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006744 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006745 /* we have msg->sov which points to the first byte of message
6746 * body, and res->buf.p still points to the beginning of the
6747 * message. We forward the headers now, as we don't need them
6748 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006749 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006750 b_adv(res->buf, msg->sov);
6751 msg->next -= msg->sov;
6752 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006753
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006754 /* The previous analysers guarantee that the state is somewhere
6755 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6756 * msg->next are always correct.
6757 */
6758 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6759 if (msg->flags & HTTP_MSGF_TE_CHNK)
6760 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6761 else
6762 msg->msg_state = HTTP_MSG_DATA;
6763 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006764 }
6765
Willy Tarreauefdf0942014-04-24 20:08:57 +02006766 if (res->to_forward) {
6767 /* We can't process the buffer's contents yet */
6768 res->flags |= CF_WAKE_WRITE;
6769 goto missing_data;
6770 }
6771
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006772 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6773 /* We need a compression buffer in the DATA state to put the
6774 * output of compressed data, and in CRLF state to let the
6775 * TRAILERS state finish the job of removing the trailing CRLF.
6776 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006777 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006778 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006779 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006780 goto aborted_xfer; /* no memory */
6781 }
6782
6783 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006784 if (ret < 0) {
6785 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006786 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006787 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006788 compressing = 1;
6789 }
6790
Willy Tarreaud98cf932009-12-27 22:54:55 +01006791 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006792 switch (msg->msg_state - HTTP_MSG_DATA) {
6793 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006794 /* we may have some pending data starting at res->buf->p */
6795 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006796 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006797 if (ret < 0)
6798 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006799
Willy Tarreaud5a67832014-04-21 10:54:27 +02006800 if (msg->chunk_len) {
6801 /* input empty or output full */
6802 if (res->buf->i > msg->next)
6803 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006804 goto missing_data;
6805 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006806 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006807 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006808 if (msg->chunk_len > res->buf->i - msg->next) {
6809 /* output full */
6810 res->flags |= CF_WAKE_WRITE;
6811 goto missing_data;
6812 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006813 msg->next += msg->chunk_len;
6814 msg->chunk_len = 0;
6815 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006816
6817 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006818 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006819 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006820 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006821 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006822 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006823 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006824 /* fall through for HTTP_MSG_CHUNK_CRLF */
6825
6826 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6827 /* we want the CRLF after the data */
6828
6829 ret = http_skip_chunk_crlf(msg);
6830 if (ret == 0)
6831 goto missing_data;
6832 else if (ret < 0) {
6833 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006834 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006835 goto return_bad_res;
6836 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006837 /* we're in MSG_CHUNK_SIZE now, fall through */
6838
6839 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006840 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006841 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006842 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006843 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006844
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006845 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006846 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006847 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006848 else if (ret < 0) {
6849 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006850 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006851 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006852 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006853 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006854 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006855
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006856 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006857 if (unlikely(compressing)) {
6858 /* we need to flush output contents before syncing FSMs */
6859 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6860 compressing = 0;
6861 }
6862
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006863 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006864 if (ret == 0)
6865 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006866 else if (ret < 0) {
6867 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006868 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006869 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006870 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006871 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006872
6873 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006874 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006875 if (unlikely(compressing)) {
6876 /* we need to flush output contents before syncing FSMs */
6877 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6878 compressing = 0;
6879 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006880
Willy Tarreauc623c172014-04-18 09:53:50 +02006881 /* we may have some pending data starting at res->buf->p
6882 * such as a last chunk of data or trailers.
6883 */
6884 b_adv(res->buf, msg->next);
6885 msg->next = 0;
6886
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006887 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006888 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006889 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6890 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006891 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006892
Willy Tarreau610ecce2010-01-04 21:15:02 +01006893 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006894 /* some state changes occurred, maybe the analyser
6895 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006896 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006897 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006898 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006899 /* response errors are most likely due to
6900 * the client aborting the transfer.
6901 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006902 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006903 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006904 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006905 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006906 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006907 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006908 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006909 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006910 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006911 }
6912 }
6913
Willy Tarreaud98cf932009-12-27 22:54:55 +01006914 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006915 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006916 if (unlikely(compressing)) {
6917 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006918 compressing = 0;
6919 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006920
Willy Tarreauc623c172014-04-18 09:53:50 +02006921 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6922 b_adv(res->buf, msg->next);
6923 msg->next = 0;
6924 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6925 }
6926
Willy Tarreauf003d372012-11-26 13:35:37 +01006927 if (res->flags & CF_SHUTW)
6928 goto aborted_xfer;
6929
6930 /* stop waiting for data if the input is closed before the end. If the
6931 * client side was already closed, it means that the client has aborted,
6932 * so we don't want to count this as a server abort. Otherwise it's a
6933 * server abort.
6934 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006935 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006936 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006937 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006938 if (!(s->flags & SF_ERR_MASK))
6939 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006940 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006941 if (objt_server(s->target))
6942 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006943 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006944 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006945
Willy Tarreau40dba092010-03-04 18:14:51 +01006946 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006947 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006948 goto return_bad_res;
6949
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006950 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006951 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006952 * Similarly, with keep-alive on the client side, we don't want to forward a
6953 * close.
6954 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006955 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006956 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6957 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006958 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006959
Willy Tarreau5c620922011-05-11 19:56:11 +02006960 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006961 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006962 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006963 * modes are already handled by the stream sock layer. We must not do
6964 * this in content-length mode because it could present the MSG_MORE
6965 * flag with the last block of forwarded data, which would cause an
6966 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006967 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006968 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006969 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006970
Willy Tarreau87b09662015-04-03 00:22:06 +02006971 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006972 return 0;
6973
Willy Tarreau40dba092010-03-04 18:14:51 +01006974 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006975 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006976 if (objt_server(s->target))
6977 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006978
6979 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006980 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006981 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006982 compressing = 0;
6983 }
6984
Willy Tarreauc623c172014-04-18 09:53:50 +02006985 /* we may have some pending data starting at res->buf->p */
6986 if (s->comp_algo == NULL) {
6987 b_adv(res->buf, msg->next);
6988 msg->next = 0;
6989 }
6990
Willy Tarreaud98cf932009-12-27 22:54:55 +01006991 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006992 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006993 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006994 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006995 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006996 if (objt_server(s->target))
6997 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006998
Willy Tarreaue7dff022015-04-03 01:14:29 +02006999 if (!(s->flags & SF_ERR_MASK))
7000 s->flags |= SF_ERR_PRXCOND;
7001 if (!(s->flags & SF_FINST_MASK))
7002 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007003 return 0;
7004
7005 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007006 if (unlikely(compressing)) {
7007 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7008 compressing = 0;
7009 }
7010
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007011 txn->rsp.msg_state = HTTP_MSG_ERROR;
7012 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007013 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007014 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007015 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007016
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007017 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007018 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007019 if (objt_server(s->target))
7020 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007021
Willy Tarreaue7dff022015-04-03 01:14:29 +02007022 if (!(s->flags & SF_ERR_MASK))
7023 s->flags |= SF_ERR_CLICL;
7024 if (!(s->flags & SF_FINST_MASK))
7025 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007026 return 0;
7027}
7028
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007029/* Iterate the same filter through all request headers.
7030 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007031 * Since it can manage the switch to another backend, it updates the per-proxy
7032 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007033 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007034int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007035{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007036 char *cur_ptr, *cur_end, *cur_next;
7037 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007038 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007039 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007040 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007041
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007042 last_hdr = 0;
7043
Willy Tarreau9b28e032012-10-12 23:49:43 +02007044 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007045 old_idx = 0;
7046
7047 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007048 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007049 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007050 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007051 (exp->action == ACT_ALLOW ||
7052 exp->action == ACT_DENY ||
7053 exp->action == ACT_TARPIT))
7054 return 0;
7055
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007056 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007057 if (!cur_idx)
7058 break;
7059
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007060 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007061 cur_ptr = cur_next;
7062 cur_end = cur_ptr + cur_hdr->len;
7063 cur_next = cur_end + cur_hdr->cr + 1;
7064
7065 /* Now we have one header between cur_ptr and cur_end,
7066 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007067 */
7068
Willy Tarreau15a53a42015-01-21 13:39:42 +01007069 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007070 switch (exp->action) {
7071 case ACT_SETBE:
7072 /* It is not possible to jump a second time.
7073 * FIXME: should we return an HTTP/500 here so that
7074 * the admin knows there's a problem ?
7075 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007076 if (s->be != strm_fe(s))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007077 break;
7078
7079 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02007080 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007081 last_hdr = 1;
7082 break;
7083
7084 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007085 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007086 last_hdr = 1;
7087 break;
7088
7089 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007090 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007091 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007092 break;
7093
7094 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007095 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007096 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007097 break;
7098
7099 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007100 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7101 if (trash.len < 0)
7102 return -1;
7103
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007104 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007105 /* FIXME: if the user adds a newline in the replacement, the
7106 * index will not be recalculated for now, and the new line
7107 * will not be counted as a new header.
7108 */
7109
7110 cur_end += delta;
7111 cur_next += delta;
7112 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007113 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007114 break;
7115
7116 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007117 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007118 cur_next += delta;
7119
Willy Tarreaufa355d42009-11-29 18:12:29 +01007120 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007121 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7122 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007123 cur_hdr->len = 0;
7124 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007125 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007126 break;
7127
7128 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007129 }
7130
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007131 /* keep the link from this header to next one in case of later
7132 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007133 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007134 old_idx = cur_idx;
7135 }
7136 return 0;
7137}
7138
7139
7140/* Apply the filter to the request line.
7141 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7142 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143 * Since it can manage the switch to another backend, it updates the per-proxy
7144 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007145 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007146int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007147{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007148 char *cur_ptr, *cur_end;
7149 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007150 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007151 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007152
Willy Tarreau3d300592007-03-18 18:34:41 +01007153 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007154 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007155 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007156 (exp->action == ACT_ALLOW ||
7157 exp->action == ACT_DENY ||
7158 exp->action == ACT_TARPIT))
7159 return 0;
7160 else if (exp->action == ACT_REMOVE)
7161 return 0;
7162
7163 done = 0;
7164
Willy Tarreau9b28e032012-10-12 23:49:43 +02007165 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007166 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007167
7168 /* Now we have the request line between cur_ptr and cur_end */
7169
Willy Tarreau15a53a42015-01-21 13:39:42 +01007170 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007171 switch (exp->action) {
7172 case ACT_SETBE:
7173 /* It is not possible to jump a second time.
7174 * FIXME: should we return an HTTP/500 here so that
7175 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007176 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007177 if (s->be != strm_fe(s))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007178 break;
7179
7180 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02007181 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007182 done = 1;
7183 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007184
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007185 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007186 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007187 done = 1;
7188 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007189
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007190 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007191 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007192 done = 1;
7193 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007194
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007195 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007196 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007197 done = 1;
7198 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007199
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007200 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007201 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7202 if (trash.len < 0)
7203 return -1;
7204
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007205 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007206 /* FIXME: if the user adds a newline in the replacement, the
7207 * index will not be recalculated for now, and the new line
7208 * will not be counted as a new header.
7209 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007210
Willy Tarreaufa355d42009-11-29 18:12:29 +01007211 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007212 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007213 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007214 HTTP_MSG_RQMETH,
7215 cur_ptr, cur_end + 1,
7216 NULL, NULL);
7217 if (unlikely(!cur_end))
7218 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007219
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007220 /* we have a full request and we know that we have either a CR
7221 * or an LF at <ptr>.
7222 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007223 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7224 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007225 /* there is no point trying this regex on headers */
7226 return 1;
7227 }
7228 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007229 return done;
7230}
Willy Tarreau97de6242006-12-27 17:18:38 +01007231
Willy Tarreau58f10d72006-12-04 02:26:12 +01007232
Willy Tarreau58f10d72006-12-04 02:26:12 +01007233
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007234/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007235 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007236 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007237 * unparsable request. Since it can manage the switch to another backend, it
7238 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007239 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007240int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007241{
Willy Tarreau192252e2015-04-04 01:47:55 +02007242 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007243 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007244 struct hdr_exp *exp;
7245
7246 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007247 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007248
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007249 /*
7250 * The interleaving of transformations and verdicts
7251 * makes it difficult to decide to continue or stop
7252 * the evaluation.
7253 */
7254
Willy Tarreau6c123b12010-01-28 20:22:06 +01007255 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7256 break;
7257
Willy Tarreau3d300592007-03-18 18:34:41 +01007258 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007259 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007260 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007261 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007262
7263 /* if this filter had a condition, evaluate it now and skip to
7264 * next filter if the condition does not match.
7265 */
7266 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007267 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007268 ret = acl_pass(ret);
7269 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7270 ret = !ret;
7271
7272 if (!ret)
7273 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007274 }
7275
7276 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007277 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007278 if (unlikely(ret < 0))
7279 return -1;
7280
7281 if (likely(ret == 0)) {
7282 /* The filter did not match the request, it can be
7283 * iterated through all headers.
7284 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007285 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7286 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007287 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007288 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007289 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007290}
7291
7292
Willy Tarreaua15645d2007-03-18 16:22:39 +01007293
Willy Tarreau58f10d72006-12-04 02:26:12 +01007294/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007295 * Try to retrieve the server associated to the appsession.
Willy Tarreau87b09662015-04-03 00:22:06 +02007296 * If the server is found, it's assigned to the stream.
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007297 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007298void manage_client_side_appsession(struct stream *s, const char *buf, int len) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02007299 struct http_txn *txn = s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007300 appsess *asession = NULL;
7301 char *sessid_temp = NULL;
7302
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007303 if (len > s->be->appsession_len) {
7304 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007305 }
7306
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007307 if (s->be->options2 & PR_O2_AS_REQL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007308 /* request-learn option is enabled : store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007309 if (txn->sessid != NULL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007310 /* free previously allocated memory as we don't need the stream id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007311 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007312 }
7313
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007314 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007315 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007316 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007317 return;
7318 }
7319
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007320 memcpy(txn->sessid, buf, len);
7321 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007322 }
7323
7324 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7325 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007326 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007327 return;
7328 }
7329
Cyril Bontéb21570a2009-11-29 20:04:48 +01007330 memcpy(sessid_temp, buf, len);
7331 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007332
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007333 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007334 /* free previously allocated memory */
7335 pool_free2(apools.sessid, sessid_temp);
7336
7337 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007338 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7339 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007340 asession->request_count++;
7341
7342 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007343 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007344
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007345 while (srv) {
7346 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007347 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007348 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007349 (s->flags & SF_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007350 /* we found the server and it's usable */
7351 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007352 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007353 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007354 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007355
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007356 break;
7357 } else {
7358 txn->flags &= ~TX_CK_MASK;
7359 txn->flags |= TX_CK_DOWN;
7360 }
7361 }
7362 srv = srv->next;
7363 }
7364 }
7365 }
7366}
7367
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007368/* Find the end of a cookie value contained between <s> and <e>. It works the
7369 * same way as with headers above except that the semi-colon also ends a token.
7370 * See RFC2965 for more information. Note that it requires a valid header to
7371 * return a valid result.
7372 */
7373char *find_cookie_value_end(char *s, const char *e)
7374{
7375 int quoted, qdpair;
7376
7377 quoted = qdpair = 0;
7378 for (; s < e; s++) {
7379 if (qdpair) qdpair = 0;
7380 else if (quoted) {
7381 if (*s == '\\') qdpair = 1;
7382 else if (*s == '"') quoted = 0;
7383 }
7384 else if (*s == '"') quoted = 1;
7385 else if (*s == ',' || *s == ';') return s;
7386 }
7387 return s;
7388}
7389
7390/* Delete a value in a header between delimiters <from> and <next> in buffer
7391 * <buf>. The number of characters displaced is returned, and the pointer to
7392 * the first delimiter is updated if required. The function tries as much as
7393 * possible to respect the following principles :
7394 * - replace <from> delimiter by the <next> one unless <from> points to a
7395 * colon, in which case <next> is simply removed
7396 * - set exactly one space character after the new first delimiter, unless
7397 * there are not enough characters in the block being moved to do so.
7398 * - remove unneeded spaces before the previous delimiter and after the new
7399 * one.
7400 *
7401 * It is the caller's responsibility to ensure that :
7402 * - <from> points to a valid delimiter or the colon ;
7403 * - <next> points to a valid delimiter or the final CR/LF ;
7404 * - there are non-space chars before <from> ;
7405 * - there is a CR/LF at or after <next>.
7406 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007407int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007408{
7409 char *prev = *from;
7410
7411 if (*prev == ':') {
7412 /* We're removing the first value, preserve the colon and add a
7413 * space if possible.
7414 */
7415 if (!http_is_crlf[(unsigned char)*next])
7416 next++;
7417 prev++;
7418 if (prev < next)
7419 *prev++ = ' ';
7420
7421 while (http_is_spht[(unsigned char)*next])
7422 next++;
7423 } else {
7424 /* Remove useless spaces before the old delimiter. */
7425 while (http_is_spht[(unsigned char)*(prev-1)])
7426 prev--;
7427 *from = prev;
7428
7429 /* copy the delimiter and if possible a space if we're
7430 * not at the end of the line.
7431 */
7432 if (!http_is_crlf[(unsigned char)*next]) {
7433 *prev++ = *next++;
7434 if (prev + 1 < next)
7435 *prev++ = ' ';
7436 while (http_is_spht[(unsigned char)*next])
7437 next++;
7438 }
7439 }
7440 return buffer_replace2(buf, prev, next, NULL, 0);
7441}
7442
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007443/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007444 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007445 * desirable to call it only when needed. This code is quite complex because
7446 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7447 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007448 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007449void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007450{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007451 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007452 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007453 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007454 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007455 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7456 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007457
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007458 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007459 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007460 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007461
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007462 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007463 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007464 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007465
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007466 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007467 hdr_beg = hdr_next;
7468 hdr_end = hdr_beg + cur_hdr->len;
7469 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007470
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007471 /* We have one full header between hdr_beg and hdr_end, and the
7472 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007473 * "Cookie:" headers.
7474 */
7475
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007476 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007477 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007478 old_idx = cur_idx;
7479 continue;
7480 }
7481
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007482 del_from = NULL; /* nothing to be deleted */
7483 preserve_hdr = 0; /* assume we may kill the whole header */
7484
Willy Tarreau58f10d72006-12-04 02:26:12 +01007485 /* Now look for cookies. Conforming to RFC2109, we have to support
7486 * attributes whose name begin with a '$', and associate them with
7487 * the right cookie, if we want to delete this cookie.
7488 * So there are 3 cases for each cookie read :
7489 * 1) it's a special attribute, beginning with a '$' : ignore it.
7490 * 2) it's a server id cookie that we *MAY* want to delete : save
7491 * some pointers on it (last semi-colon, beginning of cookie...)
7492 * 3) it's an application cookie : we *MAY* have to delete a previous
7493 * "special" cookie.
7494 * At the end of loop, if a "special" cookie remains, we may have to
7495 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007496 * *MUST* delete it.
7497 *
7498 * Note: RFC2965 is unclear about the processing of spaces around
7499 * the equal sign in the ATTR=VALUE form. A careful inspection of
7500 * the RFC explicitly allows spaces before it, and not within the
7501 * tokens (attrs or values). An inspection of RFC2109 allows that
7502 * too but section 10.1.3 lets one think that spaces may be allowed
7503 * after the equal sign too, resulting in some (rare) buggy
7504 * implementations trying to do that. So let's do what servers do.
7505 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7506 * allowed quoted strings in values, with any possible character
7507 * after a backslash, including control chars and delimitors, which
7508 * causes parsing to become ambiguous. Browsers also allow spaces
7509 * within values even without quotes.
7510 *
7511 * We have to keep multiple pointers in order to support cookie
7512 * removal at the beginning, middle or end of header without
7513 * corrupting the header. All of these headers are valid :
7514 *
7515 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7516 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7517 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7518 * | | | | | | | | |
7519 * | | | | | | | | hdr_end <--+
7520 * | | | | | | | +--> next
7521 * | | | | | | +----> val_end
7522 * | | | | | +-----------> val_beg
7523 * | | | | +--------------> equal
7524 * | | | +----------------> att_end
7525 * | | +---------------------> att_beg
7526 * | +--------------------------> prev
7527 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007528 */
7529
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007530 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7531 /* Iterate through all cookies on this line */
7532
7533 /* find att_beg */
7534 att_beg = prev + 1;
7535 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7536 att_beg++;
7537
7538 /* find att_end : this is the first character after the last non
7539 * space before the equal. It may be equal to hdr_end.
7540 */
7541 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007542
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007543 while (equal < hdr_end) {
7544 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007545 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007546 if (http_is_spht[(unsigned char)*equal++])
7547 continue;
7548 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007549 }
7550
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007551 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7552 * is between <att_beg> and <equal>, both may be identical.
7553 */
7554
7555 /* look for end of cookie if there is an equal sign */
7556 if (equal < hdr_end && *equal == '=') {
7557 /* look for the beginning of the value */
7558 val_beg = equal + 1;
7559 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7560 val_beg++;
7561
7562 /* find the end of the value, respecting quotes */
7563 next = find_cookie_value_end(val_beg, hdr_end);
7564
7565 /* make val_end point to the first white space or delimitor after the value */
7566 val_end = next;
7567 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7568 val_end--;
7569 } else {
7570 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007571 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007572
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007573 /* We have nothing to do with attributes beginning with '$'. However,
7574 * they will automatically be removed if a header before them is removed,
7575 * since they're supposed to be linked together.
7576 */
7577 if (*att_beg == '$')
7578 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007579
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007580 /* Ignore cookies with no equal sign */
7581 if (equal == next) {
7582 /* This is not our cookie, so we must preserve it. But if we already
7583 * scheduled another cookie for removal, we cannot remove the
7584 * complete header, but we can remove the previous block itself.
7585 */
7586 preserve_hdr = 1;
7587 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007588 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007589 val_end += delta;
7590 next += delta;
7591 hdr_end += delta;
7592 hdr_next += delta;
7593 cur_hdr->len += delta;
7594 http_msg_move_end(&txn->req, delta);
7595 prev = del_from;
7596 del_from = NULL;
7597 }
7598 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007599 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007600
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007601 /* if there are spaces around the equal sign, we need to
7602 * strip them otherwise we'll get trouble for cookie captures,
7603 * or even for rewrites. Since this happens extremely rarely,
7604 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007605 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007606 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7607 int stripped_before = 0;
7608 int stripped_after = 0;
7609
7610 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007611 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007612 equal += stripped_before;
7613 val_beg += stripped_before;
7614 }
7615
7616 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007617 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007618 val_beg += stripped_after;
7619 stripped_before += stripped_after;
7620 }
7621
7622 val_end += stripped_before;
7623 next += stripped_before;
7624 hdr_end += stripped_before;
7625 hdr_next += stripped_before;
7626 cur_hdr->len += stripped_before;
7627 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007628 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007629 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007630
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007631 /* First, let's see if we want to capture this cookie. We check
7632 * that we don't already have a client side cookie, because we
7633 * can only capture one. Also as an optimisation, we ignore
7634 * cookies shorter than the declared name.
7635 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007636 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7637 (val_end - att_beg >= sess->fe->capture_namelen) &&
7638 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007639 int log_len = val_end - att_beg;
7640
7641 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7642 Alert("HTTP logging : out of memory.\n");
7643 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007644 if (log_len > sess->fe->capture_len)
7645 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007646 memcpy(txn->cli_cookie, att_beg, log_len);
7647 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007648 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007649 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007650
Willy Tarreaubca99692010-10-06 19:25:55 +02007651 /* Persistence cookies in passive, rewrite or insert mode have the
7652 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007653 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007654 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007655 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007656 * For cookies in prefix mode, the form is :
7657 *
7658 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007659 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007660 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7661 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7662 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007663 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007664
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007665 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7666 * have the server ID between val_beg and delim, and the original cookie between
7667 * delim+1 and val_end. Otherwise, delim==val_end :
7668 *
7669 * Cookie: NAME=SRV; # in all but prefix modes
7670 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7671 * | || || | |+-> next
7672 * | || || | +--> val_end
7673 * | || || +---------> delim
7674 * | || |+------------> val_beg
7675 * | || +-------------> att_end = equal
7676 * | |+-----------------> att_beg
7677 * | +------------------> prev
7678 * +-------------------------> hdr_beg
7679 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007680
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007681 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007682 for (delim = val_beg; delim < val_end; delim++)
7683 if (*delim == COOKIE_DELIM)
7684 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007685 } else {
7686 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007687 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007688 /* Now check if the cookie contains a date field, which would
7689 * appear after a vertical bar ('|') just after the server name
7690 * and before the delimiter.
7691 */
7692 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7693 if (vbar1) {
7694 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007695 * right is the last seen date. It is a base64 encoded
7696 * 30-bit value representing the UNIX date since the
7697 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007698 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007699 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007700 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007701 if (val_end - vbar1 >= 5) {
7702 val = b64tos30(vbar1);
7703 if (val > 0)
7704 txn->cookie_last_date = val << 2;
7705 }
7706 /* look for a second vertical bar */
7707 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7708 if (vbar1 && (val_end - vbar1 > 5)) {
7709 val = b64tos30(vbar1 + 1);
7710 if (val > 0)
7711 txn->cookie_first_date = val << 2;
7712 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007713 }
7714 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007715
Willy Tarreauf64d1412010-10-07 20:06:11 +02007716 /* if the cookie has an expiration date and the proxy wants to check
7717 * it, then we do that now. We first check if the cookie is too old,
7718 * then only if it has expired. We detect strict overflow because the
7719 * time resolution here is not great (4 seconds). Cookies with dates
7720 * in the future are ignored if their offset is beyond one day. This
7721 * allows an admin to fix timezone issues without expiring everyone
7722 * and at the same time avoids keeping unwanted side effects for too
7723 * long.
7724 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007725 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7726 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007727 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007728 txn->flags &= ~TX_CK_MASK;
7729 txn->flags |= TX_CK_OLD;
7730 delim = val_beg; // let's pretend we have not found the cookie
7731 txn->cookie_first_date = 0;
7732 txn->cookie_last_date = 0;
7733 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007734 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7735 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007736 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007737 txn->flags &= ~TX_CK_MASK;
7738 txn->flags |= TX_CK_EXPIRED;
7739 delim = val_beg; // let's pretend we have not found the cookie
7740 txn->cookie_first_date = 0;
7741 txn->cookie_last_date = 0;
7742 }
7743
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007744 /* Here, we'll look for the first running server which supports the cookie.
7745 * This allows to share a same cookie between several servers, for example
7746 * to dedicate backup servers to specific servers only.
7747 * However, to prevent clients from sticking to cookie-less backup server
7748 * when they have incidentely learned an empty cookie, we simply ignore
7749 * empty cookies and mark them as invalid.
7750 * The same behaviour is applied when persistence must be ignored.
7751 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007752 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007753 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007754
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007755 while (srv) {
7756 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7757 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007758 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007759 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007760 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007761 /* we found the server and we can use it */
7762 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007763 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007764 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007765 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007766 break;
7767 } else {
7768 /* we found a server, but it's down,
7769 * mark it as such and go on in case
7770 * another one is available.
7771 */
7772 txn->flags &= ~TX_CK_MASK;
7773 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007774 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007775 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007776 srv = srv->next;
7777 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007778
Willy Tarreauf64d1412010-10-07 20:06:11 +02007779 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007780 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007781 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007782 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007783 txn->flags |= TX_CK_UNUSED;
7784 else
7785 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007786 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007787
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007788 /* depending on the cookie mode, we may have to either :
7789 * - delete the complete cookie if we're in insert+indirect mode, so that
7790 * the server never sees it ;
7791 * - remove the server id from the cookie value, and tag the cookie as an
7792 * application cookie so that it does not get accidentely removed later,
7793 * if we're in cookie prefix mode
7794 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007795 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007796 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007797
Willy Tarreau9b28e032012-10-12 23:49:43 +02007798 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007799 val_end += delta;
7800 next += delta;
7801 hdr_end += delta;
7802 hdr_next += delta;
7803 cur_hdr->len += delta;
7804 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007805
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007806 del_from = NULL;
7807 preserve_hdr = 1; /* we want to keep this cookie */
7808 }
7809 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007810 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007811 del_from = prev;
7812 }
7813 } else {
7814 /* This is not our cookie, so we must preserve it. But if we already
7815 * scheduled another cookie for removal, we cannot remove the
7816 * complete header, but we can remove the previous block itself.
7817 */
7818 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007819
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007820 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007821 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007822 if (att_beg >= del_from)
7823 att_beg += delta;
7824 if (att_end >= del_from)
7825 att_end += delta;
7826 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007827 val_end += delta;
7828 next += delta;
7829 hdr_end += delta;
7830 hdr_next += delta;
7831 cur_hdr->len += delta;
7832 http_msg_move_end(&txn->req, delta);
7833 prev = del_from;
7834 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007835 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007836 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007837
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007838 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007839 if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007840 int cmp_len, value_len;
7841 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007842
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007843 if (s->be->options2 & PR_O2_AS_PFX) {
7844 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7845 value_begin = att_beg + s->be->appsession_name_len;
7846 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007847 } else {
7848 cmp_len = att_end - att_beg;
7849 value_begin = val_beg;
7850 value_len = val_end - val_beg;
7851 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007852
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007853 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007854 if (cmp_len == s->be->appsession_name_len &&
7855 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7856 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007857 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007858 }
7859
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007860 /* continue with next cookie on this header line */
7861 att_beg = next;
7862 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007863
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007864 /* There are no more cookies on this line.
7865 * We may still have one (or several) marked for deletion at the
7866 * end of the line. We must do this now in two ways :
7867 * - if some cookies must be preserved, we only delete from the
7868 * mark to the end of line ;
7869 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007870 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007871 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007872 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007873 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007874 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007875 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007876 cur_hdr->len += delta;
7877 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007878 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007879
7880 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007881 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7882 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007883 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007884 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007885 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007886 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007887 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007888 }
7889
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007890 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007891 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007892 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007893}
7894
7895
Willy Tarreaua15645d2007-03-18 16:22:39 +01007896/* Iterate the same filter through all response headers contained in <rtr>.
7897 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7898 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007899int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007900{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007901 char *cur_ptr, *cur_end, *cur_next;
7902 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007903 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007904 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007905 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007906
7907 last_hdr = 0;
7908
Willy Tarreau9b28e032012-10-12 23:49:43 +02007909 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007910 old_idx = 0;
7911
7912 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007913 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007914 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007915 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007916 (exp->action == ACT_ALLOW ||
7917 exp->action == ACT_DENY))
7918 return 0;
7919
7920 cur_idx = txn->hdr_idx.v[old_idx].next;
7921 if (!cur_idx)
7922 break;
7923
7924 cur_hdr = &txn->hdr_idx.v[cur_idx];
7925 cur_ptr = cur_next;
7926 cur_end = cur_ptr + cur_hdr->len;
7927 cur_next = cur_end + cur_hdr->cr + 1;
7928
7929 /* Now we have one header between cur_ptr and cur_end,
7930 * and the next header starts at cur_next.
7931 */
7932
Willy Tarreau15a53a42015-01-21 13:39:42 +01007933 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007934 switch (exp->action) {
7935 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007936 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007937 last_hdr = 1;
7938 break;
7939
7940 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007941 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007942 last_hdr = 1;
7943 break;
7944
7945 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007946 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7947 if (trash.len < 0)
7948 return -1;
7949
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007950 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007951 /* FIXME: if the user adds a newline in the replacement, the
7952 * index will not be recalculated for now, and the new line
7953 * will not be counted as a new header.
7954 */
7955
7956 cur_end += delta;
7957 cur_next += delta;
7958 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007959 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007960 break;
7961
7962 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007963 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007964 cur_next += delta;
7965
Willy Tarreaufa355d42009-11-29 18:12:29 +01007966 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007967 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7968 txn->hdr_idx.used--;
7969 cur_hdr->len = 0;
7970 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007971 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007972 break;
7973
7974 }
7975 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007976
7977 /* keep the link from this header to next one in case of later
7978 * removal of next header.
7979 */
7980 old_idx = cur_idx;
7981 }
7982 return 0;
7983}
7984
7985
7986/* Apply the filter to the status line in the response buffer <rtr>.
7987 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7988 * or -1 if a replacement resulted in an invalid status line.
7989 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007990int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007991{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007992 char *cur_ptr, *cur_end;
7993 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007994 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007995 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007996
7997
Willy Tarreau3d300592007-03-18 18:34:41 +01007998 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007999 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008000 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008001 (exp->action == ACT_ALLOW ||
8002 exp->action == ACT_DENY))
8003 return 0;
8004 else if (exp->action == ACT_REMOVE)
8005 return 0;
8006
8007 done = 0;
8008
Willy Tarreau9b28e032012-10-12 23:49:43 +02008009 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008010 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008011
8012 /* Now we have the status line between cur_ptr and cur_end */
8013
Willy Tarreau15a53a42015-01-21 13:39:42 +01008014 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008015 switch (exp->action) {
8016 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008017 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008018 done = 1;
8019 break;
8020
8021 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008022 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 done = 1;
8024 break;
8025
8026 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008027 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8028 if (trash.len < 0)
8029 return -1;
8030
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008031 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008032 /* FIXME: if the user adds a newline in the replacement, the
8033 * index will not be recalculated for now, and the new line
8034 * will not be counted as a new header.
8035 */
8036
Willy Tarreaufa355d42009-11-29 18:12:29 +01008037 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008039 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008040 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008041 cur_ptr, cur_end + 1,
8042 NULL, NULL);
8043 if (unlikely(!cur_end))
8044 return -1;
8045
8046 /* we have a full respnse and we know that we have either a CR
8047 * or an LF at <ptr>.
8048 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008049 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008050 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008051 /* there is no point trying this regex on headers */
8052 return 1;
8053 }
8054 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008055 return done;
8056}
8057
8058
8059
8060/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008061 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008062 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8063 * unparsable response.
8064 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008065int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008066{
Willy Tarreau192252e2015-04-04 01:47:55 +02008067 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008068 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008069 struct hdr_exp *exp;
8070
8071 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008072 int ret;
8073
8074 /*
8075 * The interleaving of transformations and verdicts
8076 * makes it difficult to decide to continue or stop
8077 * the evaluation.
8078 */
8079
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008080 if (txn->flags & TX_SVDENY)
8081 break;
8082
Willy Tarreau3d300592007-03-18 18:34:41 +01008083 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008084 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8085 exp->action == ACT_PASS)) {
8086 exp = exp->next;
8087 continue;
8088 }
8089
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008090 /* if this filter had a condition, evaluate it now and skip to
8091 * next filter if the condition does not match.
8092 */
8093 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008094 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008095 ret = acl_pass(ret);
8096 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8097 ret = !ret;
8098 if (!ret)
8099 continue;
8100 }
8101
Willy Tarreaua15645d2007-03-18 16:22:39 +01008102 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008103 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008104 if (unlikely(ret < 0))
8105 return -1;
8106
8107 if (likely(ret == 0)) {
8108 /* The filter did not match the response, it can be
8109 * iterated through all headers.
8110 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008111 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8112 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008113 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008114 }
8115 return 0;
8116}
8117
8118
Willy Tarreaua15645d2007-03-18 16:22:39 +01008119/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008120 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008121 * desirable to call it only when needed. This function is also used when we
8122 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008123 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008124void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008125{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008126 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008127 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008128 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008129 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008130 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008131 char *hdr_beg, *hdr_end, *hdr_next;
8132 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008133
Willy Tarreaua15645d2007-03-18 16:22:39 +01008134 /* Iterate through the headers.
8135 * we start with the start line.
8136 */
8137 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008138 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008139
8140 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8141 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008142 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008143
8144 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008145 hdr_beg = hdr_next;
8146 hdr_end = hdr_beg + cur_hdr->len;
8147 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008148
Willy Tarreau24581ba2010-08-31 22:39:35 +02008149 /* We have one full header between hdr_beg and hdr_end, and the
8150 * next header starts at hdr_next. We're only interested in
8151 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008152 */
8153
Willy Tarreau24581ba2010-08-31 22:39:35 +02008154 is_cookie2 = 0;
8155 prev = hdr_beg + 10;
8156 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008157 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008158 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8159 if (!val) {
8160 old_idx = cur_idx;
8161 continue;
8162 }
8163 is_cookie2 = 1;
8164 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008165 }
8166
Willy Tarreau24581ba2010-08-31 22:39:35 +02008167 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8168 * <prev> points to the colon.
8169 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008170 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008171
Willy Tarreau24581ba2010-08-31 22:39:35 +02008172 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8173 * check-cache is enabled) and we are not interested in checking
8174 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008175 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008176 if (s->be->cookie_name == NULL &&
8177 s->be->appsession_name == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008178 sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008179 return;
8180
Willy Tarreau24581ba2010-08-31 22:39:35 +02008181 /* OK so now we know we have to process this response cookie.
8182 * The format of the Set-Cookie header is slightly different
8183 * from the format of the Cookie header in that it does not
8184 * support the comma as a cookie delimiter (thus the header
8185 * cannot be folded) because the Expires attribute described in
8186 * the original Netscape's spec may contain an unquoted date
8187 * with a comma inside. We have to live with this because
8188 * many browsers don't support Max-Age and some browsers don't
8189 * support quoted strings. However the Set-Cookie2 header is
8190 * clean.
8191 *
8192 * We have to keep multiple pointers in order to support cookie
8193 * removal at the beginning, middle or end of header without
8194 * corrupting the header (in case of set-cookie2). A special
8195 * pointer, <scav> points to the beginning of the set-cookie-av
8196 * fields after the first semi-colon. The <next> pointer points
8197 * either to the end of line (set-cookie) or next unquoted comma
8198 * (set-cookie2). All of these headers are valid :
8199 *
8200 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8201 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8202 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8203 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8204 * | | | | | | | | | |
8205 * | | | | | | | | +-> next hdr_end <--+
8206 * | | | | | | | +------------> scav
8207 * | | | | | | +--------------> val_end
8208 * | | | | | +--------------------> val_beg
8209 * | | | | +----------------------> equal
8210 * | | | +------------------------> att_end
8211 * | | +----------------------------> att_beg
8212 * | +------------------------------> prev
8213 * +-----------------------------------------> hdr_beg
8214 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008215
Willy Tarreau24581ba2010-08-31 22:39:35 +02008216 for (; prev < hdr_end; prev = next) {
8217 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008218
Willy Tarreau24581ba2010-08-31 22:39:35 +02008219 /* find att_beg */
8220 att_beg = prev + 1;
8221 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8222 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008223
Willy Tarreau24581ba2010-08-31 22:39:35 +02008224 /* find att_end : this is the first character after the last non
8225 * space before the equal. It may be equal to hdr_end.
8226 */
8227 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008228
Willy Tarreau24581ba2010-08-31 22:39:35 +02008229 while (equal < hdr_end) {
8230 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8231 break;
8232 if (http_is_spht[(unsigned char)*equal++])
8233 continue;
8234 att_end = equal;
8235 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008236
Willy Tarreau24581ba2010-08-31 22:39:35 +02008237 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8238 * is between <att_beg> and <equal>, both may be identical.
8239 */
8240
8241 /* look for end of cookie if there is an equal sign */
8242 if (equal < hdr_end && *equal == '=') {
8243 /* look for the beginning of the value */
8244 val_beg = equal + 1;
8245 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8246 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008247
Willy Tarreau24581ba2010-08-31 22:39:35 +02008248 /* find the end of the value, respecting quotes */
8249 next = find_cookie_value_end(val_beg, hdr_end);
8250
8251 /* make val_end point to the first white space or delimitor after the value */
8252 val_end = next;
8253 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8254 val_end--;
8255 } else {
8256 /* <equal> points to next comma, semi-colon or EOL */
8257 val_beg = val_end = next = equal;
8258 }
8259
8260 if (next < hdr_end) {
8261 /* Set-Cookie2 supports multiple cookies, and <next> points to
8262 * a colon or semi-colon before the end. So skip all attr-value
8263 * pairs and look for the next comma. For Set-Cookie, since
8264 * commas are permitted in values, skip to the end.
8265 */
8266 if (is_cookie2)
8267 next = find_hdr_value_end(next, hdr_end);
8268 else
8269 next = hdr_end;
8270 }
8271
8272 /* Now everything is as on the diagram above */
8273
8274 /* Ignore cookies with no equal sign */
8275 if (equal == val_end)
8276 continue;
8277
8278 /* If there are spaces around the equal sign, we need to
8279 * strip them otherwise we'll get trouble for cookie captures,
8280 * or even for rewrites. Since this happens extremely rarely,
8281 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008282 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008283 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8284 int stripped_before = 0;
8285 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008286
Willy Tarreau24581ba2010-08-31 22:39:35 +02008287 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008288 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008289 equal += stripped_before;
8290 val_beg += stripped_before;
8291 }
8292
8293 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008294 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008295 val_beg += stripped_after;
8296 stripped_before += stripped_after;
8297 }
8298
8299 val_end += stripped_before;
8300 next += stripped_before;
8301 hdr_end += stripped_before;
8302 hdr_next += stripped_before;
8303 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008304 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008305 }
8306
8307 /* First, let's see if we want to capture this cookie. We check
8308 * that we don't already have a server side cookie, because we
8309 * can only capture one. Also as an optimisation, we ignore
8310 * cookies shorter than the declared name.
8311 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008312 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008313 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008314 (val_end - att_beg >= sess->fe->capture_namelen) &&
8315 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008316 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008317 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008318 Alert("HTTP logging : out of memory.\n");
8319 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008320 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008321 if (log_len > sess->fe->capture_len)
8322 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008323 memcpy(txn->srv_cookie, att_beg, log_len);
8324 txn->srv_cookie[log_len] = 0;
8325 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008326 }
8327
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008328 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008329 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008330 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008331 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8332 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008333 /* assume passive cookie by default */
8334 txn->flags &= ~TX_SCK_MASK;
8335 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008336
8337 /* If the cookie is in insert mode on a known server, we'll delete
8338 * this occurrence because we'll insert another one later.
8339 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008340 * a direct access.
8341 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008342 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008343 /* The "preserve" flag was set, we don't want to touch the
8344 * server's cookie.
8345 */
8346 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008347 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008348 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008349 /* this cookie must be deleted */
8350 if (*prev == ':' && next == hdr_end) {
8351 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008352 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008353 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8354 txn->hdr_idx.used--;
8355 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008356 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008357 hdr_next += delta;
8358 http_msg_move_end(&txn->rsp, delta);
8359 /* note: while both invalid now, <next> and <hdr_end>
8360 * are still equal, so the for() will stop as expected.
8361 */
8362 } else {
8363 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008364 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008365 next = prev;
8366 hdr_end += delta;
8367 hdr_next += delta;
8368 cur_hdr->len += delta;
8369 http_msg_move_end(&txn->rsp, delta);
8370 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008371 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008372 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008373 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008374 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008375 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008376 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008377 * with this server since we know it.
8378 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008379 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008380 next += delta;
8381 hdr_end += delta;
8382 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008383 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008384 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008385
Willy Tarreauf1348312010-10-07 15:54:11 +02008386 txn->flags &= ~TX_SCK_MASK;
8387 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008388 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008389 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008390 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008391 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008392 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008393 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008394 next += delta;
8395 hdr_end += delta;
8396 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008397 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008398 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008399
Willy Tarreau827aee92011-03-10 16:55:02 +01008400 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008401 txn->flags &= ~TX_SCK_MASK;
8402 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008403 }
8404 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008405 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008406 else if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008407 int cmp_len, value_len;
8408 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008409
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008410 if (s->be->options2 & PR_O2_AS_PFX) {
8411 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8412 value_begin = att_beg + s->be->appsession_name_len;
8413 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008414 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008415 cmp_len = att_end - att_beg;
8416 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008417 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008418 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008419
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008420 if ((cmp_len == s->be->appsession_name_len) &&
8421 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008422 /* free a possibly previously allocated memory */
8423 pool_free2(apools.sessid, txn->sessid);
8424
Willy Tarreau87b09662015-04-03 00:22:06 +02008425 /* Store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008426 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008427 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008428 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008429 return;
8430 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008431 memcpy(txn->sessid, value_begin, value_len);
8432 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008433 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008434 }
8435 /* that's done for this cookie, check the next one on the same
8436 * line when next != hdr_end (only if is_cookie2).
8437 */
8438 }
8439 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008440 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008441 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008442
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008443 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008444 appsess *asession = NULL;
8445 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008446 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008447 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008448 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008449 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8450 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008451 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008452 return;
8453 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008454 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8455
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008456 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8457 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008458 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8459 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008460 return;
8461 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008462 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8463 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008464
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008465 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008466 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008467 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008468 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8469 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008470 return;
8471 }
8472 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008473 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008474
8475 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008476 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008477 }
8478
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008479 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008480 asession->request_count++;
8481 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008482}
8483
8484
Willy Tarreaua15645d2007-03-18 16:22:39 +01008485/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008486 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008487 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008488void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008489{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008490 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008491 char *p1, *p2;
8492
8493 char *cur_ptr, *cur_end, *cur_next;
8494 int cur_idx;
8495
Willy Tarreau5df51872007-11-25 16:20:08 +01008496 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008497 return;
8498
8499 /* Iterate through the headers.
8500 * we start with the start line.
8501 */
8502 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008503 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008504
8505 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8506 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008507 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008508
8509 cur_hdr = &txn->hdr_idx.v[cur_idx];
8510 cur_ptr = cur_next;
8511 cur_end = cur_ptr + cur_hdr->len;
8512 cur_next = cur_end + cur_hdr->cr + 1;
8513
8514 /* We have one full header between cur_ptr and cur_end, and the
8515 * next header starts at cur_next. We're only interested in
8516 * "Cookie:" headers.
8517 */
8518
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008519 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8520 if (val) {
8521 if ((cur_end - (cur_ptr + val) >= 8) &&
8522 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8523 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8524 return;
8525 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008526 }
8527
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008528 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8529 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008530 continue;
8531
8532 /* OK, right now we know we have a cache-control header at cur_ptr */
8533
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008534 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008535
8536 if (p1 >= cur_end) /* no more info */
8537 continue;
8538
8539 /* p1 is at the beginning of the value */
8540 p2 = p1;
8541
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008542 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008543 p2++;
8544
8545 /* we have a complete value between p1 and p2 */
8546 if (p2 < cur_end && *p2 == '=') {
8547 /* we have something of the form no-cache="set-cookie" */
8548 if ((cur_end - p1 >= 21) &&
8549 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8550 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008551 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008552 continue;
8553 }
8554
8555 /* OK, so we know that either p2 points to the end of string or to a comma */
8556 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008557 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008558 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8559 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8560 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008561 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008562 return;
8563 }
8564
8565 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008566 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008567 continue;
8568 }
8569 }
8570}
8571
8572
Willy Tarreau58f10d72006-12-04 02:26:12 +01008573/*
8574 * Try to retrieve a known appsession in the URI, then the associated server.
Willy Tarreau87b09662015-04-03 00:22:06 +02008575 * If the server is found, it's assigned to the stream.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008576 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008577void get_srv_from_appsession(struct stream *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008578{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008579 char *end_params, *first_param, *cur_param, *next_param;
8580 char separator;
8581 int value_len;
8582
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008583 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008584
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008585 if (s->be->appsession_name == NULL ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02008586 (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 +01008587 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008588 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008589
Cyril Bontéb21570a2009-11-29 20:04:48 +01008590 first_param = NULL;
8591 switch (mode) {
8592 case PR_O2_AS_M_PP:
8593 first_param = memchr(begin, ';', len);
8594 break;
8595 case PR_O2_AS_M_QS:
8596 first_param = memchr(begin, '?', len);
8597 break;
8598 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008599
Cyril Bontéb21570a2009-11-29 20:04:48 +01008600 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008601 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008602 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008603
Cyril Bontéb21570a2009-11-29 20:04:48 +01008604 switch (mode) {
8605 case PR_O2_AS_M_PP:
8606 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8607 end_params = (char *) begin + len;
8608 }
8609 separator = ';';
8610 break;
8611 case PR_O2_AS_M_QS:
8612 end_params = (char *) begin + len;
8613 separator = '&';
8614 break;
8615 default:
8616 /* unknown mode, shouldn't happen */
8617 return;
8618 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008619
Cyril Bontéb21570a2009-11-29 20:04:48 +01008620 cur_param = next_param = end_params;
8621 while (cur_param > first_param) {
8622 cur_param--;
8623 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8624 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008625 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8626 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8627 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008628 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008629 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8630 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008631 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008632 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008633 }
8634 break;
8635 }
8636 next_param = cur_param;
8637 }
8638 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008639#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008640 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008641 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008642#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008643}
8644
Willy Tarreaub2513902006-12-17 14:52:38 +01008645/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008646 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008647 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008648 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008649 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008650 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008651 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008652 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008653 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008654int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008655{
8656 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008657 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008658 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008659
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008660 if (!uri_auth)
8661 return 0;
8662
Cyril Bonté70be45d2010-10-12 00:14:35 +02008663 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008664 return 0;
8665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008666 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008667 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008668 return 0;
8669
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008670 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008671 return 0;
8672
Willy Tarreaub2513902006-12-17 14:52:38 +01008673 return 1;
8674}
8675
Willy Tarreau4076a152009-04-02 15:18:36 +02008676/*
8677 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008678 * By default it tries to report the error position as msg->err_pos. However if
8679 * this one is not set, it will then report msg->next, which is the last known
8680 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008681 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008682 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008683void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008684 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008685 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008686{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008687 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008688 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008689 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008690
Willy Tarreau9b28e032012-10-12 23:49:43 +02008691 es->len = MIN(chn->buf->i, sizeof(es->buf));
8692 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008693 len1 = MIN(len1, es->len);
8694 len2 = es->len - len1; /* remaining data if buffer wraps */
8695
Willy Tarreau9b28e032012-10-12 23:49:43 +02008696 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008697 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008698 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008699
Willy Tarreau4076a152009-04-02 15:18:36 +02008700 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008701 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008702 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008703 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008704
Willy Tarreau4076a152009-04-02 15:18:36 +02008705 es->when = date; // user-visible date
8706 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008707 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008708 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008709 if (objt_conn(sess->origin))
8710 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008711 else
8712 memset(&es->src, 0, sizeof(es->src));
8713
Willy Tarreau078272e2010-12-12 12:46:33 +01008714 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008715 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008716 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008717 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008718 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008719 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008720 es->b_out = chn->buf->o;
8721 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008722 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008723 es->m_clen = msg->chunk_len;
8724 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008725}
Willy Tarreaub2513902006-12-17 14:52:38 +01008726
Willy Tarreau294c4732011-12-16 21:35:50 +01008727/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8728 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8729 * performed over the whole headers. Otherwise it must contain a valid header
8730 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8731 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8732 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8733 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008734 * -1. The value fetch stops at commas, so this function is suited for use with
8735 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008736 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008737 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008738unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008739 struct hdr_idx *idx, int occ,
8740 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008741{
Willy Tarreau294c4732011-12-16 21:35:50 +01008742 struct hdr_ctx local_ctx;
8743 char *ptr_hist[MAX_HDR_HISTORY];
8744 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008745 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008746 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008747
Willy Tarreau294c4732011-12-16 21:35:50 +01008748 if (!ctx) {
8749 local_ctx.idx = 0;
8750 ctx = &local_ctx;
8751 }
8752
Willy Tarreaubce70882009-09-07 11:51:47 +02008753 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008754 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008755 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008756 occ--;
8757 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008758 *vptr = ctx->line + ctx->val;
8759 *vlen = ctx->vlen;
8760 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008761 }
8762 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008763 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008764 }
8765
8766 /* negative occurrence, we scan all the list then walk back */
8767 if (-occ > MAX_HDR_HISTORY)
8768 return 0;
8769
Willy Tarreau294c4732011-12-16 21:35:50 +01008770 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008771 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008772 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8773 len_hist[hist_ptr] = ctx->vlen;
8774 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008775 hist_ptr = 0;
8776 found++;
8777 }
8778 if (-occ > found)
8779 return 0;
8780 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008781 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8782 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8783 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008784 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008785 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008786 if (hist_ptr >= MAX_HDR_HISTORY)
8787 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008788 *vptr = ptr_hist[hist_ptr];
8789 *vlen = len_hist[hist_ptr];
8790 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008791}
8792
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008793/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8794 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8795 * performed over the whole headers. Otherwise it must contain a valid header
8796 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8797 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8798 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8799 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8800 * -1. This function differs from http_get_hdr() in that it only returns full
8801 * line header values and does not stop at commas.
8802 * The return value is 0 if nothing was found, or non-zero otherwise.
8803 */
8804unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8805 struct hdr_idx *idx, int occ,
8806 struct hdr_ctx *ctx, char **vptr, int *vlen)
8807{
8808 struct hdr_ctx local_ctx;
8809 char *ptr_hist[MAX_HDR_HISTORY];
8810 int len_hist[MAX_HDR_HISTORY];
8811 unsigned int hist_ptr;
8812 int found;
8813
8814 if (!ctx) {
8815 local_ctx.idx = 0;
8816 ctx = &local_ctx;
8817 }
8818
8819 if (occ >= 0) {
8820 /* search from the beginning */
8821 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8822 occ--;
8823 if (occ <= 0) {
8824 *vptr = ctx->line + ctx->val;
8825 *vlen = ctx->vlen;
8826 return 1;
8827 }
8828 }
8829 return 0;
8830 }
8831
8832 /* negative occurrence, we scan all the list then walk back */
8833 if (-occ > MAX_HDR_HISTORY)
8834 return 0;
8835
8836 found = hist_ptr = 0;
8837 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8838 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8839 len_hist[hist_ptr] = ctx->vlen;
8840 if (++hist_ptr >= MAX_HDR_HISTORY)
8841 hist_ptr = 0;
8842 found++;
8843 }
8844 if (-occ > found)
8845 return 0;
8846 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8847 * find occurrence -occ, so we have to check [hist_ptr+occ].
8848 */
8849 hist_ptr += occ;
8850 if (hist_ptr >= MAX_HDR_HISTORY)
8851 hist_ptr -= MAX_HDR_HISTORY;
8852 *vptr = ptr_hist[hist_ptr];
8853 *vlen = len_hist[hist_ptr];
8854 return 1;
8855}
8856
Willy Tarreaubaaee002006-06-26 02:48:02 +02008857/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008858 * Print a debug line with a header. Always stop at the first CR or LF char,
8859 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8860 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008861 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008862void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008863{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008864 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008865 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008866
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008867 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008868 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008869 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008870 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 +02008871
8872 for (max = 0; start + max < end; max++)
8873 if (start[max] == '\r' || start[max] == '\n')
8874 break;
8875
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008876 UBOUND(max, trash.size - trash.len - 3);
8877 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8878 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008879 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008880}
8881
Willy Tarreaueee5b512015-04-03 23:46:31 +02008882
8883/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8884 * The hdr_idx is allocated as well. In case of allocation failure, everything
8885 * allocated is freed and NULL is returned. Otherwise the new transaction is
8886 * assigned to the stream and returned.
8887 */
8888struct http_txn *http_alloc_txn(struct stream *s)
8889{
8890 struct http_txn *txn = s->txn;
8891
8892 if (txn)
8893 return txn;
8894
8895 txn = pool_alloc2(pool2_http_txn);
8896 if (!txn)
8897 return txn;
8898
8899 txn->hdr_idx.size = global.tune.max_http_hdr;
8900 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8901 if (!txn->hdr_idx.v) {
8902 pool_free2(pool2_http_txn, txn);
8903 return NULL;
8904 }
8905
8906 s->txn = txn;
8907 return txn;
8908}
8909
Willy Tarreau0937bc42009-12-22 15:03:09 +01008910/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008911 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008912 * the required fields are properly allocated and that we only need to (re)init
8913 * them. This should be used before processing any new request.
8914 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008915void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008916{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008917 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008918 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008919
8920 txn->flags = 0;
8921 txn->status = -1;
8922
Willy Tarreauf64d1412010-10-07 20:06:11 +02008923 txn->cookie_first_date = 0;
8924 txn->cookie_last_date = 0;
8925
Willy Tarreaueee5b512015-04-03 23:46:31 +02008926 txn->sessid = NULL;
8927 txn->srv_cookie = NULL;
8928 txn->cli_cookie = NULL;
8929 txn->uri = NULL;
8930
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008931 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008932 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008933 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008934 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008935 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008936 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008937 txn->req.chunk_len = 0LL;
8938 txn->req.body_len = 0LL;
8939 txn->rsp.chunk_len = 0LL;
8940 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008941 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8942 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008943 txn->req.chn = &s->req;
8944 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008945
8946 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008947
8948 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8949 if (fe->options2 & PR_O2_REQBUG_OK)
8950 txn->req.err_pos = -1; /* let buggy requests pass */
8951
Willy Tarreau0937bc42009-12-22 15:03:09 +01008952 if (txn->hdr_idx.v)
8953 hdr_idx_init(&txn->hdr_idx);
8954}
8955
8956/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008957void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008958{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008959 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008960 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008961
Willy Tarreau75195602014-03-11 15:48:55 +01008962 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008963 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008964 s->comp_algo->end(&s->comp_ctx);
8965 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008966 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008967
Willy Tarreau0937bc42009-12-22 15:03:09 +01008968 /* these ones will have been dynamically allocated */
8969 pool_free2(pool2_requri, txn->uri);
8970 pool_free2(pool2_capture, txn->cli_cookie);
8971 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008972 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008973 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008974
William Lallemanda73203e2012-03-12 12:48:57 +01008975 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008976 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008977 txn->uri = NULL;
8978 txn->srv_cookie = NULL;
8979 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008980
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008981 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008982 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008983 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008984 pool_free2(h->pool, s->req_cap[h->index]);
8985 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008986 }
8987
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008988 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008989 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008990 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008991 pool_free2(h->pool, s->res_cap[h->index]);
8992 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008993 }
8994
Willy Tarreau0937bc42009-12-22 15:03:09 +01008995}
8996
8997/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008998void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008999{
9000 http_end_txn(s);
9001 http_init_txn(s);
9002
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01009003 /* reinitialise the current rule list pointer to NULL. We are sure that
9004 * any rulelist match the NULL pointer.
9005 */
9006 s->current_rule_list = NULL;
9007
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009008 s->be = strm_fe(s);
9009 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02009010 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02009011 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01009012 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009013 /* re-init store persistence */
9014 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01009015 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009016
Willy Tarreau0937bc42009-12-22 15:03:09 +01009017 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009018
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009019 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01009020
Willy Tarreau739cfba2010-01-25 23:11:14 +01009021 /* We must trim any excess data from the response buffer, because we
9022 * may have blocked an invalid response from a server that we don't
9023 * want to accidentely forward once we disable the analysers, nor do
9024 * we want those data to come along with next response. A typical
9025 * example of such data would be from a buggy server responding to
9026 * a HEAD with some data, or sending more than the advertised
9027 * content-length.
9028 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009029 if (unlikely(s->res.buf->i))
9030 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009031
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009032 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009033 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009034
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009035 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009036 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009037
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009038 s->req.rex = TICK_ETERNITY;
9039 s->req.wex = TICK_ETERNITY;
9040 s->req.analyse_exp = TICK_ETERNITY;
9041 s->res.rex = TICK_ETERNITY;
9042 s->res.wex = TICK_ETERNITY;
9043 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009044}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009045
Sasha Pachev218f0642014-06-16 12:05:59 -06009046void free_http_res_rules(struct list *r)
9047{
9048 struct http_res_rule *tr, *pr;
9049
9050 list_for_each_entry_safe(pr, tr, r, list) {
9051 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009052 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009053 free(pr);
9054 }
9055}
9056
9057void free_http_req_rules(struct list *r)
9058{
Willy Tarreauff011f22011-01-06 17:51:27 +01009059 struct http_req_rule *tr, *pr;
9060
9061 list_for_each_entry_safe(pr, tr, r, list) {
9062 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01009063 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009064 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009065
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009066 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009067 free(pr);
9068 }
9069}
9070
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009071/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01009072struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9073{
9074 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009075 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009076 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009077 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009078
9079 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
9080 if (!rule) {
9081 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009082 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009083 }
9084
CJ Ess108b1dd2015-04-07 12:03:37 -04009085 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009086 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01009087 rule->action = HTTP_REQ_ACT_ALLOW;
9088 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009089 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009090 int code;
9091 int hc;
9092
Willy Tarreauff011f22011-01-06 17:51:27 +01009093 rule->action = HTTP_REQ_ACT_DENY;
9094 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009095 if (strcmp(args[cur_arg], "deny_status") == 0) {
9096 cur_arg++;
9097 if (!args[cur_arg]) {
9098 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9099 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9100 goto out_err;
9101 }
9102
9103 code = atol(args[cur_arg]);
9104 cur_arg++;
9105 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9106 if (http_err_codes[hc] == code) {
9107 rule->deny_status = hc;
9108 break;
9109 }
9110 }
9111
9112 if (hc >= HTTP_ERR_SIZE) {
9113 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9114 file, linenum, code);
9115 }
9116 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009117 } else if (!strcmp(args[0], "tarpit")) {
9118 rule->action = HTTP_REQ_ACT_TARPIT;
9119 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009120 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01009121 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009122 cur_arg = 1;
9123
9124 while(*args[cur_arg]) {
9125 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009126 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009127 cur_arg+=2;
9128 continue;
9129 } else
9130 break;
9131 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009132 } else if (!strcmp(args[0], "set-nice")) {
9133 rule->action = HTTP_REQ_ACT_SET_NICE;
9134 cur_arg = 1;
9135
9136 if (!*args[cur_arg] ||
9137 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9138 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9139 file, linenum, args[0]);
9140 goto out_err;
9141 }
9142 rule->arg.nice = atoi(args[cur_arg]);
9143 if (rule->arg.nice < -1024)
9144 rule->arg.nice = -1024;
9145 else if (rule->arg.nice > 1024)
9146 rule->arg.nice = 1024;
9147 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009148 } else if (!strcmp(args[0], "set-tos")) {
9149#ifdef IP_TOS
9150 char *err;
9151 rule->action = HTTP_REQ_ACT_SET_TOS;
9152 cur_arg = 1;
9153
9154 if (!*args[cur_arg] ||
9155 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9156 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9157 file, linenum, args[0]);
9158 goto out_err;
9159 }
9160
9161 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9162 if (err && *err != '\0') {
9163 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9164 file, linenum, err, args[0]);
9165 goto out_err;
9166 }
9167 cur_arg++;
9168#else
9169 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9170 goto out_err;
9171#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009172 } else if (!strcmp(args[0], "set-mark")) {
9173#ifdef SO_MARK
9174 char *err;
9175 rule->action = HTTP_REQ_ACT_SET_MARK;
9176 cur_arg = 1;
9177
9178 if (!*args[cur_arg] ||
9179 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9180 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9181 file, linenum, args[0]);
9182 goto out_err;
9183 }
9184
9185 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9186 if (err && *err != '\0') {
9187 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9188 file, linenum, err, args[0]);
9189 goto out_err;
9190 }
9191 cur_arg++;
9192 global.last_checks |= LSTCHK_NETADM;
9193#else
9194 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9195 goto out_err;
9196#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009197 } else if (!strcmp(args[0], "set-log-level")) {
9198 rule->action = HTTP_REQ_ACT_SET_LOGL;
9199 cur_arg = 1;
9200
9201 if (!*args[cur_arg] ||
9202 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9203 bad_log_level:
9204 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9205 file, linenum, args[0]);
9206 goto out_err;
9207 }
9208 if (strcmp(args[cur_arg], "silent") == 0)
9209 rule->arg.loglevel = -1;
9210 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9211 goto bad_log_level;
9212 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009213 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9214 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9215 cur_arg = 1;
9216
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009217 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9218 (*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 +01009219 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9220 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009221 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009222 }
9223
9224 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9225 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9226 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009227
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009228 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009229 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009230 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9231 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009232 free(proxy->conf.lfs_file);
9233 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9234 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009235 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009236 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9237 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009238 cur_arg = 1;
9239
9240 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009241 (*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 -06009242 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9243 file, linenum, args[0]);
9244 goto out_err;
9245 }
9246
9247 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9248 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9249 LIST_INIT(&rule->arg.hdr_add.fmt);
9250
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009251 error = NULL;
9252 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9253 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9254 args[cur_arg + 1], error);
9255 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009256 goto out_err;
9257 }
9258
9259 proxy->conf.args.ctx = ARGC_HRQ;
9260 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9261 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9262 file, linenum);
9263
9264 free(proxy->conf.lfs_file);
9265 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9266 proxy->conf.lfs_line = proxy->conf.args.line;
9267 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009268 } else if (strcmp(args[0], "del-header") == 0) {
9269 rule->action = HTTP_REQ_ACT_DEL_HDR;
9270 cur_arg = 1;
9271
9272 if (!*args[cur_arg] ||
9273 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9274 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9275 file, linenum, args[0]);
9276 goto out_err;
9277 }
9278
9279 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9280 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9281
9282 proxy->conf.args.ctx = ARGC_HRQ;
9283 free(proxy->conf.lfs_file);
9284 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9285 proxy->conf.lfs_line = proxy->conf.args.line;
9286 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009287 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9288 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009289 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009290 struct sample_expr *expr;
9291 unsigned int where;
9292 char *err = NULL;
9293
9294 cur_arg = 1;
9295 proxy->conf.args.ctx = ARGC_TRK;
9296
9297 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9298 if (!expr) {
9299 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9300 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9301 free(err);
9302 goto out_err;
9303 }
9304
9305 where = 0;
9306 if (proxy->cap & PR_CAP_FE)
9307 where |= SMP_VAL_FE_HRQ_HDR;
9308 if (proxy->cap & PR_CAP_BE)
9309 where |= SMP_VAL_BE_HRQ_HDR;
9310
9311 if (!(expr->fetch->val & where)) {
9312 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9313 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9314 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9315 args[cur_arg-1], sample_src_names(expr->fetch->use));
9316 free(expr);
9317 goto out_err;
9318 }
9319
9320 if (strcmp(args[cur_arg], "table") == 0) {
9321 cur_arg++;
9322 if (!args[cur_arg]) {
9323 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9324 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9325 free(expr);
9326 goto out_err;
9327 }
9328 /* we copy the table name for now, it will be resolved later */
9329 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9330 cur_arg++;
9331 }
9332 rule->act_prm.trk_ctr.expr = expr;
9333 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009334 } else if (strcmp(args[0], "redirect") == 0) {
9335 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009336 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009337
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009338 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009339 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9340 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9341 goto out_err;
9342 }
9343
9344 /* this redirect rule might already contain a parsed condition which
9345 * we'll pass to the http-request rule.
9346 */
9347 rule->action = HTTP_REQ_ACT_REDIR;
9348 rule->arg.redir = redir;
9349 rule->cond = redir->cond;
9350 redir->cond = NULL;
9351 cur_arg = 2;
9352 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009353 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9354 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9355 rule->action = HTTP_REQ_ACT_ADD_ACL;
9356 /*
9357 * '+ 8' for 'add-acl('
9358 * '- 9' for 'add-acl(' + trailing ')'
9359 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009360 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009361
9362 cur_arg = 1;
9363
9364 if (!*args[cur_arg] ||
9365 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9366 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9367 file, linenum, args[0]);
9368 goto out_err;
9369 }
9370
9371 LIST_INIT(&rule->arg.map.key);
9372 proxy->conf.args.ctx = ARGC_HRQ;
9373 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9374 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9375 file, linenum);
9376 free(proxy->conf.lfs_file);
9377 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9378 proxy->conf.lfs_line = proxy->conf.args.line;
9379 cur_arg += 1;
9380 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9381 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9382 rule->action = HTTP_REQ_ACT_DEL_ACL;
9383 /*
9384 * '+ 8' for 'del-acl('
9385 * '- 9' for 'del-acl(' + trailing ')'
9386 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009387 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009388
9389 cur_arg = 1;
9390
9391 if (!*args[cur_arg] ||
9392 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9393 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9394 file, linenum, args[0]);
9395 goto out_err;
9396 }
9397
9398 LIST_INIT(&rule->arg.map.key);
9399 proxy->conf.args.ctx = ARGC_HRQ;
9400 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9401 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9402 file, linenum);
9403 free(proxy->conf.lfs_file);
9404 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9405 proxy->conf.lfs_line = proxy->conf.args.line;
9406 cur_arg += 1;
9407 } else if (strncmp(args[0], "del-map", 7) == 0) {
9408 /* http-request del-map(<reference (map name)>) <key pattern> */
9409 rule->action = HTTP_REQ_ACT_DEL_MAP;
9410 /*
9411 * '+ 8' for 'del-map('
9412 * '- 9' for 'del-map(' + trailing ')'
9413 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009414 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009415
9416 cur_arg = 1;
9417
9418 if (!*args[cur_arg] ||
9419 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9420 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9421 file, linenum, args[0]);
9422 goto out_err;
9423 }
9424
9425 LIST_INIT(&rule->arg.map.key);
9426 proxy->conf.args.ctx = ARGC_HRQ;
9427 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9428 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9429 file, linenum);
9430 free(proxy->conf.lfs_file);
9431 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9432 proxy->conf.lfs_line = proxy->conf.args.line;
9433 cur_arg += 1;
9434 } else if (strncmp(args[0], "set-map", 7) == 0) {
9435 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9436 rule->action = HTTP_REQ_ACT_SET_MAP;
9437 /*
9438 * '+ 8' for 'set-map('
9439 * '- 9' for 'set-map(' + trailing ')'
9440 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009441 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009442
9443 cur_arg = 1;
9444
9445 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9446 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9447 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9448 file, linenum, args[0]);
9449 goto out_err;
9450 }
9451
9452 LIST_INIT(&rule->arg.map.key);
9453 LIST_INIT(&rule->arg.map.value);
9454 proxy->conf.args.ctx = ARGC_HRQ;
9455
9456 /* key pattern */
9457 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9458 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9459 file, linenum);
9460
9461 /* value pattern */
9462 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9463 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9464 file, linenum);
9465 free(proxy->conf.lfs_file);
9466 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9467 proxy->conf.lfs_line = proxy->conf.args.line;
9468
9469 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009470 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9471 char *errmsg = NULL;
9472 cur_arg = 1;
9473 /* try in the module list */
9474 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9475 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9476 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9477 free(errmsg);
9478 goto out_err;
9479 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009480 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009481 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 +01009482 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009483 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009484 }
9485
9486 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9487 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009488 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009489
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009490 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9491 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9492 file, linenum, args[0], errmsg);
9493 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009494 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009495 }
9496 rule->cond = cond;
9497 }
9498 else if (*args[cur_arg]) {
9499 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9500 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9501 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009502 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009503 }
9504
9505 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009506 out_err:
9507 free(rule);
9508 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009509}
9510
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009511/* parse an "http-respose" rule */
9512struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9513{
9514 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009515 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009516 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009517 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009518
9519 rule = calloc(1, sizeof(*rule));
9520 if (!rule) {
9521 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9522 goto out_err;
9523 }
9524
9525 if (!strcmp(args[0], "allow")) {
9526 rule->action = HTTP_RES_ACT_ALLOW;
9527 cur_arg = 1;
9528 } else if (!strcmp(args[0], "deny")) {
9529 rule->action = HTTP_RES_ACT_DENY;
9530 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009531 } else if (!strcmp(args[0], "set-nice")) {
9532 rule->action = HTTP_RES_ACT_SET_NICE;
9533 cur_arg = 1;
9534
9535 if (!*args[cur_arg] ||
9536 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9537 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9538 file, linenum, args[0]);
9539 goto out_err;
9540 }
9541 rule->arg.nice = atoi(args[cur_arg]);
9542 if (rule->arg.nice < -1024)
9543 rule->arg.nice = -1024;
9544 else if (rule->arg.nice > 1024)
9545 rule->arg.nice = 1024;
9546 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009547 } else if (!strcmp(args[0], "set-tos")) {
9548#ifdef IP_TOS
9549 char *err;
9550 rule->action = HTTP_RES_ACT_SET_TOS;
9551 cur_arg = 1;
9552
9553 if (!*args[cur_arg] ||
9554 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9555 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9556 file, linenum, args[0]);
9557 goto out_err;
9558 }
9559
9560 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9561 if (err && *err != '\0') {
9562 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9563 file, linenum, err, args[0]);
9564 goto out_err;
9565 }
9566 cur_arg++;
9567#else
9568 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9569 goto out_err;
9570#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009571 } else if (!strcmp(args[0], "set-mark")) {
9572#ifdef SO_MARK
9573 char *err;
9574 rule->action = HTTP_RES_ACT_SET_MARK;
9575 cur_arg = 1;
9576
9577 if (!*args[cur_arg] ||
9578 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9579 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9580 file, linenum, args[0]);
9581 goto out_err;
9582 }
9583
9584 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9585 if (err && *err != '\0') {
9586 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9587 file, linenum, err, args[0]);
9588 goto out_err;
9589 }
9590 cur_arg++;
9591 global.last_checks |= LSTCHK_NETADM;
9592#else
9593 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9594 goto out_err;
9595#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009596 } else if (!strcmp(args[0], "set-log-level")) {
9597 rule->action = HTTP_RES_ACT_SET_LOGL;
9598 cur_arg = 1;
9599
9600 if (!*args[cur_arg] ||
9601 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9602 bad_log_level:
9603 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9604 file, linenum, args[0]);
9605 goto out_err;
9606 }
9607 if (strcmp(args[cur_arg], "silent") == 0)
9608 rule->arg.loglevel = -1;
9609 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9610 goto bad_log_level;
9611 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009612 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9613 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9614 cur_arg = 1;
9615
9616 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9617 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9618 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9619 file, linenum, args[0]);
9620 goto out_err;
9621 }
9622
9623 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9624 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9625 LIST_INIT(&rule->arg.hdr_add.fmt);
9626
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009627 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009628 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009629 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9630 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009631 free(proxy->conf.lfs_file);
9632 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9633 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009634 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009635 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009636 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009637 cur_arg = 1;
9638
9639 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009640 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9641 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009642 file, linenum, args[0]);
9643 goto out_err;
9644 }
9645
9646 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9647 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9648 LIST_INIT(&rule->arg.hdr_add.fmt);
9649
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009650 error = NULL;
9651 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9652 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9653 args[cur_arg + 1], error);
9654 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009655 goto out_err;
9656 }
9657
9658 proxy->conf.args.ctx = ARGC_HRQ;
9659 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9660 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9661 file, linenum);
9662
9663 free(proxy->conf.lfs_file);
9664 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9665 proxy->conf.lfs_line = proxy->conf.args.line;
9666 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009667 } else if (strcmp(args[0], "del-header") == 0) {
9668 rule->action = HTTP_RES_ACT_DEL_HDR;
9669 cur_arg = 1;
9670
9671 if (!*args[cur_arg] ||
9672 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9673 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9674 file, linenum, args[0]);
9675 goto out_err;
9676 }
9677
9678 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9679 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9680
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009681 proxy->conf.args.ctx = ARGC_HRS;
9682 free(proxy->conf.lfs_file);
9683 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9684 proxy->conf.lfs_line = proxy->conf.args.line;
9685 cur_arg += 1;
9686 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9687 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9688 rule->action = HTTP_RES_ACT_ADD_ACL;
9689 /*
9690 * '+ 8' for 'add-acl('
9691 * '- 9' for 'add-acl(' + trailing ')'
9692 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009693 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009694
9695 cur_arg = 1;
9696
9697 if (!*args[cur_arg] ||
9698 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9699 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9700 file, linenum, args[0]);
9701 goto out_err;
9702 }
9703
9704 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009705 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009706 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9707 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9708 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009709 free(proxy->conf.lfs_file);
9710 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9711 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009712
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009713 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009714 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9715 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9716 rule->action = HTTP_RES_ACT_DEL_ACL;
9717 /*
9718 * '+ 8' for 'del-acl('
9719 * '- 9' for 'del-acl(' + trailing ')'
9720 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009721 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009722
9723 cur_arg = 1;
9724
9725 if (!*args[cur_arg] ||
9726 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9727 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9728 file, linenum, args[0]);
9729 goto out_err;
9730 }
9731
9732 LIST_INIT(&rule->arg.map.key);
9733 proxy->conf.args.ctx = ARGC_HRS;
9734 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9735 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9736 file, linenum);
9737 free(proxy->conf.lfs_file);
9738 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9739 proxy->conf.lfs_line = proxy->conf.args.line;
9740 cur_arg += 1;
9741 } else if (strncmp(args[0], "del-map", 7) == 0) {
9742 /* http-response del-map(<reference (map name)>) <key pattern> */
9743 rule->action = HTTP_RES_ACT_DEL_MAP;
9744 /*
9745 * '+ 8' for 'del-map('
9746 * '- 9' for 'del-map(' + trailing ')'
9747 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009748 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009749
9750 cur_arg = 1;
9751
9752 if (!*args[cur_arg] ||
9753 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9754 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9755 file, linenum, args[0]);
9756 goto out_err;
9757 }
9758
9759 LIST_INIT(&rule->arg.map.key);
9760 proxy->conf.args.ctx = ARGC_HRS;
9761 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9762 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9763 file, linenum);
9764 free(proxy->conf.lfs_file);
9765 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9766 proxy->conf.lfs_line = proxy->conf.args.line;
9767 cur_arg += 1;
9768 } else if (strncmp(args[0], "set-map", 7) == 0) {
9769 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9770 rule->action = HTTP_RES_ACT_SET_MAP;
9771 /*
9772 * '+ 8' for 'set-map('
9773 * '- 9' for 'set-map(' + trailing ')'
9774 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009775 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009776
9777 cur_arg = 1;
9778
9779 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9780 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9781 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9782 file, linenum, args[0]);
9783 goto out_err;
9784 }
9785
9786 LIST_INIT(&rule->arg.map.key);
9787 LIST_INIT(&rule->arg.map.value);
9788
9789 proxy->conf.args.ctx = ARGC_HRS;
9790
9791 /* key pattern */
9792 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9793 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9794 file, linenum);
9795
9796 /* value pattern */
9797 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9798 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9799 file, linenum);
9800
9801 free(proxy->conf.lfs_file);
9802 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9803 proxy->conf.lfs_line = proxy->conf.args.line;
9804
9805 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009806 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9807 char *errmsg = NULL;
9808 cur_arg = 1;
9809 /* try in the module list */
9810 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9811 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9812 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9813 free(errmsg);
9814 goto out_err;
9815 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009816 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009817 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 +02009818 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9819 goto out_err;
9820 }
9821
9822 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9823 struct acl_cond *cond;
9824 char *errmsg = NULL;
9825
9826 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9827 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9828 file, linenum, args[0], errmsg);
9829 free(errmsg);
9830 goto out_err;
9831 }
9832 rule->cond = cond;
9833 }
9834 else if (*args[cur_arg]) {
9835 Alert("parsing [%s:%d]: 'http-response %s' expects"
9836 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9837 file, linenum, args[0], args[cur_arg]);
9838 goto out_err;
9839 }
9840
9841 return rule;
9842 out_err:
9843 free(rule);
9844 return NULL;
9845}
9846
Willy Tarreau4baae242012-12-27 12:00:31 +01009847/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009848 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9849 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009850 */
9851struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009852 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009853{
9854 struct redirect_rule *rule;
9855 int cur_arg;
9856 int type = REDIRECT_TYPE_NONE;
9857 int code = 302;
9858 const char *destination = NULL;
9859 const char *cookie = NULL;
9860 int cookie_set = 0;
9861 unsigned int flags = REDIRECT_FLAG_NONE;
9862 struct acl_cond *cond = NULL;
9863
9864 cur_arg = 0;
9865 while (*(args[cur_arg])) {
9866 if (strcmp(args[cur_arg], "location") == 0) {
9867 if (!*args[cur_arg + 1])
9868 goto missing_arg;
9869
9870 type = REDIRECT_TYPE_LOCATION;
9871 cur_arg++;
9872 destination = args[cur_arg];
9873 }
9874 else if (strcmp(args[cur_arg], "prefix") == 0) {
9875 if (!*args[cur_arg + 1])
9876 goto missing_arg;
9877
9878 type = REDIRECT_TYPE_PREFIX;
9879 cur_arg++;
9880 destination = args[cur_arg];
9881 }
9882 else if (strcmp(args[cur_arg], "scheme") == 0) {
9883 if (!*args[cur_arg + 1])
9884 goto missing_arg;
9885
9886 type = REDIRECT_TYPE_SCHEME;
9887 cur_arg++;
9888 destination = args[cur_arg];
9889 }
9890 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9891 if (!*args[cur_arg + 1])
9892 goto missing_arg;
9893
9894 cur_arg++;
9895 cookie = args[cur_arg];
9896 cookie_set = 1;
9897 }
9898 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9899 if (!*args[cur_arg + 1])
9900 goto missing_arg;
9901
9902 cur_arg++;
9903 cookie = args[cur_arg];
9904 cookie_set = 0;
9905 }
9906 else if (strcmp(args[cur_arg], "code") == 0) {
9907 if (!*args[cur_arg + 1])
9908 goto missing_arg;
9909
9910 cur_arg++;
9911 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009912 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009913 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009914 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009915 args[cur_arg - 1], args[cur_arg]);
9916 return NULL;
9917 }
9918 }
9919 else if (!strcmp(args[cur_arg],"drop-query")) {
9920 flags |= REDIRECT_FLAG_DROP_QS;
9921 }
9922 else if (!strcmp(args[cur_arg],"append-slash")) {
9923 flags |= REDIRECT_FLAG_APPEND_SLASH;
9924 }
9925 else if (strcmp(args[cur_arg], "if") == 0 ||
9926 strcmp(args[cur_arg], "unless") == 0) {
9927 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9928 if (!cond) {
9929 memprintf(errmsg, "error in condition: %s", *errmsg);
9930 return NULL;
9931 }
9932 break;
9933 }
9934 else {
9935 memprintf(errmsg,
9936 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9937 args[cur_arg]);
9938 return NULL;
9939 }
9940 cur_arg++;
9941 }
9942
9943 if (type == REDIRECT_TYPE_NONE) {
9944 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9945 return NULL;
9946 }
9947
9948 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9949 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009950 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009951
9952 if (!use_fmt) {
9953 /* old-style static redirect rule */
9954 rule->rdr_str = strdup(destination);
9955 rule->rdr_len = strlen(destination);
9956 }
9957 else {
9958 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009959
9960 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9961 * if prefix == "/", we don't want to add anything, otherwise it
9962 * makes it hard for the user to configure a self-redirection.
9963 */
Godbachd9722032014-12-18 15:44:58 +08009964 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009965 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009966 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009967 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9968 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009969 free(curproxy->conf.lfs_file);
9970 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9971 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009972 }
9973 }
9974
Willy Tarreau4baae242012-12-27 12:00:31 +01009975 if (cookie) {
9976 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9977 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9978 */
9979 rule->cookie_len = strlen(cookie);
9980 if (cookie_set) {
9981 rule->cookie_str = malloc(rule->cookie_len + 10);
9982 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9983 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9984 rule->cookie_len += 9;
9985 } else {
9986 rule->cookie_str = malloc(rule->cookie_len + 21);
9987 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9988 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9989 rule->cookie_len += 20;
9990 }
9991 }
9992 rule->type = type;
9993 rule->code = code;
9994 rule->flags = flags;
9995 LIST_INIT(&rule->list);
9996 return rule;
9997
9998 missing_arg:
9999 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10000 return NULL;
10001}
10002
Willy Tarreau8797c062007-05-07 00:55:35 +020010003/************************************************************************/
10004/* The code below is dedicated to ACL parsing and matching */
10005/************************************************************************/
10006
10007
Willy Tarreau14174bc2012-04-16 14:34:04 +020010008/* This function ensures that the prerequisites for an L7 fetch are ready,
10009 * which means that a request or response is ready. If some data is missing,
10010 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010011 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10012 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010013 *
10014 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010015 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10016 * decide whether or not an HTTP message is present ;
10017 * 0 if the requested data cannot be fetched or if it is certain that
10018 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010019 * 1 if an HTTP message is ready
10020 */
10021static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010022smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010023 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010024{
Willy Tarreau192252e2015-04-04 01:47:55 +020010025 struct http_txn *txn;
10026 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010027
Willy Tarreaueee5b512015-04-03 23:46:31 +020010028 /* Note: this function may only be used from places where
10029 * http_init_txn() has already been done, and implies that <s>,
10030 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10031 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010032 */
10033
Willy Tarreau192252e2015-04-04 01:47:55 +020010034 if (!s)
10035 return 0;
10036 txn = s->txn;
10037
10038 if (!txn)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010039 return 0;
Willy Tarreau192252e2015-04-04 01:47:55 +020010040 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010041
10042 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +020010043 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010044
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010045 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010046 /* If the buffer does not leave enough free space at the end,
10047 * we must first realign it.
10048 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010049 if (s->req.buf->p > s->req.buf->data &&
10050 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10051 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010052
Willy Tarreau14174bc2012-04-16 14:34:04 +020010053 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010054 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010055 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010056
10057 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010058 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010059 http_msg_analyzer(msg, &txn->hdr_idx);
10060
10061 /* Still no valid request ? */
10062 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010063 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010064 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010065 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010066 }
10067 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010068 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010069 return 0;
10070 }
10071
10072 /* OK we just got a valid HTTP request. We have some minor
10073 * preparation to perform so that further checks can rely
10074 * on HTTP tests.
10075 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010076
10077 /* If the request was parsed but was too large, we must absolutely
10078 * return an error so that it is not processed. At the moment this
10079 * cannot happen, but if the parsers are to change in the future,
10080 * we want this check to be maintained.
10081 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010082 if (unlikely(s->req.buf->i + s->req.buf->p >
10083 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010084 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +020010085 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010086 return 1;
10087 }
10088
Willy Tarreau9b28e032012-10-12 23:49:43 +020010089 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010090 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010091 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010092
Willy Tarreau506d0502013-07-06 13:29:24 +020010093 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10094 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010095 }
10096
Willy Tarreau506d0502013-07-06 13:29:24 +020010097 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010098 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010099 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010100
10101 /* otherwise everything's ready for the request */
10102 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010103 else {
10104 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010105 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10106 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010107 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010108 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010109 }
10110
10111 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +020010112 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010113 return 1;
10114}
Willy Tarreau8797c062007-05-07 00:55:35 +020010115
Willy Tarreau6c616e02014-06-25 16:56:41 +020010116/* Note: these functinos *do* modify the sample. Even in case of success, at
10117 * least the type and uint value are modified.
10118 */
Willy Tarreauc0239e02012-04-16 14:42:55 +020010119#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau15e91e12015-04-04 00:52:09 +020010120 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 +020010121
Willy Tarreau24e32d82012-04-23 23:55:44 +020010122#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau15e91e12015-04-04 00:52:09 +020010123 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 +020010124
Willy Tarreau8797c062007-05-07 00:55:35 +020010125
10126/* 1. Check on METHOD
10127 * We use the pre-parsed method if it is known, and store its number as an
10128 * integer. If it is unknown, we use the pointer and the length.
10129 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010130static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010131{
10132 int len, meth;
10133
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010134 len = strlen(text);
10135 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010136
10137 pattern->val.i = meth;
10138 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010139 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010140 pattern->len = len;
10141 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010142 else {
10143 pattern->ptr.str = NULL;
10144 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010145 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010146 return 1;
10147}
10148
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010149/* This function fetches the method of current HTTP request and stores
10150 * it in the global pattern struct as a chunk. There are two possibilities :
10151 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10152 * in <len> and <ptr> is NULL ;
10153 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10154 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010155 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010156 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010157static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010158smp_fetch_meth(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010159 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010160{
10161 int meth;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010162 struct http_txn *txn = strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010163
Willy Tarreau24e32d82012-04-23 23:55:44 +020010164 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010165
Willy Tarreau8797c062007-05-07 00:55:35 +020010166 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010167 smp->type = SMP_T_METH;
10168 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010169 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010170 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10171 /* ensure the indexes are not affected */
10172 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010173 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010174 smp->data.meth.str.len = txn->req.sl.rq.m_l;
10175 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010176 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010177 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010178 return 1;
10179}
10180
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010181/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010182static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010183{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010184 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010185 struct pattern_list *lst;
10186 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010187
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010188 list_for_each_entry(lst, &expr->patterns, list) {
10189 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010190
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010191 /* well-known method */
10192 if (pattern->val.i != HTTP_METH_OTHER) {
10193 if (smp->data.meth.meth == pattern->val.i)
10194 return pattern;
10195 else
10196 continue;
10197 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010198
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010199 /* Other method, we must compare the strings */
10200 if (pattern->len != smp->data.meth.str.len)
10201 continue;
10202
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010203 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010204 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10205 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010206 return pattern;
10207 }
10208 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010209}
10210
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010211static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010212smp_fetch_rqver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010213 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010214{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010215 struct http_txn *txn = strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010216 char *ptr;
10217 int len;
10218
Willy Tarreauc0239e02012-04-16 14:42:55 +020010219 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010220
Willy Tarreau8797c062007-05-07 00:55:35 +020010221 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010222 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010223
10224 while ((len-- > 0) && (*ptr++ != '/'));
10225 if (len <= 0)
10226 return 0;
10227
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010228 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010229 smp->data.str.str = ptr;
10230 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010231
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010232 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010233 return 1;
10234}
10235
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010236static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010237smp_fetch_stver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010238 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010239{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010240 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010241 char *ptr;
10242 int len;
10243
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;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010247 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10248 return 0;
10249
Willy Tarreau8797c062007-05-07 00:55:35 +020010250 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010251 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010252
10253 while ((len-- > 0) && (*ptr++ != '/'));
10254 if (len <= 0)
10255 return 0;
10256
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010257 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010258 smp->data.str.str = ptr;
10259 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010260
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010261 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010262 return 1;
10263}
10264
10265/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010266static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010267smp_fetch_stcode(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010268 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010269{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010270 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010271 char *ptr;
10272 int len;
10273
Willy Tarreauc0239e02012-04-16 14:42:55 +020010274 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010275
Willy Tarreau15e91e12015-04-04 00:52:09 +020010276 txn = strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010277 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10278 return 0;
10279
Willy Tarreau8797c062007-05-07 00:55:35 +020010280 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010281 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010282
Willy Tarreauf853c462012-04-23 18:53:56 +020010283 smp->type = SMP_T_UINT;
10284 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010285 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010286 return 1;
10287}
10288
10289/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010290static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010291smp_fetch_url(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010292 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010293{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010294 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010295
Willy Tarreauc0239e02012-04-16 14:42:55 +020010296 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010297
Willy Tarreau15e91e12015-04-04 00:52:09 +020010298 txn = strm->txn;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010299 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010300 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010301 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010302 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010303 return 1;
10304}
10305
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010306static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010307smp_fetch_url_ip(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010308 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010309{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010310 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010311 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010312
Willy Tarreauc0239e02012-04-16 14:42:55 +020010313 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010314
Willy Tarreau15e91e12015-04-04 00:52:09 +020010315 txn = strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010316 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 +020010317 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010318 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010319
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010320 smp->type = SMP_T_IPV4;
10321 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010322 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010323 return 1;
10324}
10325
10326static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010327smp_fetch_url_port(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010328 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010329{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010330 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010331 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010332
Willy Tarreauc0239e02012-04-16 14:42:55 +020010333 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010334
Willy Tarreau15e91e12015-04-04 00:52:09 +020010335 txn = strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010336 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 +020010337 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10338 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010339
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010340 smp->type = SMP_T_UINT;
10341 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010342 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010343 return 1;
10344}
10345
Willy Tarreau185b5c42012-04-26 15:11:51 +020010346/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10347 * Accepts an optional argument of type string containing the header field name,
10348 * and an optional argument of type signed or unsigned integer to request an
10349 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010350 * headers are considered from the first one. It does not stop on commas and
10351 * returns full lines instead (useful for User-Agent or Date for example).
10352 */
10353static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010354smp_fetch_fhdr(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010355 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010356{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010357 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010358 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010359 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010360 int occ = 0;
10361 const char *name_str = NULL;
10362 int name_len = 0;
10363
10364 if (!ctx) {
10365 /* first call */
10366 ctx = &static_hdr_ctx;
10367 ctx->idx = 0;
10368 smp->ctx.a[0] = ctx;
10369 }
10370
10371 if (args) {
10372 if (args[0].type != ARGT_STR)
10373 return 0;
10374 name_str = args[0].data.str.str;
10375 name_len = args[0].data.str.len;
10376
10377 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10378 occ = args[1].data.uint;
10379 }
10380
10381 CHECK_HTTP_MESSAGE_FIRST();
10382
Willy Tarreau15e91e12015-04-04 00:52:09 +020010383 idx = &strm->txn->hdr_idx;
10384 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10385
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010386 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10387 /* search for header from the beginning */
10388 ctx->idx = 0;
10389
10390 if (!occ && !(opt & SMP_OPT_ITERATE))
10391 /* no explicit occurrence and single fetch => last header by default */
10392 occ = -1;
10393
10394 if (!occ)
10395 /* prepare to report multiple occurrences for ACL fetches */
10396 smp->flags |= SMP_F_NOT_LAST;
10397
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010398 smp->type = SMP_T_STR;
10399 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010400 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10401 return 1;
10402
10403 smp->flags &= ~SMP_F_NOT_LAST;
10404 return 0;
10405}
10406
10407/* 6. Check on HTTP header count. The number of occurrences is returned.
10408 * Accepts exactly 1 argument of type string. It does not stop on commas and
10409 * returns full lines instead (useful for User-Agent or Date for example).
10410 */
10411static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010412smp_fetch_fhdr_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010413 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010414{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010415 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010416 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010417 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010418 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010419 const char *name = NULL;
10420 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010421
Willy Tarreau601a4d12015-04-01 19:16:09 +020010422 if (args && args->type == ARGT_STR) {
10423 name = args->data.str.str;
10424 len = args->data.str.len;
10425 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010426
10427 CHECK_HTTP_MESSAGE_FIRST();
10428
Willy Tarreau15e91e12015-04-04 00:52:09 +020010429 idx = &strm->txn->hdr_idx;
10430 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10431
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010432 ctx.idx = 0;
10433 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010434 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010435 cnt++;
10436
10437 smp->type = SMP_T_UINT;
10438 smp->data.uint = cnt;
10439 smp->flags = SMP_F_VOL_HDR;
10440 return 1;
10441}
10442
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010443static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010444smp_fetch_hdr_names(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010445 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010446{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010447 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010448 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010449 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010450 struct chunk *temp;
10451 char del = ',';
10452
10453 if (args && args->type == ARGT_STR)
10454 del = *args[0].data.str.str;
10455
10456 CHECK_HTTP_MESSAGE_FIRST();
10457
Willy Tarreau15e91e12015-04-04 00:52:09 +020010458 idx = &strm->txn->hdr_idx;
10459 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10460
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010461 temp = get_trash_chunk();
10462
10463 ctx.idx = 0;
10464 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10465 if (temp->len)
10466 temp->str[temp->len++] = del;
10467 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10468 temp->len += ctx.del;
10469 }
10470
10471 smp->type = SMP_T_STR;
10472 smp->data.str.str = temp->str;
10473 smp->data.str.len = temp->len;
10474 smp->flags = SMP_F_VOL_HDR;
10475 return 1;
10476}
10477
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010478/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10479 * Accepts an optional argument of type string containing the header field name,
10480 * and an optional argument of type signed or unsigned integer to request an
10481 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010482 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010483 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010484static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010485smp_fetch_hdr(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010486 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010487{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010488 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010489 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010490 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010491 int occ = 0;
10492 const char *name_str = NULL;
10493 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010494
Willy Tarreaua890d072013-04-02 12:01:06 +020010495 if (!ctx) {
10496 /* first call */
10497 ctx = &static_hdr_ctx;
10498 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010499 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010500 }
10501
Willy Tarreau185b5c42012-04-26 15:11:51 +020010502 if (args) {
10503 if (args[0].type != ARGT_STR)
10504 return 0;
10505 name_str = args[0].data.str.str;
10506 name_len = args[0].data.str.len;
10507
10508 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10509 occ = args[1].data.uint;
10510 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010511
Willy Tarreaue333ec92012-04-16 16:26:40 +020010512 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010513
Willy Tarreau15e91e12015-04-04 00:52:09 +020010514 idx = &strm->txn->hdr_idx;
10515 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10516
Willy Tarreau185b5c42012-04-26 15:11:51 +020010517 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010518 /* search for header from the beginning */
10519 ctx->idx = 0;
10520
Willy Tarreau185b5c42012-04-26 15:11:51 +020010521 if (!occ && !(opt & SMP_OPT_ITERATE))
10522 /* no explicit occurrence and single fetch => last header by default */
10523 occ = -1;
10524
10525 if (!occ)
10526 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010527 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010528
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010529 smp->type = SMP_T_STR;
10530 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010531 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 +020010532 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010533
Willy Tarreau37406352012-04-23 16:16:37 +020010534 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010535 return 0;
10536}
10537
Willy Tarreauc11416f2007-06-17 16:58:38 +020010538/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010539 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010540 */
10541static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010542smp_fetch_hdr_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010543 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010544{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010545 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010546 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010547 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010548 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010549 const char *name = NULL;
10550 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010551
Willy Tarreau601a4d12015-04-01 19:16:09 +020010552 if (args && args->type == ARGT_STR) {
10553 name = args->data.str.str;
10554 len = args->data.str.len;
10555 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010556
Willy Tarreaue333ec92012-04-16 16:26:40 +020010557 CHECK_HTTP_MESSAGE_FIRST();
10558
Willy Tarreau15e91e12015-04-04 00:52:09 +020010559 idx = &strm->txn->hdr_idx;
10560 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10561
Willy Tarreau33a7e692007-06-10 19:45:56 +020010562 ctx.idx = 0;
10563 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010564 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010565 cnt++;
10566
Willy Tarreauf853c462012-04-23 18:53:56 +020010567 smp->type = SMP_T_UINT;
10568 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010569 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010570 return 1;
10571}
10572
Willy Tarreau185b5c42012-04-26 15:11:51 +020010573/* Fetch an HTTP header's integer value. The integer value is returned. It
10574 * takes a mandatory argument of type string and an optional one of type int
10575 * to designate a specific occurrence. It returns an unsigned integer, which
10576 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010577 */
10578static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010579smp_fetch_hdr_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010580 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010581{
Willy Tarreau192252e2015-04-04 01:47:55 +020010582 int ret = smp_fetch_hdr(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010583
Willy Tarreauf853c462012-04-23 18:53:56 +020010584 if (ret > 0) {
10585 smp->type = SMP_T_UINT;
10586 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10587 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010588
Willy Tarreaud53e2422012-04-16 17:21:11 +020010589 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010590}
10591
Cyril Bonté69fa9922012-10-25 00:01:06 +020010592/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10593 * and an optional one of type int to designate a specific occurrence.
10594 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010595 */
10596static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010597smp_fetch_hdr_ip(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010598 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010599{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010600 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010601
Willy Tarreau192252e2015-04-04 01:47:55 +020010602 while ((ret = smp_fetch_hdr(px, sess, strm, opt, args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010603 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10604 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010605 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010606 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010607 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010608 if (smp->data.str.len < temp->size - 1) {
10609 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10610 temp->str[smp->data.str.len] = '\0';
10611 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10612 smp->type = SMP_T_IPV6;
10613 break;
10614 }
10615 }
10616 }
10617
Willy Tarreaud53e2422012-04-16 17:21:11 +020010618 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010619 if (!(smp->flags & SMP_F_NOT_LAST))
10620 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010621 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010622 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010623}
10624
Willy Tarreau737b0c12007-06-10 21:28:46 +020010625/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10626 * the first '/' after the possible hostname, and ends before the possible '?'.
10627 */
10628static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010629smp_fetch_path(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010630 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010631{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010632 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010633 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010634
Willy Tarreauc0239e02012-04-16 14:42:55 +020010635 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010636
Willy Tarreau15e91e12015-04-04 00:52:09 +020010637 txn = strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010638 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010639 ptr = http_get_path(txn);
10640 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010641 return 0;
10642
10643 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010644 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010645 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010646
10647 while (ptr < end && *ptr != '?')
10648 ptr++;
10649
Willy Tarreauf853c462012-04-23 18:53:56 +020010650 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010651 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010652 return 1;
10653}
10654
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010655/* This produces a concatenation of the first occurrence of the Host header
10656 * followed by the path component if it begins with a slash ('/'). This means
10657 * that '*' will not be added, resulting in exactly the first Host entry.
10658 * If no Host header is found, then the path is returned as-is. The returned
10659 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010660 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010661 */
10662static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010663smp_fetch_base(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010664 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010665{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010666 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010667 char *ptr, *end, *beg;
10668 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010669 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010670
10671 CHECK_HTTP_MESSAGE_FIRST();
10672
Willy Tarreau15e91e12015-04-04 00:52:09 +020010673 txn = strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010674 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010675 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreau192252e2015-04-04 01:47:55 +020010676 return smp_fetch_path(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010677
10678 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010679 temp = get_trash_chunk();
10680 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010681 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010682 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010683 smp->data.str.len = ctx.vlen;
10684
10685 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010686 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010687 beg = http_get_path(txn);
10688 if (!beg)
10689 beg = end;
10690
10691 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10692
10693 if (beg < ptr && *beg == '/') {
10694 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10695 smp->data.str.len += ptr - beg;
10696 }
10697
10698 smp->flags = SMP_F_VOL_1ST;
10699 return 1;
10700}
10701
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010702/* This produces a 32-bit hash of the concatenation of the first occurrence of
10703 * the Host header followed by the path component if it begins with a slash ('/').
10704 * This means that '*' will not be added, resulting in exactly the first Host
10705 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010706 * is hashed using the path hash followed by a full avalanche hash and provides a
10707 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010708 * high-traffic sites without having to store whole paths.
10709 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010710int
Willy Tarreau192252e2015-04-04 01:47:55 +020010711smp_fetch_base32(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010712 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010713{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010714 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010715 struct hdr_ctx ctx;
10716 unsigned int hash = 0;
10717 char *ptr, *beg, *end;
10718 int len;
10719
10720 CHECK_HTTP_MESSAGE_FIRST();
10721
Willy Tarreau15e91e12015-04-04 00:52:09 +020010722 txn = strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010723 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010724 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010725 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10726 ptr = ctx.line + ctx.val;
10727 len = ctx.vlen;
10728 while (len--)
10729 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10730 }
10731
10732 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010733 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010734 beg = http_get_path(txn);
10735 if (!beg)
10736 beg = end;
10737
10738 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10739
10740 if (beg < ptr && *beg == '/') {
10741 while (beg < ptr)
10742 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10743 }
10744 hash = full_hash(hash);
10745
10746 smp->type = SMP_T_UINT;
10747 smp->data.uint = hash;
10748 smp->flags = SMP_F_VOL_1ST;
10749 return 1;
10750}
10751
Willy Tarreau4a550602012-12-09 14:53:32 +010010752/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010753 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10754 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10755 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010756 * that in environments where IPv6 is insignificant, truncating the output to
10757 * 8 bytes would still work.
10758 */
10759static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010760smp_fetch_base32_src(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010761 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010762{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010763 struct chunk *temp;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020010764 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010765
10766 if (!cli_conn)
10767 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010768
Willy Tarreau192252e2015-04-04 01:47:55 +020010769 if (!smp_fetch_base32(px, sess, strm, opt, args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010770 return 0;
10771
Willy Tarreau47ca5452012-12-23 20:22:19 +010010772 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010773 *(unsigned int *)temp->str = htonl(smp->data.uint);
10774 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010775
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010776 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010777 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010778 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010779 temp->len += 4;
10780 break;
10781 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010782 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010783 temp->len += 16;
10784 break;
10785 default:
10786 return 0;
10787 }
10788
10789 smp->data.str = *temp;
10790 smp->type = SMP_T_BIN;
10791 return 1;
10792}
10793
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010794/* Extracts the query string, which comes after the question mark '?'. If no
10795 * question mark is found, nothing is returned. Otherwise it returns a sample
10796 * of type string carrying the whole query string.
10797 */
10798static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010799smp_fetch_query(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010800 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010801{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010802 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010803 char *ptr, *end;
10804
10805 CHECK_HTTP_MESSAGE_FIRST();
10806
Willy Tarreau15e91e12015-04-04 00:52:09 +020010807 txn = strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010808 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10809 end = ptr + txn->req.sl.rq.u_l;
10810
10811 /* look up the '?' */
10812 do {
10813 if (ptr == end)
10814 return 0;
10815 } while (*ptr++ != '?');
10816
10817 smp->type = SMP_T_STR;
10818 smp->data.str.str = ptr;
10819 smp->data.str.len = end - ptr;
10820 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10821 return 1;
10822}
10823
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010824static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010825smp_fetch_proto_http(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010826 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010827{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010828 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10829 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10830 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010831
Willy Tarreau24e32d82012-04-23 23:55:44 +020010832 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010833
Willy Tarreauf853c462012-04-23 18:53:56 +020010834 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010835 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010836 return 1;
10837}
10838
Willy Tarreau7f18e522010-10-22 20:04:13 +020010839/* return a valid test if the current request is the first one on the connection */
10840static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010841smp_fetch_http_first_req(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010842 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010843{
Willy Tarreauf853c462012-04-23 18:53:56 +020010844 smp->type = SMP_T_BOOL;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010845 smp->data.uint = !(strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010846 return 1;
10847}
10848
Willy Tarreau34db1082012-04-19 17:16:54 +020010849/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010850static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010851smp_fetch_http_auth(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010852 const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010853{
10854
Willy Tarreau24e32d82012-04-23 23:55:44 +020010855 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010856 return 0;
10857
Willy Tarreauc0239e02012-04-16 14:42:55 +020010858 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010859
Willy Tarreau15e91e12015-04-04 00:52:09 +020010860 if (!get_http_auth(strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010861 return 0;
10862
Willy Tarreauf853c462012-04-23 18:53:56 +020010863 smp->type = SMP_T_BOOL;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010864 smp->data.uint = check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010865 return 1;
10866}
Willy Tarreau8797c062007-05-07 00:55:35 +020010867
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010868/* Accepts exactly 1 argument of type userlist */
10869static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010870smp_fetch_http_auth_grp(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010871 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010872{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010873 if (!args || args->type != ARGT_USR)
10874 return 0;
10875
10876 CHECK_HTTP_MESSAGE_FIRST();
10877
Willy Tarreau15e91e12015-04-04 00:52:09 +020010878 if (!get_http_auth(strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010879 return 0;
10880
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010881 /* if the user does not belong to the userlist or has a wrong password,
10882 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010883 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010884 */
Willy Tarreau15e91e12015-04-04 00:52:09 +020010885 if (!check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010886 return 0;
10887
10888 /* pat_match_auth() will need the user list */
10889 smp->ctx.a[0] = args->data.usr;
10890
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010891 smp->type = SMP_T_STR;
10892 smp->flags = SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010893 smp->data.str.str = strm->txn->auth.user;
10894 smp->data.str.len = strlen(strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010895
10896 return 1;
10897}
10898
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010899/* Try to find the next occurrence of a cookie name in a cookie header value.
10900 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10901 * the cookie value is returned into *value and *value_l, and the function
10902 * returns a pointer to the next pointer to search from if the value was found.
10903 * Otherwise if the cookie was not found, NULL is returned and neither value
10904 * nor value_l are touched. The input <hdr> string should first point to the
10905 * header's value, and the <hdr_end> pointer must point to the first character
10906 * not part of the value. <list> must be non-zero if value may represent a list
10907 * of values (cookie headers). This makes it faster to abort parsing when no
10908 * list is expected.
10909 */
10910static char *
10911extract_cookie_value(char *hdr, const char *hdr_end,
10912 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010913 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010914{
10915 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10916 char *next;
10917
10918 /* we search at least a cookie name followed by an equal, and more
10919 * generally something like this :
10920 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10921 */
10922 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10923 /* Iterate through all cookies on this line */
10924
10925 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10926 att_beg++;
10927
10928 /* find att_end : this is the first character after the last non
10929 * space before the equal. It may be equal to hdr_end.
10930 */
10931 equal = att_end = att_beg;
10932
10933 while (equal < hdr_end) {
10934 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10935 break;
10936 if (http_is_spht[(unsigned char)*equal++])
10937 continue;
10938 att_end = equal;
10939 }
10940
10941 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10942 * is between <att_beg> and <equal>, both may be identical.
10943 */
10944
10945 /* look for end of cookie if there is an equal sign */
10946 if (equal < hdr_end && *equal == '=') {
10947 /* look for the beginning of the value */
10948 val_beg = equal + 1;
10949 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10950 val_beg++;
10951
10952 /* find the end of the value, respecting quotes */
10953 next = find_cookie_value_end(val_beg, hdr_end);
10954
10955 /* make val_end point to the first white space or delimitor after the value */
10956 val_end = next;
10957 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10958 val_end--;
10959 } else {
10960 val_beg = val_end = next = equal;
10961 }
10962
10963 /* We have nothing to do with attributes beginning with '$'. However,
10964 * they will automatically be removed if a header before them is removed,
10965 * since they're supposed to be linked together.
10966 */
10967 if (*att_beg == '$')
10968 continue;
10969
10970 /* Ignore cookies with no equal sign */
10971 if (equal == next)
10972 continue;
10973
10974 /* Now we have the cookie name between att_beg and att_end, and
10975 * its value between val_beg and val_end.
10976 */
10977
10978 if (att_end - att_beg == cookie_name_l &&
10979 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10980 /* let's return this value and indicate where to go on from */
10981 *value = val_beg;
10982 *value_l = val_end - val_beg;
10983 return next + 1;
10984 }
10985
10986 /* Set-Cookie headers only have the name in the first attr=value part */
10987 if (!list)
10988 break;
10989 }
10990
10991 return NULL;
10992}
10993
William Lallemanda43ba4e2014-01-28 18:14:25 +010010994/* Fetch a captured HTTP request header. The index is the position of
10995 * the "capture" option in the configuration file
10996 */
10997static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010998smp_fetch_capture_header_req(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010999 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011000{
Willy Tarreaud0d8da92015-04-04 02:10:38 +020011001 struct proxy *fe = strm_fe(strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011002 int idx;
11003
11004 if (!args || args->type != ARGT_UINT)
11005 return 0;
11006
11007 idx = args->data.uint;
11008
Willy Tarreau15e91e12015-04-04 00:52:09 +020011009 if (idx > (fe->nb_req_cap - 1) || strm->req_cap == NULL || strm->req_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011010 return 0;
11011
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011012 smp->type = SMP_T_STR;
11013 smp->flags |= SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011014 smp->data.str.str = strm->req_cap[idx];
11015 smp->data.str.len = strlen(strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011016
11017 return 1;
11018}
11019
11020/* Fetch a captured HTTP response header. The index is the position of
11021 * the "capture" option in the configuration file
11022 */
11023static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011024smp_fetch_capture_header_res(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 Lallemanda43ba4e2014-01-28 18:14:25 +010011026{
Willy Tarreaud0d8da92015-04-04 02:10:38 +020011027 struct proxy *fe = strm_fe(strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011028 int idx;
11029
11030 if (!args || args->type != ARGT_UINT)
11031 return 0;
11032
11033 idx = args->data.uint;
11034
Willy Tarreau15e91e12015-04-04 00:52:09 +020011035 if (idx > (fe->nb_rsp_cap - 1) || strm->res_cap == NULL || strm->res_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011036 return 0;
11037
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011038 smp->type = SMP_T_STR;
11039 smp->flags |= SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011040 smp->data.str.str = strm->res_cap[idx];
11041 smp->data.str.len = strlen(strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011042
11043 return 1;
11044}
11045
William Lallemand65ad6e12014-01-31 15:08:02 +010011046/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11047static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011048smp_fetch_capture_req_method(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011049 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011050{
11051 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011052 struct http_txn *txn = strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011053 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011054
Willy Tarreau15e91e12015-04-04 00:52:09 +020011055 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011056 return 0;
11057
William Lallemand96a77852014-02-05 00:30:02 +010011058 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011059
William Lallemand96a77852014-02-05 00:30:02 +010011060 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11061 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011062
William Lallemand96a77852014-02-05 00:30:02 +010011063 temp = get_trash_chunk();
11064 temp->str = txn->uri;
11065 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011066 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011067 smp->type = SMP_T_STR;
11068 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011069
11070 return 1;
11071
11072}
11073
11074/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11075static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011076smp_fetch_capture_req_uri(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011077 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011078{
11079 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011080 struct http_txn *txn = strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011081 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011082
Willy Tarreau15e91e12015-04-04 00:52:09 +020011083 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011084 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011085
William Lallemand65ad6e12014-01-31 15:08:02 +010011086 ptr = txn->uri;
11087
11088 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11089 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011090
William Lallemand65ad6e12014-01-31 15:08:02 +010011091 if (!*ptr)
11092 return 0;
11093
11094 ptr++; /* skip the space */
11095
11096 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011097 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011098 if (!ptr)
11099 return 0;
11100 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11101 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011102
11103 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010011104 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011105 smp->type = SMP_T_STR;
11106 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011107
11108 return 1;
11109}
11110
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011111/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11112 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11113 */
11114static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011115smp_fetch_capture_req_ver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011116 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011117{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011118 struct http_txn *txn = strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011119
Willy Tarreau15e91e12015-04-04 00:52:09 +020011120 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011121 return 0;
11122
11123 if (txn->req.flags & HTTP_MSGF_VER_11)
11124 smp->data.str.str = "HTTP/1.1";
11125 else
11126 smp->data.str.str = "HTTP/1.0";
11127
11128 smp->data.str.len = 8;
11129 smp->type = SMP_T_STR;
11130 smp->flags = SMP_F_CONST;
11131 return 1;
11132
11133}
11134
11135/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11136 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11137 */
11138static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011139smp_fetch_capture_res_ver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011140 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011141{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011142 struct http_txn *txn = strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011143
Willy Tarreau15e91e12015-04-04 00:52:09 +020011144 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011145 return 0;
11146
11147 if (txn->rsp.flags & HTTP_MSGF_VER_11)
11148 smp->data.str.str = "HTTP/1.1";
11149 else
11150 smp->data.str.str = "HTTP/1.0";
11151
11152 smp->data.str.len = 8;
11153 smp->type = SMP_T_STR;
11154 smp->flags = SMP_F_CONST;
11155 return 1;
11156
11157}
11158
William Lallemand65ad6e12014-01-31 15:08:02 +010011159
Willy Tarreaue333ec92012-04-16 16:26:40 +020011160/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011161 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011162 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011163 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011164 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011165 * Accepts exactly 1 argument of type string. If the input options indicate
11166 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011167 * The returned sample is of type CSTR. Can be used to parse cookies in other
11168 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011169 */
Willy Tarreau192252e2015-04-04 01:47:55 +020011170int smp_fetch_cookie(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011171 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011172{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011173 struct http_txn *txn;
11174 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011175 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011176 const struct http_msg *msg;
11177 const char *hdr_name;
11178 int hdr_name_len;
11179 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011180 int occ = 0;
11181 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011182
Willy Tarreau24e32d82012-04-23 23:55:44 +020011183 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011184 return 0;
11185
Willy Tarreaua890d072013-04-02 12:01:06 +020011186 if (!ctx) {
11187 /* first call */
11188 ctx = &static_hdr_ctx;
11189 ctx->idx = 0;
11190 smp->ctx.a[2] = ctx;
11191 }
11192
Willy Tarreaue333ec92012-04-16 16:26:40 +020011193 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011194
Willy Tarreau15e91e12015-04-04 00:52:09 +020011195 txn = strm->txn;
11196 idx = &strm->txn->hdr_idx;
11197
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011198 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011199 msg = &txn->req;
11200 hdr_name = "Cookie";
11201 hdr_name_len = 6;
11202 } else {
11203 msg = &txn->rsp;
11204 hdr_name = "Set-Cookie";
11205 hdr_name_len = 10;
11206 }
11207
Willy Tarreau28376d62012-04-26 21:26:10 +020011208 if (!occ && !(opt & SMP_OPT_ITERATE))
11209 /* no explicit occurrence and single fetch => last cookie by default */
11210 occ = -1;
11211
11212 /* OK so basically here, either we want only one value and it's the
11213 * last one, or we want to iterate over all of them and we fetch the
11214 * next one.
11215 */
11216
Willy Tarreau9b28e032012-10-12 23:49:43 +020011217 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011218 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011219 /* search for the header from the beginning, we must first initialize
11220 * the search parameters.
11221 */
Willy Tarreau37406352012-04-23 16:16:37 +020011222 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011223 ctx->idx = 0;
11224 }
11225
Willy Tarreau28376d62012-04-26 21:26:10 +020011226 smp->flags |= SMP_F_VOL_HDR;
11227
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011228 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011229 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11230 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011231 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11232 goto out;
11233
Willy Tarreau24e32d82012-04-23 23:55:44 +020011234 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011235 continue;
11236
Willy Tarreau37406352012-04-23 16:16:37 +020011237 smp->ctx.a[0] = ctx->line + ctx->val;
11238 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011239 }
11240
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011241 smp->type = SMP_T_STR;
11242 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011243 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011244 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011245 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011246 &smp->data.str.str,
11247 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011248 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011249 found = 1;
11250 if (occ >= 0) {
11251 /* one value was returned into smp->data.str.{str,len} */
11252 smp->flags |= SMP_F_NOT_LAST;
11253 return 1;
11254 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011255 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011256 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011257 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011258 /* all cookie headers and values were scanned. If we're looking for the
11259 * last occurrence, we may return it now.
11260 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011261 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011262 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011263 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011264}
11265
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011266/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011267 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011268 * multiple cookies may be parsed on the same line. The returned sample is of
11269 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011270 */
11271static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011272smp_fetch_cookie_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011273 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011274{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011275 struct http_txn *txn;
11276 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011277 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011278 const struct http_msg *msg;
11279 const char *hdr_name;
11280 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011281 int cnt;
11282 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011283 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011284
Willy Tarreau24e32d82012-04-23 23:55:44 +020011285 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011286 return 0;
11287
Willy Tarreaue333ec92012-04-16 16:26:40 +020011288 CHECK_HTTP_MESSAGE_FIRST();
11289
Willy Tarreau15e91e12015-04-04 00:52:09 +020011290 txn = strm->txn;
11291 idx = &strm->txn->hdr_idx;
11292
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011293 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011294 msg = &txn->req;
11295 hdr_name = "Cookie";
11296 hdr_name_len = 6;
11297 } else {
11298 msg = &txn->rsp;
11299 hdr_name = "Set-Cookie";
11300 hdr_name_len = 10;
11301 }
11302
Willy Tarreau9b28e032012-10-12 23:49:43 +020011303 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011304 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011305 ctx.idx = 0;
11306 cnt = 0;
11307
11308 while (1) {
11309 /* Note: val_beg == NULL every time we need to fetch a new header */
11310 if (!val_beg) {
11311 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11312 break;
11313
Willy Tarreau24e32d82012-04-23 23:55:44 +020011314 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011315 continue;
11316
11317 val_beg = ctx.line + ctx.val;
11318 val_end = val_beg + ctx.vlen;
11319 }
11320
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011321 smp->type = SMP_T_STR;
11322 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011323 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011324 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011325 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011326 &smp->data.str.str,
11327 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011328 cnt++;
11329 }
11330 }
11331
Willy Tarreaub169eba2013-12-16 15:14:43 +010011332 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011333 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011334 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011335 return 1;
11336}
11337
Willy Tarreau51539362012-05-08 12:46:28 +020011338/* Fetch an cookie's integer value. The integer value is returned. It
11339 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11340 */
11341static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011342smp_fetch_cookie_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011343 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011344{
Willy Tarreau192252e2015-04-04 01:47:55 +020011345 int ret = smp_fetch_cookie(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011346
11347 if (ret > 0) {
11348 smp->type = SMP_T_UINT;
11349 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11350 }
11351
11352 return ret;
11353}
11354
Willy Tarreau8797c062007-05-07 00:55:35 +020011355/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011356/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011357/************************************************************************/
11358
David Cournapeau16023ee2010-12-23 20:55:41 +090011359/*
11360 * Given a path string and its length, find the position of beginning of the
11361 * query string. Returns NULL if no query string is found in the path.
11362 *
11363 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11364 *
11365 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11366 */
bedis4c75cca2012-10-05 08:38:24 +020011367static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011368{
11369 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011370
bedis4c75cca2012-10-05 08:38:24 +020011371 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011372 return p ? p + 1 : NULL;
11373}
11374
bedis4c75cca2012-10-05 08:38:24 +020011375static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011376{
bedis4c75cca2012-10-05 08:38:24 +020011377 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011378}
11379
11380/*
11381 * Given a url parameter, find the starting position of the first occurence,
11382 * or NULL if the parameter is not found.
11383 *
11384 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11385 * the function will return query_string+8.
11386 */
11387static char*
11388find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011389 char* url_param_name, size_t url_param_name_l,
11390 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011391{
11392 char *pos, *last;
11393
11394 pos = query_string;
11395 last = query_string + query_string_l - url_param_name_l - 1;
11396
11397 while (pos <= last) {
11398 if (pos[url_param_name_l] == '=') {
11399 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11400 return pos;
11401 pos += url_param_name_l + 1;
11402 }
bedis4c75cca2012-10-05 08:38:24 +020011403 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011404 pos++;
11405 pos++;
11406 }
11407 return NULL;
11408}
11409
11410/*
11411 * Given a url parameter name, returns its value and size into *value and
11412 * *value_l respectively, and returns non-zero. If the parameter is not found,
11413 * zero is returned and value/value_l are not touched.
11414 */
11415static int
11416find_url_param_value(char* path, size_t path_l,
11417 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011418 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011419{
11420 char *query_string, *qs_end;
11421 char *arg_start;
11422 char *value_start, *value_end;
11423
bedis4c75cca2012-10-05 08:38:24 +020011424 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011425 if (!query_string)
11426 return 0;
11427
11428 qs_end = path + path_l;
11429 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011430 url_param_name, url_param_name_l,
11431 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011432 if (!arg_start)
11433 return 0;
11434
11435 value_start = arg_start + url_param_name_l + 1;
11436 value_end = value_start;
11437
bedis4c75cca2012-10-05 08:38:24 +020011438 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011439 value_end++;
11440
11441 *value = value_start;
11442 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011443 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011444}
11445
11446static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011447smp_fetch_url_param(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011448 const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011449{
bedis4c75cca2012-10-05 08:38:24 +020011450 char delim = '?';
Willy Tarreau15e91e12015-04-04 00:52:09 +020011451 struct http_msg *msg;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011452
bedis4c75cca2012-10-05 08:38:24 +020011453 if (!args || args[0].type != ARGT_STR ||
11454 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011455 return 0;
11456
11457 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011458
Willy Tarreau15e91e12015-04-04 00:52:09 +020011459 msg = &strm->txn->req;
11460
bedis4c75cca2012-10-05 08:38:24 +020011461 if (args[1].type)
11462 delim = *args[1].data.str.str;
11463
Willy Tarreau9b28e032012-10-12 23:49:43 +020011464 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011465 args->data.str.str, args->data.str.len,
11466 &smp->data.str.str, &smp->data.str.len,
11467 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011468 return 0;
11469
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011470 smp->type = SMP_T_STR;
11471 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011472 return 1;
11473}
11474
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011475/* Return the signed integer value for the specified url parameter (see url_param
11476 * above).
11477 */
11478static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011479smp_fetch_url_param_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011480 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011481{
Willy Tarreau192252e2015-04-04 01:47:55 +020011482 int ret = smp_fetch_url_param(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011483
11484 if (ret > 0) {
11485 smp->type = SMP_T_UINT;
11486 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11487 }
11488
11489 return ret;
11490}
11491
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011492/* This produces a 32-bit hash of the concatenation of the first occurrence of
11493 * the Host header followed by the path component if it begins with a slash ('/').
11494 * This means that '*' will not be added, resulting in exactly the first Host
11495 * entry. If no Host header is found, then the path is used. The resulting value
11496 * is hashed using the url hash followed by a full avalanche hash and provides a
11497 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11498 * high-traffic sites without having to store whole paths.
11499 * this differs from the base32 functions in that it includes the url parameters
11500 * as well as the path
11501 */
11502static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011503smp_fetch_url32(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011504 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011505{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011506 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011507 struct hdr_ctx ctx;
11508 unsigned int hash = 0;
11509 char *ptr, *beg, *end;
11510 int len;
11511
11512 CHECK_HTTP_MESSAGE_FIRST();
11513
Willy Tarreau15e91e12015-04-04 00:52:09 +020011514 txn = strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011515 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011516 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011517 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11518 ptr = ctx.line + ctx.val;
11519 len = ctx.vlen;
11520 while (len--)
11521 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11522 }
11523
11524 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011525 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 +000011526 beg = http_get_path(txn);
11527 if (!beg)
11528 beg = end;
11529
11530 for (ptr = beg; ptr < end ; ptr++);
11531
11532 if (beg < ptr && *beg == '/') {
11533 while (beg < ptr)
11534 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11535 }
11536 hash = full_hash(hash);
11537
11538 smp->type = SMP_T_UINT;
11539 smp->data.uint = hash;
11540 smp->flags = SMP_F_VOL_1ST;
11541 return 1;
11542}
11543
11544/* This concatenates the source address with the 32-bit hash of the Host and
11545 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11546 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11547 * on the source address length. The URL hash is stored before the address so
11548 * that in environments where IPv6 is insignificant, truncating the output to
11549 * 8 bytes would still work.
11550 */
11551static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011552smp_fetch_url32_src(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011553 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011554{
11555 struct chunk *temp;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020011556 struct connection *cli_conn = objt_conn(sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011557
Willy Tarreau192252e2015-04-04 01:47:55 +020011558 if (!smp_fetch_url32(px, sess, strm, opt, args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011559 return 0;
11560
11561 temp = get_trash_chunk();
11562 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11563 temp->len += sizeof(smp->data.uint);
11564
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011565 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011566 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011567 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011568 temp->len += 4;
11569 break;
11570 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011571 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011572 temp->len += 16;
11573 break;
11574 default:
11575 return 0;
11576 }
11577
11578 smp->data.str = *temp;
11579 smp->type = SMP_T_BIN;
11580 return 1;
11581}
11582
Willy Tarreau185b5c42012-04-26 15:11:51 +020011583/* This function is used to validate the arguments passed to any "hdr" fetch
11584 * keyword. These keywords support an optional positive or negative occurrence
11585 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11586 * is assumed that the types are already the correct ones. Returns 0 on error,
11587 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11588 * error message in case of error, that the caller is responsible for freeing.
11589 * The initial location must either be freeable or NULL.
11590 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011591int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011592{
11593 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011594 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011595 return 0;
11596 }
11597 return 1;
11598}
11599
Willy Tarreau276fae92013-07-25 14:36:01 +020011600/* takes an UINT value on input supposed to represent the time since EPOCH,
11601 * adds an optional offset found in args[0] and emits a string representing
11602 * the date in RFC-1123/5322 format.
11603 */
Willy Tarreau87b09662015-04-03 00:22:06 +020011604static int sample_conv_http_date(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011605 struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011606{
11607 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11608 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11609 struct chunk *temp;
11610 struct tm *tm;
11611 time_t curr_date = smp->data.uint;
11612
11613 /* add offset */
11614 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11615 curr_date += args[0].data.sint;
11616
11617 tm = gmtime(&curr_date);
11618
11619 temp = get_trash_chunk();
11620 temp->len = snprintf(temp->str, temp->size - temp->len,
11621 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11622 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11623 tm->tm_hour, tm->tm_min, tm->tm_sec);
11624
11625 smp->data.str = *temp;
11626 smp->type = SMP_T_STR;
11627 return 1;
11628}
11629
Thierry FOURNIERad903512014-04-11 17:51:01 +020011630/* Match language range with language tag. RFC2616 14.4:
11631 *
11632 * A language-range matches a language-tag if it exactly equals
11633 * the tag, or if it exactly equals a prefix of the tag such
11634 * that the first tag character following the prefix is "-".
11635 *
11636 * Return 1 if the strings match, else return 0.
11637 */
11638static inline int language_range_match(const char *range, int range_len,
11639 const char *tag, int tag_len)
11640{
11641 const char *end = range + range_len;
11642 const char *tend = tag + tag_len;
11643 while (range < end) {
11644 if (*range == '-' && tag == tend)
11645 return 1;
11646 if (*range != *tag || tag == tend)
11647 return 0;
11648 range++;
11649 tag++;
11650 }
11651 /* Return true only if the last char of the tag is matched. */
11652 return tag == tend;
11653}
11654
11655/* Arguments: The list of expected value, the number of parts returned and the separator */
Willy Tarreau87b09662015-04-03 00:22:06 +020011656static int sample_conv_q_prefered(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011657 struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011658{
11659 const char *al = smp->data.str.str;
11660 const char *end = al + smp->data.str.len;
11661 const char *token;
11662 int toklen;
11663 int qvalue;
11664 const char *str;
11665 const char *w;
11666 int best_q = 0;
11667
11668 /* Set the constant to the sample, because the output of the
11669 * function will be peek in the constant configuration string.
11670 */
11671 smp->flags |= SMP_F_CONST;
11672 smp->data.str.size = 0;
11673 smp->data.str.str = "";
11674 smp->data.str.len = 0;
11675
11676 /* Parse the accept language */
11677 while (1) {
11678
11679 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011680 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011681 al++;
11682 if (al >= end)
11683 break;
11684
11685 /* Start of the fisrt word. */
11686 token = al;
11687
11688 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011689 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011690 al++;
11691 if (al == token)
11692 goto expect_comma;
11693
11694 /* Length of the token. */
11695 toklen = al - token;
11696 qvalue = 1000;
11697
11698 /* Check if the token exists in the list. If the token not exists,
11699 * jump to the next token.
11700 */
11701 str = args[0].data.str.str;
11702 w = str;
11703 while (1) {
11704 if (*str == ';' || *str == '\0') {
11705 if (language_range_match(token, toklen, w, str-w))
11706 goto look_for_q;
11707 if (*str == '\0')
11708 goto expect_comma;
11709 w = str + 1;
11710 }
11711 str++;
11712 }
11713 goto expect_comma;
11714
11715look_for_q:
11716
11717 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011718 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011719 al++;
11720 if (al >= end)
11721 goto process_value;
11722
11723 /* If ',' is found, process the result */
11724 if (*al == ',')
11725 goto process_value;
11726
11727 /* If the character is different from ';', look
11728 * for the end of the header part in best effort.
11729 */
11730 if (*al != ';')
11731 goto expect_comma;
11732
11733 /* Assumes that the char is ';', now expect "q=". */
11734 al++;
11735
11736 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011737 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011738 al++;
11739 if (al >= end)
11740 goto process_value;
11741
11742 /* Expect 'q'. If no 'q', continue in best effort */
11743 if (*al != 'q')
11744 goto process_value;
11745 al++;
11746
11747 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011748 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011749 al++;
11750 if (al >= end)
11751 goto process_value;
11752
11753 /* Expect '='. If no '=', continue in best effort */
11754 if (*al != '=')
11755 goto process_value;
11756 al++;
11757
11758 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011759 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011760 al++;
11761 if (al >= end)
11762 goto process_value;
11763
11764 /* Parse the q value. */
11765 qvalue = parse_qvalue(al, &al);
11766
11767process_value:
11768
11769 /* If the new q value is the best q value, then store the associated
11770 * language in the response. If qvalue is the biggest value (1000),
11771 * break the process.
11772 */
11773 if (qvalue > best_q) {
11774 smp->data.str.str = (char *)w;
11775 smp->data.str.len = str - w;
11776 if (qvalue >= 1000)
11777 break;
11778 best_q = qvalue;
11779 }
11780
11781expect_comma:
11782
11783 /* Expect comma or end. If the end is detected, quit the loop. */
11784 while (al < end && *al != ',')
11785 al++;
11786 if (al >= end)
11787 break;
11788
11789 /* Comma is found, jump it and restart the analyzer. */
11790 al++;
11791 }
11792
11793 /* Set default value if required. */
11794 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11795 smp->data.str.str = args[1].data.str.str;
11796 smp->data.str.len = args[1].data.str.len;
11797 }
11798
11799 /* Return true only if a matching language was found. */
11800 return smp->data.str.len != 0;
11801}
11802
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011803/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011804 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011805 * the relevant part of the request line accordingly. Then it updates various
11806 * pointers to the next elements which were moved, and the total buffer length.
11807 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011808 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11809 * error, though this can be revisited when this code is finally exploited.
11810 *
11811 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11812 * query string and 3 to replace uri.
11813 *
11814 * In query string case, the mark question '?' must be set at the start of the
11815 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011816 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011817int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011818 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011819{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011820 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011821 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011822 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011823 int delta;
11824
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011825 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011826 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011827 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011828 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11829
11830 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011831 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011832 txn->req.sl.rq.m_l += delta;
11833 txn->req.sl.rq.u += delta;
11834 txn->req.sl.rq.v += delta;
11835 break;
11836
11837 case 1: // path
11838 cur_ptr = http_get_path(txn);
11839 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011840 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011841
11842 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011843 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 +010011844 cur_end++;
11845
11846 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011847 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011848 txn->req.sl.rq.u_l += delta;
11849 txn->req.sl.rq.v += delta;
11850 break;
11851
11852 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011853 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011854 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011855 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11856 while (cur_ptr < cur_end && *cur_ptr != '?')
11857 cur_ptr++;
11858
11859 /* skip the question mark or indicate that we must insert it
11860 * (but only if the format string is not empty then).
11861 */
11862 if (cur_ptr < cur_end)
11863 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011864 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011865 offset = 0;
11866
11867 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011868 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011869 txn->req.sl.rq.u_l += delta;
11870 txn->req.sl.rq.v += delta;
11871 break;
11872
11873 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011874 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011875 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11876
11877 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011878 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011879 txn->req.sl.rq.u_l += delta;
11880 txn->req.sl.rq.v += delta;
11881 break;
11882
11883 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011884 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011885 }
11886
11887 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011888 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011889 txn->req.sl.rq.l += delta;
11890 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011891 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011892 return 0;
11893}
11894
11895/* This function executes one of the set-{method,path,query,uri} actions. It
11896 * builds a string in the trash from the specified format string. It finds
11897 * the action to be performed in p[2], previously filled by function
11898 * parse_set_req_line(). The replacement action is excuted by the function
11899 * http_action_set_req_line_exec(). It always returns 1. If an error occurs
11900 * the action is canceled, but the rule processing continue.
11901 */
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011902int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011903{
11904 chunk_reset(&trash);
11905
11906 /* If we have to create a query string, prepare a '?'. */
11907 if (*(int *)&rule->arg.act.p[2] == 2)
11908 trash.str[trash.len++] = '?';
11909 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
11910
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011911 http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s);
Thierry FOURNIER01c30122015-03-14 14:14:47 +010011912 return 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011913}
11914
11915/* parse an http-request action among :
11916 * set-method
11917 * set-path
11918 * set-query
11919 * set-uri
11920 *
11921 * All of them accept a single argument of type string representing a log-format.
11922 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11923 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
11924 * It returns 0 on success, < 0 on error.
11925 */
11926int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
11927{
11928 int cur_arg = *orig_arg;
11929
11930 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
11931
11932 switch (args[0][4]) {
11933 case 'm' :
11934 *(int *)&rule->arg.act.p[2] = 0;
11935 rule->action_ptr = http_action_set_req_line;
11936 break;
11937 case 'p' :
11938 *(int *)&rule->arg.act.p[2] = 1;
11939 rule->action_ptr = http_action_set_req_line;
11940 break;
11941 case 'q' :
11942 *(int *)&rule->arg.act.p[2] = 2;
11943 rule->action_ptr = http_action_set_req_line;
11944 break;
11945 case 'u' :
11946 *(int *)&rule->arg.act.p[2] = 3;
11947 rule->action_ptr = http_action_set_req_line;
11948 break;
11949 default:
11950 memprintf(err, "internal error: unhandled action '%s'", args[0]);
11951 return -1;
11952 }
11953
11954 if (!*args[cur_arg] ||
11955 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11956 memprintf(err, "expects exactly 1 argument <format>");
11957 return -1;
11958 }
11959
11960 LIST_INIT((struct list *)&rule->arg.act.p[0]);
11961 proxy->conf.args.ctx = ARGC_HRQ;
11962 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
11963 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
11964 proxy->conf.args.file, proxy->conf.args.line);
11965
11966 (*orig_arg)++;
11967 return 0;
11968}
11969
William Lallemand73025dd2014-04-24 14:38:37 +020011970/*
11971 * Return the struct http_req_action_kw associated to a keyword.
11972 */
11973struct http_req_action_kw *action_http_req_custom(const char *kw)
11974{
11975 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11976 struct http_req_action_kw_list *kw_list;
11977 int i;
11978
11979 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11980 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11981 if (!strcmp(kw, kw_list->kw[i].kw))
11982 return &kw_list->kw[i];
11983 }
11984 }
11985 }
11986 return NULL;
11987}
11988
11989/*
11990 * Return the struct http_res_action_kw associated to a keyword.
11991 */
11992struct http_res_action_kw *action_http_res_custom(const char *kw)
11993{
11994 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11995 struct http_res_action_kw_list *kw_list;
11996 int i;
11997
11998 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11999 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
12000 if (!strcmp(kw, kw_list->kw[i].kw))
12001 return &kw_list->kw[i];
12002 }
12003 }
12004 }
12005 return NULL;
12006}
12007
Willy Tarreau4a568972010-05-12 08:08:50 +020012008/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012009/* All supported ACL keywords must be declared here. */
12010/************************************************************************/
12011
12012/* Note: must not be declared <const> as its list will be overwritten.
12013 * Please take care of keeping this list alphabetically sorted.
12014 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012015static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012016 { "base", "base", PAT_MATCH_STR },
12017 { "base_beg", "base", PAT_MATCH_BEG },
12018 { "base_dir", "base", PAT_MATCH_DIR },
12019 { "base_dom", "base", PAT_MATCH_DOM },
12020 { "base_end", "base", PAT_MATCH_END },
12021 { "base_len", "base", PAT_MATCH_LEN },
12022 { "base_reg", "base", PAT_MATCH_REG },
12023 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012024
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012025 { "cook", "req.cook", PAT_MATCH_STR },
12026 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12027 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12028 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12029 { "cook_end", "req.cook", PAT_MATCH_END },
12030 { "cook_len", "req.cook", PAT_MATCH_LEN },
12031 { "cook_reg", "req.cook", PAT_MATCH_REG },
12032 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012033
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012034 { "hdr", "req.hdr", PAT_MATCH_STR },
12035 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12036 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12037 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12038 { "hdr_end", "req.hdr", PAT_MATCH_END },
12039 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12040 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12041 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012042
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012043 /* these two declarations uses strings with list storage (in place
12044 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12045 * and delete functions are relative to the list management. The parse
12046 * and match method are related to the corresponding fetch methods. This
12047 * is very particular ACL declaration mode.
12048 */
12049 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12050 { "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 +020012051
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012052 { "path", "path", PAT_MATCH_STR },
12053 { "path_beg", "path", PAT_MATCH_BEG },
12054 { "path_dir", "path", PAT_MATCH_DIR },
12055 { "path_dom", "path", PAT_MATCH_DOM },
12056 { "path_end", "path", PAT_MATCH_END },
12057 { "path_len", "path", PAT_MATCH_LEN },
12058 { "path_reg", "path", PAT_MATCH_REG },
12059 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012060
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012061 { "req_ver", "req.ver", PAT_MATCH_STR },
12062 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012063
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012064 { "scook", "res.cook", PAT_MATCH_STR },
12065 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12066 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12067 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12068 { "scook_end", "res.cook", PAT_MATCH_END },
12069 { "scook_len", "res.cook", PAT_MATCH_LEN },
12070 { "scook_reg", "res.cook", PAT_MATCH_REG },
12071 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012072
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012073 { "shdr", "res.hdr", PAT_MATCH_STR },
12074 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12075 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12076 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12077 { "shdr_end", "res.hdr", PAT_MATCH_END },
12078 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12079 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12080 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012081
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012082 { "url", "url", PAT_MATCH_STR },
12083 { "url_beg", "url", PAT_MATCH_BEG },
12084 { "url_dir", "url", PAT_MATCH_DIR },
12085 { "url_dom", "url", PAT_MATCH_DOM },
12086 { "url_end", "url", PAT_MATCH_END },
12087 { "url_len", "url", PAT_MATCH_LEN },
12088 { "url_reg", "url", PAT_MATCH_REG },
12089 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012090
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012091 { "urlp", "urlp", PAT_MATCH_STR },
12092 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12093 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12094 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12095 { "urlp_end", "urlp", PAT_MATCH_END },
12096 { "urlp_len", "urlp", PAT_MATCH_LEN },
12097 { "urlp_reg", "urlp", PAT_MATCH_REG },
12098 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012099
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012100 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012101}};
12102
12103/************************************************************************/
12104/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012105/************************************************************************/
12106/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012107static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012108 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012109 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12110 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12111
Willy Tarreau87b09662015-04-03 00:22:06 +020012112 /* capture are allocated and are permanent in the stream */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012113 { "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 +020012114
12115 /* retrieve these captures from the HTTP logs */
12116 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12117 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12118 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12119
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012120 { "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 +020012121 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012122
Willy Tarreau409bcde2013-01-08 00:31:00 +010012123 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12124 * are only here to match the ACL's name, are request-only and are used
12125 * for ACL compatibility only.
12126 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012127 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12128 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012129 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12130 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12131
12132 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12133 * only here to match the ACL's name, are request-only and are used for
12134 * ACL compatibility only.
12135 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012136 { "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 +010012137 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12138 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
12139 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12140
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012141 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012142 { "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 +010012143 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012144 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012145 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012146 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012147
12148 /* HTTP protocol on the request path */
12149 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012150 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012151
12152 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012153 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12154 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012155
12156 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012157 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12158 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012159
Willy Tarreau18ed2562013-01-14 15:56:36 +010012160 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012161 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012162 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12163 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12164
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012165 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012166 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012167 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012168 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12169 { "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 +010012170 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012171 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12172
12173 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012174 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012175 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12176 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12177
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012178 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012179 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012180 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012181 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12182 { "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 +010012183 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012184 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12185
Willy Tarreau409bcde2013-01-08 00:31:00 +010012186 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012187 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012188 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12189 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012190 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012191
12192 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012193 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012194 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12195 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
12196 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12197
12198 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012199 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012200 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12201 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012202 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
12203 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012204 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12205 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012206 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12207 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012208}};
12209
Willy Tarreau8797c062007-05-07 00:55:35 +020012210
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012211/************************************************************************/
12212/* All supported converter keywords must be declared here. */
12213/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012214/* Note: must not be declared <const> as its list will be overwritten */
12215static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020012216 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
12217 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012218 { NULL, NULL, 0, 0, 0 },
12219}};
12220
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012221/************************************************************************/
12222/* All supported http-request action keywords must be declared here. */
12223/************************************************************************/
12224struct http_req_action_kw_list http_req_actions = {
12225 .scope = "http",
12226 .kw = {
12227 { "set-method", parse_set_req_line },
12228 { "set-path", parse_set_req_line },
12229 { "set-query", parse_set_req_line },
12230 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012231 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012232 }
12233};
12234
Willy Tarreau8797c062007-05-07 00:55:35 +020012235__attribute__((constructor))
12236static void __http_protocol_init(void)
12237{
12238 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012239 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012240 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012241 http_req_keywords_register(&http_req_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012242}
12243
12244
Willy Tarreau58f10d72006-12-04 02:26:12 +010012245/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012246 * Local variables:
12247 * c-indent-level: 8
12248 * c-basic-offset: 8
12249 * End:
12250 */