blob: a0c9e1ce608b6c1b9c3a0aa0cca0a594922044ff [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 Tarreau91852eb2015-05-01 13:26:00 +02002946 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2947 * exactly one digit "." one digit. This check may be disabled using
2948 * option accept-invalid-http-request.
2949 */
2950 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2951 if (msg->sl.rq.v_l != 8) {
2952 msg->err_pos = msg->sl.rq.v;
2953 goto return_bad_req;
2954 }
2955
2956 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2957 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2958 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2959 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2960 msg->err_pos = msg->sl.rq.v + 4;
2961 goto return_bad_req;
2962 }
2963 }
2964
Willy Tarreau5b154472009-12-21 20:11:07 +01002965 /* ... and check if the request is HTTP/1.1 or above */
2966 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002967 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2968 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2969 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002970 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002971
2972 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002973 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 +01002974
Willy Tarreau88d349d2010-01-25 12:15:43 +01002975 /* if the frontend has "option http-use-proxy-header", we'll check if
2976 * we have what looks like a proxied connection instead of a connection,
2977 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2978 * Note that this is *not* RFC-compliant, however browsers and proxies
2979 * happen to do that despite being non-standard :-(
2980 * We consider that a request not beginning with either '/' or '*' is
2981 * a proxied connection, which covers both "scheme://location" and
2982 * CONNECT ip:port.
2983 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002984 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002985 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002986 txn->flags |= TX_USE_PX_CONN;
2987
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002988 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002989 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002990
Willy Tarreau59234e92008-11-30 23:51:27 +01002991 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002992 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002993 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002994 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002995
Willy Tarreau557f1992015-05-01 10:05:17 +02002996 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2997 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002998 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002999 * The length of a message body is determined by one of the following
3000 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003001 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003002 * 1. Any response to a HEAD request and any response with a 1xx
3003 * (Informational), 204 (No Content), or 304 (Not Modified) status
3004 * code is always terminated by the first empty line after the
3005 * header fields, regardless of the header fields present in the
3006 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003007 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003008 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3009 * the connection will become a tunnel immediately after the empty
3010 * line that concludes the header fields. A client MUST ignore any
3011 * Content-Length or Transfer-Encoding header fields received in
3012 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003013 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003014 * 3. If a Transfer-Encoding header field is present and the chunked
3015 * transfer coding (Section 4.1) is the final encoding, the message
3016 * body length is determined by reading and decoding the chunked
3017 * data until the transfer coding indicates the data is complete.
3018 *
3019 * If a Transfer-Encoding header field is present in a response and
3020 * the chunked transfer coding is not the final encoding, the
3021 * message body length is determined by reading the connection until
3022 * it is closed by the server. If a Transfer-Encoding header field
3023 * is present in a request and the chunked transfer coding is not
3024 * the final encoding, the message body length cannot be determined
3025 * reliably; the server MUST respond with the 400 (Bad Request)
3026 * status code and then close the connection.
3027 *
3028 * If a message is received with both a Transfer-Encoding and a
3029 * Content-Length header field, the Transfer-Encoding overrides the
3030 * Content-Length. Such a message might indicate an attempt to
3031 * perform request smuggling (Section 9.5) or response splitting
3032 * (Section 9.4) and ought to be handled as an error. A sender MUST
3033 * remove the received Content-Length field prior to forwarding such
3034 * a message downstream.
3035 *
3036 * 4. If a message is received without Transfer-Encoding and with
3037 * either multiple Content-Length header fields having differing
3038 * field-values or a single Content-Length header field having an
3039 * invalid value, then the message framing is invalid and the
3040 * recipient MUST treat it as an unrecoverable error. If this is a
3041 * request message, the server MUST respond with a 400 (Bad Request)
3042 * status code and then close the connection. If this is a response
3043 * message received by a proxy, the proxy MUST close the connection
3044 * to the server, discard the received response, and send a 502 (Bad
3045 * Gateway) response to the client. If this is a response message
3046 * received by a user agent, the user agent MUST close the
3047 * connection to the server and discard the received response.
3048 *
3049 * 5. If a valid Content-Length header field is present without
3050 * Transfer-Encoding, its decimal value defines the expected message
3051 * body length in octets. If the sender closes the connection or
3052 * the recipient times out before the indicated number of octets are
3053 * received, the recipient MUST consider the message to be
3054 * incomplete and close the connection.
3055 *
3056 * 6. If this is a request message and none of the above are true, then
3057 * the message body length is zero (no message body is present).
3058 *
3059 * 7. Otherwise, this is a response message without a declared message
3060 * body length, so the message body length is determined by the
3061 * number of octets received prior to the server closing the
3062 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003063 */
3064
Willy Tarreau32b47f42009-10-18 20:55:02 +02003065 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003066 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003067 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003068 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003069 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3070 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003071 /* chunked not last, return badreq */
3072 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003073 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003074 }
3075
Willy Tarreau1c913912015-04-30 10:57:51 +02003076 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003077 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003078 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3079 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3080 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3081 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003082 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003083 signed long long cl;
3084
Willy Tarreauad14f752011-09-02 20:33:27 +02003085 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003086 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003087 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003088 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003089
Willy Tarreauad14f752011-09-02 20:33:27 +02003090 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003091 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003092 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003093 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003094
Willy Tarreauad14f752011-09-02 20:33:27 +02003095 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003096 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003097 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003098 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003099
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003100 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003101 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003102 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003103 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003104
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003105 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003106 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003107 }
3108
Willy Tarreau34dfc602015-05-01 10:09:49 +02003109 /* even bodyless requests have a known length */
3110 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003111
Willy Tarreau179085c2014-04-28 16:48:56 +02003112 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3113 * only change if both the request and the config reference something else.
3114 * Option httpclose by itself sets tunnel mode where headers are mangled.
3115 * However, if another mode is set, it will affect it (eg: server-close/
3116 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3117 * if FE and BE have the same settings (common). The method consists in
3118 * checking if options changed between the two calls (implying that either
3119 * one is non-null, or one of them is non-null and we are there for the first
3120 * time.
3121 */
3122 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003123 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003124 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003125
Willy Tarreaud787e662009-07-07 10:14:51 +02003126 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003127 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003128 req->analyse_exp = TICK_ETERNITY;
3129 return 1;
3130
3131 return_bad_req:
3132 /* We centralize bad requests processing here */
3133 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3134 /* we detected a parsing error. We want to archive this request
3135 * in the dedicated proxy area for later troubleshooting.
3136 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003137 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003138 }
3139
3140 txn->req.msg_state = HTTP_MSG_ERROR;
3141 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003142 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003143
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003144 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003145 if (sess->listener->counters)
3146 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003147
3148 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003149 if (!(s->flags & SF_ERR_MASK))
3150 s->flags |= SF_ERR_PRXCOND;
3151 if (!(s->flags & SF_FINST_MASK))
3152 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003153
3154 req->analysers = 0;
3155 req->analyse_exp = TICK_ETERNITY;
3156 return 0;
3157}
3158
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003159
Willy Tarreau347a35d2013-11-22 17:51:09 +01003160/* This function prepares an applet to handle the stats. It can deal with the
3161 * "100-continue" expectation, check that admin rules are met for POST requests,
3162 * and program a response message if something was unexpected. It cannot fail
3163 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003164 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003165 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003166 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003167 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003168int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003169{
3170 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003171 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003172 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003173 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003174 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003175 struct uri_auth *uri_auth = s->be->uri_auth;
3176 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003177 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003178
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003179 appctx = si_appctx(si);
3180 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3181 appctx->st1 = appctx->st2 = 0;
3182 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3183 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003184 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003185 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003186
3187 uri = msg->chn->buf->p + msg->sl.rq.u;
3188 lookup = uri + uri_auth->uri_len;
3189
3190 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3191 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003192 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003193 break;
3194 }
3195 }
3196
3197 if (uri_auth->refresh) {
3198 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3199 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003200 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003201 break;
3202 }
3203 }
3204 }
3205
3206 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3207 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003208 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003209 break;
3210 }
3211 }
3212
3213 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3214 if (memcmp(h, ";st=", 4) == 0) {
3215 int i;
3216 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003217 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003218 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3219 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003220 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003221 break;
3222 }
3223 }
3224 break;
3225 }
3226 }
3227
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003228 appctx->ctx.stats.scope_str = 0;
3229 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003230 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3231 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3232 int itx = 0;
3233 const char *h2;
3234 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3235 const char *err;
3236
3237 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3238 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003239 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003240 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3241 itx++;
3242 h++;
3243 }
3244
3245 if (itx > STAT_SCOPE_TXT_MAXLEN)
3246 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003247 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003248
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003249 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003250 memcpy(scope_txt, h2, itx);
3251 scope_txt[itx] = '\0';
3252 err = invalid_char(scope_txt);
3253 if (err) {
3254 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003255 appctx->ctx.stats.scope_str = 0;
3256 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003257 }
3258 break;
3259 }
3260 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003261
3262 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003263 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003264 int ret = 1;
3265
3266 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003267 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 +01003268 ret = acl_pass(ret);
3269 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3270 ret = !ret;
3271 }
3272
3273 if (ret) {
3274 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003275 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003276 break;
3277 }
3278 }
3279
3280 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003281 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003282 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003283 /* we'll need the request body, possibly after sending 100-continue */
3284 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003285 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003286 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003287 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003288 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3289 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003290 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003291 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003292 else {
3293 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003294 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003295 }
3296
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003297 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003298 return 1;
3299}
3300
Lukas Tribus67db8df2013-06-23 17:37:13 +02003301/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3302 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3303 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003304void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003305{
3306#ifdef IP_TOS
3307 if (from.ss_family == AF_INET)
3308 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3309#endif
3310#ifdef IPV6_TCLASS
3311 if (from.ss_family == AF_INET6) {
3312 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3313 /* v4-mapped addresses need IP_TOS */
3314 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3315 else
3316 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3317 }
3318#endif
3319}
3320
Willy Tarreau87b09662015-04-03 00:22:06 +02003321int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003322 const char* name, unsigned int name_len,
3323 const char *str, struct my_regex *re,
3324 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003325{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003326 struct hdr_ctx ctx;
3327 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003328 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003329 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3330 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003331 struct chunk *output = get_trash_chunk();
3332
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003333 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003334
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003335 /* Choose the header browsing function. */
3336 switch (action) {
3337 case HTTP_REQ_ACT_REPLACE_VAL:
3338 case HTTP_RES_ACT_REPLACE_VAL:
3339 http_find_hdr_func = http_find_header2;
3340 break;
3341 case HTTP_REQ_ACT_REPLACE_HDR:
3342 case HTTP_RES_ACT_REPLACE_HDR:
3343 http_find_hdr_func = http_find_full_header2;
3344 break;
3345 default: /* impossible */
3346 return -1;
3347 }
3348
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003349 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3350 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003351 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003352 char *val = ctx.line + ctx.val;
3353 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003354
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003355 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3356 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003357
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003358 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003359 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003360 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003361
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003362 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003363
3364 hdr->len += delta;
3365 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003366
3367 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003368 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003369 }
3370
3371 return 0;
3372}
3373
Willy Tarreau87b09662015-04-03 00:22:06 +02003374static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003375 const char* name, unsigned int name_len,
3376 struct list *fmt, struct my_regex *re,
3377 int action)
3378{
3379 struct chunk *replace = get_trash_chunk();
3380
3381 replace->len = build_logline(s, replace->str, replace->size, fmt);
3382 if (replace->len >= replace->size - 1)
3383 return -1;
3384
3385 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3386}
3387
Willy Tarreau87b09662015-04-03 00:22:06 +02003388/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003389 * transaction <txn>. Returns the verdict of the first rule that prevents
3390 * further processing of the request (auth, deny, ...), and defaults to
3391 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3392 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3393 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003394 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003395enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003396http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003397{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003398 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003399 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003400 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003401 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003402 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003403 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003404
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003405 /* If "the current_rule_list" match the executed rule list, we are in
3406 * resume condition. If a resume is needed it is always in the action
3407 * and never in the ACL or converters. In this case, we initialise the
3408 * current rule, and go to the action execution point.
3409 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003410 if (s->current_rule) {
3411 rule = s->current_rule;
3412 s->current_rule = NULL;
3413 if (s->current_rule_list == rules)
3414 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003415 }
3416 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003417
Willy Tarreauff011f22011-01-06 17:51:27 +01003418 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003419 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003420 continue;
3421
Willy Tarreau96257ec2012-12-27 10:46:37 +01003422 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003423 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003424 int ret;
3425
Willy Tarreau192252e2015-04-04 01:47:55 +02003426 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003427 ret = acl_pass(ret);
3428
Willy Tarreauff011f22011-01-06 17:51:27 +01003429 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003430 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003431
3432 if (!ret) /* condition not matched */
3433 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003434 }
3435
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003436resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003437 switch (rule->action) {
3438 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003439 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003440
3441 case HTTP_REQ_ACT_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003442 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003443 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003444
Willy Tarreauccbcc372012-12-27 12:37:57 +01003445 case HTTP_REQ_ACT_TARPIT:
3446 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003447 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003448 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003449
Willy Tarreau96257ec2012-12-27 10:46:37 +01003450 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003451 /* Auth might be performed on regular http-req rules as well as on stats */
3452 auth_realm = rule->arg.auth.realm;
3453 if (!auth_realm) {
3454 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3455 auth_realm = STATS_DEFAULT_REALM;
3456 else
3457 auth_realm = px->id;
3458 }
3459 /* send 401/407 depending on whether we use a proxy or not. We still
3460 * count one error, because normal browsing won't significantly
3461 * increase the counter but brute force attempts will.
3462 */
3463 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3464 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3465 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003466 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003467 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003468
Willy Tarreau81499eb2012-12-27 12:19:02 +01003469 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003470 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3471 return HTTP_RULE_RES_BADREQ;
3472 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003473
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003474 case HTTP_REQ_ACT_SET_NICE:
3475 s->task->nice = rule->arg.nice;
3476 break;
3477
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003478 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003479 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003480 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003481 break;
3482
Willy Tarreau51347ed2013-06-11 19:34:13 +02003483 case HTTP_REQ_ACT_SET_MARK:
3484#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003485 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003486 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003487#endif
3488 break;
3489
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003490 case HTTP_REQ_ACT_SET_LOGL:
3491 s->logs.level = rule->arg.loglevel;
3492 break;
3493
Sasha Pachev218f0642014-06-16 12:05:59 -06003494 case HTTP_REQ_ACT_REPLACE_HDR:
3495 case HTTP_REQ_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003496 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3497 rule->arg.hdr_add.name_len,
3498 &rule->arg.hdr_add.fmt,
3499 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003500 return HTTP_RULE_RES_BADREQ;
3501 break;
3502
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003503 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003504 ctx.idx = 0;
3505 /* remove all occurrences of the header */
3506 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3507 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3508 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003509 }
Willy Tarreau85603282015-01-21 20:39:27 +01003510 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003511
Willy Tarreau85603282015-01-21 20:39:27 +01003512 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003513 case HTTP_REQ_ACT_ADD_HDR:
3514 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3515 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3516 trash.len = rule->arg.hdr_add.name_len;
3517 trash.str[trash.len++] = ':';
3518 trash.str[trash.len++] = ' ';
3519 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 +01003520
3521 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3522 /* remove all occurrences of the header */
3523 ctx.idx = 0;
3524 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3525 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3526 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3527 }
3528 }
3529
Willy Tarreau96257ec2012-12-27 10:46:37 +01003530 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3531 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003532
3533 case HTTP_REQ_ACT_DEL_ACL:
3534 case HTTP_REQ_ACT_DEL_MAP: {
3535 struct pat_ref *ref;
3536 char *key;
3537 int len;
3538
3539 /* collect reference */
3540 ref = pat_ref_lookup(rule->arg.map.ref);
3541 if (!ref)
3542 continue;
3543
3544 /* collect key */
3545 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3546 key = trash.str;
3547 key[len] = '\0';
3548
3549 /* perform update */
3550 /* returned code: 1=ok, 0=ko */
3551 pat_ref_delete(ref, key);
3552
3553 break;
3554 }
3555
3556 case HTTP_REQ_ACT_ADD_ACL: {
3557 struct pat_ref *ref;
3558 char *key;
3559 struct chunk *trash_key;
3560 int len;
3561
3562 trash_key = get_trash_chunk();
3563
3564 /* collect reference */
3565 ref = pat_ref_lookup(rule->arg.map.ref);
3566 if (!ref)
3567 continue;
3568
3569 /* collect key */
3570 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3571 key = trash_key->str;
3572 key[len] = '\0';
3573
3574 /* perform update */
3575 /* add entry only if it does not already exist */
3576 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003577 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003578
3579 break;
3580 }
3581
3582 case HTTP_REQ_ACT_SET_MAP: {
3583 struct pat_ref *ref;
3584 char *key, *value;
3585 struct chunk *trash_key, *trash_value;
3586 int len;
3587
3588 trash_key = get_trash_chunk();
3589 trash_value = get_trash_chunk();
3590
3591 /* collect reference */
3592 ref = pat_ref_lookup(rule->arg.map.ref);
3593 if (!ref)
3594 continue;
3595
3596 /* collect key */
3597 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3598 key = trash_key->str;
3599 key[len] = '\0';
3600
3601 /* collect value */
3602 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3603 value = trash_value->str;
3604 value[len] = '\0';
3605
3606 /* perform update */
3607 if (pat_ref_find_elt(ref, key) != NULL)
3608 /* update entry if it exists */
3609 pat_ref_set(ref, key, value, NULL);
3610 else
3611 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003612 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003613
3614 break;
3615 }
William Lallemand73025dd2014-04-24 14:38:37 +02003616
3617 case HTTP_REQ_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003618 if (!rule->action_ptr(rule, px, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003619 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003620 return HTTP_RULE_RES_YIELD;
3621 }
William Lallemand73025dd2014-04-24 14:38:37 +02003622 break;
3623
3624 case HTTP_REQ_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003625 rule->action_ptr(rule, px, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003626 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003627
3628 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3629 /* Note: only the first valid tracking parameter of each
3630 * applies.
3631 */
3632
3633 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3634 struct stktable *t;
3635 struct stksess *ts;
3636 struct stktable_key *key;
3637 void *ptr;
3638
3639 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau192252e2015-04-04 01:47:55 +02003640 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 +02003641
3642 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003643 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003644
3645 /* let's count a new HTTP request as it's the first time we do it */
3646 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3647 if (ptr)
3648 stktable_data_cast(ptr, http_req_cnt)++;
3649
3650 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3651 if (ptr)
3652 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3653 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3654
3655 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003656 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003657 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3658 }
3659 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003660 }
3661 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003662
3663 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003664 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003665}
3666
Willy Tarreau71241ab2012-12-27 11:30:54 +01003667
Willy Tarreau87b09662015-04-03 00:22:06 +02003668/* Executes the http-response rules <rules> for stream <s>, proxy <px> and
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003669 * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD
3670 * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the
3671 * evaluation of next rule list. If *STOP is returned, the process must stop
3672 * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny
3673 * rule. If *YIELD is returned, the czller must call again the function with
3674 * the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003675 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003676static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003677http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003678{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003679 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003680 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003681 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003682 struct http_res_rule *rule;
3683 struct hdr_ctx ctx;
3684
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003685 /* If "the current_rule_list" match the executed rule list, we are in
3686 * resume condition. If a resume is needed it is always in the action
3687 * and never in the ACL or converters. In this case, we initialise the
3688 * current rule, and go to the action execution point.
3689 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003690 if (s->current_rule) {
3691 rule = s->current_rule;
3692 s->current_rule = NULL;
3693 if (s->current_rule_list == rules)
3694 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003695 }
3696 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003697
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003698 list_for_each_entry(rule, rules, list) {
3699 if (rule->action >= HTTP_RES_ACT_MAX)
3700 continue;
3701
3702 /* check optional condition */
3703 if (rule->cond) {
3704 int ret;
3705
Willy Tarreau192252e2015-04-04 01:47:55 +02003706 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003707 ret = acl_pass(ret);
3708
3709 if (rule->cond->pol == ACL_COND_UNLESS)
3710 ret = !ret;
3711
3712 if (!ret) /* condition not matched */
3713 continue;
3714 }
3715
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003716resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003717 switch (rule->action) {
3718 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003719 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003720
3721 case HTTP_RES_ACT_DENY:
3722 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003723 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003724
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003725 case HTTP_RES_ACT_SET_NICE:
3726 s->task->nice = rule->arg.nice;
3727 break;
3728
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003729 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003730 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003731 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003732 break;
3733
Willy Tarreau51347ed2013-06-11 19:34:13 +02003734 case HTTP_RES_ACT_SET_MARK:
3735#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003736 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003737 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003738#endif
3739 break;
3740
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003741 case HTTP_RES_ACT_SET_LOGL:
3742 s->logs.level = rule->arg.loglevel;
3743 break;
3744
Sasha Pachev218f0642014-06-16 12:05:59 -06003745 case HTTP_RES_ACT_REPLACE_HDR:
3746 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003747 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3748 rule->arg.hdr_add.name_len,
3749 &rule->arg.hdr_add.fmt,
3750 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003751 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003752 break;
3753
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003754 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003755 ctx.idx = 0;
3756 /* remove all occurrences of the header */
3757 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3758 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3759 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3760 }
Willy Tarreau85603282015-01-21 20:39:27 +01003761 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003762
Willy Tarreau85603282015-01-21 20:39:27 +01003763 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003764 case HTTP_RES_ACT_ADD_HDR:
3765 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3766 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3767 trash.len = rule->arg.hdr_add.name_len;
3768 trash.str[trash.len++] = ':';
3769 trash.str[trash.len++] = ' ';
3770 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 +01003771
3772 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3773 /* remove all occurrences of the header */
3774 ctx.idx = 0;
3775 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3776 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3777 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3778 }
3779 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003780 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3781 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003782
3783 case HTTP_RES_ACT_DEL_ACL:
3784 case HTTP_RES_ACT_DEL_MAP: {
3785 struct pat_ref *ref;
3786 char *key;
3787 int len;
3788
3789 /* collect reference */
3790 ref = pat_ref_lookup(rule->arg.map.ref);
3791 if (!ref)
3792 continue;
3793
3794 /* collect key */
3795 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3796 key = trash.str;
3797 key[len] = '\0';
3798
3799 /* perform update */
3800 /* returned code: 1=ok, 0=ko */
3801 pat_ref_delete(ref, key);
3802
3803 break;
3804 }
3805
3806 case HTTP_RES_ACT_ADD_ACL: {
3807 struct pat_ref *ref;
3808 char *key;
3809 struct chunk *trash_key;
3810 int len;
3811
3812 trash_key = get_trash_chunk();
3813
3814 /* collect reference */
3815 ref = pat_ref_lookup(rule->arg.map.ref);
3816 if (!ref)
3817 continue;
3818
3819 /* collect key */
3820 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3821 key = trash_key->str;
3822 key[len] = '\0';
3823
3824 /* perform update */
3825 /* check if the entry already exists */
3826 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003827 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003828
3829 break;
3830 }
3831
3832 case HTTP_RES_ACT_SET_MAP: {
3833 struct pat_ref *ref;
3834 char *key, *value;
3835 struct chunk *trash_key, *trash_value;
3836 int len;
3837
3838 trash_key = get_trash_chunk();
3839 trash_value = get_trash_chunk();
3840
3841 /* collect reference */
3842 ref = pat_ref_lookup(rule->arg.map.ref);
3843 if (!ref)
3844 continue;
3845
3846 /* collect key */
3847 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3848 key = trash_key->str;
3849 key[len] = '\0';
3850
3851 /* collect value */
3852 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3853 value = trash_value->str;
3854 value[len] = '\0';
3855
3856 /* perform update */
3857 if (pat_ref_find_elt(ref, key) != NULL)
3858 /* update entry if it exists */
3859 pat_ref_set(ref, key, value, NULL);
3860 else
3861 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003862 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003863
3864 break;
3865 }
William Lallemand73025dd2014-04-24 14:38:37 +02003866
3867 case HTTP_RES_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003868 if (!rule->action_ptr(rule, px, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003869 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003870 return HTTP_RULE_RES_YIELD;
3871 }
William Lallemand73025dd2014-04-24 14:38:37 +02003872 break;
3873
3874 case HTTP_RES_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003875 rule->action_ptr(rule, px, s);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003876 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003877 }
3878 }
3879
3880 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003881 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003882}
3883
3884
Willy Tarreau71241ab2012-12-27 11:30:54 +01003885/* Perform an HTTP redirect based on the information in <rule>. The function
3886 * returns non-zero on success, or zero in case of a, irrecoverable error such
3887 * as too large a request to build a valid response.
3888 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003889static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003890{
3891 struct http_msg *msg = &txn->req;
3892 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003893 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003894
3895 /* build redirect message */
3896 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003897 case 308:
3898 msg_fmt = HTTP_308;
3899 break;
3900 case 307:
3901 msg_fmt = HTTP_307;
3902 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003903 case 303:
3904 msg_fmt = HTTP_303;
3905 break;
3906 case 301:
3907 msg_fmt = HTTP_301;
3908 break;
3909 case 302:
3910 default:
3911 msg_fmt = HTTP_302;
3912 break;
3913 }
3914
3915 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3916 return 0;
3917
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003918 location = trash.str + trash.len;
3919
Willy Tarreau71241ab2012-12-27 11:30:54 +01003920 switch(rule->type) {
3921 case REDIRECT_TYPE_SCHEME: {
3922 const char *path;
3923 const char *host;
3924 struct hdr_ctx ctx;
3925 int pathlen;
3926 int hostlen;
3927
3928 host = "";
3929 hostlen = 0;
3930 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003931 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003932 host = ctx.line + ctx.val;
3933 hostlen = ctx.vlen;
3934 }
3935
3936 path = http_get_path(txn);
3937 /* build message using path */
3938 if (path) {
3939 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3940 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3941 int qs = 0;
3942 while (qs < pathlen) {
3943 if (path[qs] == '?') {
3944 pathlen = qs;
3945 break;
3946 }
3947 qs++;
3948 }
3949 }
3950 } else {
3951 path = "/";
3952 pathlen = 1;
3953 }
3954
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003955 if (rule->rdr_str) { /* this is an old "redirect" rule */
3956 /* check if we can add scheme + "://" + host + path */
3957 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3958 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003959
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003960 /* add scheme */
3961 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3962 trash.len += rule->rdr_len;
3963 }
3964 else {
3965 /* add scheme with executing log format */
3966 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003967
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003968 /* check if we can add scheme + "://" + host + path */
3969 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3970 return 0;
3971 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003972 /* add "://" */
3973 memcpy(trash.str + trash.len, "://", 3);
3974 trash.len += 3;
3975
3976 /* add host */
3977 memcpy(trash.str + trash.len, host, hostlen);
3978 trash.len += hostlen;
3979
3980 /* add path */
3981 memcpy(trash.str + trash.len, path, pathlen);
3982 trash.len += pathlen;
3983
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003984 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003985 if (trash.len && trash.str[trash.len - 1] != '/' &&
3986 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3987 if (trash.len > trash.size - 5)
3988 return 0;
3989 trash.str[trash.len] = '/';
3990 trash.len++;
3991 }
3992
3993 break;
3994 }
3995 case REDIRECT_TYPE_PREFIX: {
3996 const char *path;
3997 int pathlen;
3998
3999 path = http_get_path(txn);
4000 /* build message using path */
4001 if (path) {
4002 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
4003 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4004 int qs = 0;
4005 while (qs < pathlen) {
4006 if (path[qs] == '?') {
4007 pathlen = qs;
4008 break;
4009 }
4010 qs++;
4011 }
4012 }
4013 } else {
4014 path = "/";
4015 pathlen = 1;
4016 }
4017
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004018 if (rule->rdr_str) { /* this is an old "redirect" rule */
4019 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4020 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004021
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004022 /* add prefix. Note that if prefix == "/", we don't want to
4023 * add anything, otherwise it makes it hard for the user to
4024 * configure a self-redirection.
4025 */
4026 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4027 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4028 trash.len += rule->rdr_len;
4029 }
4030 }
4031 else {
4032 /* add prefix with executing log format */
4033 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4034
4035 /* Check length */
4036 if (trash.len + pathlen > trash.size - 4)
4037 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004038 }
4039
4040 /* add path */
4041 memcpy(trash.str + trash.len, path, pathlen);
4042 trash.len += pathlen;
4043
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004044 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004045 if (trash.len && trash.str[trash.len - 1] != '/' &&
4046 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4047 if (trash.len > trash.size - 5)
4048 return 0;
4049 trash.str[trash.len] = '/';
4050 trash.len++;
4051 }
4052
4053 break;
4054 }
4055 case REDIRECT_TYPE_LOCATION:
4056 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004057 if (rule->rdr_str) { /* this is an old "redirect" rule */
4058 if (trash.len + rule->rdr_len > trash.size - 4)
4059 return 0;
4060
4061 /* add location */
4062 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4063 trash.len += rule->rdr_len;
4064 }
4065 else {
4066 /* add location with executing log format */
4067 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004068
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004069 /* Check left length */
4070 if (trash.len > trash.size - 4)
4071 return 0;
4072 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004073 break;
4074 }
4075
4076 if (rule->cookie_len) {
4077 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4078 trash.len += 14;
4079 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4080 trash.len += rule->cookie_len;
4081 memcpy(trash.str + trash.len, "\r\n", 2);
4082 trash.len += 2;
4083 }
4084
4085 /* add end of headers and the keep-alive/close status.
4086 * We may choose to set keep-alive if the Location begins
4087 * with a slash, because the client will come back to the
4088 * same server.
4089 */
4090 txn->status = rule->code;
4091 /* let's log the request time */
4092 s->logs.tv_request = now;
4093
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004094 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004095 (msg->flags & HTTP_MSGF_XFER_LEN) &&
4096 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
4097 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4098 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4099 /* keep-alive possible */
4100 if (!(msg->flags & HTTP_MSGF_VER_11)) {
4101 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4102 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4103 trash.len += 30;
4104 } else {
4105 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4106 trash.len += 24;
4107 }
4108 }
4109 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4110 trash.len += 4;
4111 bo_inject(txn->rsp.chn, trash.str, trash.len);
4112 /* "eat" the request */
4113 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02004114 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004115 msg->sov = 0;
4116 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004117 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004118 txn->req.msg_state = HTTP_MSG_CLOSED;
4119 txn->rsp.msg_state = HTTP_MSG_DONE;
4120 } else {
4121 /* keep-alive not possible */
4122 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4123 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4124 trash.len += 29;
4125 } else {
4126 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4127 trash.len += 23;
4128 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004129 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004130 txn->req.chn->analysers = 0;
4131 }
4132
Willy Tarreaue7dff022015-04-03 01:14:29 +02004133 if (!(s->flags & SF_ERR_MASK))
4134 s->flags |= SF_ERR_LOCAL;
4135 if (!(s->flags & SF_FINST_MASK))
4136 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004137
4138 return 1;
4139}
4140
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004141/* This stream analyser runs all HTTP request processing which is common to
4142 * frontends and backends, which means blocking ACLs, filters, connection-close,
4143 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004144 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004145 * either needs more data or wants to immediately abort the request (eg: deny,
4146 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004147 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004148int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004149{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004150 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004151 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004152 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004153 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004154 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004155 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004156
Willy Tarreau655dce92009-11-08 13:10:58 +01004157 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004158 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004159 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004160 }
4161
Willy Tarreau87b09662015-04-03 00:22:06 +02004162 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 +02004163 now_ms, __FUNCTION__,
4164 s,
4165 req,
4166 req->rex, req->wex,
4167 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004168 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004169 req->analysers);
4170
Willy Tarreau65410832014-04-28 21:25:43 +02004171 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004172 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004173
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004174 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004175 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004176 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004177
Willy Tarreau0b748332014-04-29 00:13:29 +02004178 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004179 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4180 goto return_prx_yield;
4181
Willy Tarreau0b748332014-04-29 00:13:29 +02004182 case HTTP_RULE_RES_CONT:
4183 case HTTP_RULE_RES_STOP: /* nothing to do */
4184 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004185
Willy Tarreau0b748332014-04-29 00:13:29 +02004186 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4187 if (txn->flags & TX_CLTARPIT)
4188 goto tarpit;
4189 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004190
Willy Tarreau0b748332014-04-29 00:13:29 +02004191 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4192 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004193
Willy Tarreau0b748332014-04-29 00:13:29 +02004194 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004195 goto done;
4196
Willy Tarreau0b748332014-04-29 00:13:29 +02004197 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4198 goto return_bad_req;
4199 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004200 }
4201
Willy Tarreau52542592014-04-28 18:33:26 +02004202 /* OK at this stage, we know that the request was accepted according to
4203 * the http-request rules, we can check for the stats. Note that the
4204 * URI is detected *before* the req* rules in order not to be affected
4205 * by a possible reqrep, while they are processed *after* so that a
4206 * reqdeny can still block them. This clearly needs to change in 1.6!
4207 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004208 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004209 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004210 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004211 txn->status = 500;
4212 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004213 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004214
Willy Tarreaue7dff022015-04-03 01:14:29 +02004215 if (!(s->flags & SF_ERR_MASK))
4216 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004217 goto return_prx_cond;
4218 }
4219
4220 /* parse the whole stats request and extract the relevant information */
4221 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004222 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004223 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004224
Willy Tarreau0b748332014-04-29 00:13:29 +02004225 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4226 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004227
Willy Tarreau0b748332014-04-29 00:13:29 +02004228 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4229 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004230 }
4231
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004232 /* evaluate the req* rules except reqadd */
4233 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004234 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004235 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004236
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004237 if (txn->flags & TX_CLDENY)
4238 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004239
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004240 if (txn->flags & TX_CLTARPIT)
4241 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004242 }
Willy Tarreau06619262006-12-17 08:37:22 +01004243
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004244 /* add request headers from the rule sets in the same order */
4245 list_for_each_entry(wl, &px->req_add, list) {
4246 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004247 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004248 ret = acl_pass(ret);
4249 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4250 ret = !ret;
4251 if (!ret)
4252 continue;
4253 }
4254
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004255 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004256 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004257 }
4258
Willy Tarreau52542592014-04-28 18:33:26 +02004259
4260 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004261 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004262 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004263 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4264 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004265
Willy Tarreaue7dff022015-04-03 01:14:29 +02004266 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4267 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4268 if (!(s->flags & SF_FINST_MASK))
4269 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004270
Willy Tarreau70730dd2014-04-24 18:06:27 +02004271 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004272 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004273 select_compression_request_header(s, req->buf);
4274
4275 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004276 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004277 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004278 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004279
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004280 /* check whether we have some ACLs set to redirect this request */
4281 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004282 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004283 int ret;
4284
Willy Tarreau192252e2015-04-04 01:47:55 +02004285 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004286 ret = acl_pass(ret);
4287 if (rule->cond->pol == ACL_COND_UNLESS)
4288 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004289 if (!ret)
4290 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004291 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004292 if (!http_apply_redirect_rule(rule, s, txn))
4293 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004294 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004295 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004296
Willy Tarreau2be39392010-01-03 17:24:51 +01004297 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4298 * If this happens, then the data will not come immediately, so we must
4299 * send all what we have without waiting. Note that due to the small gain
4300 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004301 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004302 * itself once used.
4303 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004304 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004305
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004306 done: /* done with this analyser, continue with next ones that the calling
4307 * points will have set, if any.
4308 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004309 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004310 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4311 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004312 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004313
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004314 tarpit:
4315 /* When a connection is tarpitted, we use the tarpit timeout,
4316 * which may be the same as the connect timeout if unspecified.
4317 * If unset, then set it to zero because we really want it to
4318 * eventually expire. We build the tarpit as an analyser.
4319 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004320 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004321
4322 /* wipe the request out so that we can drop the connection early
4323 * if the client closes first.
4324 */
4325 channel_dont_connect(req);
4326 req->analysers = 0; /* remove switching rules etc... */
4327 req->analysers |= AN_REQ_HTTP_TARPIT;
4328 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4329 if (!req->analyse_exp)
4330 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004331 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004332 sess->fe->fe_counters.denied_req++;
4333 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004334 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004335 if (sess->listener->counters)
4336 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004337 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004338
4339 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004340 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004341 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004342 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004343 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004344 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004345 sess->fe->fe_counters.denied_req++;
4346 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004347 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004348 if (sess->listener->counters)
4349 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004350 goto return_prx_cond;
4351
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004352 return_bad_req:
4353 /* We centralize bad requests processing here */
4354 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4355 /* we detected a parsing error. We want to archive this request
4356 * in the dedicated proxy area for later troubleshooting.
4357 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004358 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004359 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004360
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004361 txn->req.msg_state = HTTP_MSG_ERROR;
4362 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004363 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004364
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004365 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004366 if (sess->listener->counters)
4367 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004368
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004369 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004370 if (!(s->flags & SF_ERR_MASK))
4371 s->flags |= SF_ERR_PRXCOND;
4372 if (!(s->flags & SF_FINST_MASK))
4373 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004374
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004375 req->analysers = 0;
4376 req->analyse_exp = TICK_ETERNITY;
4377 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004378
4379 return_prx_yield:
4380 channel_dont_connect(req);
4381 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004382}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004383
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004384/* This function performs all the processing enabled for the current request.
4385 * It returns 1 if the processing can continue on next analysers, or zero if it
4386 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004387 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004388 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004389int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004390{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004391 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004392 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004393 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004394 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004395
Willy Tarreau655dce92009-11-08 13:10:58 +01004396 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004397 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004398 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004399 return 0;
4400 }
4401
Willy Tarreau87b09662015-04-03 00:22:06 +02004402 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 +02004403 now_ms, __FUNCTION__,
4404 s,
4405 req,
4406 req->rex, req->wex,
4407 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004408 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004409 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004410
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004411 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004412 select_compression_request_header(s, req->buf);
4413
Willy Tarreau59234e92008-11-30 23:51:27 +01004414 /*
4415 * Right now, we know that we have processed the entire headers
4416 * and that unwanted requests have been filtered out. We can do
4417 * whatever we want with the remaining request. Also, now we
4418 * may have separate values for ->fe, ->be.
4419 */
Willy Tarreau06619262006-12-17 08:37:22 +01004420
Willy Tarreau59234e92008-11-30 23:51:27 +01004421 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004422 * If HTTP PROXY is set we simply get remote server address parsing
4423 * incoming request. Note that this requires that a connection is
4424 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004425 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004426 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004427 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004428 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004429
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004430 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau350f4872014-11-28 14:42:25 +01004431 if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004432 txn->req.msg_state = HTTP_MSG_ERROR;
4433 txn->status = 500;
4434 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004435 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004436
Willy Tarreaue7dff022015-04-03 01:14:29 +02004437 if (!(s->flags & SF_ERR_MASK))
4438 s->flags |= SF_ERR_RESOURCE;
4439 if (!(s->flags & SF_FINST_MASK))
4440 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004441
4442 return 0;
4443 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004444
4445 path = http_get_path(txn);
4446 url2sa(req->buf->p + msg->sl.rq.u,
4447 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004448 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004449 /* if the path was found, we have to remove everything between
4450 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4451 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4452 * u_l characters by a single "/".
4453 */
4454 if (path) {
4455 char *cur_ptr = req->buf->p;
4456 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4457 int delta;
4458
4459 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4460 http_msg_move_end(&txn->req, delta);
4461 cur_end += delta;
4462 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4463 goto return_bad_req;
4464 }
4465 else {
4466 char *cur_ptr = req->buf->p;
4467 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4468 int delta;
4469
4470 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4471 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4472 http_msg_move_end(&txn->req, delta);
4473 cur_end += delta;
4474 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4475 goto return_bad_req;
4476 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004477 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004478
Willy Tarreau59234e92008-11-30 23:51:27 +01004479 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004480 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004481 * Note that doing so might move headers in the request, but
4482 * the fields will stay coherent and the URI will not move.
4483 * This should only be performed in the backend.
4484 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004485 if ((s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004486 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4487 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004488
Willy Tarreau59234e92008-11-30 23:51:27 +01004489 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004490 * 8: the appsession cookie was looked up very early in 1.2,
4491 * so let's do the same now.
4492 */
4493
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004494 /* It needs to look into the URI unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004495 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004496 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 +01004497 }
4498
William Lallemanda73203e2012-03-12 12:48:57 +01004499 /* add unique-id if "header-unique-id" is specified */
4500
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004501 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004502 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4503 goto return_bad_req;
4504 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004505 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004506 }
William Lallemanda73203e2012-03-12 12:48:57 +01004507
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004508 if (sess->fe->header_unique_id && s->unique_id) {
4509 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004510 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004511 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004512 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004513 goto return_bad_req;
4514 }
4515
Cyril Bontéb21570a2009-11-29 20:04:48 +01004516 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004517 * 9: add X-Forwarded-For if either the frontend or the backend
4518 * asks for it.
4519 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004520 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004521 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004522 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4523 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4524 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004525 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004526 /* The header is set to be added only if none is present
4527 * and we found it, so don't do anything.
4528 */
4529 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004530 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004531 /* Add an X-Forwarded-For header unless the source IP is
4532 * in the 'except' network range.
4533 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004534 if ((!sess->fe->except_mask.s_addr ||
4535 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4536 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004537 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004538 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004539 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004540 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004541 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004542 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004543
4544 /* Note: we rely on the backend to get the header name to be used for
4545 * x-forwarded-for, because the header is really meant for the backends.
4546 * However, if the backend did not specify any option, we have to rely
4547 * on the frontend's header name.
4548 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004549 if (s->be->fwdfor_hdr_len) {
4550 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004551 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004552 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004553 len = sess->fe->fwdfor_hdr_len;
4554 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004555 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004556 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 +01004557
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004558 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004559 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004560 }
4561 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004562 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004563 /* FIXME: for the sake of completeness, we should also support
4564 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004565 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004566 int len;
4567 char pn[INET6_ADDRSTRLEN];
4568 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004569 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004570 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004571
Willy Tarreau59234e92008-11-30 23:51:27 +01004572 /* Note: we rely on the backend to get the header name to be used for
4573 * x-forwarded-for, because the header is really meant for the backends.
4574 * However, if the backend did not specify any option, we have to rely
4575 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004576 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004577 if (s->be->fwdfor_hdr_len) {
4578 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004579 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004580 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004581 len = sess->fe->fwdfor_hdr_len;
4582 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004583 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004584 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004585
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004586 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004587 goto return_bad_req;
4588 }
4589 }
4590
4591 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004592 * 10: add X-Original-To if either the frontend or the backend
4593 * asks for it.
4594 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004595 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004596
4597 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004598 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004599 /* Add an X-Original-To header unless the destination IP is
4600 * in the 'except' network range.
4601 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004602 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004603
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004604 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004605 ((!sess->fe->except_mask_to.s_addr ||
4606 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4607 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004608 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004609 (((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 +02004610 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004611 int len;
4612 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004613 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004614
4615 /* Note: we rely on the backend to get the header name to be used for
4616 * x-original-to, because the header is really meant for the backends.
4617 * However, if the backend did not specify any option, we have to rely
4618 * on the frontend's header name.
4619 */
4620 if (s->be->orgto_hdr_len) {
4621 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004622 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004623 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004624 len = sess->fe->orgto_hdr_len;
4625 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004626 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004627 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 +02004628
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004629 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004630 goto return_bad_req;
4631 }
4632 }
4633 }
4634
Willy Tarreau50fc7772012-11-11 22:19:57 +01004635 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4636 * If an "Upgrade" token is found, the header is left untouched in order not to have
4637 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4638 * "Upgrade" is present in the Connection header.
4639 */
4640 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4641 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004642 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004643 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004644 unsigned int want_flags = 0;
4645
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004646 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004647 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004648 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004649 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004650 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004651 want_flags |= TX_CON_CLO_SET;
4652 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004653 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004654 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004655 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004656 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004657 want_flags |= TX_CON_KAL_SET;
4658 }
4659
4660 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004661 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004662 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004663
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004664
Willy Tarreau522d6c02009-12-06 18:49:18 +01004665 /* If we have no server assigned yet and we're balancing on url_param
4666 * with a POST request, we may be interested in checking the body for
4667 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004668 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004669 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004670 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004671 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004672 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004673 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004674 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004675
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004676 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004677 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004678#ifdef TCP_QUICKACK
4679 /* We expect some data from the client. Unless we know for sure
4680 * we already have a full request, we have to re-enable quick-ack
4681 * in case we previously disabled it, otherwise we might cause
4682 * the client to delay further data.
4683 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004684 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004685 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004686 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004687 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004688 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004689#endif
4690 }
Willy Tarreau03945942009-12-22 16:50:27 +01004691
Willy Tarreau59234e92008-11-30 23:51:27 +01004692 /*************************************************************
4693 * OK, that's finished for the headers. We have done what we *
4694 * could. Let's switch to the DATA state. *
4695 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004696 req->analyse_exp = TICK_ETERNITY;
4697 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004698
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004699 /* if the server closes the connection, we want to immediately react
4700 * and close the socket to save packets and syscalls.
4701 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004702 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004703 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004704
Willy Tarreau59234e92008-11-30 23:51:27 +01004705 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004706 /* OK let's go on with the BODY now */
4707 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004708
Willy Tarreau59234e92008-11-30 23:51:27 +01004709 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004710 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004711 /* we detected a parsing error. We want to archive this request
4712 * in the dedicated proxy area for later troubleshooting.
4713 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004714 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004715 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004716
Willy Tarreau59234e92008-11-30 23:51:27 +01004717 txn->req.msg_state = HTTP_MSG_ERROR;
4718 txn->status = 400;
4719 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004720 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004721
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004722 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004723 if (sess->listener->counters)
4724 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004725
Willy Tarreaue7dff022015-04-03 01:14:29 +02004726 if (!(s->flags & SF_ERR_MASK))
4727 s->flags |= SF_ERR_PRXCOND;
4728 if (!(s->flags & SF_FINST_MASK))
4729 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004730 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004731}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004732
Willy Tarreau60b85b02008-11-30 23:28:40 +01004733/* This function is an analyser which processes the HTTP tarpit. It always
4734 * returns zero, at the beginning because it prevents any other processing
4735 * from occurring, and at the end because it terminates the request.
4736 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004737int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004738{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004739 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004740
4741 /* This connection is being tarpitted. The CLIENT side has
4742 * already set the connect expiration date to the right
4743 * timeout. We just have to check that the client is still
4744 * there and that the timeout has not expired.
4745 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004746 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004747 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004748 !tick_is_expired(req->analyse_exp, now_ms))
4749 return 0;
4750
4751 /* We will set the queue timer to the time spent, just for
4752 * logging purposes. We fake a 500 server error, so that the
4753 * attacker will not suspect his connection has been tarpitted.
4754 * It will not cause trouble to the logs because we can exclude
4755 * the tarpitted connections by filtering on the 'PT' status flags.
4756 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004757 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4758
4759 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004760 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004761 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004762
4763 req->analysers = 0;
4764 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004765
Willy Tarreaue7dff022015-04-03 01:14:29 +02004766 if (!(s->flags & SF_ERR_MASK))
4767 s->flags |= SF_ERR_PRXCOND;
4768 if (!(s->flags & SF_FINST_MASK))
4769 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004770 return 0;
4771}
4772
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004773/* This function is an analyser which waits for the HTTP request body. It waits
4774 * for either the buffer to be full, or the full advertised contents to have
4775 * reached the buffer. It must only be called after the standard HTTP request
4776 * processing has occurred, because it expects the request to be parsed and will
4777 * look for the Expect header. It may send a 100-Continue interim response. It
4778 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4779 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4780 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004781 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004782int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004783{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004784 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004785 struct http_txn *txn = s->txn;
4786 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004787
4788 /* We have to parse the HTTP request body to find any required data.
4789 * "balance url_param check_post" should have been the only way to get
4790 * into this. We were brought here after HTTP header analysis, so all
4791 * related structures are ready.
4792 */
4793
Willy Tarreau890988f2014-04-10 11:59:33 +02004794 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4795 /* This is the first call */
4796 if (msg->msg_state < HTTP_MSG_BODY)
4797 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004798
Willy Tarreau890988f2014-04-10 11:59:33 +02004799 if (msg->msg_state < HTTP_MSG_100_SENT) {
4800 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4801 * send an HTTP/1.1 100 Continue intermediate response.
4802 */
4803 if (msg->flags & HTTP_MSGF_VER_11) {
4804 struct hdr_ctx ctx;
4805 ctx.idx = 0;
4806 /* Expect is allowed in 1.1, look for it */
4807 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4808 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004809 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004810 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004811 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004812 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004813 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004814
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004815 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004816 * req->buf->p still points to the beginning of the message. We
4817 * must save the body in msg->next because it survives buffer
4818 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004819 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004820 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004821
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004822 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004823 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4824 else
4825 msg->msg_state = HTTP_MSG_DATA;
4826 }
4827
Willy Tarreau890988f2014-04-10 11:59:33 +02004828 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4829 /* We're in content-length mode, we just have to wait for enough data. */
4830 if (req->buf->i - msg->sov < msg->body_len)
4831 goto missing_data;
4832
4833 /* OK we have everything we need now */
4834 goto http_end;
4835 }
4836
4837 /* OK here we're parsing a chunked-encoded message */
4838
Willy Tarreau522d6c02009-12-06 18:49:18 +01004839 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004840 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004841 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004842 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004843 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004844 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004845
Willy Tarreau115acb92009-12-26 13:56:06 +01004846 if (!ret)
4847 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004848 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004849 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004850 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004851 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004852 }
4853
Willy Tarreaud98cf932009-12-27 22:54:55 +01004854 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004855 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004856 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004857 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004858 if (msg->msg_state == HTTP_MSG_TRAILERS)
4859 goto http_end;
4860
Willy Tarreau226071e2014-04-10 11:55:45 +02004861 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004862 goto http_end;
4863
4864 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004865 /* we get here if we need to wait for more data. If the buffer is full,
4866 * we have the maximum we can expect.
4867 */
4868 if (buffer_full(req->buf, global.tune.maxrewrite))
4869 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004870
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004871 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004872 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004873 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004874
Willy Tarreaue7dff022015-04-03 01:14:29 +02004875 if (!(s->flags & SF_ERR_MASK))
4876 s->flags |= SF_ERR_CLITO;
4877 if (!(s->flags & SF_FINST_MASK))
4878 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004879 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004880 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004881
4882 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004883 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004884 /* Not enough data. We'll re-use the http-request
4885 * timeout here. Ideally, we should set the timeout
4886 * relative to the accept() date. We just set the
4887 * request timeout once at the beginning of the
4888 * request.
4889 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004890 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004891 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004892 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004893 return 0;
4894 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004895
4896 http_end:
4897 /* The situation will not evolve, so let's give up on the analysis. */
4898 s->logs.tv_request = now; /* update the request timer to reflect full request */
4899 req->analysers &= ~an_bit;
4900 req->analyse_exp = TICK_ETERNITY;
4901 return 1;
4902
4903 return_bad_req: /* let's centralize all bad requests */
4904 txn->req.msg_state = HTTP_MSG_ERROR;
4905 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004906 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004907
Willy Tarreaue7dff022015-04-03 01:14:29 +02004908 if (!(s->flags & SF_ERR_MASK))
4909 s->flags |= SF_ERR_PRXCOND;
4910 if (!(s->flags & SF_FINST_MASK))
4911 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004912
Willy Tarreau522d6c02009-12-06 18:49:18 +01004913 return_err_msg:
4914 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004915 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004916 if (sess->listener->counters)
4917 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004918 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004919}
4920
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004921/* send a server's name with an outgoing request over an established connection.
4922 * Note: this function is designed to be called once the request has been scheduled
4923 * for being forwarded. This is the reason why it rewinds the buffer before
4924 * proceeding.
4925 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004926int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004927
4928 struct hdr_ctx ctx;
4929
Mark Lamourinec2247f02012-01-04 13:02:01 -05004930 char *hdr_name = be->server_id_hdr_name;
4931 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004932 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004933 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004934 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004935
William Lallemandd9e90662012-01-30 17:27:17 +01004936 ctx.idx = 0;
4937
Willy Tarreau211cdec2014-04-17 20:18:08 +02004938 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004939 if (old_o) {
4940 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004941 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004942 txn->req.next += old_o;
4943 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004944 }
4945
Willy Tarreau9b28e032012-10-12 23:49:43 +02004946 old_i = chn->buf->i;
4947 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 -05004948 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004949 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004950 }
4951
4952 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004953 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004954 memcpy(hdr_val, hdr_name, hdr_name_len);
4955 hdr_val += hdr_name_len;
4956 *hdr_val++ = ':';
4957 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004958 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4959 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004960
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004961 if (old_o) {
4962 /* If this was a forwarded request, we must readjust the amount of
4963 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004964 * variations. Note that the current state is >= HTTP_MSG_BODY,
4965 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004966 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004967 old_o += chn->buf->i - old_i;
4968 b_adv(chn->buf, old_o);
4969 txn->req.next -= old_o;
4970 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004971 }
4972
Mark Lamourinec2247f02012-01-04 13:02:01 -05004973 return 0;
4974}
4975
Willy Tarreau610ecce2010-01-04 21:15:02 +01004976/* Terminate current transaction and prepare a new one. This is very tricky
4977 * right now but it works.
4978 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004979void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004980{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004981 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004982 struct proxy *fe = strm_fe(s);
Willy Tarreau068621e2013-12-23 15:11:25 +01004983
Willy Tarreau610ecce2010-01-04 21:15:02 +01004984 /* FIXME: We need a more portable way of releasing a backend's and a
4985 * server's connections. We need a safer way to reinitialize buffer
4986 * flags. We also need a more accurate method for computing per-request
4987 * data.
4988 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004989
Willy Tarreau4213a112013-12-15 10:25:42 +01004990 /* unless we're doing keep-alive, we want to quickly close the connection
4991 * to the server.
4992 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004993 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004994 !si_conn_ready(&s->si[1])) {
4995 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4996 si_shutr(&s->si[1]);
4997 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004998 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004999
Willy Tarreaue7dff022015-04-03 01:14:29 +02005000 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005001 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005002 if (unlikely(s->srv_conn))
5003 sess_change_server(s, NULL);
5004 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005005
5006 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005007 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005008
Willy Tarreaueee5b512015-04-03 23:46:31 +02005009 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005010 int n;
5011
Willy Tarreaueee5b512015-04-03 23:46:31 +02005012 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005013 if (n < 1 || n > 5)
5014 n = 0;
5015
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005016 if (fe->mode == PR_MODE_HTTP) {
5017 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005018 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005019 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005020 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005021 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005022 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005023 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005024 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005025 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005026 s->be->be_counters.p.http.comp_rsp++;
5027 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005028 }
5029
5030 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005031 s->logs.bytes_in -= s->req.buf->i;
5032 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005033
5034 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005035 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005036 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005037 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005038 s->do_log(s);
5039 }
5040
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005041 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005042 stream_stop_content_counters(s);
5043 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005044
Willy Tarreau610ecce2010-01-04 21:15:02 +01005045 s->logs.accept_date = date; /* user-visible date for logging */
5046 s->logs.tv_accept = now; /* corrected date for internal use */
5047 tv_zero(&s->logs.tv_request);
5048 s->logs.t_queue = -1;
5049 s->logs.t_connect = -1;
5050 s->logs.t_data = -1;
5051 s->logs.t_close = 0;
5052 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5053 s->logs.srv_queue_size = 0; /* we will get this number soon */
5054
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005055 s->logs.bytes_in = s->req.total = s->req.buf->i;
5056 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005057
5058 if (s->pend_pos)
5059 pendconn_free(s->pend_pos);
5060
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005061 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005062 if (s->flags & SF_CURR_SESS) {
5063 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005064 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005065 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005066 if (may_dequeue_tasks(objt_server(s->target), s->be))
5067 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005068 }
5069
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005070 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005071
Willy Tarreau4213a112013-12-15 10:25:42 +01005072 /* only release our endpoint if we don't intend to reuse the
5073 * connection.
5074 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005075 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005076 !si_conn_ready(&s->si[1])) {
5077 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005078 }
5079
Willy Tarreau350f4872014-11-28 14:42:25 +01005080 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5081 s->si[1].err_type = SI_ET_NONE;
5082 s->si[1].conn_retries = 0; /* used for logging too */
5083 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005084 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 +01005085 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);
5086 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 +02005087 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5088 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5089 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005090
Willy Tarreaueee5b512015-04-03 23:46:31 +02005091 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005092 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005093 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005094
5095 if (prev_status == 401 || prev_status == 407) {
5096 /* In HTTP keep-alive mode, if we receive a 401, we still have
5097 * a chance of being able to send the visitor again to the same
5098 * server over the same connection. This is required by some
5099 * broken protocols such as NTLM, and anyway whenever there is
5100 * an opportunity for sending the challenge to the proper place,
5101 * it's better to do it (at least it helps with debugging).
5102 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005103 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01005104 }
5105
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005106 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005107 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005108
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005109 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005110 s->req.flags |= CF_NEVER_WAIT;
5111 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005112 }
5113
Willy Tarreau610ecce2010-01-04 21:15:02 +01005114 /* if the request buffer is not empty, it means we're
5115 * about to process another request, so send pending
5116 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005117 * Just don't do this if the buffer is close to be full,
5118 * because the request will wait for it to flush a little
5119 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005120 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005121 if (s->req.buf->i) {
5122 if (s->res.buf->o &&
5123 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5124 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5125 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005126 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005127
5128 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005129 channel_auto_read(&s->req);
5130 channel_auto_close(&s->req);
5131 channel_auto_read(&s->res);
5132 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005133
Willy Tarreau27375622013-12-17 00:00:28 +01005134 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau350f4872014-11-28 14:42:25 +01005135 si_idle_conn(&s->si[1]);
Willy Tarreau27375622013-12-17 00:00:28 +01005136
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005137 s->req.analysers = strm_li(s)->analysers;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005138 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005139}
5140
5141
5142/* This function updates the request state machine according to the response
5143 * state machine and buffer flags. It returns 1 if it changes anything (flag
5144 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5145 * it is only used to find when a request/response couple is complete. Both
5146 * this function and its equivalent should loop until both return zero. It
5147 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5148 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005149int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005150{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005151 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005152 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005153 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005154 unsigned int old_state = txn->req.msg_state;
5155
Willy Tarreau610ecce2010-01-04 21:15:02 +01005156 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5157 return 0;
5158
5159 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005160 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005161 * We can shut the read side unless we want to abort_on_close,
5162 * or we have a POST request. The issue with POST requests is
5163 * that some browsers still send a CRLF after the request, and
5164 * this CRLF must be read so that it does not remain in the kernel
5165 * buffers, otherwise a close could cause an RST on some systems
5166 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005167 * Note that if we're using keep-alive on the client side, we'd
5168 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005169 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005170 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005171 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005172 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5173 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5174 !(s->be->options & PR_O_ABRT_CLOSE) &&
5175 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005176 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005177
Willy Tarreau40f151a2012-12-20 12:10:09 +01005178 /* if the server closes the connection, we want to immediately react
5179 * and close the socket to save packets and syscalls.
5180 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005181 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005182
Willy Tarreau610ecce2010-01-04 21:15:02 +01005183 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5184 goto wait_other_side;
5185
5186 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5187 /* The server has not finished to respond, so we
5188 * don't want to move in order not to upset it.
5189 */
5190 goto wait_other_side;
5191 }
5192
5193 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5194 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005195 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005196 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005197 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005198 goto wait_other_side;
5199 }
5200
5201 /* When we get here, it means that both the request and the
5202 * response have finished receiving. Depending on the connection
5203 * mode, we'll have to wait for the last bytes to leave in either
5204 * direction, and sometimes for a close to be effective.
5205 */
5206
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005207 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5208 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005209 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5210 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005211 }
5212 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5213 /* Option forceclose is set, or either side wants to close,
5214 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005215 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005216 * once both states are CLOSED.
5217 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005218 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5219 channel_shutr_now(chn);
5220 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005221 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005222 }
5223 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005224 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5225 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005226 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005227 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5228 channel_auto_read(chn);
5229 txn->req.msg_state = HTTP_MSG_TUNNEL;
5230 chn->flags |= CF_NEVER_WAIT;
5231 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005232 }
5233
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005234 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005235 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005236 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005237
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005238 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005239 txn->req.msg_state = HTTP_MSG_CLOSING;
5240 goto http_msg_closing;
5241 }
5242 else {
5243 txn->req.msg_state = HTTP_MSG_CLOSED;
5244 goto http_msg_closed;
5245 }
5246 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005247 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005248 }
5249
5250 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5251 http_msg_closing:
5252 /* nothing else to forward, just waiting for the output buffer
5253 * to be empty and for the shutw_now to take effect.
5254 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005255 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005256 txn->req.msg_state = HTTP_MSG_CLOSED;
5257 goto http_msg_closed;
5258 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005259 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005260 txn->req.msg_state = HTTP_MSG_ERROR;
5261 goto wait_other_side;
5262 }
5263 }
5264
5265 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5266 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005267 /* see above in MSG_DONE why we only do this in these states */
5268 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5269 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5270 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005271 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005272 goto wait_other_side;
5273 }
5274
5275 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005276 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005277}
5278
5279
5280/* This function updates the response state machine according to the request
5281 * state machine and buffer flags. It returns 1 if it changes anything (flag
5282 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5283 * it is only used to find when a request/response couple is complete. Both
5284 * this function and its equivalent should loop until both return zero. It
5285 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5286 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005287int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005288{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005289 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005290 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005291 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005292 unsigned int old_state = txn->rsp.msg_state;
5293
Willy Tarreau610ecce2010-01-04 21:15:02 +01005294 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5295 return 0;
5296
5297 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5298 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005299 * still monitor the server connection for a possible close
5300 * while the request is being uploaded, so we don't disable
5301 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005302 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005303 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304
5305 if (txn->req.msg_state == HTTP_MSG_ERROR)
5306 goto wait_other_side;
5307
5308 if (txn->req.msg_state < HTTP_MSG_DONE) {
5309 /* The client seems to still be sending data, probably
5310 * because we got an error response during an upload.
5311 * We have the choice of either breaking the connection
5312 * or letting it pass through. Let's do the later.
5313 */
5314 goto wait_other_side;
5315 }
5316
5317 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5318 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005319 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005320 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005321 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005322 goto wait_other_side;
5323 }
5324
5325 /* When we get here, it means that both the request and the
5326 * response have finished receiving. Depending on the connection
5327 * mode, we'll have to wait for the last bytes to leave in either
5328 * direction, and sometimes for a close to be effective.
5329 */
5330
5331 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5332 /* Server-close mode : shut read and wait for the request
5333 * side to close its output buffer. The caller will detect
5334 * when we're in DONE and the other is in CLOSED and will
5335 * catch that for the final cleanup.
5336 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005337 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5338 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005339 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005340 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5341 /* Option forceclose is set, or either side wants to close,
5342 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005343 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005344 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005345 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005346 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5347 channel_shutr_now(chn);
5348 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005349 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005350 }
5351 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005352 /* The last possible modes are keep-alive and tunnel. Tunnel will
5353 * need to forward remaining data. Keep-alive will need to monitor
5354 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005355 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005356 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005357 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005358 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5359 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005360 }
5361
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005362 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005363 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005364 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005365 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5366 goto http_msg_closing;
5367 }
5368 else {
5369 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5370 goto http_msg_closed;
5371 }
5372 }
5373 goto wait_other_side;
5374 }
5375
5376 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5377 http_msg_closing:
5378 /* nothing else to forward, just waiting for the output buffer
5379 * to be empty and for the shutw_now to take effect.
5380 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005381 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005382 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5383 goto http_msg_closed;
5384 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005385 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005386 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005387 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005388 if (objt_server(s->target))
5389 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005390 goto wait_other_side;
5391 }
5392 }
5393
5394 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5395 http_msg_closed:
5396 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005397 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005398 channel_auto_close(chn);
5399 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005400 goto wait_other_side;
5401 }
5402
5403 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005404 /* We force the response to leave immediately if we're waiting for the
5405 * other side, since there is no pending shutdown to push it out.
5406 */
5407 if (!channel_is_empty(chn))
5408 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005409 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005410}
5411
5412
5413/* Resync the request and response state machines. Return 1 if either state
5414 * changes.
5415 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005416int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005417{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005418 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005419 int old_req_state = txn->req.msg_state;
5420 int old_res_state = txn->rsp.msg_state;
5421
Willy Tarreau610ecce2010-01-04 21:15:02 +01005422 http_sync_req_state(s);
5423 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005424 if (!http_sync_res_state(s))
5425 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005426 if (!http_sync_req_state(s))
5427 break;
5428 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005429
Willy Tarreau610ecce2010-01-04 21:15:02 +01005430 /* OK, both state machines agree on a compatible state.
5431 * There are a few cases we're interested in :
5432 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5433 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5434 * directions, so let's simply disable both analysers.
5435 * - HTTP_MSG_CLOSED on the response only means we must abort the
5436 * request.
5437 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5438 * with server-close mode means we've completed one request and we
5439 * must re-initialize the server connection.
5440 */
5441
5442 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5443 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5444 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5445 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005446 s->req.analysers = 0;
5447 channel_auto_close(&s->req);
5448 channel_auto_read(&s->req);
5449 s->res.analysers = 0;
5450 channel_auto_close(&s->res);
5451 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005452 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005453 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005454 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005455 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005456 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005457 s->res.analysers = 0;
5458 channel_auto_close(&s->res);
5459 channel_auto_read(&s->res);
5460 s->req.analysers = 0;
5461 channel_abort(&s->req);
5462 channel_auto_close(&s->req);
5463 channel_auto_read(&s->req);
5464 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005465 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005466 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5467 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005468 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005469 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5470 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5471 /* server-close/keep-alive: terminate this transaction,
5472 * possibly killing the server connection and reinitialize
5473 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005474 */
5475 http_end_txn_clean_session(s);
5476 }
5477
Willy Tarreau610ecce2010-01-04 21:15:02 +01005478 return txn->req.msg_state != old_req_state ||
5479 txn->rsp.msg_state != old_res_state;
5480}
5481
Willy Tarreaud98cf932009-12-27 22:54:55 +01005482/* This function is an analyser which forwards request body (including chunk
5483 * sizes if any). It is called as soon as we must forward, even if we forward
5484 * zero byte. The only situation where it must not be called is when we're in
5485 * tunnel mode and we want to forward till the close. It's used both to forward
5486 * remaining data and to resync after end of body. It expects the msg_state to
5487 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005488 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005489 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005490 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005491 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005492int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005493{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005494 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005495 struct http_txn *txn = s->txn;
5496 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005497
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005498 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5499 return 0;
5500
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005501 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005502 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005503 /* Output closed while we were sending data. We must abort and
5504 * wake the other side up.
5505 */
5506 msg->msg_state = HTTP_MSG_ERROR;
5507 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005508 return 1;
5509 }
5510
Willy Tarreaud98cf932009-12-27 22:54:55 +01005511 /* Note that we don't have to send 100-continue back because we don't
5512 * need the data to complete our job, and it's up to the server to
5513 * decide whether to return 100, 417 or anything else in return of
5514 * an "Expect: 100-continue" header.
5515 */
5516
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005517 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005518 /* we have msg->sov which points to the first byte of message
5519 * body, and req->buf.p still points to the beginning of the
5520 * message. We forward the headers now, as we don't need them
5521 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005522 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005523 b_adv(req->buf, msg->sov);
5524 msg->next -= msg->sov;
5525 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005526
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005527 /* The previous analysers guarantee that the state is somewhere
5528 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5529 * msg->next are always correct.
5530 */
5531 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5532 if (msg->flags & HTTP_MSGF_TE_CHNK)
5533 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5534 else
5535 msg->msg_state = HTTP_MSG_DATA;
5536 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005537 }
5538
Willy Tarreau7ba23542014-04-17 21:50:00 +02005539 /* Some post-connect processing might want us to refrain from starting to
5540 * forward data. Currently, the only reason for this is "balance url_param"
5541 * whichs need to parse/process the request after we've enabled forwarding.
5542 */
5543 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005544 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005545 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005546 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005547 goto missing_data;
5548 }
5549 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5550 }
5551
Willy Tarreau80a92c02014-03-12 10:41:13 +01005552 /* in most states, we should abort in case of early close */
5553 channel_auto_close(req);
5554
Willy Tarreauefdf0942014-04-24 20:08:57 +02005555 if (req->to_forward) {
5556 /* We can't process the buffer's contents yet */
5557 req->flags |= CF_WAKE_WRITE;
5558 goto missing_data;
5559 }
5560
Willy Tarreaud98cf932009-12-27 22:54:55 +01005561 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005562 if (msg->msg_state == HTTP_MSG_DATA) {
5563 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005564 /* we may have some pending data starting at req->buf->p */
5565 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005566 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005567 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005568 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005569 msg->next += msg->chunk_len;
5570 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005571
5572 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005573 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005574 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005575 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005576 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005577 }
5578 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005579 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005580 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005581 * TRAILERS state.
5582 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005583 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005584
Willy Tarreau54d23df2012-10-25 19:04:45 +02005585 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005586 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005587 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005588 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005589 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005590 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005591 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005592 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005593 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005594 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005595 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005596 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005597 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005598
5599 if (ret == 0)
5600 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005601 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005602 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005603 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005604 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005605 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005606 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005607 /* we're in MSG_CHUNK_SIZE now */
5608 }
5609 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005610 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005611
5612 if (ret == 0)
5613 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005614 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005615 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005616 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005617 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005618 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005619 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005620 /* we're in HTTP_MSG_DONE now */
5621 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005622 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005623 int old_state = msg->msg_state;
5624
Willy Tarreau610ecce2010-01-04 21:15:02 +01005625 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005626
5627 /* we may have some pending data starting at req->buf->p
5628 * such as last chunk of data or trailers.
5629 */
5630 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005631 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005632 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005633 msg->next = 0;
5634
Willy Tarreau4fe41902010-06-07 22:27:41 +02005635 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005636 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5637 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005638 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005639 if (http_resync_states(s)) {
5640 /* some state changes occurred, maybe the analyser
5641 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005642 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005643 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005644 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005645 /* request errors are most likely due to
5646 * the server aborting the transfer.
5647 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005648 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005649 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005650 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005651 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005652 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005653 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005654 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005655 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005656
5657 /* If "option abortonclose" is set on the backend, we
5658 * want to monitor the client's connection and forward
5659 * any shutdown notification to the server, which will
5660 * decide whether to close or to go on processing the
5661 * request.
5662 */
5663 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005664 channel_auto_read(req);
5665 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005666 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005667 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005668 /* POST requests may require to read extra CRLF
5669 * sent by broken browsers and which could cause
5670 * an RST to be sent upon close on some systems
5671 * (eg: Linux).
5672 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005673 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005674 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005675
Willy Tarreau610ecce2010-01-04 21:15:02 +01005676 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005677 }
5678 }
5679
Willy Tarreaud98cf932009-12-27 22:54:55 +01005680 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005681 /* we may have some pending data starting at req->buf->p */
5682 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005683 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005684 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5685
Willy Tarreaubed410e2014-04-22 08:19:34 +02005686 msg->next = 0;
5687 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5688
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005689 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005690 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005691 if (!(s->flags & SF_ERR_MASK))
5692 s->flags |= SF_ERR_CLICL;
5693 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005694 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005695 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005696 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005697 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005698 }
5699
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005700 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005701 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005702 if (objt_server(s->target))
5703 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005704
5705 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005706 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005707
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005708 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005709 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005710 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005711
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005712 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005713 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005714 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005715 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005716 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005717
Willy Tarreau5c620922011-05-11 19:56:11 +02005718 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005719 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005720 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005721 * modes are already handled by the stream sock layer. We must not do
5722 * this in content-length mode because it could present the MSG_MORE
5723 * flag with the last block of forwarded data, which would cause an
5724 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005725 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005726 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005727 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005728
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005729 return 0;
5730
Willy Tarreaud98cf932009-12-27 22:54:55 +01005731 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005732 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005733 if (sess->listener->counters)
5734 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005735
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005736 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005737 /* we may have some pending data starting at req->buf->p */
5738 b_adv(req->buf, msg->next);
5739 msg->next = 0;
5740
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005741 txn->req.msg_state = HTTP_MSG_ERROR;
5742 if (txn->status) {
5743 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005744 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005745 } else {
5746 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005747 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005748 }
5749 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005750 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005751
Willy Tarreaue7dff022015-04-03 01:14:29 +02005752 if (!(s->flags & SF_ERR_MASK))
5753 s->flags |= SF_ERR_PRXCOND;
5754 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005755 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005756 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005757 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005758 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005759 }
5760 return 0;
5761
5762 aborted_xfer:
5763 txn->req.msg_state = HTTP_MSG_ERROR;
5764 if (txn->status) {
5765 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005766 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005767 } else {
5768 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005769 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005770 }
5771 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005772 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005773
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005774 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005775 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005776 if (objt_server(s->target))
5777 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005778
Willy Tarreaue7dff022015-04-03 01:14:29 +02005779 if (!(s->flags & SF_ERR_MASK))
5780 s->flags |= SF_ERR_SRVCL;
5781 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005782 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005783 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005784 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005785 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005786 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005787 return 0;
5788}
5789
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005790/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5791 * processing can continue on next analysers, or zero if it either needs more
5792 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005793 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005794 * when it has nothing left to do, and may remove any analyser when it wants to
5795 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005796 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005797int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005798{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005799 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005800 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005801 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005802 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005803 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005804 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005805 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005806
Willy Tarreau87b09662015-04-03 00:22:06 +02005807 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 +02005808 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005809 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005810 rep,
5811 rep->rex, rep->wex,
5812 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005813 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005814 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005815
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005816 /*
5817 * Now parse the partial (or complete) lines.
5818 * We will check the response syntax, and also join multi-line
5819 * headers. An index of all the lines will be elaborated while
5820 * parsing.
5821 *
5822 * For the parsing, we use a 28 states FSM.
5823 *
5824 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005825 * rep->buf->p = beginning of response
5826 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5827 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005828 * msg->eol = end of current header or line (LF or CRLF)
5829 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005830 */
5831
Willy Tarreau628c40c2014-04-24 19:11:26 +02005832 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005833 /* There's a protected area at the end of the buffer for rewriting
5834 * purposes. We don't want to start to parse the request if the
5835 * protected area is affected, because we may have to move processed
5836 * data later, which is much more complicated.
5837 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005838 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005839 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005840 /* some data has still not left the buffer, wake us once that's done */
5841 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5842 goto abort_response;
5843 channel_dont_close(rep);
5844 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005845 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005846 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005847 }
5848
Willy Tarreau379357a2013-06-08 12:55:46 +02005849 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5850 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5851 buffer_slow_realign(rep->buf);
5852
Willy Tarreau9b28e032012-10-12 23:49:43 +02005853 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005854 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005855 }
5856
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005857 /* 1: we might have to print this header in debug mode */
5858 if (unlikely((global.mode & MODE_DEBUG) &&
5859 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005860 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005861 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005862
Willy Tarreau9b28e032012-10-12 23:49:43 +02005863 sol = rep->buf->p;
5864 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005865 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005866
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005867 sol += hdr_idx_first_pos(&txn->hdr_idx);
5868 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005869
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005870 while (cur_idx) {
5871 eol = sol + txn->hdr_idx.v[cur_idx].len;
5872 debug_hdr("srvhdr", s, sol, eol);
5873 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5874 cur_idx = txn->hdr_idx.v[cur_idx].next;
5875 }
5876 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005877
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005878 /*
5879 * Now we quickly check if we have found a full valid response.
5880 * If not so, we check the FD and buffer states before leaving.
5881 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005882 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005883 * responses are checked first.
5884 *
5885 * Depending on whether the client is still there or not, we
5886 * may send an error response back or not. Note that normally
5887 * we should only check for HTTP status there, and check I/O
5888 * errors somewhere else.
5889 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005890
Willy Tarreau655dce92009-11-08 13:10:58 +01005891 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005892 /* Invalid response */
5893 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5894 /* we detected a parsing error. We want to archive this response
5895 * in the dedicated proxy area for later troubleshooting.
5896 */
5897 hdr_response_bad:
5898 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005899 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005900
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005901 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005902 if (objt_server(s->target)) {
5903 objt_server(s->target)->counters.failed_resp++;
5904 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005905 }
Willy Tarreau64648412010-03-05 10:41:54 +01005906 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005907 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005908 rep->analysers = 0;
5909 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005910 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005911 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005912 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005913
Willy Tarreaue7dff022015-04-03 01:14:29 +02005914 if (!(s->flags & SF_ERR_MASK))
5915 s->flags |= SF_ERR_PRXCOND;
5916 if (!(s->flags & SF_FINST_MASK))
5917 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005918
5919 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005920 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005921
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005922 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005923 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005924 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005925 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005926 goto hdr_response_bad;
5927 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005928
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005929 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005930 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005931 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005932 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005933 else if (txn->flags & TX_NOT_FIRST)
5934 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005935
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005936 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005937 if (objt_server(s->target)) {
5938 objt_server(s->target)->counters.failed_resp++;
5939 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005940 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005941
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005942 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005943 rep->analysers = 0;
5944 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005945 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005946 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005947 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005948
Willy Tarreaue7dff022015-04-03 01:14:29 +02005949 if (!(s->flags & SF_ERR_MASK))
5950 s->flags |= SF_ERR_SRVCL;
5951 if (!(s->flags & SF_FINST_MASK))
5952 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005953 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005954 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005955
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005956 /* read timeout : return a 504 to the client. */
5957 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005958 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005959 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005960 else if (txn->flags & TX_NOT_FIRST)
5961 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005962
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005963 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005964 if (objt_server(s->target)) {
5965 objt_server(s->target)->counters.failed_resp++;
5966 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005967 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005968
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005969 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005970 rep->analysers = 0;
5971 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005972 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005973 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005974 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005975
Willy Tarreaue7dff022015-04-03 01:14:29 +02005976 if (!(s->flags & SF_ERR_MASK))
5977 s->flags |= SF_ERR_SRVTO;
5978 if (!(s->flags & SF_FINST_MASK))
5979 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005980 return 0;
5981 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005982
Willy Tarreauf003d372012-11-26 13:35:37 +01005983 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005984 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005985 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005986 s->be->be_counters.cli_aborts++;
5987 if (objt_server(s->target))
5988 objt_server(s->target)->counters.cli_aborts++;
5989
5990 rep->analysers = 0;
5991 channel_auto_close(rep);
5992
5993 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005994 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005995 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01005996
Willy Tarreaue7dff022015-04-03 01:14:29 +02005997 if (!(s->flags & SF_ERR_MASK))
5998 s->flags |= SF_ERR_CLICL;
5999 if (!(s->flags & SF_FINST_MASK))
6000 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006001
Willy Tarreau87b09662015-04-03 00:22:06 +02006002 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006003 return 0;
6004 }
6005
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006006 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006007 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006008 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006009 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006010 else if (txn->flags & TX_NOT_FIRST)
6011 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006012
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006013 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006014 if (objt_server(s->target)) {
6015 objt_server(s->target)->counters.failed_resp++;
6016 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006017 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006018
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006019 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006020 rep->analysers = 0;
6021 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006022 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006023 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006024 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006025
Willy Tarreaue7dff022015-04-03 01:14:29 +02006026 if (!(s->flags & SF_ERR_MASK))
6027 s->flags |= SF_ERR_SRVCL;
6028 if (!(s->flags & SF_FINST_MASK))
6029 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006030 return 0;
6031 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006032
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006033 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006034 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006035 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006036 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006037 else if (txn->flags & TX_NOT_FIRST)
6038 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006039
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006040 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006041 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006042 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006043
Willy Tarreaue7dff022015-04-03 01:14:29 +02006044 if (!(s->flags & SF_ERR_MASK))
6045 s->flags |= SF_ERR_CLICL;
6046 if (!(s->flags & SF_FINST_MASK))
6047 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006048
Willy Tarreau87b09662015-04-03 00:22:06 +02006049 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006050 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006051 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006052
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006053 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006054 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006055 return 0;
6056 }
6057
6058 /* More interesting part now : we know that we have a complete
6059 * response which at least looks like HTTP. We have an indicator
6060 * of each header's length, so we can parse them quickly.
6061 */
6062
6063 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006064 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006065
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006066 /*
6067 * 1: get the status code
6068 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006069 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006070 if (n < 1 || n > 5)
6071 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006072 /* when the client triggers a 4xx from the server, it's most often due
6073 * to a missing object or permission. These events should be tracked
6074 * because if they happen often, it may indicate a brute force or a
6075 * vulnerability scan.
6076 */
6077 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006078 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006079
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006080 if (objt_server(s->target))
6081 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006082
Willy Tarreau91852eb2015-05-01 13:26:00 +02006083 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6084 * exactly one digit "." one digit. This check may be disabled using
6085 * option accept-invalid-http-response.
6086 */
6087 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6088 if (msg->sl.st.v_l != 8) {
6089 msg->err_pos = 0;
6090 goto hdr_response_bad;
6091 }
6092
6093 if (rep->buf->p[4] != '/' ||
6094 !isdigit((unsigned char)rep->buf->p[5]) ||
6095 rep->buf->p[6] != '.' ||
6096 !isdigit((unsigned char)rep->buf->p[7])) {
6097 msg->err_pos = 4;
6098 goto hdr_response_bad;
6099 }
6100 }
6101
Willy Tarreau5b154472009-12-21 20:11:07 +01006102 /* check if the response is HTTP/1.1 or above */
6103 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006104 ((rep->buf->p[5] > '1') ||
6105 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006106 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006107
6108 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006109 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 +01006110
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006111 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006112 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006113
Willy Tarreau9b28e032012-10-12 23:49:43 +02006114 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006115
Willy Tarreau39650402010-03-15 19:44:39 +01006116 /* Adjust server's health based on status code. Note: status codes 501
6117 * and 505 are triggered on demand by client request, so we must not
6118 * count them as server failures.
6119 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006120 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006121 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006122 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006123 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006124 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006125 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006126
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006127 /*
6128 * 2: check for cacheability.
6129 */
6130
6131 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006132 case 100:
6133 /*
6134 * We may be facing a 100-continue response, in which case this
6135 * is not the right response, and we're waiting for the next one.
6136 * Let's allow this response to go to the client and wait for the
6137 * next one.
6138 */
6139 hdr_idx_init(&txn->hdr_idx);
6140 msg->next -= channel_forward(rep, msg->next);
6141 msg->msg_state = HTTP_MSG_RPBEFORE;
6142 txn->status = 0;
6143 s->logs.t_data = -1; /* was not a response yet */
6144 goto next_one;
6145
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006146 case 200:
6147 case 203:
6148 case 206:
6149 case 300:
6150 case 301:
6151 case 410:
6152 /* RFC2616 @13.4:
6153 * "A response received with a status code of
6154 * 200, 203, 206, 300, 301 or 410 MAY be stored
6155 * by a cache (...) unless a cache-control
6156 * directive prohibits caching."
6157 *
6158 * RFC2616 @9.5: POST method :
6159 * "Responses to this method are not cacheable,
6160 * unless the response includes appropriate
6161 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006162 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006163 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006164 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006165 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6166 break;
6167 default:
6168 break;
6169 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006170
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006171 /*
6172 * 3: we may need to capture headers
6173 */
6174 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006175 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006176 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006177 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006178
Willy Tarreau557f1992015-05-01 10:05:17 +02006179 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6180 * by RFC7230#3.3.3 :
6181 *
6182 * The length of a message body is determined by one of the following
6183 * (in order of precedence):
6184 *
6185 * 1. Any response to a HEAD request and any response with a 1xx
6186 * (Informational), 204 (No Content), or 304 (Not Modified) status
6187 * code is always terminated by the first empty line after the
6188 * header fields, regardless of the header fields present in the
6189 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006190 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006191 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6192 * the connection will become a tunnel immediately after the empty
6193 * line that concludes the header fields. A client MUST ignore any
6194 * Content-Length or Transfer-Encoding header fields received in
6195 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006196 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006197 * 3. If a Transfer-Encoding header field is present and the chunked
6198 * transfer coding (Section 4.1) is the final encoding, the message
6199 * body length is determined by reading and decoding the chunked
6200 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006201 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006202 * If a Transfer-Encoding header field is present in a response and
6203 * the chunked transfer coding is not the final encoding, the
6204 * message body length is determined by reading the connection until
6205 * it is closed by the server. If a Transfer-Encoding header field
6206 * is present in a request and the chunked transfer coding is not
6207 * the final encoding, the message body length cannot be determined
6208 * reliably; the server MUST respond with the 400 (Bad Request)
6209 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006210 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006211 * If a message is received with both a Transfer-Encoding and a
6212 * Content-Length header field, the Transfer-Encoding overrides the
6213 * Content-Length. Such a message might indicate an attempt to
6214 * perform request smuggling (Section 9.5) or response splitting
6215 * (Section 9.4) and ought to be handled as an error. A sender MUST
6216 * remove the received Content-Length field prior to forwarding such
6217 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006218 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006219 * 4. If a message is received without Transfer-Encoding and with
6220 * either multiple Content-Length header fields having differing
6221 * field-values or a single Content-Length header field having an
6222 * invalid value, then the message framing is invalid and the
6223 * recipient MUST treat it as an unrecoverable error. If this is a
6224 * request message, the server MUST respond with a 400 (Bad Request)
6225 * status code and then close the connection. If this is a response
6226 * message received by a proxy, the proxy MUST close the connection
6227 * to the server, discard the received response, and send a 502 (Bad
6228 * Gateway) response to the client. If this is a response message
6229 * received by a user agent, the user agent MUST close the
6230 * connection to the server and discard the received response.
6231 *
6232 * 5. If a valid Content-Length header field is present without
6233 * Transfer-Encoding, its decimal value defines the expected message
6234 * body length in octets. If the sender closes the connection or
6235 * the recipient times out before the indicated number of octets are
6236 * received, the recipient MUST consider the message to be
6237 * incomplete and close the connection.
6238 *
6239 * 6. If this is a request message and none of the above are true, then
6240 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006241 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006242 * 7. Otherwise, this is a response message without a declared message
6243 * body length, so the message body length is determined by the
6244 * number of octets received prior to the server closing the
6245 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006246 */
6247
6248 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006249 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006250 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006251 * FIXME: should we parse anyway and return an error on chunked encoding ?
6252 */
6253 if (txn->meth == HTTP_METH_HEAD ||
6254 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006255 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006256 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006257 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006258 goto skip_content_length;
6259 }
6260
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006261 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006262 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006263 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006264 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006265 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6266 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006267 /* bad transfer-encoding (chunked followed by something else) */
6268 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006269 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006270 break;
6271 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006272 }
6273
Willy Tarreau1c913912015-04-30 10:57:51 +02006274 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006275 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006276 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006277 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6278 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6279 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006280 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006281 signed long long cl;
6282
Willy Tarreauad14f752011-09-02 20:33:27 +02006283 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006284 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006285 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006286 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006287
Willy Tarreauad14f752011-09-02 20:33:27 +02006288 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006289 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006290 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006291 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006292
Willy Tarreauad14f752011-09-02 20:33:27 +02006293 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006294 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006295 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006296 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006297
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006298 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006299 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006300 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006301 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006302
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006303 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006304 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006305 }
6306
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006307 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006308 select_compression_response_header(s, rep->buf);
6309
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006310skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006311 /* Now we have to check if we need to modify the Connection header.
6312 * This is more difficult on the response than it is on the request,
6313 * because we can have two different HTTP versions and we don't know
6314 * how the client will interprete a response. For instance, let's say
6315 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6316 * HTTP/1.1 response without any header. Maybe it will bound itself to
6317 * HTTP/1.0 because it only knows about it, and will consider the lack
6318 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6319 * the lack of header as a keep-alive. Thus we will use two flags
6320 * indicating how a request MAY be understood by the client. In case
6321 * of multiple possibilities, we'll fix the header to be explicit. If
6322 * ambiguous cases such as both close and keepalive are seen, then we
6323 * will fall back to explicit close. Note that we won't take risks with
6324 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006325 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006326 */
6327
Willy Tarreaudc008c52010-02-01 16:20:08 +01006328 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6329 txn->status == 101)) {
6330 /* Either we've established an explicit tunnel, or we're
6331 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006332 * to understand the next protocols. We have to switch to tunnel
6333 * mode, so that we transfer the request and responses then let
6334 * this protocol pass unmodified. When we later implement specific
6335 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006336 * header which contains information about that protocol for
6337 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006338 */
6339 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6340 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006341 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6342 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006343 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006344 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006345 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006346
Willy Tarreau70dffda2014-01-30 03:07:23 +01006347 /* this situation happens when combining pretend-keepalive with httpclose. */
6348 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006349 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006350 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006351 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6352
Willy Tarreau60466522010-01-18 19:08:45 +01006353 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006354 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006355 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6356 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006357
Willy Tarreau60466522010-01-18 19:08:45 +01006358 /* now adjust header transformations depending on current state */
6359 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6360 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6361 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006362 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006363 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006364 }
Willy Tarreau60466522010-01-18 19:08:45 +01006365 else { /* SCL / KAL */
6366 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006367 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006368 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006369 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006370
Willy Tarreau60466522010-01-18 19:08:45 +01006371 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006372 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006373
Willy Tarreau60466522010-01-18 19:08:45 +01006374 /* Some keep-alive responses are converted to Server-close if
6375 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006376 */
Willy Tarreau60466522010-01-18 19:08:45 +01006377 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6378 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006379 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006380 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006381 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006382 }
6383
Willy Tarreau7959a552013-09-23 16:44:27 +02006384 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006385 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006386
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006387 /* end of job, return OK */
6388 rep->analysers &= ~an_bit;
6389 rep->analyse_exp = TICK_ETERNITY;
6390 channel_auto_close(rep);
6391 return 1;
6392
6393 abort_keep_alive:
6394 /* A keep-alive request to the server failed on a network error.
6395 * The client is required to retry. We need to close without returning
6396 * any other information so that the client retries.
6397 */
6398 txn->status = 0;
6399 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006400 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006401 channel_auto_close(rep);
6402 s->logs.logwait = 0;
6403 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006404 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006405 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006406 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006407 return 0;
6408}
6409
6410/* This function performs all the processing enabled for the current response.
6411 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006412 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006413 * other functions. It works like process_request (see indications above).
6414 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006415int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006416{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006417 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006418 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006419 struct http_msg *msg = &txn->rsp;
6420 struct proxy *cur_proxy;
6421 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006422 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006423
Willy Tarreau87b09662015-04-03 00:22:06 +02006424 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 +02006425 now_ms, __FUNCTION__,
6426 s,
6427 rep,
6428 rep->rex, rep->wex,
6429 rep->flags,
6430 rep->buf->i,
6431 rep->analysers);
6432
6433 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6434 return 0;
6435
Willy Tarreau70730dd2014-04-24 18:06:27 +02006436 /* The stats applet needs to adjust the Connection header but we don't
6437 * apply any filter there.
6438 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006439 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6440 rep->analysers &= ~an_bit;
6441 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006442 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006443 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006444
Willy Tarreau58975672014-04-24 21:13:57 +02006445 /*
6446 * We will have to evaluate the filters.
6447 * As opposed to version 1.2, now they will be evaluated in the
6448 * filters order and not in the header order. This means that
6449 * each filter has to be validated among all headers.
6450 *
6451 * Filters are tried with ->be first, then with ->fe if it is
6452 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006453 *
6454 * Maybe we are in resume condiion. In this case I choose the
6455 * "struct proxy" which contains the rule list matching the resume
6456 * pointer. If none of theses "struct proxy" match, I initialise
6457 * the process with the first one.
6458 *
6459 * In fact, I check only correspondance betwwen the current list
6460 * pointer and the ->fe rule list. If it doesn't match, I initialize
6461 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006462 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006463 if (s->current_rule_list == &sess->fe->http_res_rules)
6464 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006465 else
6466 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006467 while (1) {
6468 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006469
Willy Tarreau58975672014-04-24 21:13:57 +02006470 /* evaluate http-response rules */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006471 if (ret == HTTP_RULE_RES_CONT)
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006472 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006473
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006474 /* we need to be called again. */
6475 if (ret == HTTP_RULE_RES_YIELD) {
6476 channel_dont_close(rep);
6477 return 0;
6478 }
6479
Willy Tarreau58975672014-04-24 21:13:57 +02006480 /* try headers filters */
6481 if (rule_set->rsp_exp != NULL) {
6482 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6483 return_bad_resp:
6484 if (objt_server(s->target)) {
6485 objt_server(s->target)->counters.failed_resp++;
6486 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006487 }
Willy Tarreau58975672014-04-24 21:13:57 +02006488 s->be->be_counters.failed_resp++;
6489 return_srv_prx_502:
6490 rep->analysers = 0;
6491 txn->status = 502;
6492 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006493 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006494 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006495 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006496 if (!(s->flags & SF_ERR_MASK))
6497 s->flags |= SF_ERR_PRXCOND;
6498 if (!(s->flags & SF_FINST_MASK))
6499 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006500 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006501 }
Willy Tarreau58975672014-04-24 21:13:57 +02006502 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006503
Willy Tarreau58975672014-04-24 21:13:57 +02006504 /* has the response been denied ? */
6505 if (txn->flags & TX_SVDENY) {
6506 if (objt_server(s->target))
6507 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006508
Willy Tarreau58975672014-04-24 21:13:57 +02006509 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006510 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006511 if (sess->listener->counters)
6512 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006513
Willy Tarreau58975672014-04-24 21:13:57 +02006514 goto return_srv_prx_502;
6515 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006516
Willy Tarreau58975672014-04-24 21:13:57 +02006517 /* add response headers from the rule sets in the same order */
6518 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006519 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006520 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006521 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006522 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006523 ret = acl_pass(ret);
6524 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6525 ret = !ret;
6526 if (!ret)
6527 continue;
6528 }
6529 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6530 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006531 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006532
Willy Tarreau58975672014-04-24 21:13:57 +02006533 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006534 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006535 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006536 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006537 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006538
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006539 /* After this point, this anayzer can't return yield, so we can
6540 * remove the bit corresponding to this analyzer from the list.
6541 *
6542 * Note that the intermediate returns and goto found previously
6543 * reset the analyzers.
6544 */
6545 rep->analysers &= ~an_bit;
6546 rep->analyse_exp = TICK_ETERNITY;
6547
Willy Tarreau58975672014-04-24 21:13:57 +02006548 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006549 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006550 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006551
Willy Tarreau58975672014-04-24 21:13:57 +02006552 /*
6553 * Now check for a server cookie.
6554 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006555 if (s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name ||
Willy Tarreau58975672014-04-24 21:13:57 +02006556 (s->be->options & PR_O_CHK_CACHE))
6557 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006558
Willy Tarreau58975672014-04-24 21:13:57 +02006559 /*
6560 * Check for cache-control or pragma headers if required.
6561 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006562 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 +02006563 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006564
Willy Tarreau58975672014-04-24 21:13:57 +02006565 /*
6566 * Add server cookie in the response if needed
6567 */
6568 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6569 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006570 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006571 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6572 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6573 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6574 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6575 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006576 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006577 /* the server is known, it's not the one the client requested, or the
6578 * cookie's last seen date needs to be refreshed. We have to
6579 * insert a set-cookie here, except if we want to insert only on POST
6580 * requests and this one isn't. Note that servers which don't have cookies
6581 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006582 */
Willy Tarreau58975672014-04-24 21:13:57 +02006583 if (!objt_server(s->target)->cookie) {
6584 chunk_printf(&trash,
6585 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6586 s->be->cookie_name);
6587 }
6588 else {
6589 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006590
Willy Tarreau58975672014-04-24 21:13:57 +02006591 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6592 /* emit last_date, which is mandatory */
6593 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6594 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6595 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006596
Willy Tarreau58975672014-04-24 21:13:57 +02006597 if (s->be->cookie_maxlife) {
6598 /* emit first_date, which is either the original one or
6599 * the current date.
6600 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006601 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006602 s30tob64(txn->cookie_first_date ?
6603 txn->cookie_first_date >> 2 :
6604 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006605 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006606 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006607 }
Willy Tarreau58975672014-04-24 21:13:57 +02006608 chunk_appendf(&trash, "; path=/");
6609 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006610
Willy Tarreau58975672014-04-24 21:13:57 +02006611 if (s->be->cookie_domain)
6612 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006613
Willy Tarreau58975672014-04-24 21:13:57 +02006614 if (s->be->ck_opts & PR_CK_HTTPONLY)
6615 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006616
Willy Tarreau58975672014-04-24 21:13:57 +02006617 if (s->be->ck_opts & PR_CK_SECURE)
6618 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006619
Willy Tarreau58975672014-04-24 21:13:57 +02006620 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6621 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006622
Willy Tarreau58975672014-04-24 21:13:57 +02006623 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006624 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006625 /* the server did not change, only the date was updated */
6626 txn->flags |= TX_SCK_UPDATED;
6627 else
6628 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006629
Willy Tarreau58975672014-04-24 21:13:57 +02006630 /* Here, we will tell an eventual cache on the client side that we don't
6631 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6632 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6633 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006634 */
Willy Tarreau58975672014-04-24 21:13:57 +02006635 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006636
Willy Tarreau58975672014-04-24 21:13:57 +02006637 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006638
Willy Tarreau58975672014-04-24 21:13:57 +02006639 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6640 "Cache-control: private", 22) < 0))
6641 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006642 }
Willy Tarreau58975672014-04-24 21:13:57 +02006643 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006644
Willy Tarreau58975672014-04-24 21:13:57 +02006645 /*
6646 * Check if result will be cacheable with a cookie.
6647 * We'll block the response if security checks have caught
6648 * nasty things such as a cacheable cookie.
6649 */
6650 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6651 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6652 (s->be->options & PR_O_CHK_CACHE)) {
6653 /* we're in presence of a cacheable response containing
6654 * a set-cookie header. We'll block it as requested by
6655 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006656 */
Willy Tarreau58975672014-04-24 21:13:57 +02006657 if (objt_server(s->target))
6658 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006659
Willy Tarreau58975672014-04-24 21:13:57 +02006660 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006661 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006662 if (sess->listener->counters)
6663 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006664
Willy Tarreau58975672014-04-24 21:13:57 +02006665 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6666 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6667 send_log(s->be, LOG_ALERT,
6668 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6669 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6670 goto return_srv_prx_502;
6671 }
Willy Tarreau03945942009-12-22 16:50:27 +01006672
Willy Tarreau70730dd2014-04-24 18:06:27 +02006673 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006674 /*
6675 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6676 * If an "Upgrade" token is found, the header is left untouched in order
6677 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006678 * if anything but "Upgrade" is present in the Connection header. We don't
6679 * want to touch any 101 response either since it's switching to another
6680 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006681 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006682 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006683 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006684 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006685 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6686 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006687
Willy Tarreau58975672014-04-24 21:13:57 +02006688 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6689 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6690 /* we want a keep-alive response here. Keep-alive header
6691 * required if either side is not 1.1.
6692 */
6693 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6694 want_flags |= TX_CON_KAL_SET;
6695 }
6696 else {
6697 /* we want a close response here. Close header required if
6698 * the server is 1.1, regardless of the client.
6699 */
6700 if (msg->flags & HTTP_MSGF_VER_11)
6701 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006702 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006703
Willy Tarreau58975672014-04-24 21:13:57 +02006704 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6705 http_change_connection_header(txn, msg, want_flags);
6706 }
6707
6708 skip_header_mangling:
6709 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6710 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6711 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006712
Willy Tarreau58975672014-04-24 21:13:57 +02006713 /* if the user wants to log as soon as possible, without counting
6714 * bytes from the server, then this is the right moment. We have
6715 * to temporarily assign bytes_out to log what we currently have.
6716 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006717 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006718 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6719 s->logs.bytes_out = txn->rsp.eoh;
6720 s->do_log(s);
6721 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006722 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006723 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006724}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006725
Willy Tarreaud98cf932009-12-27 22:54:55 +01006726/* This function is an analyser which forwards response body (including chunk
6727 * sizes if any). It is called as soon as we must forward, even if we forward
6728 * zero byte. The only situation where it must not be called is when we're in
6729 * tunnel mode and we want to forward till the close. It's used both to forward
6730 * remaining data and to resync after end of body. It expects the msg_state to
6731 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006732 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006733 *
6734 * It is capable of compressing response data both in content-length mode and
6735 * in chunked mode. The state machines follows different flows depending on
6736 * whether content-length and chunked modes are used, since there are no
6737 * trailers in content-length :
6738 *
6739 * chk-mode cl-mode
6740 * ,----- BODY -----.
6741 * / \
6742 * V size > 0 V chk-mode
6743 * .--> SIZE -------------> DATA -------------> CRLF
6744 * | | size == 0 | last byte |
6745 * | v final crlf v inspected |
6746 * | TRAILERS -----------> DONE |
6747 * | |
6748 * `----------------------------------------------'
6749 *
6750 * Compression only happens in the DATA state, and must be flushed in final
6751 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6752 * is performed at once on final states for all bytes parsed, or when leaving
6753 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006754 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006755int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006756{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006757 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006758 struct http_txn *txn = s->txn;
6759 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006760 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006761 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006762 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006763
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006764 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6765 return 0;
6766
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006767 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006768 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006769 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006770 /* Output closed while we were sending data. We must abort and
6771 * wake the other side up.
6772 */
6773 msg->msg_state = HTTP_MSG_ERROR;
6774 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006775 return 1;
6776 }
6777
Willy Tarreau4fe41902010-06-07 22:27:41 +02006778 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006779 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006780
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006781 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006782 /* we have msg->sov which points to the first byte of message
6783 * body, and res->buf.p still points to the beginning of the
6784 * message. We forward the headers now, as we don't need them
6785 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006786 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006787 b_adv(res->buf, msg->sov);
6788 msg->next -= msg->sov;
6789 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006790
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006791 /* The previous analysers guarantee that the state is somewhere
6792 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6793 * msg->next are always correct.
6794 */
6795 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6796 if (msg->flags & HTTP_MSGF_TE_CHNK)
6797 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6798 else
6799 msg->msg_state = HTTP_MSG_DATA;
6800 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006801 }
6802
Willy Tarreauefdf0942014-04-24 20:08:57 +02006803 if (res->to_forward) {
6804 /* We can't process the buffer's contents yet */
6805 res->flags |= CF_WAKE_WRITE;
6806 goto missing_data;
6807 }
6808
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006809 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6810 /* We need a compression buffer in the DATA state to put the
6811 * output of compressed data, and in CRLF state to let the
6812 * TRAILERS state finish the job of removing the trailing CRLF.
6813 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006814 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006815 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006816 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006817 goto aborted_xfer; /* no memory */
6818 }
6819
6820 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006821 if (ret < 0) {
6822 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006823 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006824 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006825 compressing = 1;
6826 }
6827
Willy Tarreaud98cf932009-12-27 22:54:55 +01006828 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006829 switch (msg->msg_state - HTTP_MSG_DATA) {
6830 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006831 /* we may have some pending data starting at res->buf->p */
6832 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006833 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006834 if (ret < 0)
6835 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006836
Willy Tarreaud5a67832014-04-21 10:54:27 +02006837 if (msg->chunk_len) {
6838 /* input empty or output full */
6839 if (res->buf->i > msg->next)
6840 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006841 goto missing_data;
6842 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006843 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006844 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006845 if (msg->chunk_len > res->buf->i - msg->next) {
6846 /* output full */
6847 res->flags |= CF_WAKE_WRITE;
6848 goto missing_data;
6849 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006850 msg->next += msg->chunk_len;
6851 msg->chunk_len = 0;
6852 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006853
6854 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006855 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006856 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006857 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006858 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006859 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006860 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006861 /* fall through for HTTP_MSG_CHUNK_CRLF */
6862
6863 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6864 /* we want the CRLF after the data */
6865
6866 ret = http_skip_chunk_crlf(msg);
6867 if (ret == 0)
6868 goto missing_data;
6869 else if (ret < 0) {
6870 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006871 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006872 goto return_bad_res;
6873 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006874 /* we're in MSG_CHUNK_SIZE now, fall through */
6875
6876 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006877 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006878 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006879 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006880 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006881
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006882 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006883 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006884 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006885 else if (ret < 0) {
6886 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006887 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006888 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006889 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006890 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006891 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006892
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006893 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006894 if (unlikely(compressing)) {
6895 /* we need to flush output contents before syncing FSMs */
6896 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6897 compressing = 0;
6898 }
6899
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006900 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006901 if (ret == 0)
6902 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006903 else if (ret < 0) {
6904 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006905 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006906 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006907 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006908 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006909
6910 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006911 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006912 if (unlikely(compressing)) {
6913 /* we need to flush output contents before syncing FSMs */
6914 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6915 compressing = 0;
6916 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006917
Willy Tarreauc623c172014-04-18 09:53:50 +02006918 /* we may have some pending data starting at res->buf->p
6919 * such as a last chunk of data or trailers.
6920 */
6921 b_adv(res->buf, msg->next);
6922 msg->next = 0;
6923
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006924 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006925 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006926 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6927 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006928 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006929
Willy Tarreau610ecce2010-01-04 21:15:02 +01006930 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006931 /* some state changes occurred, maybe the analyser
6932 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006933 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006934 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006935 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006936 /* response errors are most likely due to
6937 * the client aborting the transfer.
6938 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006939 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006940 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006941 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006942 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006943 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006944 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006945 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006946 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006947 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006948 }
6949 }
6950
Willy Tarreaud98cf932009-12-27 22:54:55 +01006951 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006952 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006953 if (unlikely(compressing)) {
6954 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006955 compressing = 0;
6956 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006957
Willy Tarreauc623c172014-04-18 09:53:50 +02006958 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6959 b_adv(res->buf, msg->next);
6960 msg->next = 0;
6961 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6962 }
6963
Willy Tarreauf003d372012-11-26 13:35:37 +01006964 if (res->flags & CF_SHUTW)
6965 goto aborted_xfer;
6966
6967 /* stop waiting for data if the input is closed before the end. If the
6968 * client side was already closed, it means that the client has aborted,
6969 * so we don't want to count this as a server abort. Otherwise it's a
6970 * server abort.
6971 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006972 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006973 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006974 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006975 if (!(s->flags & SF_ERR_MASK))
6976 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006977 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006978 if (objt_server(s->target))
6979 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006980 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006981 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006982
Willy Tarreau40dba092010-03-04 18:14:51 +01006983 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006984 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006985 goto return_bad_res;
6986
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006987 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006988 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006989 * Similarly, with keep-alive on the client side, we don't want to forward a
6990 * close.
6991 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006992 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006993 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6994 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006995 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006996
Willy Tarreau5c620922011-05-11 19:56:11 +02006997 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006998 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006999 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007000 * modes are already handled by the stream sock layer. We must not do
7001 * this in content-length mode because it could present the MSG_MORE
7002 * flag with the last block of forwarded data, which would cause an
7003 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007004 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007005 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007006 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007007
Willy Tarreau87b09662015-04-03 00:22:06 +02007008 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007009 return 0;
7010
Willy Tarreau40dba092010-03-04 18:14:51 +01007011 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007012 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007013 if (objt_server(s->target))
7014 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007015
7016 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007017 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007018 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007019 compressing = 0;
7020 }
7021
Willy Tarreauc623c172014-04-18 09:53:50 +02007022 /* we may have some pending data starting at res->buf->p */
7023 if (s->comp_algo == NULL) {
7024 b_adv(res->buf, msg->next);
7025 msg->next = 0;
7026 }
7027
Willy Tarreaud98cf932009-12-27 22:54:55 +01007028 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007029 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007030 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007031 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007032 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007033 if (objt_server(s->target))
7034 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007035
Willy Tarreaue7dff022015-04-03 01:14:29 +02007036 if (!(s->flags & SF_ERR_MASK))
7037 s->flags |= SF_ERR_PRXCOND;
7038 if (!(s->flags & SF_FINST_MASK))
7039 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007040 return 0;
7041
7042 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007043 if (unlikely(compressing)) {
7044 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7045 compressing = 0;
7046 }
7047
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007048 txn->rsp.msg_state = HTTP_MSG_ERROR;
7049 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007050 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007051 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007052 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007053
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007054 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007055 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007056 if (objt_server(s->target))
7057 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007058
Willy Tarreaue7dff022015-04-03 01:14:29 +02007059 if (!(s->flags & SF_ERR_MASK))
7060 s->flags |= SF_ERR_CLICL;
7061 if (!(s->flags & SF_FINST_MASK))
7062 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007063 return 0;
7064}
7065
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007066/* Iterate the same filter through all request headers.
7067 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007068 * Since it can manage the switch to another backend, it updates the per-proxy
7069 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007070 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007071int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007072{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007073 char *cur_ptr, *cur_end, *cur_next;
7074 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007075 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007076 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007077 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007079 last_hdr = 0;
7080
Willy Tarreau9b28e032012-10-12 23:49:43 +02007081 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007082 old_idx = 0;
7083
7084 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007085 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007086 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007087 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007088 (exp->action == ACT_ALLOW ||
7089 exp->action == ACT_DENY ||
7090 exp->action == ACT_TARPIT))
7091 return 0;
7092
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007093 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007094 if (!cur_idx)
7095 break;
7096
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007097 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007098 cur_ptr = cur_next;
7099 cur_end = cur_ptr + cur_hdr->len;
7100 cur_next = cur_end + cur_hdr->cr + 1;
7101
7102 /* Now we have one header between cur_ptr and cur_end,
7103 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007104 */
7105
Willy Tarreau15a53a42015-01-21 13:39:42 +01007106 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007107 switch (exp->action) {
7108 case ACT_SETBE:
7109 /* It is not possible to jump a second time.
7110 * FIXME: should we return an HTTP/500 here so that
7111 * the admin knows there's a problem ?
7112 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007113 if (s->be != strm_fe(s))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007114 break;
7115
7116 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02007117 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007118 last_hdr = 1;
7119 break;
7120
7121 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007122 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007123 last_hdr = 1;
7124 break;
7125
7126 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007127 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007128 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007129 break;
7130
7131 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007132 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007133 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007134 break;
7135
7136 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007137 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7138 if (trash.len < 0)
7139 return -1;
7140
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007141 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007142 /* FIXME: if the user adds a newline in the replacement, the
7143 * index will not be recalculated for now, and the new line
7144 * will not be counted as a new header.
7145 */
7146
7147 cur_end += delta;
7148 cur_next += delta;
7149 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007150 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007151 break;
7152
7153 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007154 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007155 cur_next += delta;
7156
Willy Tarreaufa355d42009-11-29 18:12:29 +01007157 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007158 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7159 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007160 cur_hdr->len = 0;
7161 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007162 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007163 break;
7164
7165 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007166 }
7167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007168 /* keep the link from this header to next one in case of later
7169 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007170 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007171 old_idx = cur_idx;
7172 }
7173 return 0;
7174}
7175
7176
7177/* Apply the filter to the request line.
7178 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7179 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007180 * Since it can manage the switch to another backend, it updates the per-proxy
7181 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007182 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007183int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007184{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007185 char *cur_ptr, *cur_end;
7186 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007187 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007188 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007189
Willy Tarreau3d300592007-03-18 18:34:41 +01007190 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007191 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007192 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007193 (exp->action == ACT_ALLOW ||
7194 exp->action == ACT_DENY ||
7195 exp->action == ACT_TARPIT))
7196 return 0;
7197 else if (exp->action == ACT_REMOVE)
7198 return 0;
7199
7200 done = 0;
7201
Willy Tarreau9b28e032012-10-12 23:49:43 +02007202 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007203 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007204
7205 /* Now we have the request line between cur_ptr and cur_end */
7206
Willy Tarreau15a53a42015-01-21 13:39:42 +01007207 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007208 switch (exp->action) {
7209 case ACT_SETBE:
7210 /* It is not possible to jump a second time.
7211 * FIXME: should we return an HTTP/500 here so that
7212 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007213 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007214 if (s->be != strm_fe(s))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007215 break;
7216
7217 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02007218 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007219 done = 1;
7220 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007221
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007222 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007223 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007224 done = 1;
7225 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007226
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007227 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007228 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007229 done = 1;
7230 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007231
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007232 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007233 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007234 done = 1;
7235 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007236
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007237 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007238 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7239 if (trash.len < 0)
7240 return -1;
7241
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007242 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007243 /* FIXME: if the user adds a newline in the replacement, the
7244 * index will not be recalculated for now, and the new line
7245 * will not be counted as a new header.
7246 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007247
Willy Tarreaufa355d42009-11-29 18:12:29 +01007248 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007249 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007250 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007251 HTTP_MSG_RQMETH,
7252 cur_ptr, cur_end + 1,
7253 NULL, NULL);
7254 if (unlikely(!cur_end))
7255 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007256
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007257 /* we have a full request and we know that we have either a CR
7258 * or an LF at <ptr>.
7259 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007260 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7261 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007262 /* there is no point trying this regex on headers */
7263 return 1;
7264 }
7265 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007266 return done;
7267}
Willy Tarreau97de6242006-12-27 17:18:38 +01007268
Willy Tarreau58f10d72006-12-04 02:26:12 +01007269
Willy Tarreau58f10d72006-12-04 02:26:12 +01007270
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007272 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007273 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007274 * unparsable request. Since it can manage the switch to another backend, it
7275 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007276 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007277int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007278{
Willy Tarreau192252e2015-04-04 01:47:55 +02007279 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007280 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007281 struct hdr_exp *exp;
7282
7283 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007285
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007286 /*
7287 * The interleaving of transformations and verdicts
7288 * makes it difficult to decide to continue or stop
7289 * the evaluation.
7290 */
7291
Willy Tarreau6c123b12010-01-28 20:22:06 +01007292 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7293 break;
7294
Willy Tarreau3d300592007-03-18 18:34:41 +01007295 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007296 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007297 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007298 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007299
7300 /* if this filter had a condition, evaluate it now and skip to
7301 * next filter if the condition does not match.
7302 */
7303 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007304 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007305 ret = acl_pass(ret);
7306 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7307 ret = !ret;
7308
7309 if (!ret)
7310 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007311 }
7312
7313 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007314 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007315 if (unlikely(ret < 0))
7316 return -1;
7317
7318 if (likely(ret == 0)) {
7319 /* The filter did not match the request, it can be
7320 * iterated through all headers.
7321 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007322 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7323 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007324 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007325 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007326 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007327}
7328
7329
Willy Tarreaua15645d2007-03-18 16:22:39 +01007330
Willy Tarreau58f10d72006-12-04 02:26:12 +01007331/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007332 * Try to retrieve the server associated to the appsession.
Willy Tarreau87b09662015-04-03 00:22:06 +02007333 * If the server is found, it's assigned to the stream.
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007334 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007335void manage_client_side_appsession(struct stream *s, const char *buf, int len) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02007336 struct http_txn *txn = s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007337 appsess *asession = NULL;
7338 char *sessid_temp = NULL;
7339
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007340 if (len > s->be->appsession_len) {
7341 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007342 }
7343
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007344 if (s->be->options2 & PR_O2_AS_REQL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007345 /* request-learn option is enabled : store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007346 if (txn->sessid != NULL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007347 /* free previously allocated memory as we don't need the stream id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007348 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007349 }
7350
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007351 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007352 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007353 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007354 return;
7355 }
7356
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007357 memcpy(txn->sessid, buf, len);
7358 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007359 }
7360
7361 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7362 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007363 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007364 return;
7365 }
7366
Cyril Bontéb21570a2009-11-29 20:04:48 +01007367 memcpy(sessid_temp, buf, len);
7368 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007369
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007370 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007371 /* free previously allocated memory */
7372 pool_free2(apools.sessid, sessid_temp);
7373
7374 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007375 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7376 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007377 asession->request_count++;
7378
7379 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007380 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007381
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007382 while (srv) {
7383 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007384 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007385 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007386 (s->flags & SF_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007387 /* we found the server and it's usable */
7388 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007389 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007390 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007391 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007392
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007393 break;
7394 } else {
7395 txn->flags &= ~TX_CK_MASK;
7396 txn->flags |= TX_CK_DOWN;
7397 }
7398 }
7399 srv = srv->next;
7400 }
7401 }
7402 }
7403}
7404
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007405/* Find the end of a cookie value contained between <s> and <e>. It works the
7406 * same way as with headers above except that the semi-colon also ends a token.
7407 * See RFC2965 for more information. Note that it requires a valid header to
7408 * return a valid result.
7409 */
7410char *find_cookie_value_end(char *s, const char *e)
7411{
7412 int quoted, qdpair;
7413
7414 quoted = qdpair = 0;
7415 for (; s < e; s++) {
7416 if (qdpair) qdpair = 0;
7417 else if (quoted) {
7418 if (*s == '\\') qdpair = 1;
7419 else if (*s == '"') quoted = 0;
7420 }
7421 else if (*s == '"') quoted = 1;
7422 else if (*s == ',' || *s == ';') return s;
7423 }
7424 return s;
7425}
7426
7427/* Delete a value in a header between delimiters <from> and <next> in buffer
7428 * <buf>. The number of characters displaced is returned, and the pointer to
7429 * the first delimiter is updated if required. The function tries as much as
7430 * possible to respect the following principles :
7431 * - replace <from> delimiter by the <next> one unless <from> points to a
7432 * colon, in which case <next> is simply removed
7433 * - set exactly one space character after the new first delimiter, unless
7434 * there are not enough characters in the block being moved to do so.
7435 * - remove unneeded spaces before the previous delimiter and after the new
7436 * one.
7437 *
7438 * It is the caller's responsibility to ensure that :
7439 * - <from> points to a valid delimiter or the colon ;
7440 * - <next> points to a valid delimiter or the final CR/LF ;
7441 * - there are non-space chars before <from> ;
7442 * - there is a CR/LF at or after <next>.
7443 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007444int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007445{
7446 char *prev = *from;
7447
7448 if (*prev == ':') {
7449 /* We're removing the first value, preserve the colon and add a
7450 * space if possible.
7451 */
7452 if (!http_is_crlf[(unsigned char)*next])
7453 next++;
7454 prev++;
7455 if (prev < next)
7456 *prev++ = ' ';
7457
7458 while (http_is_spht[(unsigned char)*next])
7459 next++;
7460 } else {
7461 /* Remove useless spaces before the old delimiter. */
7462 while (http_is_spht[(unsigned char)*(prev-1)])
7463 prev--;
7464 *from = prev;
7465
7466 /* copy the delimiter and if possible a space if we're
7467 * not at the end of the line.
7468 */
7469 if (!http_is_crlf[(unsigned char)*next]) {
7470 *prev++ = *next++;
7471 if (prev + 1 < next)
7472 *prev++ = ' ';
7473 while (http_is_spht[(unsigned char)*next])
7474 next++;
7475 }
7476 }
7477 return buffer_replace2(buf, prev, next, NULL, 0);
7478}
7479
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007480/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007481 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007482 * desirable to call it only when needed. This code is quite complex because
7483 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7484 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007485 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007486void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007487{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007488 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007489 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007490 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007491 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007492 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7493 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007494
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007495 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007496 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007497 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007498
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007499 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007500 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007501 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007502
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007503 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007504 hdr_beg = hdr_next;
7505 hdr_end = hdr_beg + cur_hdr->len;
7506 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007507
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007508 /* We have one full header between hdr_beg and hdr_end, and the
7509 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007510 * "Cookie:" headers.
7511 */
7512
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007513 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007514 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007515 old_idx = cur_idx;
7516 continue;
7517 }
7518
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007519 del_from = NULL; /* nothing to be deleted */
7520 preserve_hdr = 0; /* assume we may kill the whole header */
7521
Willy Tarreau58f10d72006-12-04 02:26:12 +01007522 /* Now look for cookies. Conforming to RFC2109, we have to support
7523 * attributes whose name begin with a '$', and associate them with
7524 * the right cookie, if we want to delete this cookie.
7525 * So there are 3 cases for each cookie read :
7526 * 1) it's a special attribute, beginning with a '$' : ignore it.
7527 * 2) it's a server id cookie that we *MAY* want to delete : save
7528 * some pointers on it (last semi-colon, beginning of cookie...)
7529 * 3) it's an application cookie : we *MAY* have to delete a previous
7530 * "special" cookie.
7531 * At the end of loop, if a "special" cookie remains, we may have to
7532 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007533 * *MUST* delete it.
7534 *
7535 * Note: RFC2965 is unclear about the processing of spaces around
7536 * the equal sign in the ATTR=VALUE form. A careful inspection of
7537 * the RFC explicitly allows spaces before it, and not within the
7538 * tokens (attrs or values). An inspection of RFC2109 allows that
7539 * too but section 10.1.3 lets one think that spaces may be allowed
7540 * after the equal sign too, resulting in some (rare) buggy
7541 * implementations trying to do that. So let's do what servers do.
7542 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7543 * allowed quoted strings in values, with any possible character
7544 * after a backslash, including control chars and delimitors, which
7545 * causes parsing to become ambiguous. Browsers also allow spaces
7546 * within values even without quotes.
7547 *
7548 * We have to keep multiple pointers in order to support cookie
7549 * removal at the beginning, middle or end of header without
7550 * corrupting the header. All of these headers are valid :
7551 *
7552 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7553 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7554 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7555 * | | | | | | | | |
7556 * | | | | | | | | hdr_end <--+
7557 * | | | | | | | +--> next
7558 * | | | | | | +----> val_end
7559 * | | | | | +-----------> val_beg
7560 * | | | | +--------------> equal
7561 * | | | +----------------> att_end
7562 * | | +---------------------> att_beg
7563 * | +--------------------------> prev
7564 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007565 */
7566
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007567 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7568 /* Iterate through all cookies on this line */
7569
7570 /* find att_beg */
7571 att_beg = prev + 1;
7572 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7573 att_beg++;
7574
7575 /* find att_end : this is the first character after the last non
7576 * space before the equal. It may be equal to hdr_end.
7577 */
7578 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007579
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007580 while (equal < hdr_end) {
7581 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007582 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007583 if (http_is_spht[(unsigned char)*equal++])
7584 continue;
7585 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007586 }
7587
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007588 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7589 * is between <att_beg> and <equal>, both may be identical.
7590 */
7591
7592 /* look for end of cookie if there is an equal sign */
7593 if (equal < hdr_end && *equal == '=') {
7594 /* look for the beginning of the value */
7595 val_beg = equal + 1;
7596 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7597 val_beg++;
7598
7599 /* find the end of the value, respecting quotes */
7600 next = find_cookie_value_end(val_beg, hdr_end);
7601
7602 /* make val_end point to the first white space or delimitor after the value */
7603 val_end = next;
7604 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7605 val_end--;
7606 } else {
7607 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007608 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007609
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007610 /* We have nothing to do with attributes beginning with '$'. However,
7611 * they will automatically be removed if a header before them is removed,
7612 * since they're supposed to be linked together.
7613 */
7614 if (*att_beg == '$')
7615 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007616
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007617 /* Ignore cookies with no equal sign */
7618 if (equal == next) {
7619 /* This is not our cookie, so we must preserve it. But if we already
7620 * scheduled another cookie for removal, we cannot remove the
7621 * complete header, but we can remove the previous block itself.
7622 */
7623 preserve_hdr = 1;
7624 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007625 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007626 val_end += delta;
7627 next += delta;
7628 hdr_end += delta;
7629 hdr_next += delta;
7630 cur_hdr->len += delta;
7631 http_msg_move_end(&txn->req, delta);
7632 prev = del_from;
7633 del_from = NULL;
7634 }
7635 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007636 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007637
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007638 /* if there are spaces around the equal sign, we need to
7639 * strip them otherwise we'll get trouble for cookie captures,
7640 * or even for rewrites. Since this happens extremely rarely,
7641 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007642 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007643 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7644 int stripped_before = 0;
7645 int stripped_after = 0;
7646
7647 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007648 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007649 equal += stripped_before;
7650 val_beg += stripped_before;
7651 }
7652
7653 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007654 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007655 val_beg += stripped_after;
7656 stripped_before += stripped_after;
7657 }
7658
7659 val_end += stripped_before;
7660 next += stripped_before;
7661 hdr_end += stripped_before;
7662 hdr_next += stripped_before;
7663 cur_hdr->len += stripped_before;
7664 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007665 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007666 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007667
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007668 /* First, let's see if we want to capture this cookie. We check
7669 * that we don't already have a client side cookie, because we
7670 * can only capture one. Also as an optimisation, we ignore
7671 * cookies shorter than the declared name.
7672 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007673 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7674 (val_end - att_beg >= sess->fe->capture_namelen) &&
7675 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007676 int log_len = val_end - att_beg;
7677
7678 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7679 Alert("HTTP logging : out of memory.\n");
7680 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007681 if (log_len > sess->fe->capture_len)
7682 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007683 memcpy(txn->cli_cookie, att_beg, log_len);
7684 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007685 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007686 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007687
Willy Tarreaubca99692010-10-06 19:25:55 +02007688 /* Persistence cookies in passive, rewrite or insert mode have the
7689 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007690 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007691 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007692 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007693 * For cookies in prefix mode, the form is :
7694 *
7695 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007696 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007697 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7698 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7699 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007700 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007701
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007702 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7703 * have the server ID between val_beg and delim, and the original cookie between
7704 * delim+1 and val_end. Otherwise, delim==val_end :
7705 *
7706 * Cookie: NAME=SRV; # in all but prefix modes
7707 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7708 * | || || | |+-> next
7709 * | || || | +--> val_end
7710 * | || || +---------> delim
7711 * | || |+------------> val_beg
7712 * | || +-------------> att_end = equal
7713 * | |+-----------------> att_beg
7714 * | +------------------> prev
7715 * +-------------------------> hdr_beg
7716 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007717
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007718 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007719 for (delim = val_beg; delim < val_end; delim++)
7720 if (*delim == COOKIE_DELIM)
7721 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007722 } else {
7723 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007724 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007725 /* Now check if the cookie contains a date field, which would
7726 * appear after a vertical bar ('|') just after the server name
7727 * and before the delimiter.
7728 */
7729 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7730 if (vbar1) {
7731 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007732 * right is the last seen date. It is a base64 encoded
7733 * 30-bit value representing the UNIX date since the
7734 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007735 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007736 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007737 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007738 if (val_end - vbar1 >= 5) {
7739 val = b64tos30(vbar1);
7740 if (val > 0)
7741 txn->cookie_last_date = val << 2;
7742 }
7743 /* look for a second vertical bar */
7744 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7745 if (vbar1 && (val_end - vbar1 > 5)) {
7746 val = b64tos30(vbar1 + 1);
7747 if (val > 0)
7748 txn->cookie_first_date = val << 2;
7749 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007750 }
7751 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007752
Willy Tarreauf64d1412010-10-07 20:06:11 +02007753 /* if the cookie has an expiration date and the proxy wants to check
7754 * it, then we do that now. We first check if the cookie is too old,
7755 * then only if it has expired. We detect strict overflow because the
7756 * time resolution here is not great (4 seconds). Cookies with dates
7757 * in the future are ignored if their offset is beyond one day. This
7758 * allows an admin to fix timezone issues without expiring everyone
7759 * and at the same time avoids keeping unwanted side effects for too
7760 * long.
7761 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007762 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7763 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007764 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007765 txn->flags &= ~TX_CK_MASK;
7766 txn->flags |= TX_CK_OLD;
7767 delim = val_beg; // let's pretend we have not found the cookie
7768 txn->cookie_first_date = 0;
7769 txn->cookie_last_date = 0;
7770 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007771 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7772 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007773 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007774 txn->flags &= ~TX_CK_MASK;
7775 txn->flags |= TX_CK_EXPIRED;
7776 delim = val_beg; // let's pretend we have not found the cookie
7777 txn->cookie_first_date = 0;
7778 txn->cookie_last_date = 0;
7779 }
7780
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007781 /* Here, we'll look for the first running server which supports the cookie.
7782 * This allows to share a same cookie between several servers, for example
7783 * to dedicate backup servers to specific servers only.
7784 * However, to prevent clients from sticking to cookie-less backup server
7785 * when they have incidentely learned an empty cookie, we simply ignore
7786 * empty cookies and mark them as invalid.
7787 * The same behaviour is applied when persistence must be ignored.
7788 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007789 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007790 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007791
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007792 while (srv) {
7793 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7794 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007795 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007796 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007797 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007798 /* we found the server and we can use it */
7799 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007800 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007801 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007802 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007803 break;
7804 } else {
7805 /* we found a server, but it's down,
7806 * mark it as such and go on in case
7807 * another one is available.
7808 */
7809 txn->flags &= ~TX_CK_MASK;
7810 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007811 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007812 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007813 srv = srv->next;
7814 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007815
Willy Tarreauf64d1412010-10-07 20:06:11 +02007816 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007817 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007818 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007819 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007820 txn->flags |= TX_CK_UNUSED;
7821 else
7822 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007823 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007824
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007825 /* depending on the cookie mode, we may have to either :
7826 * - delete the complete cookie if we're in insert+indirect mode, so that
7827 * the server never sees it ;
7828 * - remove the server id from the cookie value, and tag the cookie as an
7829 * application cookie so that it does not get accidentely removed later,
7830 * if we're in cookie prefix mode
7831 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007832 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007833 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007834
Willy Tarreau9b28e032012-10-12 23:49:43 +02007835 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007836 val_end += delta;
7837 next += delta;
7838 hdr_end += delta;
7839 hdr_next += delta;
7840 cur_hdr->len += delta;
7841 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007842
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007843 del_from = NULL;
7844 preserve_hdr = 1; /* we want to keep this cookie */
7845 }
7846 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007847 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007848 del_from = prev;
7849 }
7850 } else {
7851 /* This is not our cookie, so we must preserve it. But if we already
7852 * scheduled another cookie for removal, we cannot remove the
7853 * complete header, but we can remove the previous block itself.
7854 */
7855 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007856
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007857 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007858 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007859 if (att_beg >= del_from)
7860 att_beg += delta;
7861 if (att_end >= del_from)
7862 att_end += delta;
7863 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007864 val_end += delta;
7865 next += delta;
7866 hdr_end += delta;
7867 hdr_next += delta;
7868 cur_hdr->len += delta;
7869 http_msg_move_end(&txn->req, delta);
7870 prev = del_from;
7871 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007872 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007873 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007874
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007875 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007876 if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007877 int cmp_len, value_len;
7878 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007879
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007880 if (s->be->options2 & PR_O2_AS_PFX) {
7881 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7882 value_begin = att_beg + s->be->appsession_name_len;
7883 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007884 } else {
7885 cmp_len = att_end - att_beg;
7886 value_begin = val_beg;
7887 value_len = val_end - val_beg;
7888 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007889
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007890 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007891 if (cmp_len == s->be->appsession_name_len &&
7892 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7893 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007894 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007895 }
7896
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007897 /* continue with next cookie on this header line */
7898 att_beg = next;
7899 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007900
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007901 /* There are no more cookies on this line.
7902 * We may still have one (or several) marked for deletion at the
7903 * end of the line. We must do this now in two ways :
7904 * - if some cookies must be preserved, we only delete from the
7905 * mark to the end of line ;
7906 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007907 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007908 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007909 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007910 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007911 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007912 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007913 cur_hdr->len += delta;
7914 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007915 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007916
7917 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007918 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7919 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007920 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007921 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007922 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007923 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007924 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007925 }
7926
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007927 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007928 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007929 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007930}
7931
7932
Willy Tarreaua15645d2007-03-18 16:22:39 +01007933/* Iterate the same filter through all response headers contained in <rtr>.
7934 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7935 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007936int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007937{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007938 char *cur_ptr, *cur_end, *cur_next;
7939 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007940 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007941 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007942 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007943
7944 last_hdr = 0;
7945
Willy Tarreau9b28e032012-10-12 23:49:43 +02007946 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007947 old_idx = 0;
7948
7949 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007950 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007951 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007952 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007953 (exp->action == ACT_ALLOW ||
7954 exp->action == ACT_DENY))
7955 return 0;
7956
7957 cur_idx = txn->hdr_idx.v[old_idx].next;
7958 if (!cur_idx)
7959 break;
7960
7961 cur_hdr = &txn->hdr_idx.v[cur_idx];
7962 cur_ptr = cur_next;
7963 cur_end = cur_ptr + cur_hdr->len;
7964 cur_next = cur_end + cur_hdr->cr + 1;
7965
7966 /* Now we have one header between cur_ptr and cur_end,
7967 * and the next header starts at cur_next.
7968 */
7969
Willy Tarreau15a53a42015-01-21 13:39:42 +01007970 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007971 switch (exp->action) {
7972 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007973 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007974 last_hdr = 1;
7975 break;
7976
7977 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007978 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007979 last_hdr = 1;
7980 break;
7981
7982 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007983 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7984 if (trash.len < 0)
7985 return -1;
7986
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007987 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007988 /* FIXME: if the user adds a newline in the replacement, the
7989 * index will not be recalculated for now, and the new line
7990 * will not be counted as a new header.
7991 */
7992
7993 cur_end += delta;
7994 cur_next += delta;
7995 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007996 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007997 break;
7998
7999 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008000 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008001 cur_next += delta;
8002
Willy Tarreaufa355d42009-11-29 18:12:29 +01008003 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008004 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8005 txn->hdr_idx.used--;
8006 cur_hdr->len = 0;
8007 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008008 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008009 break;
8010
8011 }
8012 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008013
8014 /* keep the link from this header to next one in case of later
8015 * removal of next header.
8016 */
8017 old_idx = cur_idx;
8018 }
8019 return 0;
8020}
8021
8022
8023/* Apply the filter to the status line in the response buffer <rtr>.
8024 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8025 * or -1 if a replacement resulted in an invalid status line.
8026 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008027int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008029 char *cur_ptr, *cur_end;
8030 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008031 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008032 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008033
8034
Willy Tarreau3d300592007-03-18 18:34:41 +01008035 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008036 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008037 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038 (exp->action == ACT_ALLOW ||
8039 exp->action == ACT_DENY))
8040 return 0;
8041 else if (exp->action == ACT_REMOVE)
8042 return 0;
8043
8044 done = 0;
8045
Willy Tarreau9b28e032012-10-12 23:49:43 +02008046 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008047 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008048
8049 /* Now we have the status line between cur_ptr and cur_end */
8050
Willy Tarreau15a53a42015-01-21 13:39:42 +01008051 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008052 switch (exp->action) {
8053 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008054 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008055 done = 1;
8056 break;
8057
8058 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008059 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008060 done = 1;
8061 break;
8062
8063 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008064 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8065 if (trash.len < 0)
8066 return -1;
8067
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008068 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008069 /* FIXME: if the user adds a newline in the replacement, the
8070 * index will not be recalculated for now, and the new line
8071 * will not be counted as a new header.
8072 */
8073
Willy Tarreaufa355d42009-11-29 18:12:29 +01008074 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008075 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008076 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008077 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008078 cur_ptr, cur_end + 1,
8079 NULL, NULL);
8080 if (unlikely(!cur_end))
8081 return -1;
8082
8083 /* we have a full respnse and we know that we have either a CR
8084 * or an LF at <ptr>.
8085 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008086 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008087 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008088 /* there is no point trying this regex on headers */
8089 return 1;
8090 }
8091 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008092 return done;
8093}
8094
8095
8096
8097/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008098 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008099 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8100 * unparsable response.
8101 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008102int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008103{
Willy Tarreau192252e2015-04-04 01:47:55 +02008104 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008105 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008106 struct hdr_exp *exp;
8107
8108 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008109 int ret;
8110
8111 /*
8112 * The interleaving of transformations and verdicts
8113 * makes it difficult to decide to continue or stop
8114 * the evaluation.
8115 */
8116
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008117 if (txn->flags & TX_SVDENY)
8118 break;
8119
Willy Tarreau3d300592007-03-18 18:34:41 +01008120 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008121 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8122 exp->action == ACT_PASS)) {
8123 exp = exp->next;
8124 continue;
8125 }
8126
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008127 /* if this filter had a condition, evaluate it now and skip to
8128 * next filter if the condition does not match.
8129 */
8130 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008131 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008132 ret = acl_pass(ret);
8133 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8134 ret = !ret;
8135 if (!ret)
8136 continue;
8137 }
8138
Willy Tarreaua15645d2007-03-18 16:22:39 +01008139 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008140 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008141 if (unlikely(ret < 0))
8142 return -1;
8143
8144 if (likely(ret == 0)) {
8145 /* The filter did not match the response, it can be
8146 * iterated through all headers.
8147 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008148 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8149 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008150 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008151 }
8152 return 0;
8153}
8154
8155
Willy Tarreaua15645d2007-03-18 16:22:39 +01008156/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008157 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008158 * desirable to call it only when needed. This function is also used when we
8159 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008160 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008161void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008162{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008163 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008164 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008165 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008166 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008167 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008168 char *hdr_beg, *hdr_end, *hdr_next;
8169 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008170
Willy Tarreaua15645d2007-03-18 16:22:39 +01008171 /* Iterate through the headers.
8172 * we start with the start line.
8173 */
8174 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008175 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008176
8177 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8178 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008179 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008180
8181 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008182 hdr_beg = hdr_next;
8183 hdr_end = hdr_beg + cur_hdr->len;
8184 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008185
Willy Tarreau24581ba2010-08-31 22:39:35 +02008186 /* We have one full header between hdr_beg and hdr_end, and the
8187 * next header starts at hdr_next. We're only interested in
8188 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008189 */
8190
Willy Tarreau24581ba2010-08-31 22:39:35 +02008191 is_cookie2 = 0;
8192 prev = hdr_beg + 10;
8193 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008194 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008195 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8196 if (!val) {
8197 old_idx = cur_idx;
8198 continue;
8199 }
8200 is_cookie2 = 1;
8201 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008202 }
8203
Willy Tarreau24581ba2010-08-31 22:39:35 +02008204 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8205 * <prev> points to the colon.
8206 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008207 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008208
Willy Tarreau24581ba2010-08-31 22:39:35 +02008209 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8210 * check-cache is enabled) and we are not interested in checking
8211 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008212 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008213 if (s->be->cookie_name == NULL &&
8214 s->be->appsession_name == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008215 sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008216 return;
8217
Willy Tarreau24581ba2010-08-31 22:39:35 +02008218 /* OK so now we know we have to process this response cookie.
8219 * The format of the Set-Cookie header is slightly different
8220 * from the format of the Cookie header in that it does not
8221 * support the comma as a cookie delimiter (thus the header
8222 * cannot be folded) because the Expires attribute described in
8223 * the original Netscape's spec may contain an unquoted date
8224 * with a comma inside. We have to live with this because
8225 * many browsers don't support Max-Age and some browsers don't
8226 * support quoted strings. However the Set-Cookie2 header is
8227 * clean.
8228 *
8229 * We have to keep multiple pointers in order to support cookie
8230 * removal at the beginning, middle or end of header without
8231 * corrupting the header (in case of set-cookie2). A special
8232 * pointer, <scav> points to the beginning of the set-cookie-av
8233 * fields after the first semi-colon. The <next> pointer points
8234 * either to the end of line (set-cookie) or next unquoted comma
8235 * (set-cookie2). All of these headers are valid :
8236 *
8237 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8238 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8239 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8240 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8241 * | | | | | | | | | |
8242 * | | | | | | | | +-> next hdr_end <--+
8243 * | | | | | | | +------------> scav
8244 * | | | | | | +--------------> val_end
8245 * | | | | | +--------------------> val_beg
8246 * | | | | +----------------------> equal
8247 * | | | +------------------------> att_end
8248 * | | +----------------------------> att_beg
8249 * | +------------------------------> prev
8250 * +-----------------------------------------> hdr_beg
8251 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008252
Willy Tarreau24581ba2010-08-31 22:39:35 +02008253 for (; prev < hdr_end; prev = next) {
8254 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255
Willy Tarreau24581ba2010-08-31 22:39:35 +02008256 /* find att_beg */
8257 att_beg = prev + 1;
8258 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8259 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008260
Willy Tarreau24581ba2010-08-31 22:39:35 +02008261 /* find att_end : this is the first character after the last non
8262 * space before the equal. It may be equal to hdr_end.
8263 */
8264 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008265
Willy Tarreau24581ba2010-08-31 22:39:35 +02008266 while (equal < hdr_end) {
8267 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8268 break;
8269 if (http_is_spht[(unsigned char)*equal++])
8270 continue;
8271 att_end = equal;
8272 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008273
Willy Tarreau24581ba2010-08-31 22:39:35 +02008274 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8275 * is between <att_beg> and <equal>, both may be identical.
8276 */
8277
8278 /* look for end of cookie if there is an equal sign */
8279 if (equal < hdr_end && *equal == '=') {
8280 /* look for the beginning of the value */
8281 val_beg = equal + 1;
8282 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8283 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008284
Willy Tarreau24581ba2010-08-31 22:39:35 +02008285 /* find the end of the value, respecting quotes */
8286 next = find_cookie_value_end(val_beg, hdr_end);
8287
8288 /* make val_end point to the first white space or delimitor after the value */
8289 val_end = next;
8290 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8291 val_end--;
8292 } else {
8293 /* <equal> points to next comma, semi-colon or EOL */
8294 val_beg = val_end = next = equal;
8295 }
8296
8297 if (next < hdr_end) {
8298 /* Set-Cookie2 supports multiple cookies, and <next> points to
8299 * a colon or semi-colon before the end. So skip all attr-value
8300 * pairs and look for the next comma. For Set-Cookie, since
8301 * commas are permitted in values, skip to the end.
8302 */
8303 if (is_cookie2)
8304 next = find_hdr_value_end(next, hdr_end);
8305 else
8306 next = hdr_end;
8307 }
8308
8309 /* Now everything is as on the diagram above */
8310
8311 /* Ignore cookies with no equal sign */
8312 if (equal == val_end)
8313 continue;
8314
8315 /* If there are spaces around the equal sign, we need to
8316 * strip them otherwise we'll get trouble for cookie captures,
8317 * or even for rewrites. Since this happens extremely rarely,
8318 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008319 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008320 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8321 int stripped_before = 0;
8322 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008323
Willy Tarreau24581ba2010-08-31 22:39:35 +02008324 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008325 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008326 equal += stripped_before;
8327 val_beg += stripped_before;
8328 }
8329
8330 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008331 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008332 val_beg += stripped_after;
8333 stripped_before += stripped_after;
8334 }
8335
8336 val_end += stripped_before;
8337 next += stripped_before;
8338 hdr_end += stripped_before;
8339 hdr_next += stripped_before;
8340 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008341 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008342 }
8343
8344 /* First, let's see if we want to capture this cookie. We check
8345 * that we don't already have a server side cookie, because we
8346 * can only capture one. Also as an optimisation, we ignore
8347 * cookies shorter than the declared name.
8348 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008349 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008350 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008351 (val_end - att_beg >= sess->fe->capture_namelen) &&
8352 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008353 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008354 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008355 Alert("HTTP logging : out of memory.\n");
8356 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008357 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008358 if (log_len > sess->fe->capture_len)
8359 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008360 memcpy(txn->srv_cookie, att_beg, log_len);
8361 txn->srv_cookie[log_len] = 0;
8362 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008363 }
8364
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008365 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008366 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008367 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008368 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8369 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008370 /* assume passive cookie by default */
8371 txn->flags &= ~TX_SCK_MASK;
8372 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008373
8374 /* If the cookie is in insert mode on a known server, we'll delete
8375 * this occurrence because we'll insert another one later.
8376 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008377 * a direct access.
8378 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008379 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008380 /* The "preserve" flag was set, we don't want to touch the
8381 * server's cookie.
8382 */
8383 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008384 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008385 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008386 /* this cookie must be deleted */
8387 if (*prev == ':' && next == hdr_end) {
8388 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008389 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008390 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8391 txn->hdr_idx.used--;
8392 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008393 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008394 hdr_next += delta;
8395 http_msg_move_end(&txn->rsp, delta);
8396 /* note: while both invalid now, <next> and <hdr_end>
8397 * are still equal, so the for() will stop as expected.
8398 */
8399 } else {
8400 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008401 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008402 next = prev;
8403 hdr_end += delta;
8404 hdr_next += delta;
8405 cur_hdr->len += delta;
8406 http_msg_move_end(&txn->rsp, delta);
8407 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008408 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008409 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008410 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008411 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008412 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008413 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008414 * with this server since we know it.
8415 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008416 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008417 next += delta;
8418 hdr_end += delta;
8419 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008420 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008421 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008422
Willy Tarreauf1348312010-10-07 15:54:11 +02008423 txn->flags &= ~TX_SCK_MASK;
8424 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008425 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008426 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008427 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008428 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008429 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008430 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008431 next += delta;
8432 hdr_end += delta;
8433 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008434 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008435 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008436
Willy Tarreau827aee92011-03-10 16:55:02 +01008437 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008438 txn->flags &= ~TX_SCK_MASK;
8439 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008440 }
8441 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008442 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008443 else if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008444 int cmp_len, value_len;
8445 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008446
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008447 if (s->be->options2 & PR_O2_AS_PFX) {
8448 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8449 value_begin = att_beg + s->be->appsession_name_len;
8450 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008451 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008452 cmp_len = att_end - att_beg;
8453 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008454 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008455 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008456
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008457 if ((cmp_len == s->be->appsession_name_len) &&
8458 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008459 /* free a possibly previously allocated memory */
8460 pool_free2(apools.sessid, txn->sessid);
8461
Willy Tarreau87b09662015-04-03 00:22:06 +02008462 /* Store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008463 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008464 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008465 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008466 return;
8467 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008468 memcpy(txn->sessid, value_begin, value_len);
8469 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008470 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008471 }
8472 /* that's done for this cookie, check the next one on the same
8473 * line when next != hdr_end (only if is_cookie2).
8474 */
8475 }
8476 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008477 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008478 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008479
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008480 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008481 appsess *asession = NULL;
8482 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008483 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008484 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008485 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008486 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8487 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008488 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008489 return;
8490 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008491 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8492
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008493 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8494 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008495 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8496 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008497 return;
8498 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008499 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8500 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008501
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008502 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008503 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008504 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008505 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8506 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008507 return;
8508 }
8509 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008510 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008511
8512 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008513 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008514 }
8515
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008516 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008517 asession->request_count++;
8518 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008519}
8520
8521
Willy Tarreaua15645d2007-03-18 16:22:39 +01008522/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008523 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008524 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008525void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008526{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008527 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008528 char *p1, *p2;
8529
8530 char *cur_ptr, *cur_end, *cur_next;
8531 int cur_idx;
8532
Willy Tarreau5df51872007-11-25 16:20:08 +01008533 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008534 return;
8535
8536 /* Iterate through the headers.
8537 * we start with the start line.
8538 */
8539 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008540 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008541
8542 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8543 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008544 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008545
8546 cur_hdr = &txn->hdr_idx.v[cur_idx];
8547 cur_ptr = cur_next;
8548 cur_end = cur_ptr + cur_hdr->len;
8549 cur_next = cur_end + cur_hdr->cr + 1;
8550
8551 /* We have one full header between cur_ptr and cur_end, and the
8552 * next header starts at cur_next. We're only interested in
8553 * "Cookie:" headers.
8554 */
8555
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008556 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8557 if (val) {
8558 if ((cur_end - (cur_ptr + val) >= 8) &&
8559 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8560 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8561 return;
8562 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008563 }
8564
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008565 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8566 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008567 continue;
8568
8569 /* OK, right now we know we have a cache-control header at cur_ptr */
8570
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008571 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008572
8573 if (p1 >= cur_end) /* no more info */
8574 continue;
8575
8576 /* p1 is at the beginning of the value */
8577 p2 = p1;
8578
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008579 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008580 p2++;
8581
8582 /* we have a complete value between p1 and p2 */
8583 if (p2 < cur_end && *p2 == '=') {
8584 /* we have something of the form no-cache="set-cookie" */
8585 if ((cur_end - p1 >= 21) &&
8586 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8587 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008588 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008589 continue;
8590 }
8591
8592 /* OK, so we know that either p2 points to the end of string or to a comma */
8593 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008594 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008595 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8596 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8597 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008598 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008599 return;
8600 }
8601
8602 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008603 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008604 continue;
8605 }
8606 }
8607}
8608
8609
Willy Tarreau58f10d72006-12-04 02:26:12 +01008610/*
8611 * Try to retrieve a known appsession in the URI, then the associated server.
Willy Tarreau87b09662015-04-03 00:22:06 +02008612 * If the server is found, it's assigned to the stream.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008613 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008614void get_srv_from_appsession(struct stream *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008615{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008616 char *end_params, *first_param, *cur_param, *next_param;
8617 char separator;
8618 int value_len;
8619
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008620 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008621
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008622 if (s->be->appsession_name == NULL ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02008623 (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 +01008624 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008625 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008626
Cyril Bontéb21570a2009-11-29 20:04:48 +01008627 first_param = NULL;
8628 switch (mode) {
8629 case PR_O2_AS_M_PP:
8630 first_param = memchr(begin, ';', len);
8631 break;
8632 case PR_O2_AS_M_QS:
8633 first_param = memchr(begin, '?', len);
8634 break;
8635 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008636
Cyril Bontéb21570a2009-11-29 20:04:48 +01008637 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008638 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008639 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008640
Cyril Bontéb21570a2009-11-29 20:04:48 +01008641 switch (mode) {
8642 case PR_O2_AS_M_PP:
8643 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8644 end_params = (char *) begin + len;
8645 }
8646 separator = ';';
8647 break;
8648 case PR_O2_AS_M_QS:
8649 end_params = (char *) begin + len;
8650 separator = '&';
8651 break;
8652 default:
8653 /* unknown mode, shouldn't happen */
8654 return;
8655 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008656
Cyril Bontéb21570a2009-11-29 20:04:48 +01008657 cur_param = next_param = end_params;
8658 while (cur_param > first_param) {
8659 cur_param--;
8660 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8661 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008662 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8663 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8664 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008665 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008666 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8667 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008668 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008669 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008670 }
8671 break;
8672 }
8673 next_param = cur_param;
8674 }
8675 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008676#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008677 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008678 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008679#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008680}
8681
Willy Tarreaub2513902006-12-17 14:52:38 +01008682/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008683 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008684 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008685 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008686 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008687 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008688 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008689 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008690 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008691int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008692{
8693 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008694 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008695 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008696
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008697 if (!uri_auth)
8698 return 0;
8699
Cyril Bonté70be45d2010-10-12 00:14:35 +02008700 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008701 return 0;
8702
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008703 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008704 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008705 return 0;
8706
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008707 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008708 return 0;
8709
Willy Tarreaub2513902006-12-17 14:52:38 +01008710 return 1;
8711}
8712
Willy Tarreau4076a152009-04-02 15:18:36 +02008713/*
8714 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008715 * By default it tries to report the error position as msg->err_pos. However if
8716 * this one is not set, it will then report msg->next, which is the last known
8717 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008718 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008719 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008720void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008721 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008722 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008723{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008724 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008725 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008726 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008727
Willy Tarreau9b28e032012-10-12 23:49:43 +02008728 es->len = MIN(chn->buf->i, sizeof(es->buf));
8729 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008730 len1 = MIN(len1, es->len);
8731 len2 = es->len - len1; /* remaining data if buffer wraps */
8732
Willy Tarreau9b28e032012-10-12 23:49:43 +02008733 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008734 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008735 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008736
Willy Tarreau4076a152009-04-02 15:18:36 +02008737 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008738 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008739 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008740 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008741
Willy Tarreau4076a152009-04-02 15:18:36 +02008742 es->when = date; // user-visible date
8743 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008744 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008745 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008746 if (objt_conn(sess->origin))
8747 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008748 else
8749 memset(&es->src, 0, sizeof(es->src));
8750
Willy Tarreau078272e2010-12-12 12:46:33 +01008751 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008752 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008753 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008754 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008755 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008756 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008757 es->b_out = chn->buf->o;
8758 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008759 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008760 es->m_clen = msg->chunk_len;
8761 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008762}
Willy Tarreaub2513902006-12-17 14:52:38 +01008763
Willy Tarreau294c4732011-12-16 21:35:50 +01008764/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8765 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8766 * performed over the whole headers. Otherwise it must contain a valid header
8767 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8768 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8769 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8770 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008771 * -1. The value fetch stops at commas, so this function is suited for use with
8772 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008773 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008774 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008775unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008776 struct hdr_idx *idx, int occ,
8777 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008778{
Willy Tarreau294c4732011-12-16 21:35:50 +01008779 struct hdr_ctx local_ctx;
8780 char *ptr_hist[MAX_HDR_HISTORY];
8781 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008782 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008783 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008784
Willy Tarreau294c4732011-12-16 21:35:50 +01008785 if (!ctx) {
8786 local_ctx.idx = 0;
8787 ctx = &local_ctx;
8788 }
8789
Willy Tarreaubce70882009-09-07 11:51:47 +02008790 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008791 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008792 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008793 occ--;
8794 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008795 *vptr = ctx->line + ctx->val;
8796 *vlen = ctx->vlen;
8797 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008798 }
8799 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008800 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008801 }
8802
8803 /* negative occurrence, we scan all the list then walk back */
8804 if (-occ > MAX_HDR_HISTORY)
8805 return 0;
8806
Willy Tarreau294c4732011-12-16 21:35:50 +01008807 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008808 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008809 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8810 len_hist[hist_ptr] = ctx->vlen;
8811 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008812 hist_ptr = 0;
8813 found++;
8814 }
8815 if (-occ > found)
8816 return 0;
8817 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008818 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8819 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8820 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008821 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008822 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008823 if (hist_ptr >= MAX_HDR_HISTORY)
8824 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008825 *vptr = ptr_hist[hist_ptr];
8826 *vlen = len_hist[hist_ptr];
8827 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008828}
8829
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008830/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8831 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8832 * performed over the whole headers. Otherwise it must contain a valid header
8833 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8834 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8835 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8836 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8837 * -1. This function differs from http_get_hdr() in that it only returns full
8838 * line header values and does not stop at commas.
8839 * The return value is 0 if nothing was found, or non-zero otherwise.
8840 */
8841unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8842 struct hdr_idx *idx, int occ,
8843 struct hdr_ctx *ctx, char **vptr, int *vlen)
8844{
8845 struct hdr_ctx local_ctx;
8846 char *ptr_hist[MAX_HDR_HISTORY];
8847 int len_hist[MAX_HDR_HISTORY];
8848 unsigned int hist_ptr;
8849 int found;
8850
8851 if (!ctx) {
8852 local_ctx.idx = 0;
8853 ctx = &local_ctx;
8854 }
8855
8856 if (occ >= 0) {
8857 /* search from the beginning */
8858 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8859 occ--;
8860 if (occ <= 0) {
8861 *vptr = ctx->line + ctx->val;
8862 *vlen = ctx->vlen;
8863 return 1;
8864 }
8865 }
8866 return 0;
8867 }
8868
8869 /* negative occurrence, we scan all the list then walk back */
8870 if (-occ > MAX_HDR_HISTORY)
8871 return 0;
8872
8873 found = hist_ptr = 0;
8874 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8875 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8876 len_hist[hist_ptr] = ctx->vlen;
8877 if (++hist_ptr >= MAX_HDR_HISTORY)
8878 hist_ptr = 0;
8879 found++;
8880 }
8881 if (-occ > found)
8882 return 0;
8883 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8884 * find occurrence -occ, so we have to check [hist_ptr+occ].
8885 */
8886 hist_ptr += occ;
8887 if (hist_ptr >= MAX_HDR_HISTORY)
8888 hist_ptr -= MAX_HDR_HISTORY;
8889 *vptr = ptr_hist[hist_ptr];
8890 *vlen = len_hist[hist_ptr];
8891 return 1;
8892}
8893
Willy Tarreaubaaee002006-06-26 02:48:02 +02008894/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008895 * Print a debug line with a header. Always stop at the first CR or LF char,
8896 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8897 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008898 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008899void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008900{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008901 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008902 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008903
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008904 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008905 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008906 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008907 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 +02008908
8909 for (max = 0; start + max < end; max++)
8910 if (start[max] == '\r' || start[max] == '\n')
8911 break;
8912
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008913 UBOUND(max, trash.size - trash.len - 3);
8914 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8915 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008916 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008917}
8918
Willy Tarreaueee5b512015-04-03 23:46:31 +02008919
8920/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8921 * The hdr_idx is allocated as well. In case of allocation failure, everything
8922 * allocated is freed and NULL is returned. Otherwise the new transaction is
8923 * assigned to the stream and returned.
8924 */
8925struct http_txn *http_alloc_txn(struct stream *s)
8926{
8927 struct http_txn *txn = s->txn;
8928
8929 if (txn)
8930 return txn;
8931
8932 txn = pool_alloc2(pool2_http_txn);
8933 if (!txn)
8934 return txn;
8935
8936 txn->hdr_idx.size = global.tune.max_http_hdr;
8937 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8938 if (!txn->hdr_idx.v) {
8939 pool_free2(pool2_http_txn, txn);
8940 return NULL;
8941 }
8942
8943 s->txn = txn;
8944 return txn;
8945}
8946
Willy Tarreau0937bc42009-12-22 15:03:09 +01008947/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008948 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008949 * the required fields are properly allocated and that we only need to (re)init
8950 * them. This should be used before processing any new request.
8951 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008952void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008953{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008954 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008955 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008956
8957 txn->flags = 0;
8958 txn->status = -1;
8959
Willy Tarreauf64d1412010-10-07 20:06:11 +02008960 txn->cookie_first_date = 0;
8961 txn->cookie_last_date = 0;
8962
Willy Tarreaueee5b512015-04-03 23:46:31 +02008963 txn->sessid = NULL;
8964 txn->srv_cookie = NULL;
8965 txn->cli_cookie = NULL;
8966 txn->uri = NULL;
8967
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008968 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008969 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008970 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008971 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008972 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008973 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008974 txn->req.chunk_len = 0LL;
8975 txn->req.body_len = 0LL;
8976 txn->rsp.chunk_len = 0LL;
8977 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008978 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8979 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008980 txn->req.chn = &s->req;
8981 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008982
8983 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008984
8985 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8986 if (fe->options2 & PR_O2_REQBUG_OK)
8987 txn->req.err_pos = -1; /* let buggy requests pass */
8988
Willy Tarreau0937bc42009-12-22 15:03:09 +01008989 if (txn->hdr_idx.v)
8990 hdr_idx_init(&txn->hdr_idx);
8991}
8992
8993/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008994void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008995{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008996 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008997 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008998
Willy Tarreau75195602014-03-11 15:48:55 +01008999 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02009000 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01009001 s->comp_algo->end(&s->comp_ctx);
9002 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02009003 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01009004
Willy Tarreau0937bc42009-12-22 15:03:09 +01009005 /* these ones will have been dynamically allocated */
9006 pool_free2(pool2_requri, txn->uri);
9007 pool_free2(pool2_capture, txn->cli_cookie);
9008 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01009009 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01009010 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009011
William Lallemanda73203e2012-03-12 12:48:57 +01009012 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01009013 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009014 txn->uri = NULL;
9015 txn->srv_cookie = NULL;
9016 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01009017
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009018 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01009019 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02009020 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009021 pool_free2(h->pool, s->req_cap[h->index]);
9022 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01009023 }
9024
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009025 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01009026 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02009027 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009028 pool_free2(h->pool, s->res_cap[h->index]);
9029 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01009030 }
9031
Willy Tarreau0937bc42009-12-22 15:03:09 +01009032}
9033
9034/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02009035void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01009036{
9037 http_end_txn(s);
9038 http_init_txn(s);
9039
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01009040 /* reinitialise the current rule list pointer to NULL. We are sure that
9041 * any rulelist match the NULL pointer.
9042 */
9043 s->current_rule_list = NULL;
9044
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009045 s->be = strm_fe(s);
9046 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02009047 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02009048 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01009049 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009050 /* re-init store persistence */
9051 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01009052 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009053
Willy Tarreau0937bc42009-12-22 15:03:09 +01009054 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009055
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009056 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01009057
Willy Tarreau739cfba2010-01-25 23:11:14 +01009058 /* We must trim any excess data from the response buffer, because we
9059 * may have blocked an invalid response from a server that we don't
9060 * want to accidentely forward once we disable the analysers, nor do
9061 * we want those data to come along with next response. A typical
9062 * example of such data would be from a buggy server responding to
9063 * a HEAD with some data, or sending more than the advertised
9064 * content-length.
9065 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009066 if (unlikely(s->res.buf->i))
9067 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009068
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009069 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009070 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009071
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009072 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009073 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009074
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009075 s->req.rex = TICK_ETERNITY;
9076 s->req.wex = TICK_ETERNITY;
9077 s->req.analyse_exp = TICK_ETERNITY;
9078 s->res.rex = TICK_ETERNITY;
9079 s->res.wex = TICK_ETERNITY;
9080 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009081}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009082
Sasha Pachev218f0642014-06-16 12:05:59 -06009083void free_http_res_rules(struct list *r)
9084{
9085 struct http_res_rule *tr, *pr;
9086
9087 list_for_each_entry_safe(pr, tr, r, list) {
9088 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009089 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009090 free(pr);
9091 }
9092}
9093
9094void free_http_req_rules(struct list *r)
9095{
Willy Tarreauff011f22011-01-06 17:51:27 +01009096 struct http_req_rule *tr, *pr;
9097
9098 list_for_each_entry_safe(pr, tr, r, list) {
9099 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01009100 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009101 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009102
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009103 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009104 free(pr);
9105 }
9106}
9107
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009108/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01009109struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9110{
9111 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009112 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009113 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009114 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009115
9116 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
9117 if (!rule) {
9118 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009119 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009120 }
9121
CJ Ess108b1dd2015-04-07 12:03:37 -04009122 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009123 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01009124 rule->action = HTTP_REQ_ACT_ALLOW;
9125 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009126 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009127 int code;
9128 int hc;
9129
Willy Tarreauff011f22011-01-06 17:51:27 +01009130 rule->action = HTTP_REQ_ACT_DENY;
9131 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009132 if (strcmp(args[cur_arg], "deny_status") == 0) {
9133 cur_arg++;
9134 if (!args[cur_arg]) {
9135 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9136 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9137 goto out_err;
9138 }
9139
9140 code = atol(args[cur_arg]);
9141 cur_arg++;
9142 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9143 if (http_err_codes[hc] == code) {
9144 rule->deny_status = hc;
9145 break;
9146 }
9147 }
9148
9149 if (hc >= HTTP_ERR_SIZE) {
9150 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9151 file, linenum, code);
9152 }
9153 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009154 } else if (!strcmp(args[0], "tarpit")) {
9155 rule->action = HTTP_REQ_ACT_TARPIT;
9156 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009157 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01009158 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009159 cur_arg = 1;
9160
9161 while(*args[cur_arg]) {
9162 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009163 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009164 cur_arg+=2;
9165 continue;
9166 } else
9167 break;
9168 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009169 } else if (!strcmp(args[0], "set-nice")) {
9170 rule->action = HTTP_REQ_ACT_SET_NICE;
9171 cur_arg = 1;
9172
9173 if (!*args[cur_arg] ||
9174 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9175 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9176 file, linenum, args[0]);
9177 goto out_err;
9178 }
9179 rule->arg.nice = atoi(args[cur_arg]);
9180 if (rule->arg.nice < -1024)
9181 rule->arg.nice = -1024;
9182 else if (rule->arg.nice > 1024)
9183 rule->arg.nice = 1024;
9184 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009185 } else if (!strcmp(args[0], "set-tos")) {
9186#ifdef IP_TOS
9187 char *err;
9188 rule->action = HTTP_REQ_ACT_SET_TOS;
9189 cur_arg = 1;
9190
9191 if (!*args[cur_arg] ||
9192 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9193 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9194 file, linenum, args[0]);
9195 goto out_err;
9196 }
9197
9198 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9199 if (err && *err != '\0') {
9200 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9201 file, linenum, err, args[0]);
9202 goto out_err;
9203 }
9204 cur_arg++;
9205#else
9206 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9207 goto out_err;
9208#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009209 } else if (!strcmp(args[0], "set-mark")) {
9210#ifdef SO_MARK
9211 char *err;
9212 rule->action = HTTP_REQ_ACT_SET_MARK;
9213 cur_arg = 1;
9214
9215 if (!*args[cur_arg] ||
9216 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9217 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9218 file, linenum, args[0]);
9219 goto out_err;
9220 }
9221
9222 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9223 if (err && *err != '\0') {
9224 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9225 file, linenum, err, args[0]);
9226 goto out_err;
9227 }
9228 cur_arg++;
9229 global.last_checks |= LSTCHK_NETADM;
9230#else
9231 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9232 goto out_err;
9233#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009234 } else if (!strcmp(args[0], "set-log-level")) {
9235 rule->action = HTTP_REQ_ACT_SET_LOGL;
9236 cur_arg = 1;
9237
9238 if (!*args[cur_arg] ||
9239 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9240 bad_log_level:
9241 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9242 file, linenum, args[0]);
9243 goto out_err;
9244 }
9245 if (strcmp(args[cur_arg], "silent") == 0)
9246 rule->arg.loglevel = -1;
9247 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9248 goto bad_log_level;
9249 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009250 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9251 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9252 cur_arg = 1;
9253
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009254 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9255 (*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 +01009256 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9257 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009258 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009259 }
9260
9261 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9262 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9263 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009264
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009265 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009266 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009267 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9268 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009269 free(proxy->conf.lfs_file);
9270 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9271 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009272 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009273 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9274 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009275 cur_arg = 1;
9276
9277 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009278 (*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 -06009279 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9280 file, linenum, args[0]);
9281 goto out_err;
9282 }
9283
9284 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9285 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9286 LIST_INIT(&rule->arg.hdr_add.fmt);
9287
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009288 error = NULL;
9289 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9290 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9291 args[cur_arg + 1], error);
9292 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009293 goto out_err;
9294 }
9295
9296 proxy->conf.args.ctx = ARGC_HRQ;
9297 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9298 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9299 file, linenum);
9300
9301 free(proxy->conf.lfs_file);
9302 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9303 proxy->conf.lfs_line = proxy->conf.args.line;
9304 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009305 } else if (strcmp(args[0], "del-header") == 0) {
9306 rule->action = HTTP_REQ_ACT_DEL_HDR;
9307 cur_arg = 1;
9308
9309 if (!*args[cur_arg] ||
9310 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9311 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9312 file, linenum, args[0]);
9313 goto out_err;
9314 }
9315
9316 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9317 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9318
9319 proxy->conf.args.ctx = ARGC_HRQ;
9320 free(proxy->conf.lfs_file);
9321 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9322 proxy->conf.lfs_line = proxy->conf.args.line;
9323 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009324 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9325 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009326 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009327 struct sample_expr *expr;
9328 unsigned int where;
9329 char *err = NULL;
9330
9331 cur_arg = 1;
9332 proxy->conf.args.ctx = ARGC_TRK;
9333
9334 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9335 if (!expr) {
9336 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9337 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9338 free(err);
9339 goto out_err;
9340 }
9341
9342 where = 0;
9343 if (proxy->cap & PR_CAP_FE)
9344 where |= SMP_VAL_FE_HRQ_HDR;
9345 if (proxy->cap & PR_CAP_BE)
9346 where |= SMP_VAL_BE_HRQ_HDR;
9347
9348 if (!(expr->fetch->val & where)) {
9349 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9350 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9351 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9352 args[cur_arg-1], sample_src_names(expr->fetch->use));
9353 free(expr);
9354 goto out_err;
9355 }
9356
9357 if (strcmp(args[cur_arg], "table") == 0) {
9358 cur_arg++;
9359 if (!args[cur_arg]) {
9360 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9361 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9362 free(expr);
9363 goto out_err;
9364 }
9365 /* we copy the table name for now, it will be resolved later */
9366 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9367 cur_arg++;
9368 }
9369 rule->act_prm.trk_ctr.expr = expr;
9370 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009371 } else if (strcmp(args[0], "redirect") == 0) {
9372 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009373 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009374
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009375 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009376 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9377 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9378 goto out_err;
9379 }
9380
9381 /* this redirect rule might already contain a parsed condition which
9382 * we'll pass to the http-request rule.
9383 */
9384 rule->action = HTTP_REQ_ACT_REDIR;
9385 rule->arg.redir = redir;
9386 rule->cond = redir->cond;
9387 redir->cond = NULL;
9388 cur_arg = 2;
9389 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009390 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9391 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9392 rule->action = HTTP_REQ_ACT_ADD_ACL;
9393 /*
9394 * '+ 8' for 'add-acl('
9395 * '- 9' for 'add-acl(' + trailing ')'
9396 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009397 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009398
9399 cur_arg = 1;
9400
9401 if (!*args[cur_arg] ||
9402 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9403 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9404 file, linenum, args[0]);
9405 goto out_err;
9406 }
9407
9408 LIST_INIT(&rule->arg.map.key);
9409 proxy->conf.args.ctx = ARGC_HRQ;
9410 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9411 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9412 file, linenum);
9413 free(proxy->conf.lfs_file);
9414 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9415 proxy->conf.lfs_line = proxy->conf.args.line;
9416 cur_arg += 1;
9417 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9418 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9419 rule->action = HTTP_REQ_ACT_DEL_ACL;
9420 /*
9421 * '+ 8' for 'del-acl('
9422 * '- 9' for 'del-acl(' + trailing ')'
9423 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009424 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009425
9426 cur_arg = 1;
9427
9428 if (!*args[cur_arg] ||
9429 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9430 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9431 file, linenum, args[0]);
9432 goto out_err;
9433 }
9434
9435 LIST_INIT(&rule->arg.map.key);
9436 proxy->conf.args.ctx = ARGC_HRQ;
9437 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9438 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9439 file, linenum);
9440 free(proxy->conf.lfs_file);
9441 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9442 proxy->conf.lfs_line = proxy->conf.args.line;
9443 cur_arg += 1;
9444 } else if (strncmp(args[0], "del-map", 7) == 0) {
9445 /* http-request del-map(<reference (map name)>) <key pattern> */
9446 rule->action = HTTP_REQ_ACT_DEL_MAP;
9447 /*
9448 * '+ 8' for 'del-map('
9449 * '- 9' for 'del-map(' + trailing ')'
9450 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009451 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009452
9453 cur_arg = 1;
9454
9455 if (!*args[cur_arg] ||
9456 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9457 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9458 file, linenum, args[0]);
9459 goto out_err;
9460 }
9461
9462 LIST_INIT(&rule->arg.map.key);
9463 proxy->conf.args.ctx = ARGC_HRQ;
9464 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9465 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9466 file, linenum);
9467 free(proxy->conf.lfs_file);
9468 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9469 proxy->conf.lfs_line = proxy->conf.args.line;
9470 cur_arg += 1;
9471 } else if (strncmp(args[0], "set-map", 7) == 0) {
9472 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9473 rule->action = HTTP_REQ_ACT_SET_MAP;
9474 /*
9475 * '+ 8' for 'set-map('
9476 * '- 9' for 'set-map(' + trailing ')'
9477 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009478 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009479
9480 cur_arg = 1;
9481
9482 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9483 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9484 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9485 file, linenum, args[0]);
9486 goto out_err;
9487 }
9488
9489 LIST_INIT(&rule->arg.map.key);
9490 LIST_INIT(&rule->arg.map.value);
9491 proxy->conf.args.ctx = ARGC_HRQ;
9492
9493 /* key pattern */
9494 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9495 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9496 file, linenum);
9497
9498 /* value pattern */
9499 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9500 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9501 file, linenum);
9502 free(proxy->conf.lfs_file);
9503 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9504 proxy->conf.lfs_line = proxy->conf.args.line;
9505
9506 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009507 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9508 char *errmsg = NULL;
9509 cur_arg = 1;
9510 /* try in the module list */
9511 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9512 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9513 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9514 free(errmsg);
9515 goto out_err;
9516 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009517 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009518 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 +01009519 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009520 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009521 }
9522
9523 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9524 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009525 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009526
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009527 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9528 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9529 file, linenum, args[0], errmsg);
9530 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009531 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009532 }
9533 rule->cond = cond;
9534 }
9535 else if (*args[cur_arg]) {
9536 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9537 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9538 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009539 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009540 }
9541
9542 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009543 out_err:
9544 free(rule);
9545 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009546}
9547
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009548/* parse an "http-respose" rule */
9549struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9550{
9551 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009552 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009553 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009554 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009555
9556 rule = calloc(1, sizeof(*rule));
9557 if (!rule) {
9558 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9559 goto out_err;
9560 }
9561
9562 if (!strcmp(args[0], "allow")) {
9563 rule->action = HTTP_RES_ACT_ALLOW;
9564 cur_arg = 1;
9565 } else if (!strcmp(args[0], "deny")) {
9566 rule->action = HTTP_RES_ACT_DENY;
9567 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009568 } else if (!strcmp(args[0], "set-nice")) {
9569 rule->action = HTTP_RES_ACT_SET_NICE;
9570 cur_arg = 1;
9571
9572 if (!*args[cur_arg] ||
9573 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9574 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9575 file, linenum, args[0]);
9576 goto out_err;
9577 }
9578 rule->arg.nice = atoi(args[cur_arg]);
9579 if (rule->arg.nice < -1024)
9580 rule->arg.nice = -1024;
9581 else if (rule->arg.nice > 1024)
9582 rule->arg.nice = 1024;
9583 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009584 } else if (!strcmp(args[0], "set-tos")) {
9585#ifdef IP_TOS
9586 char *err;
9587 rule->action = HTTP_RES_ACT_SET_TOS;
9588 cur_arg = 1;
9589
9590 if (!*args[cur_arg] ||
9591 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9592 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9593 file, linenum, args[0]);
9594 goto out_err;
9595 }
9596
9597 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9598 if (err && *err != '\0') {
9599 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9600 file, linenum, err, args[0]);
9601 goto out_err;
9602 }
9603 cur_arg++;
9604#else
9605 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9606 goto out_err;
9607#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009608 } else if (!strcmp(args[0], "set-mark")) {
9609#ifdef SO_MARK
9610 char *err;
9611 rule->action = HTTP_RES_ACT_SET_MARK;
9612 cur_arg = 1;
9613
9614 if (!*args[cur_arg] ||
9615 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9616 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9617 file, linenum, args[0]);
9618 goto out_err;
9619 }
9620
9621 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9622 if (err && *err != '\0') {
9623 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9624 file, linenum, err, args[0]);
9625 goto out_err;
9626 }
9627 cur_arg++;
9628 global.last_checks |= LSTCHK_NETADM;
9629#else
9630 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9631 goto out_err;
9632#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009633 } else if (!strcmp(args[0], "set-log-level")) {
9634 rule->action = HTTP_RES_ACT_SET_LOGL;
9635 cur_arg = 1;
9636
9637 if (!*args[cur_arg] ||
9638 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9639 bad_log_level:
9640 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9641 file, linenum, args[0]);
9642 goto out_err;
9643 }
9644 if (strcmp(args[cur_arg], "silent") == 0)
9645 rule->arg.loglevel = -1;
9646 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9647 goto bad_log_level;
9648 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009649 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9650 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9651 cur_arg = 1;
9652
9653 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9654 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9655 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9656 file, linenum, args[0]);
9657 goto out_err;
9658 }
9659
9660 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9661 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9662 LIST_INIT(&rule->arg.hdr_add.fmt);
9663
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009664 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009665 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009666 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9667 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009668 free(proxy->conf.lfs_file);
9669 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9670 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009671 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009672 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009673 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009674 cur_arg = 1;
9675
9676 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009677 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9678 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009679 file, linenum, args[0]);
9680 goto out_err;
9681 }
9682
9683 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9684 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9685 LIST_INIT(&rule->arg.hdr_add.fmt);
9686
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009687 error = NULL;
9688 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9689 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9690 args[cur_arg + 1], error);
9691 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009692 goto out_err;
9693 }
9694
9695 proxy->conf.args.ctx = ARGC_HRQ;
9696 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9697 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9698 file, linenum);
9699
9700 free(proxy->conf.lfs_file);
9701 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9702 proxy->conf.lfs_line = proxy->conf.args.line;
9703 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009704 } else if (strcmp(args[0], "del-header") == 0) {
9705 rule->action = HTTP_RES_ACT_DEL_HDR;
9706 cur_arg = 1;
9707
9708 if (!*args[cur_arg] ||
9709 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9710 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9711 file, linenum, args[0]);
9712 goto out_err;
9713 }
9714
9715 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9716 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9717
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009718 proxy->conf.args.ctx = ARGC_HRS;
9719 free(proxy->conf.lfs_file);
9720 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9721 proxy->conf.lfs_line = proxy->conf.args.line;
9722 cur_arg += 1;
9723 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9724 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9725 rule->action = HTTP_RES_ACT_ADD_ACL;
9726 /*
9727 * '+ 8' for 'add-acl('
9728 * '- 9' for 'add-acl(' + trailing ')'
9729 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009730 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009731
9732 cur_arg = 1;
9733
9734 if (!*args[cur_arg] ||
9735 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9736 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9737 file, linenum, args[0]);
9738 goto out_err;
9739 }
9740
9741 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009742 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009743 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9744 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9745 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009746 free(proxy->conf.lfs_file);
9747 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9748 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009749
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009750 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009751 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9752 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9753 rule->action = HTTP_RES_ACT_DEL_ACL;
9754 /*
9755 * '+ 8' for 'del-acl('
9756 * '- 9' for 'del-acl(' + trailing ')'
9757 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009758 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009759
9760 cur_arg = 1;
9761
9762 if (!*args[cur_arg] ||
9763 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9764 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9765 file, linenum, args[0]);
9766 goto out_err;
9767 }
9768
9769 LIST_INIT(&rule->arg.map.key);
9770 proxy->conf.args.ctx = ARGC_HRS;
9771 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9772 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9773 file, linenum);
9774 free(proxy->conf.lfs_file);
9775 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9776 proxy->conf.lfs_line = proxy->conf.args.line;
9777 cur_arg += 1;
9778 } else if (strncmp(args[0], "del-map", 7) == 0) {
9779 /* http-response del-map(<reference (map name)>) <key pattern> */
9780 rule->action = HTTP_RES_ACT_DEL_MAP;
9781 /*
9782 * '+ 8' for 'del-map('
9783 * '- 9' for 'del-map(' + trailing ')'
9784 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009785 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009786
9787 cur_arg = 1;
9788
9789 if (!*args[cur_arg] ||
9790 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9791 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9792 file, linenum, args[0]);
9793 goto out_err;
9794 }
9795
9796 LIST_INIT(&rule->arg.map.key);
9797 proxy->conf.args.ctx = ARGC_HRS;
9798 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9799 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9800 file, linenum);
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 cur_arg += 1;
9805 } else if (strncmp(args[0], "set-map", 7) == 0) {
9806 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9807 rule->action = HTTP_RES_ACT_SET_MAP;
9808 /*
9809 * '+ 8' for 'set-map('
9810 * '- 9' for 'set-map(' + trailing ')'
9811 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009812 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009813
9814 cur_arg = 1;
9815
9816 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9817 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9818 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9819 file, linenum, args[0]);
9820 goto out_err;
9821 }
9822
9823 LIST_INIT(&rule->arg.map.key);
9824 LIST_INIT(&rule->arg.map.value);
9825
9826 proxy->conf.args.ctx = ARGC_HRS;
9827
9828 /* key pattern */
9829 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9830 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9831 file, linenum);
9832
9833 /* value pattern */
9834 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9835 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9836 file, linenum);
9837
9838 free(proxy->conf.lfs_file);
9839 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9840 proxy->conf.lfs_line = proxy->conf.args.line;
9841
9842 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009843 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9844 char *errmsg = NULL;
9845 cur_arg = 1;
9846 /* try in the module list */
9847 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9848 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9849 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9850 free(errmsg);
9851 goto out_err;
9852 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009853 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009854 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 +02009855 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9856 goto out_err;
9857 }
9858
9859 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9860 struct acl_cond *cond;
9861 char *errmsg = NULL;
9862
9863 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9864 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9865 file, linenum, args[0], errmsg);
9866 free(errmsg);
9867 goto out_err;
9868 }
9869 rule->cond = cond;
9870 }
9871 else if (*args[cur_arg]) {
9872 Alert("parsing [%s:%d]: 'http-response %s' expects"
9873 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9874 file, linenum, args[0], args[cur_arg]);
9875 goto out_err;
9876 }
9877
9878 return rule;
9879 out_err:
9880 free(rule);
9881 return NULL;
9882}
9883
Willy Tarreau4baae242012-12-27 12:00:31 +01009884/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009885 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9886 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009887 */
9888struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009889 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009890{
9891 struct redirect_rule *rule;
9892 int cur_arg;
9893 int type = REDIRECT_TYPE_NONE;
9894 int code = 302;
9895 const char *destination = NULL;
9896 const char *cookie = NULL;
9897 int cookie_set = 0;
9898 unsigned int flags = REDIRECT_FLAG_NONE;
9899 struct acl_cond *cond = NULL;
9900
9901 cur_arg = 0;
9902 while (*(args[cur_arg])) {
9903 if (strcmp(args[cur_arg], "location") == 0) {
9904 if (!*args[cur_arg + 1])
9905 goto missing_arg;
9906
9907 type = REDIRECT_TYPE_LOCATION;
9908 cur_arg++;
9909 destination = args[cur_arg];
9910 }
9911 else if (strcmp(args[cur_arg], "prefix") == 0) {
9912 if (!*args[cur_arg + 1])
9913 goto missing_arg;
9914
9915 type = REDIRECT_TYPE_PREFIX;
9916 cur_arg++;
9917 destination = args[cur_arg];
9918 }
9919 else if (strcmp(args[cur_arg], "scheme") == 0) {
9920 if (!*args[cur_arg + 1])
9921 goto missing_arg;
9922
9923 type = REDIRECT_TYPE_SCHEME;
9924 cur_arg++;
9925 destination = args[cur_arg];
9926 }
9927 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9928 if (!*args[cur_arg + 1])
9929 goto missing_arg;
9930
9931 cur_arg++;
9932 cookie = args[cur_arg];
9933 cookie_set = 1;
9934 }
9935 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9936 if (!*args[cur_arg + 1])
9937 goto missing_arg;
9938
9939 cur_arg++;
9940 cookie = args[cur_arg];
9941 cookie_set = 0;
9942 }
9943 else if (strcmp(args[cur_arg], "code") == 0) {
9944 if (!*args[cur_arg + 1])
9945 goto missing_arg;
9946
9947 cur_arg++;
9948 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009949 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009950 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009951 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009952 args[cur_arg - 1], args[cur_arg]);
9953 return NULL;
9954 }
9955 }
9956 else if (!strcmp(args[cur_arg],"drop-query")) {
9957 flags |= REDIRECT_FLAG_DROP_QS;
9958 }
9959 else if (!strcmp(args[cur_arg],"append-slash")) {
9960 flags |= REDIRECT_FLAG_APPEND_SLASH;
9961 }
9962 else if (strcmp(args[cur_arg], "if") == 0 ||
9963 strcmp(args[cur_arg], "unless") == 0) {
9964 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9965 if (!cond) {
9966 memprintf(errmsg, "error in condition: %s", *errmsg);
9967 return NULL;
9968 }
9969 break;
9970 }
9971 else {
9972 memprintf(errmsg,
9973 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9974 args[cur_arg]);
9975 return NULL;
9976 }
9977 cur_arg++;
9978 }
9979
9980 if (type == REDIRECT_TYPE_NONE) {
9981 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9982 return NULL;
9983 }
9984
9985 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9986 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009987 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009988
9989 if (!use_fmt) {
9990 /* old-style static redirect rule */
9991 rule->rdr_str = strdup(destination);
9992 rule->rdr_len = strlen(destination);
9993 }
9994 else {
9995 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009996
9997 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9998 * if prefix == "/", we don't want to add anything, otherwise it
9999 * makes it hard for the user to configure a self-redirection.
10000 */
Godbachd9722032014-12-18 15:44:58 +080010001 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010002 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +010010003 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +010010004 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10005 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010006 free(curproxy->conf.lfs_file);
10007 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10008 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010009 }
10010 }
10011
Willy Tarreau4baae242012-12-27 12:00:31 +010010012 if (cookie) {
10013 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10014 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10015 */
10016 rule->cookie_len = strlen(cookie);
10017 if (cookie_set) {
10018 rule->cookie_str = malloc(rule->cookie_len + 10);
10019 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10020 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10021 rule->cookie_len += 9;
10022 } else {
10023 rule->cookie_str = malloc(rule->cookie_len + 21);
10024 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10025 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10026 rule->cookie_len += 20;
10027 }
10028 }
10029 rule->type = type;
10030 rule->code = code;
10031 rule->flags = flags;
10032 LIST_INIT(&rule->list);
10033 return rule;
10034
10035 missing_arg:
10036 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10037 return NULL;
10038}
10039
Willy Tarreau8797c062007-05-07 00:55:35 +020010040/************************************************************************/
10041/* The code below is dedicated to ACL parsing and matching */
10042/************************************************************************/
10043
10044
Willy Tarreau14174bc2012-04-16 14:34:04 +020010045/* This function ensures that the prerequisites for an L7 fetch are ready,
10046 * which means that a request or response is ready. If some data is missing,
10047 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010048 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10049 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010050 *
10051 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010052 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10053 * decide whether or not an HTTP message is present ;
10054 * 0 if the requested data cannot be fetched or if it is certain that
10055 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010056 * 1 if an HTTP message is ready
10057 */
10058static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010059smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010060 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010061{
Willy Tarreau192252e2015-04-04 01:47:55 +020010062 struct http_txn *txn;
10063 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010064
Willy Tarreaueee5b512015-04-03 23:46:31 +020010065 /* Note: this function may only be used from places where
10066 * http_init_txn() has already been done, and implies that <s>,
10067 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10068 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010069 */
10070
Willy Tarreau192252e2015-04-04 01:47:55 +020010071 if (!s)
10072 return 0;
10073 txn = s->txn;
10074
10075 if (!txn)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010076 return 0;
Willy Tarreau192252e2015-04-04 01:47:55 +020010077 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010078
10079 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +020010080 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010081
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010082 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010083 /* If the buffer does not leave enough free space at the end,
10084 * we must first realign it.
10085 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010086 if (s->req.buf->p > s->req.buf->data &&
10087 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10088 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010089
Willy Tarreau14174bc2012-04-16 14:34:04 +020010090 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010091 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010092 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010093
10094 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010095 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010096 http_msg_analyzer(msg, &txn->hdr_idx);
10097
10098 /* Still no valid request ? */
10099 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010100 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010101 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010102 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010103 }
10104 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010105 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010106 return 0;
10107 }
10108
10109 /* OK we just got a valid HTTP request. We have some minor
10110 * preparation to perform so that further checks can rely
10111 * on HTTP tests.
10112 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010113
10114 /* If the request was parsed but was too large, we must absolutely
10115 * return an error so that it is not processed. At the moment this
10116 * cannot happen, but if the parsers are to change in the future,
10117 * we want this check to be maintained.
10118 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010119 if (unlikely(s->req.buf->i + s->req.buf->p >
10120 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010121 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +020010122 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010123 return 1;
10124 }
10125
Willy Tarreau9b28e032012-10-12 23:49:43 +020010126 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010127 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010128 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010129
Willy Tarreau506d0502013-07-06 13:29:24 +020010130 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10131 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010132 }
10133
Willy Tarreau506d0502013-07-06 13:29:24 +020010134 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010135 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010136 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010137
10138 /* otherwise everything's ready for the request */
10139 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010140 else {
10141 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010142 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10143 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010144 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010145 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010146 }
10147
10148 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +020010149 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010150 return 1;
10151}
Willy Tarreau8797c062007-05-07 00:55:35 +020010152
Willy Tarreau6c616e02014-06-25 16:56:41 +020010153/* Note: these functinos *do* modify the sample. Even in case of success, at
10154 * least the type and uint value are modified.
10155 */
Willy Tarreauc0239e02012-04-16 14:42:55 +020010156#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau15e91e12015-04-04 00:52:09 +020010157 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 +020010158
Willy Tarreau24e32d82012-04-23 23:55:44 +020010159#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau15e91e12015-04-04 00:52:09 +020010160 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 +020010161
Willy Tarreau8797c062007-05-07 00:55:35 +020010162
10163/* 1. Check on METHOD
10164 * We use the pre-parsed method if it is known, and store its number as an
10165 * integer. If it is unknown, we use the pointer and the length.
10166 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010167static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010168{
10169 int len, meth;
10170
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010171 len = strlen(text);
10172 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010173
10174 pattern->val.i = meth;
10175 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010176 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010177 pattern->len = len;
10178 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010179 else {
10180 pattern->ptr.str = NULL;
10181 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010182 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010183 return 1;
10184}
10185
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010186/* This function fetches the method of current HTTP request and stores
10187 * it in the global pattern struct as a chunk. There are two possibilities :
10188 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10189 * in <len> and <ptr> is NULL ;
10190 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10191 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010192 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010193 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010194static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010195smp_fetch_meth(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010196 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010197{
10198 int meth;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010199 struct http_txn *txn = strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010200
Willy Tarreau24e32d82012-04-23 23:55:44 +020010201 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010202
Willy Tarreau8797c062007-05-07 00:55:35 +020010203 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010204 smp->type = SMP_T_METH;
10205 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010206 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010207 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10208 /* ensure the indexes are not affected */
10209 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010210 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010211 smp->data.meth.str.len = txn->req.sl.rq.m_l;
10212 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010213 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010214 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010215 return 1;
10216}
10217
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010218/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010219static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010220{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010221 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010222 struct pattern_list *lst;
10223 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010224
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010225 list_for_each_entry(lst, &expr->patterns, list) {
10226 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010227
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010228 /* well-known method */
10229 if (pattern->val.i != HTTP_METH_OTHER) {
10230 if (smp->data.meth.meth == pattern->val.i)
10231 return pattern;
10232 else
10233 continue;
10234 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010235
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010236 /* Other method, we must compare the strings */
10237 if (pattern->len != smp->data.meth.str.len)
10238 continue;
10239
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010240 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010241 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10242 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010243 return pattern;
10244 }
10245 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010246}
10247
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010248static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010249smp_fetch_rqver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010250 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010251{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010252 struct http_txn *txn = strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010253 char *ptr;
10254 int len;
10255
Willy Tarreauc0239e02012-04-16 14:42:55 +020010256 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010257
Willy Tarreau8797c062007-05-07 00:55:35 +020010258 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010259 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010260
10261 while ((len-- > 0) && (*ptr++ != '/'));
10262 if (len <= 0)
10263 return 0;
10264
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010265 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010266 smp->data.str.str = ptr;
10267 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010268
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010269 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010270 return 1;
10271}
10272
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010273static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010274smp_fetch_stver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010275 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010276{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010277 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010278 char *ptr;
10279 int len;
10280
Willy Tarreauc0239e02012-04-16 14:42:55 +020010281 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010282
Willy Tarreau15e91e12015-04-04 00:52:09 +020010283 txn = strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010284 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10285 return 0;
10286
Willy Tarreau8797c062007-05-07 00:55:35 +020010287 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010288 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010289
10290 while ((len-- > 0) && (*ptr++ != '/'));
10291 if (len <= 0)
10292 return 0;
10293
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010294 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010295 smp->data.str.str = ptr;
10296 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010297
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010298 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010299 return 1;
10300}
10301
10302/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010303static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010304smp_fetch_stcode(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010305 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010306{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010307 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010308 char *ptr;
10309 int len;
10310
Willy Tarreauc0239e02012-04-16 14:42:55 +020010311 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010312
Willy Tarreau15e91e12015-04-04 00:52:09 +020010313 txn = strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010314 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10315 return 0;
10316
Willy Tarreau8797c062007-05-07 00:55:35 +020010317 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010318 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010319
Willy Tarreauf853c462012-04-23 18:53:56 +020010320 smp->type = SMP_T_UINT;
10321 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010322 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010323 return 1;
10324}
10325
10326/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010327static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010328smp_fetch_url(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010329 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010330{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010331 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010332
Willy Tarreauc0239e02012-04-16 14:42:55 +020010333 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010334
Willy Tarreau15e91e12015-04-04 00:52:09 +020010335 txn = strm->txn;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010336 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010337 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010338 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010339 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010340 return 1;
10341}
10342
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010343static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010344smp_fetch_url_ip(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010345 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010346{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010347 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010348 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010349
Willy Tarreauc0239e02012-04-16 14:42:55 +020010350 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010351
Willy Tarreau15e91e12015-04-04 00:52:09 +020010352 txn = strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010353 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 +020010354 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010355 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010356
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010357 smp->type = SMP_T_IPV4;
10358 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010359 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010360 return 1;
10361}
10362
10363static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010364smp_fetch_url_port(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010365 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010366{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010367 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010368 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010369
Willy Tarreauc0239e02012-04-16 14:42:55 +020010370 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010371
Willy Tarreau15e91e12015-04-04 00:52:09 +020010372 txn = strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010373 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 +020010374 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10375 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010376
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010377 smp->type = SMP_T_UINT;
10378 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010379 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010380 return 1;
10381}
10382
Willy Tarreau185b5c42012-04-26 15:11:51 +020010383/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10384 * Accepts an optional argument of type string containing the header field name,
10385 * and an optional argument of type signed or unsigned integer to request an
10386 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010387 * headers are considered from the first one. It does not stop on commas and
10388 * returns full lines instead (useful for User-Agent or Date for example).
10389 */
10390static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010391smp_fetch_fhdr(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010392 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010393{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010394 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010395 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010396 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010397 int occ = 0;
10398 const char *name_str = NULL;
10399 int name_len = 0;
10400
10401 if (!ctx) {
10402 /* first call */
10403 ctx = &static_hdr_ctx;
10404 ctx->idx = 0;
10405 smp->ctx.a[0] = ctx;
10406 }
10407
10408 if (args) {
10409 if (args[0].type != ARGT_STR)
10410 return 0;
10411 name_str = args[0].data.str.str;
10412 name_len = args[0].data.str.len;
10413
10414 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10415 occ = args[1].data.uint;
10416 }
10417
10418 CHECK_HTTP_MESSAGE_FIRST();
10419
Willy Tarreau15e91e12015-04-04 00:52:09 +020010420 idx = &strm->txn->hdr_idx;
10421 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10422
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010423 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10424 /* search for header from the beginning */
10425 ctx->idx = 0;
10426
10427 if (!occ && !(opt & SMP_OPT_ITERATE))
10428 /* no explicit occurrence and single fetch => last header by default */
10429 occ = -1;
10430
10431 if (!occ)
10432 /* prepare to report multiple occurrences for ACL fetches */
10433 smp->flags |= SMP_F_NOT_LAST;
10434
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010435 smp->type = SMP_T_STR;
10436 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010437 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10438 return 1;
10439
10440 smp->flags &= ~SMP_F_NOT_LAST;
10441 return 0;
10442}
10443
10444/* 6. Check on HTTP header count. The number of occurrences is returned.
10445 * Accepts exactly 1 argument of type string. It does not stop on commas and
10446 * returns full lines instead (useful for User-Agent or Date for example).
10447 */
10448static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010449smp_fetch_fhdr_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010450 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010451{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010452 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010453 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010454 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010455 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010456 const char *name = NULL;
10457 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010458
Willy Tarreau601a4d12015-04-01 19:16:09 +020010459 if (args && args->type == ARGT_STR) {
10460 name = args->data.str.str;
10461 len = args->data.str.len;
10462 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010463
10464 CHECK_HTTP_MESSAGE_FIRST();
10465
Willy Tarreau15e91e12015-04-04 00:52:09 +020010466 idx = &strm->txn->hdr_idx;
10467 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10468
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010469 ctx.idx = 0;
10470 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010471 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010472 cnt++;
10473
10474 smp->type = SMP_T_UINT;
10475 smp->data.uint = cnt;
10476 smp->flags = SMP_F_VOL_HDR;
10477 return 1;
10478}
10479
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010480static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010481smp_fetch_hdr_names(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010482 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010483{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010484 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010485 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010486 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010487 struct chunk *temp;
10488 char del = ',';
10489
10490 if (args && args->type == ARGT_STR)
10491 del = *args[0].data.str.str;
10492
10493 CHECK_HTTP_MESSAGE_FIRST();
10494
Willy Tarreau15e91e12015-04-04 00:52:09 +020010495 idx = &strm->txn->hdr_idx;
10496 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10497
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010498 temp = get_trash_chunk();
10499
10500 ctx.idx = 0;
10501 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10502 if (temp->len)
10503 temp->str[temp->len++] = del;
10504 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10505 temp->len += ctx.del;
10506 }
10507
10508 smp->type = SMP_T_STR;
10509 smp->data.str.str = temp->str;
10510 smp->data.str.len = temp->len;
10511 smp->flags = SMP_F_VOL_HDR;
10512 return 1;
10513}
10514
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010515/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10516 * Accepts an optional argument of type string containing the header field name,
10517 * and an optional argument of type signed or unsigned integer to request an
10518 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010519 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010520 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010521static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010522smp_fetch_hdr(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010523 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010524{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010525 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010526 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010527 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010528 int occ = 0;
10529 const char *name_str = NULL;
10530 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010531
Willy Tarreaua890d072013-04-02 12:01:06 +020010532 if (!ctx) {
10533 /* first call */
10534 ctx = &static_hdr_ctx;
10535 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010536 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010537 }
10538
Willy Tarreau185b5c42012-04-26 15:11:51 +020010539 if (args) {
10540 if (args[0].type != ARGT_STR)
10541 return 0;
10542 name_str = args[0].data.str.str;
10543 name_len = args[0].data.str.len;
10544
10545 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10546 occ = args[1].data.uint;
10547 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010548
Willy Tarreaue333ec92012-04-16 16:26:40 +020010549 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010550
Willy Tarreau15e91e12015-04-04 00:52:09 +020010551 idx = &strm->txn->hdr_idx;
10552 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10553
Willy Tarreau185b5c42012-04-26 15:11:51 +020010554 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010555 /* search for header from the beginning */
10556 ctx->idx = 0;
10557
Willy Tarreau185b5c42012-04-26 15:11:51 +020010558 if (!occ && !(opt & SMP_OPT_ITERATE))
10559 /* no explicit occurrence and single fetch => last header by default */
10560 occ = -1;
10561
10562 if (!occ)
10563 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010564 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010565
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010566 smp->type = SMP_T_STR;
10567 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010568 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 +020010569 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010570
Willy Tarreau37406352012-04-23 16:16:37 +020010571 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010572 return 0;
10573}
10574
Willy Tarreauc11416f2007-06-17 16:58:38 +020010575/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010576 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010577 */
10578static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010579smp_fetch_hdr_cnt(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 Tarreau15e91e12015-04-04 00:52:09 +020010582 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010583 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010584 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010585 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010586 const char *name = NULL;
10587 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010588
Willy Tarreau601a4d12015-04-01 19:16:09 +020010589 if (args && args->type == ARGT_STR) {
10590 name = args->data.str.str;
10591 len = args->data.str.len;
10592 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010593
Willy Tarreaue333ec92012-04-16 16:26:40 +020010594 CHECK_HTTP_MESSAGE_FIRST();
10595
Willy Tarreau15e91e12015-04-04 00:52:09 +020010596 idx = &strm->txn->hdr_idx;
10597 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10598
Willy Tarreau33a7e692007-06-10 19:45:56 +020010599 ctx.idx = 0;
10600 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010601 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010602 cnt++;
10603
Willy Tarreauf853c462012-04-23 18:53:56 +020010604 smp->type = SMP_T_UINT;
10605 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010606 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010607 return 1;
10608}
10609
Willy Tarreau185b5c42012-04-26 15:11:51 +020010610/* Fetch an HTTP header's integer value. The integer value is returned. It
10611 * takes a mandatory argument of type string and an optional one of type int
10612 * to designate a specific occurrence. It returns an unsigned integer, which
10613 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010614 */
10615static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010616smp_fetch_hdr_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010617 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010618{
Willy Tarreau192252e2015-04-04 01:47:55 +020010619 int ret = smp_fetch_hdr(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010620
Willy Tarreauf853c462012-04-23 18:53:56 +020010621 if (ret > 0) {
10622 smp->type = SMP_T_UINT;
10623 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10624 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010625
Willy Tarreaud53e2422012-04-16 17:21:11 +020010626 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010627}
10628
Cyril Bonté69fa9922012-10-25 00:01:06 +020010629/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10630 * and an optional one of type int to designate a specific occurrence.
10631 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010632 */
10633static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010634smp_fetch_hdr_ip(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010635 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010636{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010637 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010638
Willy Tarreau192252e2015-04-04 01:47:55 +020010639 while ((ret = smp_fetch_hdr(px, sess, strm, opt, args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010640 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10641 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010642 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010643 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010644 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010645 if (smp->data.str.len < temp->size - 1) {
10646 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10647 temp->str[smp->data.str.len] = '\0';
10648 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10649 smp->type = SMP_T_IPV6;
10650 break;
10651 }
10652 }
10653 }
10654
Willy Tarreaud53e2422012-04-16 17:21:11 +020010655 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010656 if (!(smp->flags & SMP_F_NOT_LAST))
10657 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010658 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010659 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010660}
10661
Willy Tarreau737b0c12007-06-10 21:28:46 +020010662/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10663 * the first '/' after the possible hostname, and ends before the possible '?'.
10664 */
10665static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010666smp_fetch_path(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010667 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010668{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010669 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010670 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010671
Willy Tarreauc0239e02012-04-16 14:42:55 +020010672 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010673
Willy Tarreau15e91e12015-04-04 00:52:09 +020010674 txn = strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010675 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010676 ptr = http_get_path(txn);
10677 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010678 return 0;
10679
10680 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010681 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010682 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010683
10684 while (ptr < end && *ptr != '?')
10685 ptr++;
10686
Willy Tarreauf853c462012-04-23 18:53:56 +020010687 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010688 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010689 return 1;
10690}
10691
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010692/* This produces a concatenation of the first occurrence of the Host header
10693 * followed by the path component if it begins with a slash ('/'). This means
10694 * that '*' will not be added, resulting in exactly the first Host entry.
10695 * If no Host header is found, then the path is returned as-is. The returned
10696 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010697 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010698 */
10699static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010700smp_fetch_base(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010701 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010702{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010703 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010704 char *ptr, *end, *beg;
10705 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010706 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010707
10708 CHECK_HTTP_MESSAGE_FIRST();
10709
Willy Tarreau15e91e12015-04-04 00:52:09 +020010710 txn = strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010711 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010712 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreau192252e2015-04-04 01:47:55 +020010713 return smp_fetch_path(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010714
10715 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010716 temp = get_trash_chunk();
10717 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010718 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010719 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010720 smp->data.str.len = ctx.vlen;
10721
10722 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010723 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010724 beg = http_get_path(txn);
10725 if (!beg)
10726 beg = end;
10727
10728 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10729
10730 if (beg < ptr && *beg == '/') {
10731 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10732 smp->data.str.len += ptr - beg;
10733 }
10734
10735 smp->flags = SMP_F_VOL_1ST;
10736 return 1;
10737}
10738
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010739/* This produces a 32-bit hash of the concatenation of the first occurrence of
10740 * the Host header followed by the path component if it begins with a slash ('/').
10741 * This means that '*' will not be added, resulting in exactly the first Host
10742 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010743 * is hashed using the path hash followed by a full avalanche hash and provides a
10744 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010745 * high-traffic sites without having to store whole paths.
10746 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010747int
Willy Tarreau192252e2015-04-04 01:47:55 +020010748smp_fetch_base32(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010749 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010750{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010751 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010752 struct hdr_ctx ctx;
10753 unsigned int hash = 0;
10754 char *ptr, *beg, *end;
10755 int len;
10756
10757 CHECK_HTTP_MESSAGE_FIRST();
10758
Willy Tarreau15e91e12015-04-04 00:52:09 +020010759 txn = strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010760 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010761 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010762 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10763 ptr = ctx.line + ctx.val;
10764 len = ctx.vlen;
10765 while (len--)
10766 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10767 }
10768
10769 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010770 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010771 beg = http_get_path(txn);
10772 if (!beg)
10773 beg = end;
10774
10775 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10776
10777 if (beg < ptr && *beg == '/') {
10778 while (beg < ptr)
10779 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10780 }
10781 hash = full_hash(hash);
10782
10783 smp->type = SMP_T_UINT;
10784 smp->data.uint = hash;
10785 smp->flags = SMP_F_VOL_1ST;
10786 return 1;
10787}
10788
Willy Tarreau4a550602012-12-09 14:53:32 +010010789/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010790 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10791 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10792 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010793 * that in environments where IPv6 is insignificant, truncating the output to
10794 * 8 bytes would still work.
10795 */
10796static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010797smp_fetch_base32_src(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010798 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010799{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010800 struct chunk *temp;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020010801 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010802
10803 if (!cli_conn)
10804 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010805
Willy Tarreau192252e2015-04-04 01:47:55 +020010806 if (!smp_fetch_base32(px, sess, strm, opt, args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010807 return 0;
10808
Willy Tarreau47ca5452012-12-23 20:22:19 +010010809 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010810 *(unsigned int *)temp->str = htonl(smp->data.uint);
10811 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010812
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010813 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010814 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010815 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010816 temp->len += 4;
10817 break;
10818 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010819 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010820 temp->len += 16;
10821 break;
10822 default:
10823 return 0;
10824 }
10825
10826 smp->data.str = *temp;
10827 smp->type = SMP_T_BIN;
10828 return 1;
10829}
10830
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010831/* Extracts the query string, which comes after the question mark '?'. If no
10832 * question mark is found, nothing is returned. Otherwise it returns a sample
10833 * of type string carrying the whole query string.
10834 */
10835static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010836smp_fetch_query(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010837 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010838{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010839 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010840 char *ptr, *end;
10841
10842 CHECK_HTTP_MESSAGE_FIRST();
10843
Willy Tarreau15e91e12015-04-04 00:52:09 +020010844 txn = strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010845 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10846 end = ptr + txn->req.sl.rq.u_l;
10847
10848 /* look up the '?' */
10849 do {
10850 if (ptr == end)
10851 return 0;
10852 } while (*ptr++ != '?');
10853
10854 smp->type = SMP_T_STR;
10855 smp->data.str.str = ptr;
10856 smp->data.str.len = end - ptr;
10857 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10858 return 1;
10859}
10860
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010861static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010862smp_fetch_proto_http(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010863 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010864{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010865 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10866 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10867 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010868
Willy Tarreau24e32d82012-04-23 23:55:44 +020010869 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010870
Willy Tarreauf853c462012-04-23 18:53:56 +020010871 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010872 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010873 return 1;
10874}
10875
Willy Tarreau7f18e522010-10-22 20:04:13 +020010876/* return a valid test if the current request is the first one on the connection */
10877static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010878smp_fetch_http_first_req(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010879 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010880{
Willy Tarreauf853c462012-04-23 18:53:56 +020010881 smp->type = SMP_T_BOOL;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010882 smp->data.uint = !(strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010883 return 1;
10884}
10885
Willy Tarreau34db1082012-04-19 17:16:54 +020010886/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010887static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010888smp_fetch_http_auth(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010889 const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010890{
10891
Willy Tarreau24e32d82012-04-23 23:55:44 +020010892 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010893 return 0;
10894
Willy Tarreauc0239e02012-04-16 14:42:55 +020010895 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010896
Willy Tarreau15e91e12015-04-04 00:52:09 +020010897 if (!get_http_auth(strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010898 return 0;
10899
Willy Tarreauf853c462012-04-23 18:53:56 +020010900 smp->type = SMP_T_BOOL;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010901 smp->data.uint = check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010902 return 1;
10903}
Willy Tarreau8797c062007-05-07 00:55:35 +020010904
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010905/* Accepts exactly 1 argument of type userlist */
10906static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010907smp_fetch_http_auth_grp(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010908 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010909{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010910 if (!args || args->type != ARGT_USR)
10911 return 0;
10912
10913 CHECK_HTTP_MESSAGE_FIRST();
10914
Willy Tarreau15e91e12015-04-04 00:52:09 +020010915 if (!get_http_auth(strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010916 return 0;
10917
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010918 /* if the user does not belong to the userlist or has a wrong password,
10919 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010920 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010921 */
Willy Tarreau15e91e12015-04-04 00:52:09 +020010922 if (!check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010923 return 0;
10924
10925 /* pat_match_auth() will need the user list */
10926 smp->ctx.a[0] = args->data.usr;
10927
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010928 smp->type = SMP_T_STR;
10929 smp->flags = SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010930 smp->data.str.str = strm->txn->auth.user;
10931 smp->data.str.len = strlen(strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010932
10933 return 1;
10934}
10935
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010936/* Try to find the next occurrence of a cookie name in a cookie header value.
10937 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10938 * the cookie value is returned into *value and *value_l, and the function
10939 * returns a pointer to the next pointer to search from if the value was found.
10940 * Otherwise if the cookie was not found, NULL is returned and neither value
10941 * nor value_l are touched. The input <hdr> string should first point to the
10942 * header's value, and the <hdr_end> pointer must point to the first character
10943 * not part of the value. <list> must be non-zero if value may represent a list
10944 * of values (cookie headers). This makes it faster to abort parsing when no
10945 * list is expected.
10946 */
10947static char *
10948extract_cookie_value(char *hdr, const char *hdr_end,
10949 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010950 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010951{
10952 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10953 char *next;
10954
10955 /* we search at least a cookie name followed by an equal, and more
10956 * generally something like this :
10957 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10958 */
10959 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10960 /* Iterate through all cookies on this line */
10961
10962 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10963 att_beg++;
10964
10965 /* find att_end : this is the first character after the last non
10966 * space before the equal. It may be equal to hdr_end.
10967 */
10968 equal = att_end = att_beg;
10969
10970 while (equal < hdr_end) {
10971 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10972 break;
10973 if (http_is_spht[(unsigned char)*equal++])
10974 continue;
10975 att_end = equal;
10976 }
10977
10978 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10979 * is between <att_beg> and <equal>, both may be identical.
10980 */
10981
10982 /* look for end of cookie if there is an equal sign */
10983 if (equal < hdr_end && *equal == '=') {
10984 /* look for the beginning of the value */
10985 val_beg = equal + 1;
10986 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10987 val_beg++;
10988
10989 /* find the end of the value, respecting quotes */
10990 next = find_cookie_value_end(val_beg, hdr_end);
10991
10992 /* make val_end point to the first white space or delimitor after the value */
10993 val_end = next;
10994 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10995 val_end--;
10996 } else {
10997 val_beg = val_end = next = equal;
10998 }
10999
11000 /* We have nothing to do with attributes beginning with '$'. However,
11001 * they will automatically be removed if a header before them is removed,
11002 * since they're supposed to be linked together.
11003 */
11004 if (*att_beg == '$')
11005 continue;
11006
11007 /* Ignore cookies with no equal sign */
11008 if (equal == next)
11009 continue;
11010
11011 /* Now we have the cookie name between att_beg and att_end, and
11012 * its value between val_beg and val_end.
11013 */
11014
11015 if (att_end - att_beg == cookie_name_l &&
11016 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11017 /* let's return this value and indicate where to go on from */
11018 *value = val_beg;
11019 *value_l = val_end - val_beg;
11020 return next + 1;
11021 }
11022
11023 /* Set-Cookie headers only have the name in the first attr=value part */
11024 if (!list)
11025 break;
11026 }
11027
11028 return NULL;
11029}
11030
William Lallemanda43ba4e2014-01-28 18:14:25 +010011031/* Fetch a captured HTTP request header. The index is the position of
11032 * the "capture" option in the configuration file
11033 */
11034static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011035smp_fetch_capture_header_req(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011036 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011037{
Willy Tarreaud0d8da92015-04-04 02:10:38 +020011038 struct proxy *fe = strm_fe(strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011039 int idx;
11040
11041 if (!args || args->type != ARGT_UINT)
11042 return 0;
11043
11044 idx = args->data.uint;
11045
Willy Tarreau15e91e12015-04-04 00:52:09 +020011046 if (idx > (fe->nb_req_cap - 1) || strm->req_cap == NULL || strm->req_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011047 return 0;
11048
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011049 smp->type = SMP_T_STR;
11050 smp->flags |= SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011051 smp->data.str.str = strm->req_cap[idx];
11052 smp->data.str.len = strlen(strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011053
11054 return 1;
11055}
11056
11057/* Fetch a captured HTTP response header. The index is the position of
11058 * the "capture" option in the configuration file
11059 */
11060static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011061smp_fetch_capture_header_res(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011062 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011063{
Willy Tarreaud0d8da92015-04-04 02:10:38 +020011064 struct proxy *fe = strm_fe(strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011065 int idx;
11066
11067 if (!args || args->type != ARGT_UINT)
11068 return 0;
11069
11070 idx = args->data.uint;
11071
Willy Tarreau15e91e12015-04-04 00:52:09 +020011072 if (idx > (fe->nb_rsp_cap - 1) || strm->res_cap == NULL || strm->res_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011073 return 0;
11074
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011075 smp->type = SMP_T_STR;
11076 smp->flags |= SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011077 smp->data.str.str = strm->res_cap[idx];
11078 smp->data.str.len = strlen(strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011079
11080 return 1;
11081}
11082
William Lallemand65ad6e12014-01-31 15:08:02 +010011083/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11084static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011085smp_fetch_capture_req_method(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011086 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011087{
11088 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011089 struct http_txn *txn = strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011090 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011091
Willy Tarreau15e91e12015-04-04 00:52:09 +020011092 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011093 return 0;
11094
William Lallemand96a77852014-02-05 00:30:02 +010011095 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011096
William Lallemand96a77852014-02-05 00:30:02 +010011097 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11098 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011099
William Lallemand96a77852014-02-05 00:30:02 +010011100 temp = get_trash_chunk();
11101 temp->str = txn->uri;
11102 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011103 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011104 smp->type = SMP_T_STR;
11105 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011106
11107 return 1;
11108
11109}
11110
11111/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11112static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011113smp_fetch_capture_req_uri(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011114 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011115{
11116 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011117 struct http_txn *txn = strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011118 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011119
Willy Tarreau15e91e12015-04-04 00:52:09 +020011120 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011121 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011122
William Lallemand65ad6e12014-01-31 15:08:02 +010011123 ptr = txn->uri;
11124
11125 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11126 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011127
William Lallemand65ad6e12014-01-31 15:08:02 +010011128 if (!*ptr)
11129 return 0;
11130
11131 ptr++; /* skip the space */
11132
11133 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011134 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011135 if (!ptr)
11136 return 0;
11137 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11138 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011139
11140 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010011141 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011142 smp->type = SMP_T_STR;
11143 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011144
11145 return 1;
11146}
11147
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011148/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11149 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11150 */
11151static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011152smp_fetch_capture_req_ver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011153 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011154{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011155 struct http_txn *txn = strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011156
Willy Tarreau15e91e12015-04-04 00:52:09 +020011157 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011158 return 0;
11159
11160 if (txn->req.flags & HTTP_MSGF_VER_11)
11161 smp->data.str.str = "HTTP/1.1";
11162 else
11163 smp->data.str.str = "HTTP/1.0";
11164
11165 smp->data.str.len = 8;
11166 smp->type = SMP_T_STR;
11167 smp->flags = SMP_F_CONST;
11168 return 1;
11169
11170}
11171
11172/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11173 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11174 */
11175static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011176smp_fetch_capture_res_ver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011177 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011178{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011179 struct http_txn *txn = strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011180
Willy Tarreau15e91e12015-04-04 00:52:09 +020011181 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011182 return 0;
11183
11184 if (txn->rsp.flags & HTTP_MSGF_VER_11)
11185 smp->data.str.str = "HTTP/1.1";
11186 else
11187 smp->data.str.str = "HTTP/1.0";
11188
11189 smp->data.str.len = 8;
11190 smp->type = SMP_T_STR;
11191 smp->flags = SMP_F_CONST;
11192 return 1;
11193
11194}
11195
William Lallemand65ad6e12014-01-31 15:08:02 +010011196
Willy Tarreaue333ec92012-04-16 16:26:40 +020011197/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011198 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011199 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011200 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011201 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011202 * Accepts exactly 1 argument of type string. If the input options indicate
11203 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011204 * The returned sample is of type CSTR. Can be used to parse cookies in other
11205 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011206 */
Willy Tarreau192252e2015-04-04 01:47:55 +020011207int smp_fetch_cookie(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011208 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011209{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011210 struct http_txn *txn;
11211 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011212 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011213 const struct http_msg *msg;
11214 const char *hdr_name;
11215 int hdr_name_len;
11216 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011217 int occ = 0;
11218 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011219
Willy Tarreau24e32d82012-04-23 23:55:44 +020011220 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011221 return 0;
11222
Willy Tarreaua890d072013-04-02 12:01:06 +020011223 if (!ctx) {
11224 /* first call */
11225 ctx = &static_hdr_ctx;
11226 ctx->idx = 0;
11227 smp->ctx.a[2] = ctx;
11228 }
11229
Willy Tarreaue333ec92012-04-16 16:26:40 +020011230 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011231
Willy Tarreau15e91e12015-04-04 00:52:09 +020011232 txn = strm->txn;
11233 idx = &strm->txn->hdr_idx;
11234
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011235 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011236 msg = &txn->req;
11237 hdr_name = "Cookie";
11238 hdr_name_len = 6;
11239 } else {
11240 msg = &txn->rsp;
11241 hdr_name = "Set-Cookie";
11242 hdr_name_len = 10;
11243 }
11244
Willy Tarreau28376d62012-04-26 21:26:10 +020011245 if (!occ && !(opt & SMP_OPT_ITERATE))
11246 /* no explicit occurrence and single fetch => last cookie by default */
11247 occ = -1;
11248
11249 /* OK so basically here, either we want only one value and it's the
11250 * last one, or we want to iterate over all of them and we fetch the
11251 * next one.
11252 */
11253
Willy Tarreau9b28e032012-10-12 23:49:43 +020011254 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011255 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011256 /* search for the header from the beginning, we must first initialize
11257 * the search parameters.
11258 */
Willy Tarreau37406352012-04-23 16:16:37 +020011259 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011260 ctx->idx = 0;
11261 }
11262
Willy Tarreau28376d62012-04-26 21:26:10 +020011263 smp->flags |= SMP_F_VOL_HDR;
11264
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011265 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011266 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11267 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011268 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11269 goto out;
11270
Willy Tarreau24e32d82012-04-23 23:55:44 +020011271 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011272 continue;
11273
Willy Tarreau37406352012-04-23 16:16:37 +020011274 smp->ctx.a[0] = ctx->line + ctx->val;
11275 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011276 }
11277
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011278 smp->type = SMP_T_STR;
11279 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011280 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011281 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011282 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011283 &smp->data.str.str,
11284 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011285 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011286 found = 1;
11287 if (occ >= 0) {
11288 /* one value was returned into smp->data.str.{str,len} */
11289 smp->flags |= SMP_F_NOT_LAST;
11290 return 1;
11291 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011292 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011293 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011294 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011295 /* all cookie headers and values were scanned. If we're looking for the
11296 * last occurrence, we may return it now.
11297 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011298 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011299 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011300 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011301}
11302
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011303/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011304 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011305 * multiple cookies may be parsed on the same line. The returned sample is of
11306 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011307 */
11308static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011309smp_fetch_cookie_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011310 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011311{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011312 struct http_txn *txn;
11313 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011314 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011315 const struct http_msg *msg;
11316 const char *hdr_name;
11317 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011318 int cnt;
11319 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011320 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011321
Willy Tarreau24e32d82012-04-23 23:55:44 +020011322 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011323 return 0;
11324
Willy Tarreaue333ec92012-04-16 16:26:40 +020011325 CHECK_HTTP_MESSAGE_FIRST();
11326
Willy Tarreau15e91e12015-04-04 00:52:09 +020011327 txn = strm->txn;
11328 idx = &strm->txn->hdr_idx;
11329
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011330 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011331 msg = &txn->req;
11332 hdr_name = "Cookie";
11333 hdr_name_len = 6;
11334 } else {
11335 msg = &txn->rsp;
11336 hdr_name = "Set-Cookie";
11337 hdr_name_len = 10;
11338 }
11339
Willy Tarreau9b28e032012-10-12 23:49:43 +020011340 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011341 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011342 ctx.idx = 0;
11343 cnt = 0;
11344
11345 while (1) {
11346 /* Note: val_beg == NULL every time we need to fetch a new header */
11347 if (!val_beg) {
11348 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11349 break;
11350
Willy Tarreau24e32d82012-04-23 23:55:44 +020011351 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011352 continue;
11353
11354 val_beg = ctx.line + ctx.val;
11355 val_end = val_beg + ctx.vlen;
11356 }
11357
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011358 smp->type = SMP_T_STR;
11359 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011360 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011361 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011362 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011363 &smp->data.str.str,
11364 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011365 cnt++;
11366 }
11367 }
11368
Willy Tarreaub169eba2013-12-16 15:14:43 +010011369 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011370 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011371 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011372 return 1;
11373}
11374
Willy Tarreau51539362012-05-08 12:46:28 +020011375/* Fetch an cookie's integer value. The integer value is returned. It
11376 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11377 */
11378static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011379smp_fetch_cookie_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011380 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011381{
Willy Tarreau192252e2015-04-04 01:47:55 +020011382 int ret = smp_fetch_cookie(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011383
11384 if (ret > 0) {
11385 smp->type = SMP_T_UINT;
11386 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11387 }
11388
11389 return ret;
11390}
11391
Willy Tarreau8797c062007-05-07 00:55:35 +020011392/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011393/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011394/************************************************************************/
11395
David Cournapeau16023ee2010-12-23 20:55:41 +090011396/*
11397 * Given a path string and its length, find the position of beginning of the
11398 * query string. Returns NULL if no query string is found in the path.
11399 *
11400 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11401 *
11402 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11403 */
bedis4c75cca2012-10-05 08:38:24 +020011404static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011405{
11406 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011407
bedis4c75cca2012-10-05 08:38:24 +020011408 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011409 return p ? p + 1 : NULL;
11410}
11411
bedis4c75cca2012-10-05 08:38:24 +020011412static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011413{
bedis4c75cca2012-10-05 08:38:24 +020011414 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011415}
11416
11417/*
11418 * Given a url parameter, find the starting position of the first occurence,
11419 * or NULL if the parameter is not found.
11420 *
11421 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11422 * the function will return query_string+8.
11423 */
11424static char*
11425find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011426 char* url_param_name, size_t url_param_name_l,
11427 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011428{
11429 char *pos, *last;
11430
11431 pos = query_string;
11432 last = query_string + query_string_l - url_param_name_l - 1;
11433
11434 while (pos <= last) {
11435 if (pos[url_param_name_l] == '=') {
11436 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11437 return pos;
11438 pos += url_param_name_l + 1;
11439 }
bedis4c75cca2012-10-05 08:38:24 +020011440 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011441 pos++;
11442 pos++;
11443 }
11444 return NULL;
11445}
11446
11447/*
11448 * Given a url parameter name, returns its value and size into *value and
11449 * *value_l respectively, and returns non-zero. If the parameter is not found,
11450 * zero is returned and value/value_l are not touched.
11451 */
11452static int
11453find_url_param_value(char* path, size_t path_l,
11454 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011455 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011456{
11457 char *query_string, *qs_end;
11458 char *arg_start;
11459 char *value_start, *value_end;
11460
bedis4c75cca2012-10-05 08:38:24 +020011461 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011462 if (!query_string)
11463 return 0;
11464
11465 qs_end = path + path_l;
11466 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011467 url_param_name, url_param_name_l,
11468 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011469 if (!arg_start)
11470 return 0;
11471
11472 value_start = arg_start + url_param_name_l + 1;
11473 value_end = value_start;
11474
bedis4c75cca2012-10-05 08:38:24 +020011475 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011476 value_end++;
11477
11478 *value = value_start;
11479 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011480 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011481}
11482
11483static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011484smp_fetch_url_param(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011485 const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011486{
bedis4c75cca2012-10-05 08:38:24 +020011487 char delim = '?';
Willy Tarreau15e91e12015-04-04 00:52:09 +020011488 struct http_msg *msg;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011489
bedis4c75cca2012-10-05 08:38:24 +020011490 if (!args || args[0].type != ARGT_STR ||
11491 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011492 return 0;
11493
11494 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011495
Willy Tarreau15e91e12015-04-04 00:52:09 +020011496 msg = &strm->txn->req;
11497
bedis4c75cca2012-10-05 08:38:24 +020011498 if (args[1].type)
11499 delim = *args[1].data.str.str;
11500
Willy Tarreau9b28e032012-10-12 23:49:43 +020011501 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011502 args->data.str.str, args->data.str.len,
11503 &smp->data.str.str, &smp->data.str.len,
11504 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011505 return 0;
11506
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011507 smp->type = SMP_T_STR;
11508 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011509 return 1;
11510}
11511
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011512/* Return the signed integer value for the specified url parameter (see url_param
11513 * above).
11514 */
11515static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011516smp_fetch_url_param_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011517 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011518{
Willy Tarreau192252e2015-04-04 01:47:55 +020011519 int ret = smp_fetch_url_param(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011520
11521 if (ret > 0) {
11522 smp->type = SMP_T_UINT;
11523 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11524 }
11525
11526 return ret;
11527}
11528
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011529/* This produces a 32-bit hash of the concatenation of the first occurrence of
11530 * the Host header followed by the path component if it begins with a slash ('/').
11531 * This means that '*' will not be added, resulting in exactly the first Host
11532 * entry. If no Host header is found, then the path is used. The resulting value
11533 * is hashed using the url hash followed by a full avalanche hash and provides a
11534 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11535 * high-traffic sites without having to store whole paths.
11536 * this differs from the base32 functions in that it includes the url parameters
11537 * as well as the path
11538 */
11539static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011540smp_fetch_url32(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011541 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011542{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011543 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011544 struct hdr_ctx ctx;
11545 unsigned int hash = 0;
11546 char *ptr, *beg, *end;
11547 int len;
11548
11549 CHECK_HTTP_MESSAGE_FIRST();
11550
Willy Tarreau15e91e12015-04-04 00:52:09 +020011551 txn = strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011552 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011553 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011554 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11555 ptr = ctx.line + ctx.val;
11556 len = ctx.vlen;
11557 while (len--)
11558 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11559 }
11560
11561 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011562 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 +000011563 beg = http_get_path(txn);
11564 if (!beg)
11565 beg = end;
11566
11567 for (ptr = beg; ptr < end ; ptr++);
11568
11569 if (beg < ptr && *beg == '/') {
11570 while (beg < ptr)
11571 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11572 }
11573 hash = full_hash(hash);
11574
11575 smp->type = SMP_T_UINT;
11576 smp->data.uint = hash;
11577 smp->flags = SMP_F_VOL_1ST;
11578 return 1;
11579}
11580
11581/* This concatenates the source address with the 32-bit hash of the Host and
11582 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11583 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11584 * on the source address length. The URL hash is stored before the address so
11585 * that in environments where IPv6 is insignificant, truncating the output to
11586 * 8 bytes would still work.
11587 */
11588static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011589smp_fetch_url32_src(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011590 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011591{
11592 struct chunk *temp;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020011593 struct connection *cli_conn = objt_conn(sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011594
Willy Tarreau192252e2015-04-04 01:47:55 +020011595 if (!smp_fetch_url32(px, sess, strm, opt, args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011596 return 0;
11597
11598 temp = get_trash_chunk();
11599 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11600 temp->len += sizeof(smp->data.uint);
11601
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011602 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011603 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011604 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011605 temp->len += 4;
11606 break;
11607 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011608 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011609 temp->len += 16;
11610 break;
11611 default:
11612 return 0;
11613 }
11614
11615 smp->data.str = *temp;
11616 smp->type = SMP_T_BIN;
11617 return 1;
11618}
11619
Willy Tarreau185b5c42012-04-26 15:11:51 +020011620/* This function is used to validate the arguments passed to any "hdr" fetch
11621 * keyword. These keywords support an optional positive or negative occurrence
11622 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11623 * is assumed that the types are already the correct ones. Returns 0 on error,
11624 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11625 * error message in case of error, that the caller is responsible for freeing.
11626 * The initial location must either be freeable or NULL.
11627 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011628int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011629{
11630 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011631 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011632 return 0;
11633 }
11634 return 1;
11635}
11636
Willy Tarreau276fae92013-07-25 14:36:01 +020011637/* takes an UINT value on input supposed to represent the time since EPOCH,
11638 * adds an optional offset found in args[0] and emits a string representing
11639 * the date in RFC-1123/5322 format.
11640 */
Willy Tarreau87b09662015-04-03 00:22:06 +020011641static int sample_conv_http_date(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011642 struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011643{
11644 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11645 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11646 struct chunk *temp;
11647 struct tm *tm;
11648 time_t curr_date = smp->data.uint;
11649
11650 /* add offset */
11651 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11652 curr_date += args[0].data.sint;
11653
11654 tm = gmtime(&curr_date);
11655
11656 temp = get_trash_chunk();
11657 temp->len = snprintf(temp->str, temp->size - temp->len,
11658 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11659 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11660 tm->tm_hour, tm->tm_min, tm->tm_sec);
11661
11662 smp->data.str = *temp;
11663 smp->type = SMP_T_STR;
11664 return 1;
11665}
11666
Thierry FOURNIERad903512014-04-11 17:51:01 +020011667/* Match language range with language tag. RFC2616 14.4:
11668 *
11669 * A language-range matches a language-tag if it exactly equals
11670 * the tag, or if it exactly equals a prefix of the tag such
11671 * that the first tag character following the prefix is "-".
11672 *
11673 * Return 1 if the strings match, else return 0.
11674 */
11675static inline int language_range_match(const char *range, int range_len,
11676 const char *tag, int tag_len)
11677{
11678 const char *end = range + range_len;
11679 const char *tend = tag + tag_len;
11680 while (range < end) {
11681 if (*range == '-' && tag == tend)
11682 return 1;
11683 if (*range != *tag || tag == tend)
11684 return 0;
11685 range++;
11686 tag++;
11687 }
11688 /* Return true only if the last char of the tag is matched. */
11689 return tag == tend;
11690}
11691
11692/* Arguments: The list of expected value, the number of parts returned and the separator */
Willy Tarreau87b09662015-04-03 00:22:06 +020011693static int sample_conv_q_prefered(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011694 struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011695{
11696 const char *al = smp->data.str.str;
11697 const char *end = al + smp->data.str.len;
11698 const char *token;
11699 int toklen;
11700 int qvalue;
11701 const char *str;
11702 const char *w;
11703 int best_q = 0;
11704
11705 /* Set the constant to the sample, because the output of the
11706 * function will be peek in the constant configuration string.
11707 */
11708 smp->flags |= SMP_F_CONST;
11709 smp->data.str.size = 0;
11710 smp->data.str.str = "";
11711 smp->data.str.len = 0;
11712
11713 /* Parse the accept language */
11714 while (1) {
11715
11716 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011717 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011718 al++;
11719 if (al >= end)
11720 break;
11721
11722 /* Start of the fisrt word. */
11723 token = al;
11724
11725 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011726 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011727 al++;
11728 if (al == token)
11729 goto expect_comma;
11730
11731 /* Length of the token. */
11732 toklen = al - token;
11733 qvalue = 1000;
11734
11735 /* Check if the token exists in the list. If the token not exists,
11736 * jump to the next token.
11737 */
11738 str = args[0].data.str.str;
11739 w = str;
11740 while (1) {
11741 if (*str == ';' || *str == '\0') {
11742 if (language_range_match(token, toklen, w, str-w))
11743 goto look_for_q;
11744 if (*str == '\0')
11745 goto expect_comma;
11746 w = str + 1;
11747 }
11748 str++;
11749 }
11750 goto expect_comma;
11751
11752look_for_q:
11753
11754 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011755 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011756 al++;
11757 if (al >= end)
11758 goto process_value;
11759
11760 /* If ',' is found, process the result */
11761 if (*al == ',')
11762 goto process_value;
11763
11764 /* If the character is different from ';', look
11765 * for the end of the header part in best effort.
11766 */
11767 if (*al != ';')
11768 goto expect_comma;
11769
11770 /* Assumes that the char is ';', now expect "q=". */
11771 al++;
11772
11773 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011774 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011775 al++;
11776 if (al >= end)
11777 goto process_value;
11778
11779 /* Expect 'q'. If no 'q', continue in best effort */
11780 if (*al != 'q')
11781 goto process_value;
11782 al++;
11783
11784 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011785 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011786 al++;
11787 if (al >= end)
11788 goto process_value;
11789
11790 /* Expect '='. If no '=', continue in best effort */
11791 if (*al != '=')
11792 goto process_value;
11793 al++;
11794
11795 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011796 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011797 al++;
11798 if (al >= end)
11799 goto process_value;
11800
11801 /* Parse the q value. */
11802 qvalue = parse_qvalue(al, &al);
11803
11804process_value:
11805
11806 /* If the new q value is the best q value, then store the associated
11807 * language in the response. If qvalue is the biggest value (1000),
11808 * break the process.
11809 */
11810 if (qvalue > best_q) {
11811 smp->data.str.str = (char *)w;
11812 smp->data.str.len = str - w;
11813 if (qvalue >= 1000)
11814 break;
11815 best_q = qvalue;
11816 }
11817
11818expect_comma:
11819
11820 /* Expect comma or end. If the end is detected, quit the loop. */
11821 while (al < end && *al != ',')
11822 al++;
11823 if (al >= end)
11824 break;
11825
11826 /* Comma is found, jump it and restart the analyzer. */
11827 al++;
11828 }
11829
11830 /* Set default value if required. */
11831 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11832 smp->data.str.str = args[1].data.str.str;
11833 smp->data.str.len = args[1].data.str.len;
11834 }
11835
11836 /* Return true only if a matching language was found. */
11837 return smp->data.str.len != 0;
11838}
11839
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011840/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011841 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011842 * the relevant part of the request line accordingly. Then it updates various
11843 * pointers to the next elements which were moved, and the total buffer length.
11844 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011845 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11846 * error, though this can be revisited when this code is finally exploited.
11847 *
11848 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11849 * query string and 3 to replace uri.
11850 *
11851 * In query string case, the mark question '?' must be set at the start of the
11852 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011853 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011854int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011855 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011856{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011857 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011858 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011859 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011860 int delta;
11861
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011862 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011863 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011864 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011865 cur_end = cur_ptr + txn->req.sl.rq.m_l;
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.m_l += delta;
11870 txn->req.sl.rq.u += delta;
11871 txn->req.sl.rq.v += delta;
11872 break;
11873
11874 case 1: // path
11875 cur_ptr = http_get_path(txn);
11876 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011877 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011878
11879 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011880 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 +010011881 cur_end++;
11882
11883 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011884 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011885 txn->req.sl.rq.u_l += delta;
11886 txn->req.sl.rq.v += delta;
11887 break;
11888
11889 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011890 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011891 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011892 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11893 while (cur_ptr < cur_end && *cur_ptr != '?')
11894 cur_ptr++;
11895
11896 /* skip the question mark or indicate that we must insert it
11897 * (but only if the format string is not empty then).
11898 */
11899 if (cur_ptr < cur_end)
11900 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011901 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011902 offset = 0;
11903
11904 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011905 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011906 txn->req.sl.rq.u_l += delta;
11907 txn->req.sl.rq.v += delta;
11908 break;
11909
11910 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011911 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011912 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11913
11914 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011915 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011916 txn->req.sl.rq.u_l += delta;
11917 txn->req.sl.rq.v += delta;
11918 break;
11919
11920 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011921 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011922 }
11923
11924 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011925 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011926 txn->req.sl.rq.l += delta;
11927 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011928 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011929 return 0;
11930}
11931
11932/* This function executes one of the set-{method,path,query,uri} actions. It
11933 * builds a string in the trash from the specified format string. It finds
11934 * the action to be performed in p[2], previously filled by function
11935 * parse_set_req_line(). The replacement action is excuted by the function
11936 * http_action_set_req_line_exec(). It always returns 1. If an error occurs
11937 * the action is canceled, but the rule processing continue.
11938 */
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011939int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011940{
11941 chunk_reset(&trash);
11942
11943 /* If we have to create a query string, prepare a '?'. */
11944 if (*(int *)&rule->arg.act.p[2] == 2)
11945 trash.str[trash.len++] = '?';
11946 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
11947
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011948 http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s);
Thierry FOURNIER01c30122015-03-14 14:14:47 +010011949 return 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011950}
11951
11952/* parse an http-request action among :
11953 * set-method
11954 * set-path
11955 * set-query
11956 * set-uri
11957 *
11958 * All of them accept a single argument of type string representing a log-format.
11959 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11960 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
11961 * It returns 0 on success, < 0 on error.
11962 */
11963int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
11964{
11965 int cur_arg = *orig_arg;
11966
11967 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
11968
11969 switch (args[0][4]) {
11970 case 'm' :
11971 *(int *)&rule->arg.act.p[2] = 0;
11972 rule->action_ptr = http_action_set_req_line;
11973 break;
11974 case 'p' :
11975 *(int *)&rule->arg.act.p[2] = 1;
11976 rule->action_ptr = http_action_set_req_line;
11977 break;
11978 case 'q' :
11979 *(int *)&rule->arg.act.p[2] = 2;
11980 rule->action_ptr = http_action_set_req_line;
11981 break;
11982 case 'u' :
11983 *(int *)&rule->arg.act.p[2] = 3;
11984 rule->action_ptr = http_action_set_req_line;
11985 break;
11986 default:
11987 memprintf(err, "internal error: unhandled action '%s'", args[0]);
11988 return -1;
11989 }
11990
11991 if (!*args[cur_arg] ||
11992 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11993 memprintf(err, "expects exactly 1 argument <format>");
11994 return -1;
11995 }
11996
11997 LIST_INIT((struct list *)&rule->arg.act.p[0]);
11998 proxy->conf.args.ctx = ARGC_HRQ;
11999 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
12000 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12001 proxy->conf.args.file, proxy->conf.args.line);
12002
12003 (*orig_arg)++;
12004 return 0;
12005}
12006
William Lallemand73025dd2014-04-24 14:38:37 +020012007/*
12008 * Return the struct http_req_action_kw associated to a keyword.
12009 */
12010struct http_req_action_kw *action_http_req_custom(const char *kw)
12011{
12012 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
12013 struct http_req_action_kw_list *kw_list;
12014 int i;
12015
12016 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
12017 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
12018 if (!strcmp(kw, kw_list->kw[i].kw))
12019 return &kw_list->kw[i];
12020 }
12021 }
12022 }
12023 return NULL;
12024}
12025
12026/*
12027 * Return the struct http_res_action_kw associated to a keyword.
12028 */
12029struct http_res_action_kw *action_http_res_custom(const char *kw)
12030{
12031 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
12032 struct http_res_action_kw_list *kw_list;
12033 int i;
12034
12035 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
12036 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
12037 if (!strcmp(kw, kw_list->kw[i].kw))
12038 return &kw_list->kw[i];
12039 }
12040 }
12041 }
12042 return NULL;
12043}
12044
Willy Tarreau4a568972010-05-12 08:08:50 +020012045/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012046/* All supported ACL keywords must be declared here. */
12047/************************************************************************/
12048
12049/* Note: must not be declared <const> as its list will be overwritten.
12050 * Please take care of keeping this list alphabetically sorted.
12051 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012052static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012053 { "base", "base", PAT_MATCH_STR },
12054 { "base_beg", "base", PAT_MATCH_BEG },
12055 { "base_dir", "base", PAT_MATCH_DIR },
12056 { "base_dom", "base", PAT_MATCH_DOM },
12057 { "base_end", "base", PAT_MATCH_END },
12058 { "base_len", "base", PAT_MATCH_LEN },
12059 { "base_reg", "base", PAT_MATCH_REG },
12060 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012061
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012062 { "cook", "req.cook", PAT_MATCH_STR },
12063 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12064 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12065 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12066 { "cook_end", "req.cook", PAT_MATCH_END },
12067 { "cook_len", "req.cook", PAT_MATCH_LEN },
12068 { "cook_reg", "req.cook", PAT_MATCH_REG },
12069 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012070
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012071 { "hdr", "req.hdr", PAT_MATCH_STR },
12072 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12073 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12074 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12075 { "hdr_end", "req.hdr", PAT_MATCH_END },
12076 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12077 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12078 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012079
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012080 /* these two declarations uses strings with list storage (in place
12081 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12082 * and delete functions are relative to the list management. The parse
12083 * and match method are related to the corresponding fetch methods. This
12084 * is very particular ACL declaration mode.
12085 */
12086 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12087 { "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 +020012088
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012089 { "path", "path", PAT_MATCH_STR },
12090 { "path_beg", "path", PAT_MATCH_BEG },
12091 { "path_dir", "path", PAT_MATCH_DIR },
12092 { "path_dom", "path", PAT_MATCH_DOM },
12093 { "path_end", "path", PAT_MATCH_END },
12094 { "path_len", "path", PAT_MATCH_LEN },
12095 { "path_reg", "path", PAT_MATCH_REG },
12096 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012097
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012098 { "req_ver", "req.ver", PAT_MATCH_STR },
12099 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012100
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012101 { "scook", "res.cook", PAT_MATCH_STR },
12102 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12103 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12104 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12105 { "scook_end", "res.cook", PAT_MATCH_END },
12106 { "scook_len", "res.cook", PAT_MATCH_LEN },
12107 { "scook_reg", "res.cook", PAT_MATCH_REG },
12108 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012109
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012110 { "shdr", "res.hdr", PAT_MATCH_STR },
12111 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12112 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12113 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12114 { "shdr_end", "res.hdr", PAT_MATCH_END },
12115 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12116 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12117 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012118
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012119 { "url", "url", PAT_MATCH_STR },
12120 { "url_beg", "url", PAT_MATCH_BEG },
12121 { "url_dir", "url", PAT_MATCH_DIR },
12122 { "url_dom", "url", PAT_MATCH_DOM },
12123 { "url_end", "url", PAT_MATCH_END },
12124 { "url_len", "url", PAT_MATCH_LEN },
12125 { "url_reg", "url", PAT_MATCH_REG },
12126 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012127
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012128 { "urlp", "urlp", PAT_MATCH_STR },
12129 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12130 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12131 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12132 { "urlp_end", "urlp", PAT_MATCH_END },
12133 { "urlp_len", "urlp", PAT_MATCH_LEN },
12134 { "urlp_reg", "urlp", PAT_MATCH_REG },
12135 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012136
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012137 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012138}};
12139
12140/************************************************************************/
12141/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012142/************************************************************************/
12143/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012144static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012145 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012146 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12147 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12148
Willy Tarreau87b09662015-04-03 00:22:06 +020012149 /* capture are allocated and are permanent in the stream */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012150 { "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 +020012151
12152 /* retrieve these captures from the HTTP logs */
12153 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12154 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12155 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12156
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012157 { "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 +020012158 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012159
Willy Tarreau409bcde2013-01-08 00:31:00 +010012160 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12161 * are only here to match the ACL's name, are request-only and are used
12162 * for ACL compatibility only.
12163 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012164 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12165 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012166 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12167 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12168
12169 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12170 * only here to match the ACL's name, are request-only and are used for
12171 * ACL compatibility only.
12172 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012173 { "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 +010012174 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12175 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
12176 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12177
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012178 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012179 { "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 +010012180 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012181 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012182 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012183 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012184
12185 /* HTTP protocol on the request path */
12186 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012187 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012188
12189 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012190 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12191 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012192
12193 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012194 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12195 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012196
Willy Tarreau18ed2562013-01-14 15:56:36 +010012197 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012198 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012199 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12200 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12201
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012202 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012203 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012204 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012205 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12206 { "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 +010012207 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012208 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12209
12210 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012211 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012212 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12213 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12214
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012215 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012216 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012217 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012218 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12219 { "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 +010012220 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012221 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12222
Willy Tarreau409bcde2013-01-08 00:31:00 +010012223 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012224 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012225 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12226 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012227 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012228
12229 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012230 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012231 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12232 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
12233 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12234
12235 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012236 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012237 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12238 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012239 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
12240 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012241 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12242 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012243 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12244 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012245}};
12246
Willy Tarreau8797c062007-05-07 00:55:35 +020012247
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012248/************************************************************************/
12249/* All supported converter keywords must be declared here. */
12250/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012251/* Note: must not be declared <const> as its list will be overwritten */
12252static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020012253 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
12254 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012255 { NULL, NULL, 0, 0, 0 },
12256}};
12257
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012258/************************************************************************/
12259/* All supported http-request action keywords must be declared here. */
12260/************************************************************************/
12261struct http_req_action_kw_list http_req_actions = {
12262 .scope = "http",
12263 .kw = {
12264 { "set-method", parse_set_req_line },
12265 { "set-path", parse_set_req_line },
12266 { "set-query", parse_set_req_line },
12267 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012268 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012269 }
12270};
12271
Willy Tarreau8797c062007-05-07 00:55:35 +020012272__attribute__((constructor))
12273static void __http_protocol_init(void)
12274{
12275 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012276 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012277 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012278 http_req_keywords_register(&http_req_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012279}
12280
12281
Willy Tarreau58f10d72006-12-04 02:26:12 +010012282/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012283 * Local variables:
12284 * c-indent-level: 8
12285 * c-basic-offset: 8
12286 * End:
12287 */