blob: a60c03c51ef75f1c4c5bd18f820f819aeb17e897 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020064#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau522d6c02009-12-06 18:49:18 +010069const char HTTP_100[] =
70 "HTTP/1.1 100 Continue\r\n\r\n";
71
72const struct chunk http_100_chunk = {
73 .str = (char *)&HTTP_100,
74 .len = sizeof(HTTP_100)-1
75};
76
Willy Tarreaua9679ac2010-01-03 17:32:57 +010077/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040096
97/* same as 302 except that the browser MUST retry with the same method */
98const char *HTTP_307 =
99 "HTTP/1.1 307 Temporary Redirect\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Content-length: 0\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
104/* same as 301 except that the browser MUST retry with the same method */
105const char *HTTP_308 =
106 "HTTP/1.1 308 Permanent Redirect\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau844a7e72010-01-31 21:46:18 +0100120const char *HTTP_407_fmt =
121 "HTTP/1.0 407 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
124 "Content-Type: text/html\r\n"
125 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800127 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400134 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400136 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100137 [HTTP_ERR_500] = 500,
138 [HTTP_ERR_502] = 502,
139 [HTTP_ERR_503] = 503,
140 [HTTP_ERR_504] = 504,
141};
142
Willy Tarreau80587432006-12-24 17:47:20 +0100143static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200144 [HTTP_ERR_200] =
145 "HTTP/1.0 200 OK\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
151
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100153 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
159
160 [HTTP_ERR_403] =
161 "HTTP/1.0 403 Forbidden\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
167
CJ Ess108b1dd2015-04-07 12:03:37 -0400168 [HTTP_ERR_405] =
169 "HTTP/1.0 405 Method Not Allowed\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
175
Willy Tarreau0f772532006-12-23 20:51:41 +0100176 [HTTP_ERR_408] =
177 "HTTP/1.0 408 Request Time-out\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
183
CJ Ess108b1dd2015-04-07 12:03:37 -0400184 [HTTP_ERR_429] =
185 "HTTP/1.0 429 Too Many Requests\r\n"
186 "Cache-Control: no-cache\r\n"
187 "Connection: close\r\n"
188 "Content-Type: text/html\r\n"
189 "\r\n"
190 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
191
Willy Tarreau0f772532006-12-23 20:51:41 +0100192 [HTTP_ERR_500] =
193 "HTTP/1.0 500 Server Error\r\n"
194 "Cache-Control: no-cache\r\n"
195 "Connection: close\r\n"
196 "Content-Type: text/html\r\n"
197 "\r\n"
198 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
199
200 [HTTP_ERR_502] =
201 "HTTP/1.0 502 Bad Gateway\r\n"
202 "Cache-Control: no-cache\r\n"
203 "Connection: close\r\n"
204 "Content-Type: text/html\r\n"
205 "\r\n"
206 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
207
208 [HTTP_ERR_503] =
209 "HTTP/1.0 503 Service Unavailable\r\n"
210 "Cache-Control: no-cache\r\n"
211 "Connection: close\r\n"
212 "Content-Type: text/html\r\n"
213 "\r\n"
214 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
215
216 [HTTP_ERR_504] =
217 "HTTP/1.0 504 Gateway Time-out\r\n"
218 "Cache-Control: no-cache\r\n"
219 "Connection: close\r\n"
220 "Content-Type: text/html\r\n"
221 "\r\n"
222 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
223
224};
225
Cyril Bonté19979e12012-04-04 12:57:21 +0200226/* status codes available for the stats admin page (strictly 4 chars length) */
227const char *stat_status_codes[STAT_STATUS_SIZE] = {
228 [STAT_STATUS_DENY] = "DENY",
229 [STAT_STATUS_DONE] = "DONE",
230 [STAT_STATUS_ERRP] = "ERRP",
231 [STAT_STATUS_EXCD] = "EXCD",
232 [STAT_STATUS_NONE] = "NONE",
233 [STAT_STATUS_PART] = "PART",
234 [STAT_STATUS_UNKN] = "UNKN",
235};
236
237
William Lallemand73025dd2014-04-24 14:38:37 +0200238/* List head of all known action keywords for "http-request" */
239struct http_req_action_kw_list http_req_keywords = {
240 .list = LIST_HEAD_INIT(http_req_keywords.list)
241};
242
243/* List head of all known action keywords for "http-response" */
244struct http_res_action_kw_list http_res_keywords = {
245 .list = LIST_HEAD_INIT(http_res_keywords.list)
246};
247
Willy Tarreau80587432006-12-24 17:47:20 +0100248/* We must put the messages here since GCC cannot initialize consts depending
249 * on strlen().
250 */
251struct chunk http_err_chunks[HTTP_ERR_SIZE];
252
Willy Tarreaua890d072013-04-02 12:01:06 +0200253/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
254static struct hdr_ctx static_hdr_ctx;
255
Willy Tarreau42250582007-04-01 01:30:43 +0200256#define FD_SETS_ARE_BITFIELDS
257#ifdef FD_SETS_ARE_BITFIELDS
258/*
259 * This map is used with all the FD_* macros to check whether a particular bit
260 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
261 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
262 * byte should be encoded. Be careful to always pass bytes from 0 to 255
263 * exclusively to the macros.
264 */
265fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
266fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100267fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200268
269#else
270#error "Check if your OS uses bitfields for fd_sets"
271#endif
272
Willy Tarreau87b09662015-04-03 00:22:06 +0200273static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200274
Willy Tarreau80587432006-12-24 17:47:20 +0100275void init_proto_http()
276{
Willy Tarreau42250582007-04-01 01:30:43 +0200277 int i;
278 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100279 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200280
Willy Tarreau80587432006-12-24 17:47:20 +0100281 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
282 if (!http_err_msgs[msg]) {
283 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
284 abort();
285 }
286
287 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
288 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
289 }
Willy Tarreau42250582007-04-01 01:30:43 +0200290
291 /* initialize the log header encoding map : '{|}"#' should be encoded with
292 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
293 * URL encoding only requires '"', '#' to be encoded as well as non-
294 * printable characters above.
295 */
296 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
297 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100298 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200299 for (i = 0; i < 32; i++) {
300 FD_SET(i, hdr_encode_map);
301 FD_SET(i, url_encode_map);
302 }
303 for (i = 127; i < 256; i++) {
304 FD_SET(i, hdr_encode_map);
305 FD_SET(i, url_encode_map);
306 }
307
308 tmp = "\"#{|}";
309 while (*tmp) {
310 FD_SET(*tmp, hdr_encode_map);
311 tmp++;
312 }
313
314 tmp = "\"#";
315 while (*tmp) {
316 FD_SET(*tmp, url_encode_map);
317 tmp++;
318 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200319
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100320 /* initialize the http header encoding map. The draft httpbis define the
321 * header content as:
322 *
323 * HTTP-message = start-line
324 * *( header-field CRLF )
325 * CRLF
326 * [ message-body ]
327 * header-field = field-name ":" OWS field-value OWS
328 * field-value = *( field-content / obs-fold )
329 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
330 * obs-fold = CRLF 1*( SP / HTAB )
331 * field-vchar = VCHAR / obs-text
332 * VCHAR = %x21-7E
333 * obs-text = %x80-FF
334 *
335 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
336 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
337 * "obs-fold" is volontary forgotten because haproxy remove this.
338 */
339 memset(http_encode_map, 0, sizeof(http_encode_map));
340 for (i = 0x00; i <= 0x08; i++)
341 FD_SET(i, http_encode_map);
342 for (i = 0x0a; i <= 0x1f; i++)
343 FD_SET(i, http_encode_map);
344 FD_SET(0x7f, http_encode_map);
345
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200346 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200347 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200348 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100349 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100350}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351
Willy Tarreau53b6c742006-12-17 13:37:46 +0100352/*
353 * We have 26 list of methods (1 per first letter), each of which can have
354 * up to 3 entries (2 valid, 1 null).
355 */
356struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100357 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100358 int len;
359 const char text[8];
360};
361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100362const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100363 ['C' - 'A'] = {
364 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
365 },
366 ['D' - 'A'] = {
367 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
368 },
369 ['G' - 'A'] = {
370 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
371 },
372 ['H' - 'A'] = {
373 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
374 },
375 ['P' - 'A'] = {
376 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
377 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
378 },
379 ['T' - 'A'] = {
380 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
381 },
382 /* rest is empty like this :
383 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
384 */
385};
386
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100387const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
388 [HTTP_METH_NONE] = { "", 0 },
389 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
390 [HTTP_METH_GET] = { "GET", 3 },
391 [HTTP_METH_HEAD] = { "HEAD", 4 },
392 [HTTP_METH_POST] = { "POST", 4 },
393 [HTTP_METH_PUT] = { "PUT", 3 },
394 [HTTP_METH_DELETE] = { "DELETE", 6 },
395 [HTTP_METH_TRACE] = { "TRACE", 5 },
396 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
397};
398
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100399/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200400 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100401 * RFC2616 for those chars.
402 */
403
404const char http_is_spht[256] = {
405 [' '] = 1, ['\t'] = 1,
406};
407
408const char http_is_crlf[256] = {
409 ['\r'] = 1, ['\n'] = 1,
410};
411
412const char http_is_lws[256] = {
413 [' '] = 1, ['\t'] = 1,
414 ['\r'] = 1, ['\n'] = 1,
415};
416
417const char http_is_sep[256] = {
418 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
419 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
420 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
421 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
422 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
423};
424
425const char http_is_ctl[256] = {
426 [0 ... 31] = 1,
427 [127] = 1,
428};
429
430/*
431 * A token is any ASCII char that is neither a separator nor a CTL char.
432 * Do not overwrite values in assignment since gcc-2.95 will not handle
433 * them correctly. Instead, define every non-CTL char's status.
434 */
435const char http_is_token[256] = {
436 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
437 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
438 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
439 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
440 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
441 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
442 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
443 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
444 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
445 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
446 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
447 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
448 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
449 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
450 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
451 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
452 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
453 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
454 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
455 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
456 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
457 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
458 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
459 ['|'] = 1, ['}'] = 0, ['~'] = 1,
460};
461
462
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100463/*
464 * An http ver_token is any ASCII which can be found in an HTTP version,
465 * which includes 'H', 'T', 'P', '/', '.' and any digit.
466 */
467const char http_is_ver_token[256] = {
468 ['.'] = 1, ['/'] = 1,
469 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
470 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100471 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100472};
473
474
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100475/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100476 * Adds a header and its CRLF at the tail of the message's buffer, just before
477 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100478 * The header is also automatically added to the index <hdr_idx>, and the end
479 * of headers is automatically adjusted. The number of bytes added is returned
480 * on success, otherwise <0 is returned indicating an error.
481 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100482int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100483{
484 int bytes, len;
485
486 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200487 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100488 if (!bytes)
489 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100490 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100491 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
492}
493
494/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100495 * Adds a header and its CRLF at the tail of the message's buffer, just before
496 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100497 * the buffer is only opened and the space reserved, but nothing is copied.
498 * The header is also automatically added to the index <hdr_idx>, and the end
499 * of headers is automatically adjusted. The number of bytes added is returned
500 * on success, otherwise <0 is returned indicating an error.
501 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100502int http_header_add_tail2(struct http_msg *msg,
503 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100504{
505 int bytes;
506
Willy Tarreau9b28e032012-10-12 23:49:43 +0200507 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100508 if (!bytes)
509 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100510 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100511 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
512}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200513
514/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100515 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
516 * If so, returns the position of the first non-space character relative to
517 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
518 * to return a pointer to the place after the first space. Returns 0 if the
519 * header name does not match. Checks are case-insensitive.
520 */
521int http_header_match2(const char *hdr, const char *end,
522 const char *name, int len)
523{
524 const char *val;
525
526 if (hdr + len >= end)
527 return 0;
528 if (hdr[len] != ':')
529 return 0;
530 if (strncasecmp(hdr, name, len) != 0)
531 return 0;
532 val = hdr + len + 1;
533 while (val < end && HTTP_IS_SPHT(*val))
534 val++;
535 if ((val >= end) && (len + 2 <= end - hdr))
536 return len + 2; /* we may replace starting from second space */
537 return val - hdr;
538}
539
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200540/* Find the first or next occurrence of header <name> in message buffer <sol>
541 * using headers index <idx>, and return it in the <ctx> structure. This
542 * structure holds everything necessary to use the header and find next
543 * occurrence. If its <idx> member is 0, the header is searched from the
544 * beginning. Otherwise, the next occurrence is returned. The function returns
545 * 1 when it finds a value, and 0 when there is no more. It is very similar to
546 * http_find_header2() except that it is designed to work with full-line headers
547 * whose comma is not a delimiter but is part of the syntax. As a special case,
548 * if ctx->val is NULL when searching for a new values of a header, the current
549 * header is rescanned. This allows rescanning after a header deletion.
550 */
551int http_find_full_header2(const char *name, int len,
552 char *sol, struct hdr_idx *idx,
553 struct hdr_ctx *ctx)
554{
555 char *eol, *sov;
556 int cur_idx, old_idx;
557
558 cur_idx = ctx->idx;
559 if (cur_idx) {
560 /* We have previously returned a header, let's search another one */
561 sol = ctx->line;
562 eol = sol + idx->v[cur_idx].len;
563 goto next_hdr;
564 }
565
566 /* first request for this header */
567 sol += hdr_idx_first_pos(idx);
568 old_idx = 0;
569 cur_idx = hdr_idx_first_idx(idx);
570 while (cur_idx) {
571 eol = sol + idx->v[cur_idx].len;
572
573 if (len == 0) {
574 /* No argument was passed, we want any header.
575 * To achieve this, we simply build a fake request. */
576 while (sol + len < eol && sol[len] != ':')
577 len++;
578 name = sol;
579 }
580
581 if ((len < eol - sol) &&
582 (sol[len] == ':') &&
583 (strncasecmp(sol, name, len) == 0)) {
584 ctx->del = len;
585 sov = sol + len + 1;
586 while (sov < eol && http_is_lws[(unsigned char)*sov])
587 sov++;
588
589 ctx->line = sol;
590 ctx->prev = old_idx;
591 ctx->idx = cur_idx;
592 ctx->val = sov - sol;
593 ctx->tws = 0;
594 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
595 eol--;
596 ctx->tws++;
597 }
598 ctx->vlen = eol - sov;
599 return 1;
600 }
601 next_hdr:
602 sol = eol + idx->v[cur_idx].cr + 1;
603 old_idx = cur_idx;
604 cur_idx = idx->v[cur_idx].next;
605 }
606 return 0;
607}
608
Willy Tarreauc90dc232015-02-20 13:51:36 +0100609/* Find the first or next header field in message buffer <sol> using headers
610 * index <idx>, and return it in the <ctx> structure. This structure holds
611 * everything necessary to use the header and find next occurrence. If its
612 * <idx> member is 0, the first header is retrieved. Otherwise, the next
613 * occurrence is returned. The function returns 1 when it finds a value, and
614 * 0 when there is no more. It is equivalent to http_find_full_header2() with
615 * no header name.
616 */
617int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
618{
619 char *eol, *sov;
620 int cur_idx, old_idx;
621 int len;
622
623 cur_idx = ctx->idx;
624 if (cur_idx) {
625 /* We have previously returned a header, let's search another one */
626 sol = ctx->line;
627 eol = sol + idx->v[cur_idx].len;
628 goto next_hdr;
629 }
630
631 /* first request for this header */
632 sol += hdr_idx_first_pos(idx);
633 old_idx = 0;
634 cur_idx = hdr_idx_first_idx(idx);
635 while (cur_idx) {
636 eol = sol + idx->v[cur_idx].len;
637
638 len = 0;
639 while (1) {
640 if (len >= eol - sol)
641 goto next_hdr;
642 if (sol[len] == ':')
643 break;
644 len++;
645 }
646
647 ctx->del = len;
648 sov = sol + len + 1;
649 while (sov < eol && http_is_lws[(unsigned char)*sov])
650 sov++;
651
652 ctx->line = sol;
653 ctx->prev = old_idx;
654 ctx->idx = cur_idx;
655 ctx->val = sov - sol;
656 ctx->tws = 0;
657
658 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
659 eol--;
660 ctx->tws++;
661 }
662 ctx->vlen = eol - sov;
663 return 1;
664
665 next_hdr:
666 sol = eol + idx->v[cur_idx].cr + 1;
667 old_idx = cur_idx;
668 cur_idx = idx->v[cur_idx].next;
669 }
670 return 0;
671}
672
Willy Tarreau68085d82010-01-18 14:54:04 +0100673/* Find the end of the header value contained between <s> and <e>. See RFC2616,
674 * par 2.2 for more information. Note that it requires a valid header to return
675 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200676 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100677char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200678{
679 int quoted, qdpair;
680
681 quoted = qdpair = 0;
682 for (; s < e; s++) {
683 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200684 else if (quoted) {
685 if (*s == '\\') qdpair = 1;
686 else if (*s == '"') quoted = 0;
687 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200688 else if (*s == '"') quoted = 1;
689 else if (*s == ',') return s;
690 }
691 return s;
692}
693
694/* Find the first or next occurrence of header <name> in message buffer <sol>
695 * using headers index <idx>, and return it in the <ctx> structure. This
696 * structure holds everything necessary to use the header and find next
697 * occurrence. If its <idx> member is 0, the header is searched from the
698 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100699 * 1 when it finds a value, and 0 when there is no more. It is designed to work
700 * with headers defined as comma-separated lists. As a special case, if ctx->val
701 * is NULL when searching for a new values of a header, the current header is
702 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200703 */
704int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100705 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200706 struct hdr_ctx *ctx)
707{
Willy Tarreau68085d82010-01-18 14:54:04 +0100708 char *eol, *sov;
709 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200710
Willy Tarreau68085d82010-01-18 14:54:04 +0100711 cur_idx = ctx->idx;
712 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200713 /* We have previously returned a value, let's search
714 * another one on the same line.
715 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200716 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200717 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100718 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200719 eol = sol + idx->v[cur_idx].len;
720
721 if (sov >= eol)
722 /* no more values in this header */
723 goto next_hdr;
724
Willy Tarreau68085d82010-01-18 14:54:04 +0100725 /* values remaining for this header, skip the comma but save it
726 * for later use (eg: for header deletion).
727 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200728 sov++;
729 while (sov < eol && http_is_lws[(unsigned char)*sov])
730 sov++;
731
732 goto return_hdr;
733 }
734
735 /* first request for this header */
736 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100737 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200738 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200739 while (cur_idx) {
740 eol = sol + idx->v[cur_idx].len;
741
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200742 if (len == 0) {
743 /* No argument was passed, we want any header.
744 * To achieve this, we simply build a fake request. */
745 while (sol + len < eol && sol[len] != ':')
746 len++;
747 name = sol;
748 }
749
Willy Tarreau33a7e692007-06-10 19:45:56 +0200750 if ((len < eol - sol) &&
751 (sol[len] == ':') &&
752 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100753 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200754 sov = sol + len + 1;
755 while (sov < eol && http_is_lws[(unsigned char)*sov])
756 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100757
Willy Tarreau33a7e692007-06-10 19:45:56 +0200758 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100759 ctx->prev = old_idx;
760 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200761 ctx->idx = cur_idx;
762 ctx->val = sov - sol;
763
764 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200765 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200766 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200767 eol--;
768 ctx->tws++;
769 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200770 ctx->vlen = eol - sov;
771 return 1;
772 }
773 next_hdr:
774 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100775 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200776 cur_idx = idx->v[cur_idx].next;
777 }
778 return 0;
779}
780
781int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100782 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200783 struct hdr_ctx *ctx)
784{
785 return http_find_header2(name, strlen(name), sol, idx, ctx);
786}
787
Willy Tarreau68085d82010-01-18 14:54:04 +0100788/* Remove one value of a header. This only works on a <ctx> returned by one of
789 * the http_find_header functions. The value is removed, as well as surrounding
790 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100791 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100792 * message <msg>. The new index is returned. If it is zero, it means there is
793 * no more header, so any processing may stop. The ctx is always left in a form
794 * that can be handled by http_find_header2() to find next occurrence.
795 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100796int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100797{
798 int cur_idx = ctx->idx;
799 char *sol = ctx->line;
800 struct hdr_idx_elem *hdr;
801 int delta, skip_comma;
802
803 if (!cur_idx)
804 return 0;
805
806 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200807 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100808 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200809 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100810 http_msg_move_end(msg, delta);
811 idx->used--;
812 hdr->len = 0; /* unused entry */
813 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100814 if (idx->tail == ctx->idx)
815 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100816 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100817 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100818 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200819 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100820 return ctx->idx;
821 }
822
823 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200824 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
825 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100826 */
827
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200828 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200829 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200830 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100831 NULL, 0);
832 hdr->len += delta;
833 http_msg_move_end(msg, delta);
834 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200835 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100836 return ctx->idx;
837}
838
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100839/* This function handles a server error at the stream interface level. The
840 * stream interface is assumed to be already in a closed state. An optional
841 * message is copied into the input buffer, and an HTTP status code stored.
842 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100843 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200845static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100846 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100848 channel_auto_read(si_oc(si));
849 channel_abort(si_oc(si));
850 channel_auto_close(si_oc(si));
851 channel_erase(si_oc(si));
852 channel_auto_close(si_ic(si));
853 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100854 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200855 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100856 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200857 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200858 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200859 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200860 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200861 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200862}
863
Willy Tarreau87b09662015-04-03 00:22:06 +0200864/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100865 * and message.
866 */
867
Willy Tarreau87b09662015-04-03 00:22:06 +0200868struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100869{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200870 if (s->be->errmsg[msgnum].str)
871 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200872 else if (strm_fe(s)->errmsg[msgnum].str)
873 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100874 else
875 return &http_err_chunks[msgnum];
876}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200877
Willy Tarreau53b6c742006-12-17 13:37:46 +0100878/*
879 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
880 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
881 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100882enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100883{
884 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100885 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100886
887 m = ((unsigned)*str - 'A');
888
889 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100890 for (h = http_methods[m]; h->len > 0; h++) {
891 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100892 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100893 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100894 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100895 };
896 return HTTP_METH_OTHER;
897 }
898 return HTTP_METH_NONE;
899
900}
901
Willy Tarreau21d2af32008-02-14 20:25:24 +0100902/* Parse the URI from the given transaction (which is assumed to be in request
903 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
904 * It is returned otherwise.
905 */
906static char *
907http_get_path(struct http_txn *txn)
908{
909 char *ptr, *end;
910
Willy Tarreau9b28e032012-10-12 23:49:43 +0200911 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100912 end = ptr + txn->req.sl.rq.u_l;
913
914 if (ptr >= end)
915 return NULL;
916
917 /* RFC2616, par. 5.1.2 :
918 * Request-URI = "*" | absuri | abspath | authority
919 */
920
921 if (*ptr == '*')
922 return NULL;
923
924 if (isalpha((unsigned char)*ptr)) {
925 /* this is a scheme as described by RFC3986, par. 3.1 */
926 ptr++;
927 while (ptr < end &&
928 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
929 ptr++;
930 /* skip '://' */
931 if (ptr == end || *ptr++ != ':')
932 return NULL;
933 if (ptr == end || *ptr++ != '/')
934 return NULL;
935 if (ptr == end || *ptr++ != '/')
936 return NULL;
937 }
938 /* skip [user[:passwd]@]host[:[port]] */
939
940 while (ptr < end && *ptr != '/')
941 ptr++;
942
943 if (ptr == end)
944 return NULL;
945
946 /* OK, we got the '/' ! */
947 return ptr;
948}
949
William Lallemand65ad6e12014-01-31 15:08:02 +0100950/* Parse the URI from the given string and look for the "/" beginning the PATH.
951 * If not found, return NULL. It is returned otherwise.
952 */
953static char *
954http_get_path_from_string(char *str)
955{
956 char *ptr = str;
957
958 /* RFC2616, par. 5.1.2 :
959 * Request-URI = "*" | absuri | abspath | authority
960 */
961
962 if (*ptr == '*')
963 return NULL;
964
965 if (isalpha((unsigned char)*ptr)) {
966 /* this is a scheme as described by RFC3986, par. 3.1 */
967 ptr++;
968 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
969 ptr++;
970 /* skip '://' */
971 if (*ptr == '\0' || *ptr++ != ':')
972 return NULL;
973 if (*ptr == '\0' || *ptr++ != '/')
974 return NULL;
975 if (*ptr == '\0' || *ptr++ != '/')
976 return NULL;
977 }
978 /* skip [user[:passwd]@]host[:[port]] */
979
980 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
981 ptr++;
982
983 if (*ptr == '\0' || *ptr == ' ')
984 return NULL;
985
986 /* OK, we got the '/' ! */
987 return ptr;
988}
989
Willy Tarreau71241ab2012-12-27 11:30:54 +0100990/* Returns a 302 for a redirectable request that reaches a server working in
991 * in redirect mode. This may only be called just after the stream interface
992 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
993 * follow normal proxy processing. NOTE: this function is designed to support
994 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100995 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200996void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100997{
998 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100999 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001000 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001001 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001002
1003 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001004 trash.len = strlen(HTTP_302);
1005 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001007 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001008
Willy Tarreauefb453c2008-10-26 20:49:47 +01001009 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001010 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001011 return;
1012
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001013 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001014 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001015 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1016 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001017 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001018
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001019 /* 3: add the request URI. Since it was already forwarded, we need
1020 * to temporarily rewind the buffer.
1021 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001022 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001023 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001024
Willy Tarreauefb453c2008-10-26 20:49:47 +01001025 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001026 len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001027
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001028 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001029
Willy Tarreauefb453c2008-10-26 20:49:47 +01001030 if (!path)
1031 return;
1032
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001033 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001034 return;
1035
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001036 memcpy(trash.str + trash.len, path, len);
1037 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001038
1039 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001040 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1041 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001042 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001043 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1044 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001045 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001046
1047 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001048 si_shutr(si);
1049 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001050 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001051 si->state = SI_ST_CLO;
1052
1053 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001054 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001055
1056 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001057 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001058 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001059}
1060
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001061/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001062 * that the server side is closed. Note that err_type is actually a
1063 * bitmask, where almost only aborts may be cumulated with other
1064 * values. We consider that aborted operations are more important
1065 * than timeouts or errors due to the fact that nobody else in the
1066 * logs might explain incomplete retries. All others should avoid
1067 * being cumulated. It should normally not be possible to have multiple
1068 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001069 * Note that connection errors appearing on the second request of a keep-alive
1070 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001071 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001072void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001073{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001074 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001075
1076 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001077 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001078 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001079 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001080 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001081 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001082 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001083 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001084 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001085 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001087 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001088 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001089 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001090 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001091 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001092 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001093 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001094 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1095 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001096 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001097 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001098 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001099 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001100 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001101 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001102 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001103 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001104}
1105
Willy Tarreau42250582007-04-01 01:30:43 +02001106extern const char sess_term_cond[8];
1107extern const char sess_fin_state[8];
1108extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001109struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001110struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001111struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001112struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113
Willy Tarreau117f59e2007-03-04 18:17:17 +01001114/*
1115 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001116 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001117 */
1118void capture_headers(char *som, struct hdr_idx *idx,
1119 char **cap, struct cap_hdr *cap_hdr)
1120{
1121 char *eol, *sol, *col, *sov;
1122 int cur_idx;
1123 struct cap_hdr *h;
1124 int len;
1125
1126 sol = som + hdr_idx_first_pos(idx);
1127 cur_idx = hdr_idx_first_idx(idx);
1128
1129 while (cur_idx) {
1130 eol = sol + idx->v[cur_idx].len;
1131
1132 col = sol;
1133 while (col < eol && *col != ':')
1134 col++;
1135
1136 sov = col + 1;
1137 while (sov < eol && http_is_lws[(unsigned char)*sov])
1138 sov++;
1139
1140 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001141 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001142 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1143 if (cap[h->index] == NULL)
1144 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001145 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001146
1147 if (cap[h->index] == NULL) {
1148 Alert("HTTP capture : out of memory.\n");
1149 continue;
1150 }
1151
1152 len = eol - sov;
1153 if (len > h->len)
1154 len = h->len;
1155
1156 memcpy(cap[h->index], sov, len);
1157 cap[h->index][len]=0;
1158 }
1159 }
1160 sol = eol + idx->v[cur_idx].cr + 1;
1161 cur_idx = idx->v[cur_idx].next;
1162 }
1163}
1164
1165
Willy Tarreau42250582007-04-01 01:30:43 +02001166/* either we find an LF at <ptr> or we jump to <bad>.
1167 */
1168#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1169
1170/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1171 * otherwise to <http_msg_ood> with <state> set to <st>.
1172 */
1173#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1174 ptr++; \
1175 if (likely(ptr < end)) \
1176 goto good; \
1177 else { \
1178 state = (st); \
1179 goto http_msg_ood; \
1180 } \
1181 } while (0)
1182
1183
Willy Tarreaubaaee002006-06-26 02:48:02 +02001184/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001185 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001186 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1187 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1188 * will give undefined results.
1189 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1190 * and that msg->sol points to the beginning of the response.
1191 * If a complete line is found (which implies that at least one CR or LF is
1192 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1193 * returned indicating an incomplete line (which does not mean that parts have
1194 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1195 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1196 * upon next call.
1197 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001198 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001199 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1200 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001201 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001202 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001203const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001204 enum ht_state state, const char *ptr, const char *end,
1205 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001206{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001207 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001208
Willy Tarreau8973c702007-01-21 23:58:29 +01001209 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001210 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001211 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001212 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001213 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1214
1215 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001216 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001217 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1218 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001219 state = HTTP_MSG_ERROR;
1220 break;
1221
Willy Tarreau8973c702007-01-21 23:58:29 +01001222 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001223 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001224 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001225 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001226 goto http_msg_rpcode;
1227 }
1228 if (likely(HTTP_IS_SPHT(*ptr)))
1229 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1230 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001231 state = HTTP_MSG_ERROR;
1232 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001233
Willy Tarreau8973c702007-01-21 23:58:29 +01001234 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001235 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001236 if (likely(!HTTP_IS_LWS(*ptr)))
1237 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1238
1239 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001240 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001241 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1242 }
1243
1244 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001245 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001246 http_msg_rsp_reason:
1247 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001248 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001249 msg->sl.st.r_l = 0;
1250 goto http_msg_rpline_eol;
1251
Willy Tarreau8973c702007-01-21 23:58:29 +01001252 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001253 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001254 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001255 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001256 goto http_msg_rpreason;
1257 }
1258 if (likely(HTTP_IS_SPHT(*ptr)))
1259 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1260 /* so it's a CR/LF, so there is no reason phrase */
1261 goto http_msg_rsp_reason;
1262
Willy Tarreau8973c702007-01-21 23:58:29 +01001263 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001264 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001265 if (likely(!HTTP_IS_CRLF(*ptr)))
1266 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001267 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 http_msg_rpline_eol:
1269 /* We have seen the end of line. Note that we do not
1270 * necessarily have the \n yet, but at least we know that we
1271 * have EITHER \r OR \n, otherwise the response would not be
1272 * complete. We can then record the response length and return
1273 * to the caller which will be able to register it.
1274 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001275 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 return ptr;
1277
Willy Tarreau8973c702007-01-21 23:58:29 +01001278 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001279#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001280 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1281 exit(1);
1282#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001283 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 }
1285
1286 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001287 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001288 if (ret_state)
1289 *ret_state = state;
1290 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001291 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001292 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001293}
1294
Willy Tarreau8973c702007-01-21 23:58:29 +01001295/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001296 * This function parses a request line between <ptr> and <end>, starting with
1297 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1298 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1299 * will give undefined results.
1300 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1301 * and that msg->sol points to the beginning of the request.
1302 * If a complete line is found (which implies that at least one CR or LF is
1303 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1304 * returned indicating an incomplete line (which does not mean that parts have
1305 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1306 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1307 * upon next call.
1308 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001309 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1311 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001312 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001313 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001314const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001315 enum ht_state state, const char *ptr, const char *end,
1316 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001317{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001318 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001322 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001323 if (likely(HTTP_IS_TOKEN(*ptr)))
1324 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001327 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1329 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001330
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 if (likely(HTTP_IS_CRLF(*ptr))) {
1332 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001333 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001334 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001335 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001336 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001337 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001338 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001339 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 msg->sl.rq.v_l = 0;
1341 goto http_msg_rqline_eol;
1342 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001343 state = HTTP_MSG_ERROR;
1344 break;
1345
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001346 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001347 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001348 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001349 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 goto http_msg_rquri;
1351 }
1352 if (likely(HTTP_IS_SPHT(*ptr)))
1353 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1354 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1355 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001357 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001358 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001359 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001363 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1365 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001366
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001367 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001368 /* non-ASCII chars are forbidden unless option
1369 * accept-invalid-http-request is enabled in the frontend.
1370 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001371 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001372 if (msg->err_pos < -1)
1373 goto invalid_char;
1374 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001375 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001376 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1377 }
1378
1379 if (likely(HTTP_IS_CRLF(*ptr))) {
1380 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1381 goto http_msg_req09_uri_e;
1382 }
1383
1384 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001385 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001386 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001387 state = HTTP_MSG_ERROR;
1388 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001391 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001393 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 goto http_msg_rqver;
1395 }
1396 if (likely(HTTP_IS_SPHT(*ptr)))
1397 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1398 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1399 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001402 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001403 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001405
1406 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001407 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001408 http_msg_rqline_eol:
1409 /* We have seen the end of line. Note that we do not
1410 * necessarily have the \n yet, but at least we know that we
1411 * have EITHER \r OR \n, otherwise the request would not be
1412 * complete. We can then record the request length and return
1413 * to the caller which will be able to register it.
1414 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001415 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001416 return ptr;
1417 }
1418
1419 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001420 state = HTTP_MSG_ERROR;
1421 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001424#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1426 exit(1);
1427#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001428 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001430
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001432 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 if (ret_state)
1434 *ret_state = state;
1435 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001436 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001439
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001440/*
1441 * Returns the data from Authorization header. Function may be called more
1442 * than once so data is stored in txn->auth_data. When no header is found
1443 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001444 * searching again for something we are unable to find anyway. However, if
1445 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001446 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001447 */
1448
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001449/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1450 * set according to global.tune.bufsize.
1451 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001452char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001453
1454int
Willy Tarreau87b09662015-04-03 00:22:06 +02001455get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001456{
1457
Willy Tarreaueee5b512015-04-03 23:46:31 +02001458 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001459 struct chunk auth_method;
1460 struct hdr_ctx ctx;
1461 char *h, *p;
1462 int len;
1463
1464#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001465 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001466#endif
1467
1468 if (txn->auth.method == HTTP_AUTH_WRONG)
1469 return 0;
1470
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001471 txn->auth.method = HTTP_AUTH_WRONG;
1472
1473 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001474
1475 if (txn->flags & TX_USE_PX_CONN) {
1476 h = "Proxy-Authorization";
1477 len = strlen(h);
1478 } else {
1479 h = "Authorization";
1480 len = strlen(h);
1481 }
1482
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001483 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001484 return 0;
1485
1486 h = ctx.line + ctx.val;
1487
1488 p = memchr(h, ' ', ctx.vlen);
1489 if (!p || p == h)
1490 return 0;
1491
1492 chunk_initlen(&auth_method, h, 0, p-h);
1493 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1494
1495 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1496
1497 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001498 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001499
1500 if (len < 0)
1501 return 0;
1502
1503
1504 get_http_auth_buff[len] = '\0';
1505
1506 p = strchr(get_http_auth_buff, ':');
1507
1508 if (!p)
1509 return 0;
1510
1511 txn->auth.user = get_http_auth_buff;
1512 *p = '\0';
1513 txn->auth.pass = p+1;
1514
1515 txn->auth.method = HTTP_AUTH_BASIC;
1516 return 1;
1517 }
1518
1519 return 0;
1520}
1521
Willy Tarreau58f10d72006-12-04 02:26:12 +01001522
Willy Tarreau8973c702007-01-21 23:58:29 +01001523/*
1524 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001525 * depending on the initial msg->msg_state. The caller is responsible for
1526 * ensuring that the message does not wrap. The function can be preempted
1527 * everywhere when data are missing and recalled at the exact same location
1528 * with no information loss. The message may even be realigned between two
1529 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001530 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001531 * fields. Note that msg->sol will be initialized after completing the first
1532 * state, so that none of the msg pointers has to be initialized prior to the
1533 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001534 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001535void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536{
Willy Tarreau3770f232013-12-07 00:01:53 +01001537 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001539 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001540
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001541 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001542 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001543 ptr = buf->p + msg->next;
1544 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 if (unlikely(ptr >= end))
1547 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001548
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001550 /*
1551 * First, states that are specific to the response only.
1552 * We check them first so that request and headers are
1553 * closer to each other (accessed more often).
1554 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001555 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001556 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001557 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001558 /* we have a start of message, but we have to check
1559 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001560 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001561 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001562 if (unlikely(ptr != buf->p)) {
1563 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001564 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001565 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001566 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001567 }
Willy Tarreau26927362012-05-18 23:22:52 +02001568 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001569 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001570 hdr_idx_init(idx);
1571 state = HTTP_MSG_RPVER;
1572 goto http_msg_rpver;
1573 }
1574
1575 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1576 goto http_msg_invalid;
1577
1578 if (unlikely(*ptr == '\n'))
1579 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1580 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1581 /* stop here */
1582
Willy Tarreau8973c702007-01-21 23:58:29 +01001583 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001584 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001585 EXPECT_LF_HERE(ptr, http_msg_invalid);
1586 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1587 /* stop here */
1588
Willy Tarreau8973c702007-01-21 23:58:29 +01001589 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001590 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001591 case HTTP_MSG_RPVER_SP:
1592 case HTTP_MSG_RPCODE:
1593 case HTTP_MSG_RPCODE_SP:
1594 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001595 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001596 state, ptr, end,
1597 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001598 if (unlikely(!ptr))
1599 return;
1600
1601 /* we have a full response and we know that we have either a CR
1602 * or an LF at <ptr>.
1603 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001604 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1605
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001606 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001607 if (likely(*ptr == '\r'))
1608 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1609 goto http_msg_rpline_end;
1610
Willy Tarreau8973c702007-01-21 23:58:29 +01001611 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001612 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001613 /* msg->sol must point to the first of CR or LF. */
1614 EXPECT_LF_HERE(ptr, http_msg_invalid);
1615 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1616 /* stop here */
1617
1618 /*
1619 * Second, states that are specific to the request only
1620 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001622 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001624 /* we have a start of message, but we have to check
1625 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001626 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001627 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001628 if (likely(ptr != buf->p)) {
1629 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001630 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001631 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001632 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001633 }
Willy Tarreau26927362012-05-18 23:22:52 +02001634 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001635 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001636 state = HTTP_MSG_RQMETH;
1637 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1641 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 if (unlikely(*ptr == '\n'))
1644 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1645 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001646 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001648 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001649 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001650 EXPECT_LF_HERE(ptr, http_msg_invalid);
1651 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001652 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001655 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 case HTTP_MSG_RQMETH_SP:
1657 case HTTP_MSG_RQURI:
1658 case HTTP_MSG_RQURI_SP:
1659 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001660 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001661 state, ptr, end,
1662 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 if (unlikely(!ptr))
1664 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 /* we have a full request and we know that we have either a CR
1667 * or an LF at <ptr>.
1668 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001670
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001671 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001672 if (likely(*ptr == '\r'))
1673 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001675
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001677 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001678 /* check for HTTP/0.9 request : no version information available.
1679 * msg->sol must point to the first of CR or LF.
1680 */
1681 if (unlikely(msg->sl.rq.v_l == 0))
1682 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001683
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 EXPECT_LF_HERE(ptr, http_msg_invalid);
1685 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001686 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001687
Willy Tarreau8973c702007-01-21 23:58:29 +01001688 /*
1689 * Common states below
1690 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001691 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001692 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001693 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 if (likely(!HTTP_IS_CRLF(*ptr))) {
1695 goto http_msg_hdr_name;
1696 }
1697
1698 if (likely(*ptr == '\r'))
1699 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1700 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001701
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001702 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001703 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 /* assumes msg->sol points to the first char */
1705 if (likely(HTTP_IS_TOKEN(*ptr)))
1706 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001707
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001708 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001710
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001711 if (likely(msg->err_pos < -1) || *ptr == '\n')
1712 goto http_msg_invalid;
1713
1714 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001715 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001716
1717 /* and we still accept this non-token character */
1718 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001719
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001721 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001722 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 if (likely(HTTP_IS_SPHT(*ptr)))
1724 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001725
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001727 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001728
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 if (likely(!HTTP_IS_CRLF(*ptr))) {
1730 goto http_msg_hdr_val;
1731 }
1732
1733 if (likely(*ptr == '\r'))
1734 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1735 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001736
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001737 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001738 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 EXPECT_LF_HERE(ptr, http_msg_invalid);
1740 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001741
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001743 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 if (likely(HTTP_IS_SPHT(*ptr))) {
1745 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001746 for (; buf->p + msg->sov < ptr; msg->sov++)
1747 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 goto http_msg_hdr_l1_sp;
1749 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001750 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001751 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 goto http_msg_complete_header;
1753
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001755 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001756 /* assumes msg->sol points to the first char, and msg->sov
1757 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 */
1759 if (likely(!HTTP_IS_CRLF(*ptr)))
1760 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001761
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001762 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 /* Note: we could also copy eol into ->eoh so that we have the
1764 * real header end in case it ends with lots of LWS, but is this
1765 * really needed ?
1766 */
1767 if (likely(*ptr == '\r'))
1768 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1769 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001770
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001772 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 EXPECT_LF_HERE(ptr, http_msg_invalid);
1774 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001775
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001777 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1779 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001780 for (; buf->p + msg->eol < ptr; msg->eol++)
1781 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001782 goto http_msg_hdr_val;
1783 }
1784 http_msg_complete_header:
1785 /*
1786 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001787 * Assumes msg->sol points to the first char, msg->sov points
1788 * to the first character of the value and msg->eol to the
1789 * first CR or LF so we know how the line ends. We insert last
1790 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001792 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 idx, idx->tail) < 0))
1794 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001795
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001796 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 if (likely(!HTTP_IS_CRLF(*ptr))) {
1798 goto http_msg_hdr_name;
1799 }
1800
1801 if (likely(*ptr == '\r'))
1802 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1803 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001806 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001807 /* Assumes msg->sol points to the first of either CR or LF.
1808 * Sets ->sov and ->next to the total header length, ->eoh to
1809 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1810 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811 EXPECT_LF_HERE(ptr, http_msg_invalid);
1812 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001813 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001814 msg->eoh = msg->sol;
1815 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001816 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001817 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001818 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001819
1820 case HTTP_MSG_ERROR:
1821 /* this may only happen if we call http_msg_analyser() twice with an error */
1822 break;
1823
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001825#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1827 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001828#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001829 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 }
1831 http_msg_ood:
1832 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001833 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001834 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001836
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001837 http_msg_invalid:
1838 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001839 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001840 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001841 return;
1842}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001843
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001844/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1845 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1846 * nothing is done and 1 is returned.
1847 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001848static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001849{
1850 int delta;
1851 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001852 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001853
1854 if (msg->sl.rq.v_l != 0)
1855 return 1;
1856
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001857 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1858 if (txn->meth != HTTP_METH_GET)
1859 return 0;
1860
Willy Tarreau9b28e032012-10-12 23:49:43 +02001861 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001862 delta = 0;
1863
1864 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001865 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1866 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001867 }
1868 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001869 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001870 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001871 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001872 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001873 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001874 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001875 NULL, NULL);
1876 if (unlikely(!cur_end))
1877 return 0;
1878
1879 /* we have a full HTTP/1.0 request now and we know that
1880 * we have either a CR or an LF at <ptr>.
1881 */
1882 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1883 return 1;
1884}
1885
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001886/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001887 * and "keep-alive" values. If we already know that some headers may safely
1888 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001889 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1890 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001891 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001892 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1893 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1894 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001895 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001896 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001897void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001898{
Willy Tarreau5b154472009-12-21 20:11:07 +01001899 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001900 const char *hdr_val = "Connection";
1901 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001902
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001903 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001904 return;
1905
Willy Tarreau88d349d2010-01-25 12:15:43 +01001906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection";
1908 hdr_len = 16;
1909 }
1910
Willy Tarreau5b154472009-12-21 20:11:07 +01001911 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001912 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001913 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001914 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1915 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001916 if (to_del & 2)
1917 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001918 else
1919 txn->flags |= TX_CON_KAL_SET;
1920 }
1921 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1922 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001923 if (to_del & 1)
1924 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001925 else
1926 txn->flags |= TX_CON_CLO_SET;
1927 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001928 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1929 txn->flags |= TX_HDR_CONN_UPG;
1930 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001931 }
1932
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001933 txn->flags |= TX_HDR_CONN_PRS;
1934 return;
1935}
Willy Tarreau5b154472009-12-21 20:11:07 +01001936
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001937/* Apply desired changes on the Connection: header. Values may be removed and/or
1938 * added depending on the <wanted> flags, which are exclusively composed of
1939 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1940 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1941 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001942void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001943{
1944 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001945 const char *hdr_val = "Connection";
1946 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001947
1948 ctx.idx = 0;
1949
Willy Tarreau88d349d2010-01-25 12:15:43 +01001950
1951 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1952 hdr_val = "Proxy-Connection";
1953 hdr_len = 16;
1954 }
1955
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001956 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001957 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001958 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1959 if (wanted & TX_CON_KAL_SET)
1960 txn->flags |= TX_CON_KAL_SET;
1961 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001962 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001963 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001964 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1965 if (wanted & TX_CON_CLO_SET)
1966 txn->flags |= TX_CON_CLO_SET;
1967 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001968 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001969 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001970 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001971
1972 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1973 return;
1974
1975 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1976 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001977 hdr_val = "Connection: close";
1978 hdr_len = 17;
1979 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1980 hdr_val = "Proxy-Connection: close";
1981 hdr_len = 23;
1982 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001983 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001984 }
1985
1986 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1987 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001988 hdr_val = "Connection: keep-alive";
1989 hdr_len = 22;
1990 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1991 hdr_val = "Proxy-Connection: keep-alive";
1992 hdr_len = 28;
1993 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001994 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001995 }
1996 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001997}
1998
Willy Tarreauc24715e2014-04-17 15:21:20 +02001999/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2000 * the first byte of data after the chunk size, so that we know we can forward
2001 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2002 * the chunk size. That way it is always possible to differentiate between the
2003 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002004 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002005 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002006 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002007static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002008{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002009 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002010 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002011 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002012 const char *end = buf->data + buf->size;
2013 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002014 unsigned int chunk = 0;
2015
2016 /* The chunk size is in the following form, though we are only
2017 * interested in the size and CRLF :
2018 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2019 */
2020 while (1) {
2021 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002022 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002023 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002024 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002025 if (c < 0) /* not a hex digit anymore */
2026 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002027 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002028 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002029 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002030 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002031 chunk = (chunk << 4) + c;
2032 }
2033
Willy Tarreaud98cf932009-12-27 22:54:55 +01002034 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002035 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002036 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037
2038 while (http_is_spht[(unsigned char)*ptr]) {
2039 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002040 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002041 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002042 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002043 }
2044
Willy Tarreaud98cf932009-12-27 22:54:55 +01002045 /* Up to there, we know that at least one byte is present at *ptr. Check
2046 * for the end of chunk size.
2047 */
2048 while (1) {
2049 if (likely(HTTP_IS_CRLF(*ptr))) {
2050 /* we now have a CR or an LF at ptr */
2051 if (likely(*ptr == '\r')) {
2052 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002053 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002054 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002055 return 0;
2056 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002057
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002059 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002061 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002062 /* done */
2063 break;
2064 }
2065 else if (*ptr == ';') {
2066 /* chunk extension, ends at next CRLF */
2067 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002068 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002069 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002070 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002071
2072 while (!HTTP_IS_CRLF(*ptr)) {
2073 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002074 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002075 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002076 return 0;
2077 }
2078 /* we have a CRLF now, loop above */
2079 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002080 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002081 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002082 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002083 }
2084
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002086 * which may or may not be present. We save that into ->next,
2087 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002088 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002089 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002090 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002091 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002092 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002093 msg->chunk_len = chunk;
2094 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002096 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002097 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002098 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002099 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002100}
2101
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002102/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002103 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002104 * the trailers is found, it is automatically scheduled to be forwarded,
2105 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2106 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002107 * except maybe msg->next if it could parse some lines, and returns zero.
2108 * If a parse error is encountered, the function returns < 0 and does not
2109 * change anything except maybe msg->next. Note that the message must
2110 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002111 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002112 * forwarding, and that msg->next exactly matches the length of trailers
2113 * already parsed and not forwarded. It is also important to note that this
2114 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002115 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002116static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002117{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002118 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002119
Willy Tarreaua458b672012-03-05 11:17:50 +01002120 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002121 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002122 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002123 const char *ptr = b_ptr(buf, msg->next);
2124 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002125 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002126
2127 /* scan current line and stop at LF or CRLF */
2128 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002129 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002130 return 0;
2131
2132 if (*ptr == '\n') {
2133 if (!p1)
2134 p1 = ptr;
2135 p2 = ptr;
2136 break;
2137 }
2138
2139 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002140 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002141 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002142 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002143 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002144 p1 = ptr;
2145 }
2146
2147 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002148 if (ptr >= buf->data + buf->size)
2149 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002150 }
2151
2152 /* after LF; point to beginning of next line */
2153 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002154 if (p2 >= buf->data + buf->size)
2155 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002156
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002157 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002158 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002159 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002160
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002161 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002162 /* LF/CRLF at beginning of line => end of trailers at p2.
2163 * Everything was scheduled for forwarding, there's nothing
2164 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002165 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002166 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002167 msg->msg_state = HTTP_MSG_DONE;
2168 return 1;
2169 }
2170 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002171 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002172 }
2173}
2174
Willy Tarreauc24715e2014-04-17 15:21:20 +02002175/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2176 * or a possible LF alone at the end of a chunk. It automatically adjusts
2177 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002178 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002179 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2180 * not enough data are available, the function does not change anything and
2181 * returns zero. If a parse error is encountered, the function returns < 0 and
2182 * does not change anything. Note: this function is designed to parse wrapped
2183 * CRLF at the end of the buffer.
2184 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002185static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002186{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002187 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002188 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002189 int bytes;
2190
2191 /* NB: we'll check data availabilty at the end. It's not a
2192 * problem because whatever we match first will be checked
2193 * against the correct length.
2194 */
2195 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002196 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 if (*ptr == '\r') {
2198 bytes++;
2199 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002200 if (ptr >= buf->data + buf->size)
2201 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002202 }
2203
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002204 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002205 return 0;
2206
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002207 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002208 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002209 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002210 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002211
2212 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002213 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002214 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002215 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002216 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002217 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2218 return 1;
2219}
Willy Tarreau5b154472009-12-21 20:11:07 +01002220
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002221/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2222 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2223 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2224 * Unparsable qvalues return 1000 as "q=1.000".
2225 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002226int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002227{
2228 int q = 1000;
2229
Willy Tarreau506c69a2014-07-08 00:59:48 +02002230 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002231 goto out;
2232 q = (*qvalue++ - '0') * 1000;
2233
2234 if (*qvalue++ != '.')
2235 goto out;
2236
Willy Tarreau506c69a2014-07-08 00:59:48 +02002237 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002238 goto out;
2239 q += (*qvalue++ - '0') * 100;
2240
Willy Tarreau506c69a2014-07-08 00:59:48 +02002241 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002242 goto out;
2243 q += (*qvalue++ - '0') * 10;
2244
Willy Tarreau506c69a2014-07-08 00:59:48 +02002245 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002246 goto out;
2247 q += (*qvalue++ - '0') * 1;
2248 out:
2249 if (q > 1000)
2250 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002251 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002252 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002253 return q;
2254}
William Lallemand82fe75c2012-10-23 10:25:10 +02002255
2256/*
2257 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002258 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002259int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002260{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002261 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002262 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002263 struct hdr_ctx ctx;
2264 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002265 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002266
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002267 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2268 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002269 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2270 */
2271 ctx.idx = 0;
2272 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2273 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002274 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2275 (ctx.vlen < 31 ||
2276 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2277 ctx.line[ctx.val + 30] < '6' ||
2278 (ctx.line[ctx.val + 30] == '6' &&
2279 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2280 s->comp_algo = NULL;
2281 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002282 }
2283
William Lallemand82fe75c2012-10-23 10:25:10 +02002284 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002285 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002286 int best_q = 0;
2287
William Lallemand82fe75c2012-10-23 10:25:10 +02002288 ctx.idx = 0;
2289 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002290 const char *qval;
2291 int q;
2292 int toklen;
2293
2294 /* try to isolate the token from the optional q-value */
2295 toklen = 0;
2296 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2297 toklen++;
2298
2299 qval = ctx.line + ctx.val + toklen;
2300 while (1) {
2301 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2302 qval++;
2303
2304 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2305 qval = NULL;
2306 break;
2307 }
2308 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002309
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002310 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2311 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002312
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002313 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2314 qval = NULL;
2315 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002316 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002317 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2318 break;
2319
2320 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2321 qval++;
2322 }
2323
2324 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002325 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002326
2327 if (q <= best_q)
2328 continue;
2329
2330 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2331 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002332 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002333 s->comp_algo = comp_algo;
2334 best_q = q;
2335 break;
2336 }
2337 }
2338 }
2339 }
2340
2341 /* remove all occurrences of the header when "compression offload" is set */
2342 if (s->comp_algo) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002343 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002344 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2345 ctx.idx = 0;
2346 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2347 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002348 }
2349 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002350 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002351 }
2352
2353 /* identity is implicit does not require headers */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002354 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002355 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002356 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002357 s->comp_algo = comp_algo;
2358 return 1;
2359 }
2360 }
2361 }
2362
2363 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002364 return 0;
2365}
2366
2367/*
2368 * Selects a comression algorithm depending of the server response.
2369 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002370int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002371{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002372 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002373 struct http_msg *msg = &txn->rsp;
2374 struct hdr_ctx ctx;
2375 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002376
2377 /* no common compression algorithm was found in request header */
2378 if (s->comp_algo == NULL)
2379 goto fail;
2380
2381 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002382 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002383 goto fail;
2384
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002385 /* compress 200,201,202,203 responses only */
2386 if ((txn->status != 200) &&
2387 (txn->status != 201) &&
2388 (txn->status != 202) &&
2389 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002390 goto fail;
2391
William Lallemand82fe75c2012-10-23 10:25:10 +02002392 /* Content-Length is null */
2393 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2394 goto fail;
2395
2396 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002397 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002398 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2399 goto fail;
2400
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002401 /* no compression when Cache-Control: no-transform is present in the message */
2402 ctx.idx = 0;
2403 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2404 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2405 goto fail;
2406 }
2407
William Lallemand82fe75c2012-10-23 10:25:10 +02002408 comp_type = NULL;
2409
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002410 /* we don't want to compress multipart content-types, nor content-types that are
2411 * not listed in the "compression type" directive if any. If no content-type was
2412 * found but configuration requires one, we don't compress either. Backend has
2413 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002414 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002415 ctx.idx = 0;
2416 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2417 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2418 goto fail;
2419
2420 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002421 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002422 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002423 if (ctx.vlen >= comp_type->name_len &&
2424 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002425 /* this Content-Type should be compressed */
2426 break;
2427 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002428 /* this Content-Type should not be compressed */
2429 if (comp_type == NULL)
2430 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002431 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002432 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002433 else { /* no content-type header */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002434 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002435 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002436 }
2437
William Lallemandd85f9172012-11-09 17:05:39 +01002438 /* limit compression rate */
2439 if (global.comp_rate_lim > 0)
2440 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2441 goto fail;
2442
William Lallemand072a2bf2012-11-20 17:01:01 +01002443 /* limit cpu usage */
2444 if (idle_pct < compress_min_idle)
2445 goto fail;
2446
William Lallemand4c49fae2012-11-07 15:00:23 +01002447 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002448 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002449 goto fail;
2450
Willy Tarreaue7dff022015-04-03 01:14:29 +02002451 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002452
William Lallemand82fe75c2012-10-23 10:25:10 +02002453 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002454 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002455 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2456 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2457
2458 /* add Transfer-Encoding header */
2459 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2460 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2461
2462 /*
2463 * Add Content-Encoding header when it's not identity encoding.
2464 * RFC 2616 : Identity encoding: This content-coding is used only in the
2465 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2466 * header.
2467 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002468 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002469 trash.len = 18;
2470 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002471 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2472 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002473 trash.str[trash.len] = '\0';
2474 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002475 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002476 return 1;
2477
2478fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002479 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002480 return 0;
2481}
2482
Willy Tarreau87b09662015-04-03 00:22:06 +02002483void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002484{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002485 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002486 int tmp = TX_CON_WANT_KAL;
2487
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002488 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2489 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002490 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2491 tmp = TX_CON_WANT_TUN;
2492
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002493 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002494 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2495 tmp = TX_CON_WANT_TUN;
2496 }
2497
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002498 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002499 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2500 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002501 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002502 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2503 tmp = TX_CON_WANT_CLO;
2504 else
2505 tmp = TX_CON_WANT_SCL;
2506 }
2507
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002508 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002509 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2510 tmp = TX_CON_WANT_CLO;
2511
2512 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2513 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2514
2515 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2516 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2517 /* parse the Connection header and possibly clean it */
2518 int to_del = 0;
2519 if ((msg->flags & HTTP_MSGF_VER_11) ||
2520 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002521 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002522 to_del |= 2; /* remove "keep-alive" */
2523 if (!(msg->flags & HTTP_MSGF_VER_11))
2524 to_del |= 1; /* remove "close" */
2525 http_parse_connection_header(txn, msg, to_del);
2526 }
2527
2528 /* check if client or config asks for explicit close in KAL/SCL */
2529 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2530 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2531 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2532 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2533 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002534 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002535 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2536}
William Lallemand82fe75c2012-10-23 10:25:10 +02002537
Willy Tarreaud787e662009-07-07 10:14:51 +02002538/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2539 * processing can continue on next analysers, or zero if it either needs more
2540 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002541 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002542 * when it has nothing left to do, and may remove any analyser when it wants to
2543 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002544 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002545int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002546{
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 /*
2548 * We will parse the partial (or complete) lines.
2549 * We will check the request syntax, and also join multi-line
2550 * headers. An index of all the lines will be elaborated while
2551 * parsing.
2552 *
2553 * For the parsing, we use a 28 states FSM.
2554 *
2555 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002556 * req->buf->p = beginning of request
2557 * req->buf->p + msg->eoh = end of processed headers / start of current one
2558 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002559 * msg->eol = end of current header or line (LF or CRLF)
2560 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002561 *
2562 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002563 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002564 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2565 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002566 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002567
Willy Tarreau59234e92008-11-30 23:51:27 +01002568 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002569 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002570 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002571 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002572 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002573
Willy Tarreau87b09662015-04-03 00:22:06 +02002574 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002575 now_ms, __FUNCTION__,
2576 s,
2577 req,
2578 req->rex, req->wex,
2579 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002580 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002581 req->analysers);
2582
Willy Tarreau52a0c602009-08-16 22:45:38 +02002583 /* we're speaking HTTP here, so let's speak HTTP to the client */
2584 s->srv_error = http_return_srv_error;
2585
Willy Tarreau83e3af02009-12-28 17:39:57 +01002586 /* There's a protected area at the end of the buffer for rewriting
2587 * purposes. We don't want to start to parse the request if the
2588 * protected area is affected, because we may have to move processed
2589 * data later, which is much more complicated.
2590 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002591 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002592 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002593 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002594 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002595 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002596 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002597 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002598 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002599 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002600 return 0;
2601 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002602 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2603 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2604 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002605 }
2606
Willy Tarreau065e8332010-01-08 00:30:20 +01002607 /* Note that we have the same problem with the response ; we
2608 * may want to send a redirect, error or anything which requires
2609 * some spare space. So we'll ensure that we have at least
2610 * maxrewrite bytes available in the response buffer before
2611 * processing that one. This will only affect pipelined
2612 * keep-alive requests.
2613 */
2614 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002615 unlikely(!channel_is_rewritable(&s->res) ||
2616 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2617 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2618 if (s->res.buf->o) {
2619 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002620 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002621 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002622 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002623 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2624 s->res.flags |= CF_WAKE_WRITE;
2625 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002626 return 0;
2627 }
2628 }
2629
Willy Tarreau9b28e032012-10-12 23:49:43 +02002630 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002631 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002632 }
2633
Willy Tarreau59234e92008-11-30 23:51:27 +01002634 /* 1: we might have to print this header in debug mode */
2635 if (unlikely((global.mode & MODE_DEBUG) &&
2636 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002637 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002639
Willy Tarreau9b28e032012-10-12 23:49:43 +02002640 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002641 /* this is a bit complex : in case of error on the request line,
2642 * we know that rq.l is still zero, so we display only the part
2643 * up to the end of the line (truncated by debug_hdr).
2644 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002645 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002646 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002647
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 sol += hdr_idx_first_pos(&txn->hdr_idx);
2649 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002650
Willy Tarreau59234e92008-11-30 23:51:27 +01002651 while (cur_idx) {
2652 eol = sol + txn->hdr_idx.v[cur_idx].len;
2653 debug_hdr("clihdr", s, sol, eol);
2654 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2655 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002656 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 }
2658
Willy Tarreau58f10d72006-12-04 02:26:12 +01002659
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 /*
2661 * Now we quickly check if we have found a full valid request.
2662 * If not so, we check the FD and buffer states before leaving.
2663 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002664 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002665 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002666 * on a keep-alive stream, if we encounter and error, close, t/o,
2667 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002668 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002669 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002670 * Last, we may increase some tracked counters' http request errors on
2671 * the cases that are deliberately the client's fault. For instance,
2672 * a timeout or connection reset is not counted as an error. However
2673 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002674 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002675
Willy Tarreau655dce92009-11-08 13:10:58 +01002676 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002677 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002678 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002679 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002680 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002681 stream_inc_http_req_ctr(s);
2682 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002683 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002685 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002686
Willy Tarreau59234e92008-11-30 23:51:27 +01002687 /* 1: Since we are in header mode, if there's no space
2688 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002689 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 * must terminate it now.
2691 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002692 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 /* FIXME: check if URI is set and return Status
2694 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002695 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002696 stream_inc_http_req_ctr(s);
2697 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002698 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002699 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002700 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002701 goto return_bad_req;
2702 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002703
Willy Tarreau59234e92008-11-30 23:51:27 +01002704 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002705 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002706 if (!(s->flags & SF_ERR_MASK))
2707 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002708
Willy Tarreaufcffa692010-01-10 14:21:19 +01002709 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002710 goto failed_keep_alive;
2711
Willy Tarreau0f228a02015-05-01 15:37:53 +02002712 if (sess->fe->options & PR_O_IGNORE_PRB)
2713 goto failed_keep_alive;
2714
Willy Tarreau59234e92008-11-30 23:51:27 +01002715 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002716 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002717 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002718 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002719 }
2720
Willy Tarreaudc979f22012-12-04 10:39:01 +01002721 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002722 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 msg->msg_state = HTTP_MSG_ERROR;
2724 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002725
Willy Tarreau87b09662015-04-03 00:22:06 +02002726 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002727 proxy_inc_fe_req_ctr(sess->fe);
2728 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002729 if (sess->listener->counters)
2730 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002731
Willy Tarreaue7dff022015-04-03 01:14:29 +02002732 if (!(s->flags & SF_FINST_MASK))
2733 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 return 0;
2735 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002736
Willy Tarreau59234e92008-11-30 23:51:27 +01002737 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002738 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002739 if (!(s->flags & SF_ERR_MASK))
2740 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002741
Willy Tarreaufcffa692010-01-10 14:21:19 +01002742 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002743 goto failed_keep_alive;
2744
Willy Tarreau0f228a02015-05-01 15:37:53 +02002745 if (sess->fe->options & PR_O_IGNORE_PRB)
2746 goto failed_keep_alive;
2747
Willy Tarreau59234e92008-11-30 23:51:27 +01002748 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002749 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002750 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002751 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002752 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002753 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002754 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002755 msg->msg_state = HTTP_MSG_ERROR;
2756 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002757
Willy Tarreau87b09662015-04-03 00:22:06 +02002758 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002759 proxy_inc_fe_req_ctr(sess->fe);
2760 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002761 if (sess->listener->counters)
2762 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002763
Willy Tarreaue7dff022015-04-03 01:14:29 +02002764 if (!(s->flags & SF_FINST_MASK))
2765 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002766 return 0;
2767 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002768
Willy Tarreau59234e92008-11-30 23:51:27 +01002769 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002770 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002771 if (!(s->flags & SF_ERR_MASK))
2772 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002773
Willy Tarreaufcffa692010-01-10 14:21:19 +01002774 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002775 goto failed_keep_alive;
2776
Willy Tarreau0f228a02015-05-01 15:37:53 +02002777 if (sess->fe->options & PR_O_IGNORE_PRB)
2778 goto failed_keep_alive;
2779
Willy Tarreau4076a152009-04-02 15:18:36 +02002780 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002781 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002782 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002783 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002784 msg->msg_state = HTTP_MSG_ERROR;
2785 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002786
Willy Tarreau87b09662015-04-03 00:22:06 +02002787 stream_inc_http_err_ctr(s);
2788 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002789 proxy_inc_fe_req_ctr(sess->fe);
2790 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002791 if (sess->listener->counters)
2792 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002793
Willy Tarreaue7dff022015-04-03 01:14:29 +02002794 if (!(s->flags & SF_FINST_MASK))
2795 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002796 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002797 }
2798
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002799 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002800 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002801 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002802#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002803 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2804 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002805 /* We need more data, we have to re-enable quick-ack in case we
2806 * previously disabled it, otherwise we might cause the client
2807 * to delay next data.
2808 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002809 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002810 }
2811#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002812
Willy Tarreaufcffa692010-01-10 14:21:19 +01002813 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2814 /* If the client starts to talk, let's fall back to
2815 * request timeout processing.
2816 */
2817 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002818 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002819 }
2820
Willy Tarreau59234e92008-11-30 23:51:27 +01002821 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002822 if (!tick_isset(req->analyse_exp)) {
2823 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2824 (txn->flags & TX_WAIT_NEXT_RQ) &&
2825 tick_isset(s->be->timeout.httpka))
2826 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2827 else
2828 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2829 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002830
Willy Tarreau59234e92008-11-30 23:51:27 +01002831 /* we're not ready yet */
2832 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002833
2834 failed_keep_alive:
2835 /* Here we process low-level errors for keep-alive requests. In
2836 * short, if the request is not the first one and it experiences
2837 * a timeout, read error or shutdown, we just silently close so
2838 * that the client can try again.
2839 */
2840 txn->status = 0;
2841 msg->msg_state = HTTP_MSG_RQBEFORE;
2842 req->analysers = 0;
2843 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002844 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002845 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002846 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002847 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002848 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002849
Willy Tarreaud787e662009-07-07 10:14:51 +02002850 /* OK now we have a complete HTTP request with indexed headers. Let's
2851 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002852 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002853 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002854 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002855 * byte after the last LF. msg->sov points to the first byte of data.
2856 * msg->eol cannot be trusted because it may have been left uninitialized
2857 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002858 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002859
Willy Tarreau87b09662015-04-03 00:22:06 +02002860 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002861 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002862
Willy Tarreaub16a5742010-01-10 14:46:16 +01002863 if (txn->flags & TX_WAIT_NEXT_RQ) {
2864 /* kill the pending keep-alive timeout */
2865 txn->flags &= ~TX_WAIT_NEXT_RQ;
2866 req->analyse_exp = TICK_ETERNITY;
2867 }
2868
2869
Willy Tarreaud787e662009-07-07 10:14:51 +02002870 /* Maybe we found in invalid header name while we were configured not
2871 * to block on that, so we have to capture it now.
2872 */
2873 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002874 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002875
Willy Tarreau59234e92008-11-30 23:51:27 +01002876 /*
2877 * 1: identify the method
2878 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002879 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002880
2881 /* we can make use of server redirect on GET and HEAD */
2882 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002883 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002884
Willy Tarreau59234e92008-11-30 23:51:27 +01002885 /*
2886 * 2: check if the URI matches the monitor_uri.
2887 * We have to do this for every request which gets in, because
2888 * the monitor-uri is defined by the frontend.
2889 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002890 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2891 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002892 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002893 sess->fe->monitor_uri,
2894 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002895 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002896 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002897 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002898 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002899
Willy Tarreaue7dff022015-04-03 01:14:29 +02002900 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002901 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002902
Willy Tarreau59234e92008-11-30 23:51:27 +01002903 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002904 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002905 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002906
Willy Tarreau59234e92008-11-30 23:51:27 +01002907 ret = acl_pass(ret);
2908 if (cond->pol == ACL_COND_UNLESS)
2909 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002910
Willy Tarreau59234e92008-11-30 23:51:27 +01002911 if (ret) {
2912 /* we fail this request, let's return 503 service unavail */
2913 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002914 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002915 if (!(s->flags & SF_ERR_MASK))
2916 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002917 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002918 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002919 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002920
Willy Tarreau59234e92008-11-30 23:51:27 +01002921 /* nothing to fail, let's reply normaly */
2922 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01002923 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002924 if (!(s->flags & SF_ERR_MASK))
2925 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002926 goto return_prx_cond;
2927 }
2928
2929 /*
2930 * 3: Maybe we have to copy the original REQURI for the logs ?
2931 * Note: we cannot log anymore if the request has been
2932 * classified as invalid.
2933 */
2934 if (unlikely(s->logs.logwait & LW_REQ)) {
2935 /* we have a complete HTTP request that we must log */
2936 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2937 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002938
Willy Tarreau59234e92008-11-30 23:51:27 +01002939 if (urilen >= REQURI_LEN)
2940 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002941 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002942 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002943
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002944 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002945 s->do_log(s);
2946 } else {
2947 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002948 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002949 }
Willy Tarreau06619262006-12-17 08:37:22 +01002950
Willy Tarreau91852eb2015-05-01 13:26:00 +02002951 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2952 * exactly one digit "." one digit. This check may be disabled using
2953 * option accept-invalid-http-request.
2954 */
2955 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2956 if (msg->sl.rq.v_l != 8) {
2957 msg->err_pos = msg->sl.rq.v;
2958 goto return_bad_req;
2959 }
2960
2961 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2962 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2963 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2964 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2965 msg->err_pos = msg->sl.rq.v + 4;
2966 goto return_bad_req;
2967 }
2968 }
Willy Tarreau13317662015-05-01 13:47:08 +02002969 else {
2970 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2971 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2972 goto return_bad_req;
2973 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002974
Willy Tarreau5b154472009-12-21 20:11:07 +01002975 /* ... and check if the request is HTTP/1.1 or above */
2976 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002977 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2978 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2979 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002980 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002981
2982 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002983 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002984
Willy Tarreau88d349d2010-01-25 12:15:43 +01002985 /* if the frontend has "option http-use-proxy-header", we'll check if
2986 * we have what looks like a proxied connection instead of a connection,
2987 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2988 * Note that this is *not* RFC-compliant, however browsers and proxies
2989 * happen to do that despite being non-standard :-(
2990 * We consider that a request not beginning with either '/' or '*' is
2991 * a proxied connection, which covers both "scheme://location" and
2992 * CONNECT ip:port.
2993 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002994 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002995 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002996 txn->flags |= TX_USE_PX_CONN;
2997
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002998 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002999 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003000
Willy Tarreau59234e92008-11-30 23:51:27 +01003001 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003002 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003003 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003004 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003005
Willy Tarreau557f1992015-05-01 10:05:17 +02003006 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3007 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003008 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003009 * The length of a message body is determined by one of the following
3010 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003011 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003012 * 1. Any response to a HEAD request and any response with a 1xx
3013 * (Informational), 204 (No Content), or 304 (Not Modified) status
3014 * code is always terminated by the first empty line after the
3015 * header fields, regardless of the header fields present in the
3016 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003017 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003018 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3019 * the connection will become a tunnel immediately after the empty
3020 * line that concludes the header fields. A client MUST ignore any
3021 * Content-Length or Transfer-Encoding header fields received in
3022 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003023 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003024 * 3. If a Transfer-Encoding header field is present and the chunked
3025 * transfer coding (Section 4.1) is the final encoding, the message
3026 * body length is determined by reading and decoding the chunked
3027 * data until the transfer coding indicates the data is complete.
3028 *
3029 * If a Transfer-Encoding header field is present in a response and
3030 * the chunked transfer coding is not the final encoding, the
3031 * message body length is determined by reading the connection until
3032 * it is closed by the server. If a Transfer-Encoding header field
3033 * is present in a request and the chunked transfer coding is not
3034 * the final encoding, the message body length cannot be determined
3035 * reliably; the server MUST respond with the 400 (Bad Request)
3036 * status code and then close the connection.
3037 *
3038 * If a message is received with both a Transfer-Encoding and a
3039 * Content-Length header field, the Transfer-Encoding overrides the
3040 * Content-Length. Such a message might indicate an attempt to
3041 * perform request smuggling (Section 9.5) or response splitting
3042 * (Section 9.4) and ought to be handled as an error. A sender MUST
3043 * remove the received Content-Length field prior to forwarding such
3044 * a message downstream.
3045 *
3046 * 4. If a message is received without Transfer-Encoding and with
3047 * either multiple Content-Length header fields having differing
3048 * field-values or a single Content-Length header field having an
3049 * invalid value, then the message framing is invalid and the
3050 * recipient MUST treat it as an unrecoverable error. If this is a
3051 * request message, the server MUST respond with a 400 (Bad Request)
3052 * status code and then close the connection. If this is a response
3053 * message received by a proxy, the proxy MUST close the connection
3054 * to the server, discard the received response, and send a 502 (Bad
3055 * Gateway) response to the client. If this is a response message
3056 * received by a user agent, the user agent MUST close the
3057 * connection to the server and discard the received response.
3058 *
3059 * 5. If a valid Content-Length header field is present without
3060 * Transfer-Encoding, its decimal value defines the expected message
3061 * body length in octets. If the sender closes the connection or
3062 * the recipient times out before the indicated number of octets are
3063 * received, the recipient MUST consider the message to be
3064 * incomplete and close the connection.
3065 *
3066 * 6. If this is a request message and none of the above are true, then
3067 * the message body length is zero (no message body is present).
3068 *
3069 * 7. Otherwise, this is a response message without a declared message
3070 * body length, so the message body length is determined by the
3071 * number of octets received prior to the server closing the
3072 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003073 */
3074
Willy Tarreau32b47f42009-10-18 20:55:02 +02003075 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003076 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003077 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003078 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003079 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3080 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003081 /* chunked not last, return badreq */
3082 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003083 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003084 }
3085
Willy Tarreau1c913912015-04-30 10:57:51 +02003086 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003087 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003088 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3089 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3090 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3091 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003092 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003093 signed long long cl;
3094
Willy Tarreauad14f752011-09-02 20:33:27 +02003095 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003096 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003097 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003098 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003099
Willy Tarreauad14f752011-09-02 20:33:27 +02003100 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003101 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003102 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003103 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003104
Willy Tarreauad14f752011-09-02 20:33:27 +02003105 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003106 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003107 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003108 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003109
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003110 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003111 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003112 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003113 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003114
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003115 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003116 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003117 }
3118
Willy Tarreau34dfc602015-05-01 10:09:49 +02003119 /* even bodyless requests have a known length */
3120 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003121
Willy Tarreau179085c2014-04-28 16:48:56 +02003122 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3123 * only change if both the request and the config reference something else.
3124 * Option httpclose by itself sets tunnel mode where headers are mangled.
3125 * However, if another mode is set, it will affect it (eg: server-close/
3126 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3127 * if FE and BE have the same settings (common). The method consists in
3128 * checking if options changed between the two calls (implying that either
3129 * one is non-null, or one of them is non-null and we are there for the first
3130 * time.
3131 */
3132 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003133 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003134 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003135
Willy Tarreaud787e662009-07-07 10:14:51 +02003136 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003137 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003138 req->analyse_exp = TICK_ETERNITY;
3139 return 1;
3140
3141 return_bad_req:
3142 /* We centralize bad requests processing here */
3143 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3144 /* we detected a parsing error. We want to archive this request
3145 * in the dedicated proxy area for later troubleshooting.
3146 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003147 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003148 }
3149
3150 txn->req.msg_state = HTTP_MSG_ERROR;
3151 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003152 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003153
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003154 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003155 if (sess->listener->counters)
3156 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003157
3158 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003159 if (!(s->flags & SF_ERR_MASK))
3160 s->flags |= SF_ERR_PRXCOND;
3161 if (!(s->flags & SF_FINST_MASK))
3162 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003163
3164 req->analysers = 0;
3165 req->analyse_exp = TICK_ETERNITY;
3166 return 0;
3167}
3168
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003169
Willy Tarreau347a35d2013-11-22 17:51:09 +01003170/* This function prepares an applet to handle the stats. It can deal with the
3171 * "100-continue" expectation, check that admin rules are met for POST requests,
3172 * and program a response message if something was unexpected. It cannot fail
3173 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003174 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003175 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003176 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003177 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003178int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003179{
3180 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003181 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003182 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003183 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003184 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003185 struct uri_auth *uri_auth = s->be->uri_auth;
3186 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003187 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003188
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003189 appctx = si_appctx(si);
3190 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3191 appctx->st1 = appctx->st2 = 0;
3192 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3193 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003194 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003195 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003196
3197 uri = msg->chn->buf->p + msg->sl.rq.u;
3198 lookup = uri + uri_auth->uri_len;
3199
3200 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3201 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003202 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003203 break;
3204 }
3205 }
3206
3207 if (uri_auth->refresh) {
3208 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3209 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003210 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003211 break;
3212 }
3213 }
3214 }
3215
3216 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3217 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003218 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003219 break;
3220 }
3221 }
3222
3223 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3224 if (memcmp(h, ";st=", 4) == 0) {
3225 int i;
3226 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003227 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003228 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3229 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003230 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003231 break;
3232 }
3233 }
3234 break;
3235 }
3236 }
3237
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003238 appctx->ctx.stats.scope_str = 0;
3239 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003240 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3241 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3242 int itx = 0;
3243 const char *h2;
3244 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3245 const char *err;
3246
3247 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3248 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003249 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003250 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3251 itx++;
3252 h++;
3253 }
3254
3255 if (itx > STAT_SCOPE_TXT_MAXLEN)
3256 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003257 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003258
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003259 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003260 memcpy(scope_txt, h2, itx);
3261 scope_txt[itx] = '\0';
3262 err = invalid_char(scope_txt);
3263 if (err) {
3264 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003265 appctx->ctx.stats.scope_str = 0;
3266 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003267 }
3268 break;
3269 }
3270 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003271
3272 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003273 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003274 int ret = 1;
3275
3276 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003277 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 +01003278 ret = acl_pass(ret);
3279 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3280 ret = !ret;
3281 }
3282
3283 if (ret) {
3284 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003285 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003286 break;
3287 }
3288 }
3289
3290 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003291 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003292 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003293 /* we'll need the request body, possibly after sending 100-continue */
3294 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003295 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003296 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003297 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003298 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3299 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003300 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003301 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003302 else {
3303 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003304 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003305 }
3306
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003307 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003308 return 1;
3309}
3310
Lukas Tribus67db8df2013-06-23 17:37:13 +02003311/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3312 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3313 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003314void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003315{
3316#ifdef IP_TOS
3317 if (from.ss_family == AF_INET)
3318 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3319#endif
3320#ifdef IPV6_TCLASS
3321 if (from.ss_family == AF_INET6) {
3322 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3323 /* v4-mapped addresses need IP_TOS */
3324 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3325 else
3326 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3327 }
3328#endif
3329}
3330
Willy Tarreau87b09662015-04-03 00:22:06 +02003331int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003332 const char* name, unsigned int name_len,
3333 const char *str, struct my_regex *re,
3334 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003335{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003336 struct hdr_ctx ctx;
3337 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003338 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003339 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3340 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003341 struct chunk *output = get_trash_chunk();
3342
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003343 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003344
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003345 /* Choose the header browsing function. */
3346 switch (action) {
3347 case HTTP_REQ_ACT_REPLACE_VAL:
3348 case HTTP_RES_ACT_REPLACE_VAL:
3349 http_find_hdr_func = http_find_header2;
3350 break;
3351 case HTTP_REQ_ACT_REPLACE_HDR:
3352 case HTTP_RES_ACT_REPLACE_HDR:
3353 http_find_hdr_func = http_find_full_header2;
3354 break;
3355 default: /* impossible */
3356 return -1;
3357 }
3358
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003359 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3360 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003361 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003362 char *val = ctx.line + ctx.val;
3363 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003364
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003365 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3366 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003367
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003368 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003369 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003370 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003371
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003372 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003373
3374 hdr->len += delta;
3375 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003376
3377 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003378 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003379 }
3380
3381 return 0;
3382}
3383
Willy Tarreau87b09662015-04-03 00:22:06 +02003384static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003385 const char* name, unsigned int name_len,
3386 struct list *fmt, struct my_regex *re,
3387 int action)
3388{
3389 struct chunk *replace = get_trash_chunk();
3390
3391 replace->len = build_logline(s, replace->str, replace->size, fmt);
3392 if (replace->len >= replace->size - 1)
3393 return -1;
3394
3395 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3396}
3397
Willy Tarreau87b09662015-04-03 00:22:06 +02003398/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003399 * transaction <txn>. Returns the verdict of the first rule that prevents
3400 * further processing of the request (auth, deny, ...), and defaults to
3401 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3402 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3403 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003404 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003405enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003406http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003407{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003408 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003409 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003410 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003411 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003412 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003413 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003414
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003415 /* If "the current_rule_list" match the executed rule list, we are in
3416 * resume condition. If a resume is needed it is always in the action
3417 * and never in the ACL or converters. In this case, we initialise the
3418 * current rule, and go to the action execution point.
3419 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003420 if (s->current_rule) {
3421 rule = s->current_rule;
3422 s->current_rule = NULL;
3423 if (s->current_rule_list == rules)
3424 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003425 }
3426 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003427
Willy Tarreauff011f22011-01-06 17:51:27 +01003428 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003429 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003430 continue;
3431
Willy Tarreau96257ec2012-12-27 10:46:37 +01003432 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003433 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003434 int ret;
3435
Willy Tarreau192252e2015-04-04 01:47:55 +02003436 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003437 ret = acl_pass(ret);
3438
Willy Tarreauff011f22011-01-06 17:51:27 +01003439 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003440 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003441
3442 if (!ret) /* condition not matched */
3443 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003444 }
3445
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003446resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003447 switch (rule->action) {
3448 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003449 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003450
3451 case HTTP_REQ_ACT_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003452 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003453 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003454
Willy Tarreauccbcc372012-12-27 12:37:57 +01003455 case HTTP_REQ_ACT_TARPIT:
3456 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003457 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003458 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003459
Willy Tarreau96257ec2012-12-27 10:46:37 +01003460 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003461 /* Auth might be performed on regular http-req rules as well as on stats */
3462 auth_realm = rule->arg.auth.realm;
3463 if (!auth_realm) {
3464 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3465 auth_realm = STATS_DEFAULT_REALM;
3466 else
3467 auth_realm = px->id;
3468 }
3469 /* send 401/407 depending on whether we use a proxy or not. We still
3470 * count one error, because normal browsing won't significantly
3471 * increase the counter but brute force attempts will.
3472 */
3473 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3474 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3475 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003476 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003477 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003478
Willy Tarreau81499eb2012-12-27 12:19:02 +01003479 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003480 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3481 return HTTP_RULE_RES_BADREQ;
3482 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003483
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003484 case HTTP_REQ_ACT_SET_NICE:
3485 s->task->nice = rule->arg.nice;
3486 break;
3487
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003488 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003489 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003490 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003491 break;
3492
Willy Tarreau51347ed2013-06-11 19:34:13 +02003493 case HTTP_REQ_ACT_SET_MARK:
3494#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003495 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003496 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003497#endif
3498 break;
3499
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003500 case HTTP_REQ_ACT_SET_LOGL:
3501 s->logs.level = rule->arg.loglevel;
3502 break;
3503
Sasha Pachev218f0642014-06-16 12:05:59 -06003504 case HTTP_REQ_ACT_REPLACE_HDR:
3505 case HTTP_REQ_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003506 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3507 rule->arg.hdr_add.name_len,
3508 &rule->arg.hdr_add.fmt,
3509 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003510 return HTTP_RULE_RES_BADREQ;
3511 break;
3512
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003513 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003514 ctx.idx = 0;
3515 /* remove all occurrences of the header */
3516 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3517 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3518 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003519 }
Willy Tarreau85603282015-01-21 20:39:27 +01003520 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003521
Willy Tarreau85603282015-01-21 20:39:27 +01003522 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003523 case HTTP_REQ_ACT_ADD_HDR:
3524 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3525 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3526 trash.len = rule->arg.hdr_add.name_len;
3527 trash.str[trash.len++] = ':';
3528 trash.str[trash.len++] = ' ';
3529 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 +01003530
3531 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3532 /* remove all occurrences of the header */
3533 ctx.idx = 0;
3534 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3535 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3536 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3537 }
3538 }
3539
Willy Tarreau96257ec2012-12-27 10:46:37 +01003540 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3541 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003542
3543 case HTTP_REQ_ACT_DEL_ACL:
3544 case HTTP_REQ_ACT_DEL_MAP: {
3545 struct pat_ref *ref;
3546 char *key;
3547 int len;
3548
3549 /* collect reference */
3550 ref = pat_ref_lookup(rule->arg.map.ref);
3551 if (!ref)
3552 continue;
3553
3554 /* collect key */
3555 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3556 key = trash.str;
3557 key[len] = '\0';
3558
3559 /* perform update */
3560 /* returned code: 1=ok, 0=ko */
3561 pat_ref_delete(ref, key);
3562
3563 break;
3564 }
3565
3566 case HTTP_REQ_ACT_ADD_ACL: {
3567 struct pat_ref *ref;
3568 char *key;
3569 struct chunk *trash_key;
3570 int len;
3571
3572 trash_key = get_trash_chunk();
3573
3574 /* collect reference */
3575 ref = pat_ref_lookup(rule->arg.map.ref);
3576 if (!ref)
3577 continue;
3578
3579 /* collect key */
3580 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3581 key = trash_key->str;
3582 key[len] = '\0';
3583
3584 /* perform update */
3585 /* add entry only if it does not already exist */
3586 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003587 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003588
3589 break;
3590 }
3591
3592 case HTTP_REQ_ACT_SET_MAP: {
3593 struct pat_ref *ref;
3594 char *key, *value;
3595 struct chunk *trash_key, *trash_value;
3596 int len;
3597
3598 trash_key = get_trash_chunk();
3599 trash_value = get_trash_chunk();
3600
3601 /* collect reference */
3602 ref = pat_ref_lookup(rule->arg.map.ref);
3603 if (!ref)
3604 continue;
3605
3606 /* collect key */
3607 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3608 key = trash_key->str;
3609 key[len] = '\0';
3610
3611 /* collect value */
3612 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3613 value = trash_value->str;
3614 value[len] = '\0';
3615
3616 /* perform update */
3617 if (pat_ref_find_elt(ref, key) != NULL)
3618 /* update entry if it exists */
3619 pat_ref_set(ref, key, value, NULL);
3620 else
3621 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003622 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003623
3624 break;
3625 }
William Lallemand73025dd2014-04-24 14:38:37 +02003626
3627 case HTTP_REQ_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003628 if (!rule->action_ptr(rule, px, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003629 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003630 return HTTP_RULE_RES_YIELD;
3631 }
William Lallemand73025dd2014-04-24 14:38:37 +02003632 break;
3633
3634 case HTTP_REQ_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003635 rule->action_ptr(rule, px, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003636 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003637
3638 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3639 /* Note: only the first valid tracking parameter of each
3640 * applies.
3641 */
3642
3643 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3644 struct stktable *t;
3645 struct stksess *ts;
3646 struct stktable_key *key;
3647 void *ptr;
3648
3649 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau192252e2015-04-04 01:47:55 +02003650 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 +02003651
3652 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003653 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003654
3655 /* let's count a new HTTP request as it's the first time we do it */
3656 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3657 if (ptr)
3658 stktable_data_cast(ptr, http_req_cnt)++;
3659
3660 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3661 if (ptr)
3662 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3663 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3664
3665 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003666 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003667 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3668 }
3669 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003670 }
3671 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003672
3673 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003674 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003675}
3676
Willy Tarreau71241ab2012-12-27 11:30:54 +01003677
Willy Tarreau87b09662015-04-03 00:22:06 +02003678/* Executes the http-response rules <rules> for stream <s>, proxy <px> and
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003679 * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD
3680 * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the
3681 * evaluation of next rule list. If *STOP is returned, the process must stop
3682 * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny
3683 * rule. If *YIELD is returned, the czller must call again the function with
3684 * the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003685 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003686static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003687http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003688{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003689 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003690 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003691 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003692 struct http_res_rule *rule;
3693 struct hdr_ctx ctx;
3694
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003695 /* If "the current_rule_list" match the executed rule list, we are in
3696 * resume condition. If a resume is needed it is always in the action
3697 * and never in the ACL or converters. In this case, we initialise the
3698 * current rule, and go to the action execution point.
3699 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003700 if (s->current_rule) {
3701 rule = s->current_rule;
3702 s->current_rule = NULL;
3703 if (s->current_rule_list == rules)
3704 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003705 }
3706 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003707
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003708 list_for_each_entry(rule, rules, list) {
3709 if (rule->action >= HTTP_RES_ACT_MAX)
3710 continue;
3711
3712 /* check optional condition */
3713 if (rule->cond) {
3714 int ret;
3715
Willy Tarreau192252e2015-04-04 01:47:55 +02003716 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003717 ret = acl_pass(ret);
3718
3719 if (rule->cond->pol == ACL_COND_UNLESS)
3720 ret = !ret;
3721
3722 if (!ret) /* condition not matched */
3723 continue;
3724 }
3725
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003726resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003727 switch (rule->action) {
3728 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003729 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003730
3731 case HTTP_RES_ACT_DENY:
3732 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003733 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003734
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003735 case HTTP_RES_ACT_SET_NICE:
3736 s->task->nice = rule->arg.nice;
3737 break;
3738
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003739 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003740 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003741 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003742 break;
3743
Willy Tarreau51347ed2013-06-11 19:34:13 +02003744 case HTTP_RES_ACT_SET_MARK:
3745#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003746 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003747 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003748#endif
3749 break;
3750
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003751 case HTTP_RES_ACT_SET_LOGL:
3752 s->logs.level = rule->arg.loglevel;
3753 break;
3754
Sasha Pachev218f0642014-06-16 12:05:59 -06003755 case HTTP_RES_ACT_REPLACE_HDR:
3756 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003757 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3758 rule->arg.hdr_add.name_len,
3759 &rule->arg.hdr_add.fmt,
3760 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003761 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003762 break;
3763
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003764 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003765 ctx.idx = 0;
3766 /* remove all occurrences of the header */
3767 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3768 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3769 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3770 }
Willy Tarreau85603282015-01-21 20:39:27 +01003771 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003772
Willy Tarreau85603282015-01-21 20:39:27 +01003773 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003774 case HTTP_RES_ACT_ADD_HDR:
3775 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3776 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3777 trash.len = rule->arg.hdr_add.name_len;
3778 trash.str[trash.len++] = ':';
3779 trash.str[trash.len++] = ' ';
3780 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 +01003781
3782 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3783 /* remove all occurrences of the header */
3784 ctx.idx = 0;
3785 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3786 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3787 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3788 }
3789 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003790 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3791 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003792
3793 case HTTP_RES_ACT_DEL_ACL:
3794 case HTTP_RES_ACT_DEL_MAP: {
3795 struct pat_ref *ref;
3796 char *key;
3797 int len;
3798
3799 /* collect reference */
3800 ref = pat_ref_lookup(rule->arg.map.ref);
3801 if (!ref)
3802 continue;
3803
3804 /* collect key */
3805 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3806 key = trash.str;
3807 key[len] = '\0';
3808
3809 /* perform update */
3810 /* returned code: 1=ok, 0=ko */
3811 pat_ref_delete(ref, key);
3812
3813 break;
3814 }
3815
3816 case HTTP_RES_ACT_ADD_ACL: {
3817 struct pat_ref *ref;
3818 char *key;
3819 struct chunk *trash_key;
3820 int len;
3821
3822 trash_key = get_trash_chunk();
3823
3824 /* collect reference */
3825 ref = pat_ref_lookup(rule->arg.map.ref);
3826 if (!ref)
3827 continue;
3828
3829 /* collect key */
3830 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3831 key = trash_key->str;
3832 key[len] = '\0';
3833
3834 /* perform update */
3835 /* check if the entry already exists */
3836 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003837 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003838
3839 break;
3840 }
3841
3842 case HTTP_RES_ACT_SET_MAP: {
3843 struct pat_ref *ref;
3844 char *key, *value;
3845 struct chunk *trash_key, *trash_value;
3846 int len;
3847
3848 trash_key = get_trash_chunk();
3849 trash_value = get_trash_chunk();
3850
3851 /* collect reference */
3852 ref = pat_ref_lookup(rule->arg.map.ref);
3853 if (!ref)
3854 continue;
3855
3856 /* collect key */
3857 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3858 key = trash_key->str;
3859 key[len] = '\0';
3860
3861 /* collect value */
3862 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3863 value = trash_value->str;
3864 value[len] = '\0';
3865
3866 /* perform update */
3867 if (pat_ref_find_elt(ref, key) != NULL)
3868 /* update entry if it exists */
3869 pat_ref_set(ref, key, value, NULL);
3870 else
3871 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003872 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003873
3874 break;
3875 }
William Lallemand73025dd2014-04-24 14:38:37 +02003876
3877 case HTTP_RES_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003878 if (!rule->action_ptr(rule, px, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003879 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003880 return HTTP_RULE_RES_YIELD;
3881 }
William Lallemand73025dd2014-04-24 14:38:37 +02003882 break;
3883
3884 case HTTP_RES_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003885 rule->action_ptr(rule, px, s);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003886 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003887 }
3888 }
3889
3890 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003891 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003892}
3893
3894
Willy Tarreau71241ab2012-12-27 11:30:54 +01003895/* Perform an HTTP redirect based on the information in <rule>. The function
3896 * returns non-zero on success, or zero in case of a, irrecoverable error such
3897 * as too large a request to build a valid response.
3898 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003899static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003900{
3901 struct http_msg *msg = &txn->req;
3902 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003903 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003904
3905 /* build redirect message */
3906 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003907 case 308:
3908 msg_fmt = HTTP_308;
3909 break;
3910 case 307:
3911 msg_fmt = HTTP_307;
3912 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003913 case 303:
3914 msg_fmt = HTTP_303;
3915 break;
3916 case 301:
3917 msg_fmt = HTTP_301;
3918 break;
3919 case 302:
3920 default:
3921 msg_fmt = HTTP_302;
3922 break;
3923 }
3924
3925 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3926 return 0;
3927
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003928 location = trash.str + trash.len;
3929
Willy Tarreau71241ab2012-12-27 11:30:54 +01003930 switch(rule->type) {
3931 case REDIRECT_TYPE_SCHEME: {
3932 const char *path;
3933 const char *host;
3934 struct hdr_ctx ctx;
3935 int pathlen;
3936 int hostlen;
3937
3938 host = "";
3939 hostlen = 0;
3940 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003941 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003942 host = ctx.line + ctx.val;
3943 hostlen = ctx.vlen;
3944 }
3945
3946 path = http_get_path(txn);
3947 /* build message using path */
3948 if (path) {
3949 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3950 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3951 int qs = 0;
3952 while (qs < pathlen) {
3953 if (path[qs] == '?') {
3954 pathlen = qs;
3955 break;
3956 }
3957 qs++;
3958 }
3959 }
3960 } else {
3961 path = "/";
3962 pathlen = 1;
3963 }
3964
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003965 if (rule->rdr_str) { /* this is an old "redirect" rule */
3966 /* check if we can add scheme + "://" + host + path */
3967 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3968 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003969
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003970 /* add scheme */
3971 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3972 trash.len += rule->rdr_len;
3973 }
3974 else {
3975 /* add scheme with executing log format */
3976 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003977
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003978 /* check if we can add scheme + "://" + host + path */
3979 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3980 return 0;
3981 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003982 /* add "://" */
3983 memcpy(trash.str + trash.len, "://", 3);
3984 trash.len += 3;
3985
3986 /* add host */
3987 memcpy(trash.str + trash.len, host, hostlen);
3988 trash.len += hostlen;
3989
3990 /* add path */
3991 memcpy(trash.str + trash.len, path, pathlen);
3992 trash.len += pathlen;
3993
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003994 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003995 if (trash.len && trash.str[trash.len - 1] != '/' &&
3996 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3997 if (trash.len > trash.size - 5)
3998 return 0;
3999 trash.str[trash.len] = '/';
4000 trash.len++;
4001 }
4002
4003 break;
4004 }
4005 case REDIRECT_TYPE_PREFIX: {
4006 const char *path;
4007 int pathlen;
4008
4009 path = http_get_path(txn);
4010 /* build message using path */
4011 if (path) {
4012 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
4013 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4014 int qs = 0;
4015 while (qs < pathlen) {
4016 if (path[qs] == '?') {
4017 pathlen = qs;
4018 break;
4019 }
4020 qs++;
4021 }
4022 }
4023 } else {
4024 path = "/";
4025 pathlen = 1;
4026 }
4027
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004028 if (rule->rdr_str) { /* this is an old "redirect" rule */
4029 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4030 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004031
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004032 /* add prefix. Note that if prefix == "/", we don't want to
4033 * add anything, otherwise it makes it hard for the user to
4034 * configure a self-redirection.
4035 */
4036 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4037 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4038 trash.len += rule->rdr_len;
4039 }
4040 }
4041 else {
4042 /* add prefix with executing log format */
4043 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4044
4045 /* Check length */
4046 if (trash.len + pathlen > trash.size - 4)
4047 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004048 }
4049
4050 /* add path */
4051 memcpy(trash.str + trash.len, path, pathlen);
4052 trash.len += pathlen;
4053
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004054 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004055 if (trash.len && trash.str[trash.len - 1] != '/' &&
4056 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4057 if (trash.len > trash.size - 5)
4058 return 0;
4059 trash.str[trash.len] = '/';
4060 trash.len++;
4061 }
4062
4063 break;
4064 }
4065 case REDIRECT_TYPE_LOCATION:
4066 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004067 if (rule->rdr_str) { /* this is an old "redirect" rule */
4068 if (trash.len + rule->rdr_len > trash.size - 4)
4069 return 0;
4070
4071 /* add location */
4072 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4073 trash.len += rule->rdr_len;
4074 }
4075 else {
4076 /* add location with executing log format */
4077 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004078
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004079 /* Check left length */
4080 if (trash.len > trash.size - 4)
4081 return 0;
4082 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004083 break;
4084 }
4085
4086 if (rule->cookie_len) {
4087 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4088 trash.len += 14;
4089 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4090 trash.len += rule->cookie_len;
4091 memcpy(trash.str + trash.len, "\r\n", 2);
4092 trash.len += 2;
4093 }
4094
4095 /* add end of headers and the keep-alive/close status.
4096 * We may choose to set keep-alive if the Location begins
4097 * with a slash, because the client will come back to the
4098 * same server.
4099 */
4100 txn->status = rule->code;
4101 /* let's log the request time */
4102 s->logs.tv_request = now;
4103
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004104 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004105 (msg->flags & HTTP_MSGF_XFER_LEN) &&
4106 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
4107 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4108 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4109 /* keep-alive possible */
4110 if (!(msg->flags & HTTP_MSGF_VER_11)) {
4111 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4112 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4113 trash.len += 30;
4114 } else {
4115 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4116 trash.len += 24;
4117 }
4118 }
4119 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4120 trash.len += 4;
4121 bo_inject(txn->rsp.chn, trash.str, trash.len);
4122 /* "eat" the request */
4123 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02004124 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004125 msg->sov = 0;
4126 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004127 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004128 txn->req.msg_state = HTTP_MSG_CLOSED;
4129 txn->rsp.msg_state = HTTP_MSG_DONE;
4130 } else {
4131 /* keep-alive not possible */
4132 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4133 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4134 trash.len += 29;
4135 } else {
4136 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4137 trash.len += 23;
4138 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004139 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004140 txn->req.chn->analysers = 0;
4141 }
4142
Willy Tarreaue7dff022015-04-03 01:14:29 +02004143 if (!(s->flags & SF_ERR_MASK))
4144 s->flags |= SF_ERR_LOCAL;
4145 if (!(s->flags & SF_FINST_MASK))
4146 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004147
4148 return 1;
4149}
4150
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004151/* This stream analyser runs all HTTP request processing which is common to
4152 * frontends and backends, which means blocking ACLs, filters, connection-close,
4153 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004154 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004155 * either needs more data or wants to immediately abort the request (eg: deny,
4156 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004157 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004158int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004159{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004160 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004161 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004162 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004163 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004164 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004165 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004166
Willy Tarreau655dce92009-11-08 13:10:58 +01004167 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004168 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004169 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004170 }
4171
Willy Tarreau87b09662015-04-03 00:22:06 +02004172 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 +02004173 now_ms, __FUNCTION__,
4174 s,
4175 req,
4176 req->rex, req->wex,
4177 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004178 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004179 req->analysers);
4180
Willy Tarreau65410832014-04-28 21:25:43 +02004181 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004182 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004183
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004184 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004185 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004186 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004187
Willy Tarreau0b748332014-04-29 00:13:29 +02004188 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004189 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4190 goto return_prx_yield;
4191
Willy Tarreau0b748332014-04-29 00:13:29 +02004192 case HTTP_RULE_RES_CONT:
4193 case HTTP_RULE_RES_STOP: /* nothing to do */
4194 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004195
Willy Tarreau0b748332014-04-29 00:13:29 +02004196 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4197 if (txn->flags & TX_CLTARPIT)
4198 goto tarpit;
4199 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004200
Willy Tarreau0b748332014-04-29 00:13:29 +02004201 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4202 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004203
Willy Tarreau0b748332014-04-29 00:13:29 +02004204 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004205 goto done;
4206
Willy Tarreau0b748332014-04-29 00:13:29 +02004207 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4208 goto return_bad_req;
4209 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004210 }
4211
Willy Tarreau52542592014-04-28 18:33:26 +02004212 /* OK at this stage, we know that the request was accepted according to
4213 * the http-request rules, we can check for the stats. Note that the
4214 * URI is detected *before* the req* rules in order not to be affected
4215 * by a possible reqrep, while they are processed *after* so that a
4216 * reqdeny can still block them. This clearly needs to change in 1.6!
4217 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004218 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004219 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004220 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004221 txn->status = 500;
4222 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004223 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004224
Willy Tarreaue7dff022015-04-03 01:14:29 +02004225 if (!(s->flags & SF_ERR_MASK))
4226 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004227 goto return_prx_cond;
4228 }
4229
4230 /* parse the whole stats request and extract the relevant information */
4231 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004232 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004233 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004234
Willy Tarreau0b748332014-04-29 00:13:29 +02004235 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4236 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004237
Willy Tarreau0b748332014-04-29 00:13:29 +02004238 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4239 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004240 }
4241
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004242 /* evaluate the req* rules except reqadd */
4243 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004244 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004245 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004246
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004247 if (txn->flags & TX_CLDENY)
4248 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004249
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004250 if (txn->flags & TX_CLTARPIT)
4251 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004252 }
Willy Tarreau06619262006-12-17 08:37:22 +01004253
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004254 /* add request headers from the rule sets in the same order */
4255 list_for_each_entry(wl, &px->req_add, list) {
4256 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004257 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004258 ret = acl_pass(ret);
4259 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4260 ret = !ret;
4261 if (!ret)
4262 continue;
4263 }
4264
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004265 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004266 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004267 }
4268
Willy Tarreau52542592014-04-28 18:33:26 +02004269
4270 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004271 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004272 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004273 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4274 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004275
Willy Tarreaue7dff022015-04-03 01:14:29 +02004276 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4277 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4278 if (!(s->flags & SF_FINST_MASK))
4279 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004280
Willy Tarreau70730dd2014-04-24 18:06:27 +02004281 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004282 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004283 select_compression_request_header(s, req->buf);
4284
4285 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004286 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004287 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004288 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004289
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004290 /* check whether we have some ACLs set to redirect this request */
4291 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004292 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004293 int ret;
4294
Willy Tarreau192252e2015-04-04 01:47:55 +02004295 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004296 ret = acl_pass(ret);
4297 if (rule->cond->pol == ACL_COND_UNLESS)
4298 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004299 if (!ret)
4300 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004301 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004302 if (!http_apply_redirect_rule(rule, s, txn))
4303 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004304 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004305 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004306
Willy Tarreau2be39392010-01-03 17:24:51 +01004307 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4308 * If this happens, then the data will not come immediately, so we must
4309 * send all what we have without waiting. Note that due to the small gain
4310 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004311 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004312 * itself once used.
4313 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004314 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004315
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004316 done: /* done with this analyser, continue with next ones that the calling
4317 * points will have set, if any.
4318 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004319 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004320 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4321 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004322 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004323
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004324 tarpit:
4325 /* When a connection is tarpitted, we use the tarpit timeout,
4326 * which may be the same as the connect timeout if unspecified.
4327 * If unset, then set it to zero because we really want it to
4328 * eventually expire. We build the tarpit as an analyser.
4329 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004330 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004331
4332 /* wipe the request out so that we can drop the connection early
4333 * if the client closes first.
4334 */
4335 channel_dont_connect(req);
4336 req->analysers = 0; /* remove switching rules etc... */
4337 req->analysers |= AN_REQ_HTTP_TARPIT;
4338 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4339 if (!req->analyse_exp)
4340 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004341 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004342 sess->fe->fe_counters.denied_req++;
4343 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004344 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004345 if (sess->listener->counters)
4346 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004347 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004348
4349 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004350 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004351 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004352 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004353 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004354 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004355 sess->fe->fe_counters.denied_req++;
4356 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004357 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004358 if (sess->listener->counters)
4359 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004360 goto return_prx_cond;
4361
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004362 return_bad_req:
4363 /* We centralize bad requests processing here */
4364 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4365 /* we detected a parsing error. We want to archive this request
4366 * in the dedicated proxy area for later troubleshooting.
4367 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004368 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004369 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004370
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004371 txn->req.msg_state = HTTP_MSG_ERROR;
4372 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004373 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004374
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004375 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004376 if (sess->listener->counters)
4377 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004378
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004379 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004380 if (!(s->flags & SF_ERR_MASK))
4381 s->flags |= SF_ERR_PRXCOND;
4382 if (!(s->flags & SF_FINST_MASK))
4383 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004384
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004385 req->analysers = 0;
4386 req->analyse_exp = TICK_ETERNITY;
4387 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004388
4389 return_prx_yield:
4390 channel_dont_connect(req);
4391 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004392}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004393
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004394/* This function performs all the processing enabled for the current request.
4395 * It returns 1 if the processing can continue on next analysers, or zero if it
4396 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004397 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004398 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004399int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004400{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004401 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004402 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004403 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004404 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004405
Willy Tarreau655dce92009-11-08 13:10:58 +01004406 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004407 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004408 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004409 return 0;
4410 }
4411
Willy Tarreau87b09662015-04-03 00:22:06 +02004412 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 +02004413 now_ms, __FUNCTION__,
4414 s,
4415 req,
4416 req->rex, req->wex,
4417 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004418 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004419 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004420
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004421 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004422 select_compression_request_header(s, req->buf);
4423
Willy Tarreau59234e92008-11-30 23:51:27 +01004424 /*
4425 * Right now, we know that we have processed the entire headers
4426 * and that unwanted requests have been filtered out. We can do
4427 * whatever we want with the remaining request. Also, now we
4428 * may have separate values for ->fe, ->be.
4429 */
Willy Tarreau06619262006-12-17 08:37:22 +01004430
Willy Tarreau59234e92008-11-30 23:51:27 +01004431 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004432 * If HTTP PROXY is set we simply get remote server address parsing
4433 * incoming request. Note that this requires that a connection is
4434 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004435 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004436 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004437 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004438 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004439
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004440 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau350f4872014-11-28 14:42:25 +01004441 if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004442 txn->req.msg_state = HTTP_MSG_ERROR;
4443 txn->status = 500;
4444 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004445 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004446
Willy Tarreaue7dff022015-04-03 01:14:29 +02004447 if (!(s->flags & SF_ERR_MASK))
4448 s->flags |= SF_ERR_RESOURCE;
4449 if (!(s->flags & SF_FINST_MASK))
4450 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004451
4452 return 0;
4453 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004454
4455 path = http_get_path(txn);
4456 url2sa(req->buf->p + msg->sl.rq.u,
4457 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004458 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004459 /* if the path was found, we have to remove everything between
4460 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4461 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4462 * u_l characters by a single "/".
4463 */
4464 if (path) {
4465 char *cur_ptr = req->buf->p;
4466 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4467 int delta;
4468
4469 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4470 http_msg_move_end(&txn->req, delta);
4471 cur_end += delta;
4472 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4473 goto return_bad_req;
4474 }
4475 else {
4476 char *cur_ptr = req->buf->p;
4477 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4478 int delta;
4479
4480 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4481 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4482 http_msg_move_end(&txn->req, delta);
4483 cur_end += delta;
4484 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4485 goto return_bad_req;
4486 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004487 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004488
Willy Tarreau59234e92008-11-30 23:51:27 +01004489 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004490 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004491 * Note that doing so might move headers in the request, but
4492 * the fields will stay coherent and the URI will not move.
4493 * This should only be performed in the backend.
4494 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004495 if ((s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004496 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4497 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004498
Willy Tarreau59234e92008-11-30 23:51:27 +01004499 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004500 * 8: the appsession cookie was looked up very early in 1.2,
4501 * so let's do the same now.
4502 */
4503
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004504 /* It needs to look into the URI unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004505 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004506 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 +01004507 }
4508
William Lallemanda73203e2012-03-12 12:48:57 +01004509 /* add unique-id if "header-unique-id" is specified */
4510
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004511 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004512 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4513 goto return_bad_req;
4514 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004515 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004516 }
William Lallemanda73203e2012-03-12 12:48:57 +01004517
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004518 if (sess->fe->header_unique_id && s->unique_id) {
4519 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004520 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004521 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004522 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004523 goto return_bad_req;
4524 }
4525
Cyril Bontéb21570a2009-11-29 20:04:48 +01004526 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004527 * 9: add X-Forwarded-For if either the frontend or the backend
4528 * asks for it.
4529 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004530 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004531 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004532 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4533 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4534 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004535 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004536 /* The header is set to be added only if none is present
4537 * and we found it, so don't do anything.
4538 */
4539 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004540 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004541 /* Add an X-Forwarded-For header unless the source IP is
4542 * in the 'except' network range.
4543 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004544 if ((!sess->fe->except_mask.s_addr ||
4545 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4546 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004547 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004548 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004549 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004550 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004551 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004552 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004553
4554 /* Note: we rely on the backend to get the header name to be used for
4555 * x-forwarded-for, because the header is really meant for the backends.
4556 * However, if the backend did not specify any option, we have to rely
4557 * on the frontend's header name.
4558 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004559 if (s->be->fwdfor_hdr_len) {
4560 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004561 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004562 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004563 len = sess->fe->fwdfor_hdr_len;
4564 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004565 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004566 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 +01004567
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004568 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004569 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004570 }
4571 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004572 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004573 /* FIXME: for the sake of completeness, we should also support
4574 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004575 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004576 int len;
4577 char pn[INET6_ADDRSTRLEN];
4578 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004579 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004580 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004581
Willy Tarreau59234e92008-11-30 23:51:27 +01004582 /* Note: we rely on the backend to get the header name to be used for
4583 * x-forwarded-for, because the header is really meant for the backends.
4584 * However, if the backend did not specify any option, we have to rely
4585 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004586 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004587 if (s->be->fwdfor_hdr_len) {
4588 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004589 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004590 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004591 len = sess->fe->fwdfor_hdr_len;
4592 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004593 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004594 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004595
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004596 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004597 goto return_bad_req;
4598 }
4599 }
4600
4601 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004602 * 10: add X-Original-To if either the frontend or the backend
4603 * asks for it.
4604 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004605 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004606
4607 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004608 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004609 /* Add an X-Original-To header unless the destination IP is
4610 * in the 'except' network range.
4611 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004612 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004613
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004614 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004615 ((!sess->fe->except_mask_to.s_addr ||
4616 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4617 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004618 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004619 (((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 +02004620 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004621 int len;
4622 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004623 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004624
4625 /* Note: we rely on the backend to get the header name to be used for
4626 * x-original-to, because the header is really meant for the backends.
4627 * However, if the backend did not specify any option, we have to rely
4628 * on the frontend's header name.
4629 */
4630 if (s->be->orgto_hdr_len) {
4631 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004632 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004633 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004634 len = sess->fe->orgto_hdr_len;
4635 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004636 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004637 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 +02004638
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004639 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004640 goto return_bad_req;
4641 }
4642 }
4643 }
4644
Willy Tarreau50fc7772012-11-11 22:19:57 +01004645 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4646 * If an "Upgrade" token is found, the header is left untouched in order not to have
4647 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4648 * "Upgrade" is present in the Connection header.
4649 */
4650 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4651 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004652 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004653 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004654 unsigned int want_flags = 0;
4655
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004656 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004657 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004658 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004659 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004660 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004661 want_flags |= TX_CON_CLO_SET;
4662 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004663 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004664 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004665 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004666 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004667 want_flags |= TX_CON_KAL_SET;
4668 }
4669
4670 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004671 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004672 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004673
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004674
Willy Tarreau522d6c02009-12-06 18:49:18 +01004675 /* If we have no server assigned yet and we're balancing on url_param
4676 * with a POST request, we may be interested in checking the body for
4677 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004678 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004679 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004680 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004681 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004682 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004683 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004684 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004685
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004686 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004687 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004688#ifdef TCP_QUICKACK
4689 /* We expect some data from the client. Unless we know for sure
4690 * we already have a full request, we have to re-enable quick-ack
4691 * in case we previously disabled it, otherwise we might cause
4692 * the client to delay further data.
4693 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004694 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004695 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004696 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004697 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004698 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004699#endif
4700 }
Willy Tarreau03945942009-12-22 16:50:27 +01004701
Willy Tarreau59234e92008-11-30 23:51:27 +01004702 /*************************************************************
4703 * OK, that's finished for the headers. We have done what we *
4704 * could. Let's switch to the DATA state. *
4705 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004706 req->analyse_exp = TICK_ETERNITY;
4707 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004708
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004709 /* if the server closes the connection, we want to immediately react
4710 * and close the socket to save packets and syscalls.
4711 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004712 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004713 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004714
Willy Tarreau59234e92008-11-30 23:51:27 +01004715 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004716 /* OK let's go on with the BODY now */
4717 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004718
Willy Tarreau59234e92008-11-30 23:51:27 +01004719 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004720 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004721 /* we detected a parsing error. We want to archive this request
4722 * in the dedicated proxy area for later troubleshooting.
4723 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004724 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004725 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004726
Willy Tarreau59234e92008-11-30 23:51:27 +01004727 txn->req.msg_state = HTTP_MSG_ERROR;
4728 txn->status = 400;
4729 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004730 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004731
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004732 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004733 if (sess->listener->counters)
4734 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004735
Willy Tarreaue7dff022015-04-03 01:14:29 +02004736 if (!(s->flags & SF_ERR_MASK))
4737 s->flags |= SF_ERR_PRXCOND;
4738 if (!(s->flags & SF_FINST_MASK))
4739 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004740 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004741}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004742
Willy Tarreau60b85b02008-11-30 23:28:40 +01004743/* This function is an analyser which processes the HTTP tarpit. It always
4744 * returns zero, at the beginning because it prevents any other processing
4745 * from occurring, and at the end because it terminates the request.
4746 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004747int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004748{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004749 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004750
4751 /* This connection is being tarpitted. The CLIENT side has
4752 * already set the connect expiration date to the right
4753 * timeout. We just have to check that the client is still
4754 * there and that the timeout has not expired.
4755 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004756 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004757 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004758 !tick_is_expired(req->analyse_exp, now_ms))
4759 return 0;
4760
4761 /* We will set the queue timer to the time spent, just for
4762 * logging purposes. We fake a 500 server error, so that the
4763 * attacker will not suspect his connection has been tarpitted.
4764 * It will not cause trouble to the logs because we can exclude
4765 * the tarpitted connections by filtering on the 'PT' status flags.
4766 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004767 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4768
4769 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004770 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004771 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004772
4773 req->analysers = 0;
4774 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004775
Willy Tarreaue7dff022015-04-03 01:14:29 +02004776 if (!(s->flags & SF_ERR_MASK))
4777 s->flags |= SF_ERR_PRXCOND;
4778 if (!(s->flags & SF_FINST_MASK))
4779 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004780 return 0;
4781}
4782
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004783/* This function is an analyser which waits for the HTTP request body. It waits
4784 * for either the buffer to be full, or the full advertised contents to have
4785 * reached the buffer. It must only be called after the standard HTTP request
4786 * processing has occurred, because it expects the request to be parsed and will
4787 * look for the Expect header. It may send a 100-Continue interim response. It
4788 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4789 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4790 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004791 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004792int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004793{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004794 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004795 struct http_txn *txn = s->txn;
4796 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004797
4798 /* We have to parse the HTTP request body to find any required data.
4799 * "balance url_param check_post" should have been the only way to get
4800 * into this. We were brought here after HTTP header analysis, so all
4801 * related structures are ready.
4802 */
4803
Willy Tarreau890988f2014-04-10 11:59:33 +02004804 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4805 /* This is the first call */
4806 if (msg->msg_state < HTTP_MSG_BODY)
4807 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004808
Willy Tarreau890988f2014-04-10 11:59:33 +02004809 if (msg->msg_state < HTTP_MSG_100_SENT) {
4810 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4811 * send an HTTP/1.1 100 Continue intermediate response.
4812 */
4813 if (msg->flags & HTTP_MSGF_VER_11) {
4814 struct hdr_ctx ctx;
4815 ctx.idx = 0;
4816 /* Expect is allowed in 1.1, look for it */
4817 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4818 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004819 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004820 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004821 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004822 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004823 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004824
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004825 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004826 * req->buf->p still points to the beginning of the message. We
4827 * must save the body in msg->next because it survives buffer
4828 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004829 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004830 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004831
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004832 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004833 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4834 else
4835 msg->msg_state = HTTP_MSG_DATA;
4836 }
4837
Willy Tarreau890988f2014-04-10 11:59:33 +02004838 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4839 /* We're in content-length mode, we just have to wait for enough data. */
4840 if (req->buf->i - msg->sov < msg->body_len)
4841 goto missing_data;
4842
4843 /* OK we have everything we need now */
4844 goto http_end;
4845 }
4846
4847 /* OK here we're parsing a chunked-encoded message */
4848
Willy Tarreau522d6c02009-12-06 18:49:18 +01004849 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004850 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004851 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004852 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004853 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004854 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004855
Willy Tarreau115acb92009-12-26 13:56:06 +01004856 if (!ret)
4857 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004858 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004859 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004860 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004861 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004862 }
4863
Willy Tarreaud98cf932009-12-27 22:54:55 +01004864 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004865 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004866 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004867 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004868 if (msg->msg_state == HTTP_MSG_TRAILERS)
4869 goto http_end;
4870
Willy Tarreau226071e2014-04-10 11:55:45 +02004871 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004872 goto http_end;
4873
4874 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004875 /* we get here if we need to wait for more data. If the buffer is full,
4876 * we have the maximum we can expect.
4877 */
4878 if (buffer_full(req->buf, global.tune.maxrewrite))
4879 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004880
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004881 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004882 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004883 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004884
Willy Tarreaue7dff022015-04-03 01:14:29 +02004885 if (!(s->flags & SF_ERR_MASK))
4886 s->flags |= SF_ERR_CLITO;
4887 if (!(s->flags & SF_FINST_MASK))
4888 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004889 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004890 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004891
4892 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004893 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004894 /* Not enough data. We'll re-use the http-request
4895 * timeout here. Ideally, we should set the timeout
4896 * relative to the accept() date. We just set the
4897 * request timeout once at the beginning of the
4898 * request.
4899 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004900 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004901 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004902 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004903 return 0;
4904 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004905
4906 http_end:
4907 /* The situation will not evolve, so let's give up on the analysis. */
4908 s->logs.tv_request = now; /* update the request timer to reflect full request */
4909 req->analysers &= ~an_bit;
4910 req->analyse_exp = TICK_ETERNITY;
4911 return 1;
4912
4913 return_bad_req: /* let's centralize all bad requests */
4914 txn->req.msg_state = HTTP_MSG_ERROR;
4915 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004916 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004917
Willy Tarreaue7dff022015-04-03 01:14:29 +02004918 if (!(s->flags & SF_ERR_MASK))
4919 s->flags |= SF_ERR_PRXCOND;
4920 if (!(s->flags & SF_FINST_MASK))
4921 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004922
Willy Tarreau522d6c02009-12-06 18:49:18 +01004923 return_err_msg:
4924 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004925 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004926 if (sess->listener->counters)
4927 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004928 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004929}
4930
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004931/* send a server's name with an outgoing request over an established connection.
4932 * Note: this function is designed to be called once the request has been scheduled
4933 * for being forwarded. This is the reason why it rewinds the buffer before
4934 * proceeding.
4935 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004936int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004937
4938 struct hdr_ctx ctx;
4939
Mark Lamourinec2247f02012-01-04 13:02:01 -05004940 char *hdr_name = be->server_id_hdr_name;
4941 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004942 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004943 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004944 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004945
William Lallemandd9e90662012-01-30 17:27:17 +01004946 ctx.idx = 0;
4947
Willy Tarreau211cdec2014-04-17 20:18:08 +02004948 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004949 if (old_o) {
4950 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004951 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004952 txn->req.next += old_o;
4953 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004954 }
4955
Willy Tarreau9b28e032012-10-12 23:49:43 +02004956 old_i = chn->buf->i;
4957 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 -05004958 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004959 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004960 }
4961
4962 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004963 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004964 memcpy(hdr_val, hdr_name, hdr_name_len);
4965 hdr_val += hdr_name_len;
4966 *hdr_val++ = ':';
4967 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004968 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4969 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004970
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004971 if (old_o) {
4972 /* If this was a forwarded request, we must readjust the amount of
4973 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004974 * variations. Note that the current state is >= HTTP_MSG_BODY,
4975 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004976 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004977 old_o += chn->buf->i - old_i;
4978 b_adv(chn->buf, old_o);
4979 txn->req.next -= old_o;
4980 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004981 }
4982
Mark Lamourinec2247f02012-01-04 13:02:01 -05004983 return 0;
4984}
4985
Willy Tarreau610ecce2010-01-04 21:15:02 +01004986/* Terminate current transaction and prepare a new one. This is very tricky
4987 * right now but it works.
4988 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004989void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004990{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004991 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004992 struct proxy *fe = strm_fe(s);
Willy Tarreau068621e2013-12-23 15:11:25 +01004993
Willy Tarreau610ecce2010-01-04 21:15:02 +01004994 /* FIXME: We need a more portable way of releasing a backend's and a
4995 * server's connections. We need a safer way to reinitialize buffer
4996 * flags. We also need a more accurate method for computing per-request
4997 * data.
4998 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004999
Willy Tarreau4213a112013-12-15 10:25:42 +01005000 /* unless we're doing keep-alive, we want to quickly close the connection
5001 * to the server.
5002 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005003 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005004 !si_conn_ready(&s->si[1])) {
5005 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5006 si_shutr(&s->si[1]);
5007 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005008 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005009
Willy Tarreaue7dff022015-04-03 01:14:29 +02005010 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005011 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005012 if (unlikely(s->srv_conn))
5013 sess_change_server(s, NULL);
5014 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005015
5016 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005017 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005018
Willy Tarreaueee5b512015-04-03 23:46:31 +02005019 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005020 int n;
5021
Willy Tarreaueee5b512015-04-03 23:46:31 +02005022 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005023 if (n < 1 || n > 5)
5024 n = 0;
5025
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005026 if (fe->mode == PR_MODE_HTTP) {
5027 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005028 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005029 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005030 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005031 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005032 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005033 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005034 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005035 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005036 s->be->be_counters.p.http.comp_rsp++;
5037 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005038 }
5039
5040 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005041 s->logs.bytes_in -= s->req.buf->i;
5042 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005043
5044 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005045 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005046 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005047 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005048 s->do_log(s);
5049 }
5050
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005051 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005052 stream_stop_content_counters(s);
5053 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005054
Willy Tarreau610ecce2010-01-04 21:15:02 +01005055 s->logs.accept_date = date; /* user-visible date for logging */
5056 s->logs.tv_accept = now; /* corrected date for internal use */
5057 tv_zero(&s->logs.tv_request);
5058 s->logs.t_queue = -1;
5059 s->logs.t_connect = -1;
5060 s->logs.t_data = -1;
5061 s->logs.t_close = 0;
5062 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5063 s->logs.srv_queue_size = 0; /* we will get this number soon */
5064
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005065 s->logs.bytes_in = s->req.total = s->req.buf->i;
5066 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005067
5068 if (s->pend_pos)
5069 pendconn_free(s->pend_pos);
5070
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005071 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005072 if (s->flags & SF_CURR_SESS) {
5073 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005074 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005075 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005076 if (may_dequeue_tasks(objt_server(s->target), s->be))
5077 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005078 }
5079
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005080 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005081
Willy Tarreau4213a112013-12-15 10:25:42 +01005082 /* only release our endpoint if we don't intend to reuse the
5083 * connection.
5084 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005085 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005086 !si_conn_ready(&s->si[1])) {
5087 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005088 }
5089
Willy Tarreau350f4872014-11-28 14:42:25 +01005090 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5091 s->si[1].err_type = SI_ET_NONE;
5092 s->si[1].conn_retries = 0; /* used for logging too */
5093 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005094 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 +01005095 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);
5096 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 +02005097 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5098 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5099 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005100
Willy Tarreaueee5b512015-04-03 23:46:31 +02005101 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005102 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005103 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005104
5105 if (prev_status == 401 || prev_status == 407) {
5106 /* In HTTP keep-alive mode, if we receive a 401, we still have
5107 * a chance of being able to send the visitor again to the same
5108 * server over the same connection. This is required by some
5109 * broken protocols such as NTLM, and anyway whenever there is
5110 * an opportunity for sending the challenge to the proper place,
5111 * it's better to do it (at least it helps with debugging).
5112 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005113 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01005114 }
5115
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005116 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005117 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005118
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005119 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005120 s->req.flags |= CF_NEVER_WAIT;
5121 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005122 }
5123
Willy Tarreau610ecce2010-01-04 21:15:02 +01005124 /* if the request buffer is not empty, it means we're
5125 * about to process another request, so send pending
5126 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005127 * Just don't do this if the buffer is close to be full,
5128 * because the request will wait for it to flush a little
5129 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005130 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005131 if (s->req.buf->i) {
5132 if (s->res.buf->o &&
5133 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5134 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5135 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005136 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005137
5138 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005139 channel_auto_read(&s->req);
5140 channel_auto_close(&s->req);
5141 channel_auto_read(&s->res);
5142 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005143
Willy Tarreau27375622013-12-17 00:00:28 +01005144 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau350f4872014-11-28 14:42:25 +01005145 si_idle_conn(&s->si[1]);
Willy Tarreau27375622013-12-17 00:00:28 +01005146
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005147 s->req.analysers = strm_li(s)->analysers;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005148 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005149}
5150
5151
5152/* This function updates the request state machine according to the response
5153 * state machine and buffer flags. It returns 1 if it changes anything (flag
5154 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5155 * it is only used to find when a request/response couple is complete. Both
5156 * this function and its equivalent should loop until both return zero. It
5157 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5158 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005159int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005160{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005161 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005162 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005163 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005164 unsigned int old_state = txn->req.msg_state;
5165
Willy Tarreau610ecce2010-01-04 21:15:02 +01005166 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5167 return 0;
5168
5169 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005170 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005171 * We can shut the read side unless we want to abort_on_close,
5172 * or we have a POST request. The issue with POST requests is
5173 * that some browsers still send a CRLF after the request, and
5174 * this CRLF must be read so that it does not remain in the kernel
5175 * buffers, otherwise a close could cause an RST on some systems
5176 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005177 * Note that if we're using keep-alive on the client side, we'd
5178 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005179 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005180 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005181 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005182 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5183 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5184 !(s->be->options & PR_O_ABRT_CLOSE) &&
5185 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005186 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005187
Willy Tarreau40f151a2012-12-20 12:10:09 +01005188 /* if the server closes the connection, we want to immediately react
5189 * and close the socket to save packets and syscalls.
5190 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005191 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005192
Willy Tarreau610ecce2010-01-04 21:15:02 +01005193 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5194 goto wait_other_side;
5195
5196 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5197 /* The server has not finished to respond, so we
5198 * don't want to move in order not to upset it.
5199 */
5200 goto wait_other_side;
5201 }
5202
5203 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5204 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005205 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005206 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005207 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005208 goto wait_other_side;
5209 }
5210
5211 /* When we get here, it means that both the request and the
5212 * response have finished receiving. Depending on the connection
5213 * mode, we'll have to wait for the last bytes to leave in either
5214 * direction, and sometimes for a close to be effective.
5215 */
5216
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005217 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5218 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005219 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5220 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005221 }
5222 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5223 /* Option forceclose is set, or either side wants to close,
5224 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005225 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005226 * once both states are CLOSED.
5227 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005228 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5229 channel_shutr_now(chn);
5230 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005231 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005232 }
5233 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005234 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5235 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005236 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005237 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5238 channel_auto_read(chn);
5239 txn->req.msg_state = HTTP_MSG_TUNNEL;
5240 chn->flags |= CF_NEVER_WAIT;
5241 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005242 }
5243
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005244 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005245 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005246 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005247
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005248 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005249 txn->req.msg_state = HTTP_MSG_CLOSING;
5250 goto http_msg_closing;
5251 }
5252 else {
5253 txn->req.msg_state = HTTP_MSG_CLOSED;
5254 goto http_msg_closed;
5255 }
5256 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005257 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005258 }
5259
5260 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5261 http_msg_closing:
5262 /* nothing else to forward, just waiting for the output buffer
5263 * to be empty and for the shutw_now to take effect.
5264 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005265 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005266 txn->req.msg_state = HTTP_MSG_CLOSED;
5267 goto http_msg_closed;
5268 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005269 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005270 txn->req.msg_state = HTTP_MSG_ERROR;
5271 goto wait_other_side;
5272 }
5273 }
5274
5275 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5276 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005277 /* see above in MSG_DONE why we only do this in these states */
5278 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5279 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5280 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005281 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005282 goto wait_other_side;
5283 }
5284
5285 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005286 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005287}
5288
5289
5290/* This function updates the response state machine according to the request
5291 * state machine and buffer flags. It returns 1 if it changes anything (flag
5292 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5293 * it is only used to find when a request/response couple is complete. Both
5294 * this function and its equivalent should loop until both return zero. It
5295 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5296 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005297int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005298{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005299 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005300 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005301 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005302 unsigned int old_state = txn->rsp.msg_state;
5303
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5305 return 0;
5306
5307 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5308 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005309 * still monitor the server connection for a possible close
5310 * while the request is being uploaded, so we don't disable
5311 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005312 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005313 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005314
5315 if (txn->req.msg_state == HTTP_MSG_ERROR)
5316 goto wait_other_side;
5317
5318 if (txn->req.msg_state < HTTP_MSG_DONE) {
5319 /* The client seems to still be sending data, probably
5320 * because we got an error response during an upload.
5321 * We have the choice of either breaking the connection
5322 * or letting it pass through. Let's do the later.
5323 */
5324 goto wait_other_side;
5325 }
5326
5327 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5328 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005329 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005330 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005331 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005332 goto wait_other_side;
5333 }
5334
5335 /* When we get here, it means that both the request and the
5336 * response have finished receiving. Depending on the connection
5337 * mode, we'll have to wait for the last bytes to leave in either
5338 * direction, and sometimes for a close to be effective.
5339 */
5340
5341 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5342 /* Server-close mode : shut read and wait for the request
5343 * side to close its output buffer. The caller will detect
5344 * when we're in DONE and the other is in CLOSED and will
5345 * catch that for the final cleanup.
5346 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005347 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5348 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005349 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005350 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5351 /* Option forceclose is set, or either side wants to close,
5352 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005353 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005354 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005355 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005356 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5357 channel_shutr_now(chn);
5358 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005359 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005360 }
5361 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005362 /* The last possible modes are keep-alive and tunnel. Tunnel will
5363 * need to forward remaining data. Keep-alive will need to monitor
5364 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005365 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005366 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005367 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005368 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5369 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005370 }
5371
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005372 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005373 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005374 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005375 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5376 goto http_msg_closing;
5377 }
5378 else {
5379 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5380 goto http_msg_closed;
5381 }
5382 }
5383 goto wait_other_side;
5384 }
5385
5386 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5387 http_msg_closing:
5388 /* nothing else to forward, just waiting for the output buffer
5389 * to be empty and for the shutw_now to take effect.
5390 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005391 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005392 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5393 goto http_msg_closed;
5394 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005395 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005396 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005397 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005398 if (objt_server(s->target))
5399 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005400 goto wait_other_side;
5401 }
5402 }
5403
5404 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5405 http_msg_closed:
5406 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005407 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005408 channel_auto_close(chn);
5409 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005410 goto wait_other_side;
5411 }
5412
5413 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005414 /* We force the response to leave immediately if we're waiting for the
5415 * other side, since there is no pending shutdown to push it out.
5416 */
5417 if (!channel_is_empty(chn))
5418 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005419 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005420}
5421
5422
5423/* Resync the request and response state machines. Return 1 if either state
5424 * changes.
5425 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005426int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005427{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005428 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005429 int old_req_state = txn->req.msg_state;
5430 int old_res_state = txn->rsp.msg_state;
5431
Willy Tarreau610ecce2010-01-04 21:15:02 +01005432 http_sync_req_state(s);
5433 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005434 if (!http_sync_res_state(s))
5435 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005436 if (!http_sync_req_state(s))
5437 break;
5438 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005439
Willy Tarreau610ecce2010-01-04 21:15:02 +01005440 /* OK, both state machines agree on a compatible state.
5441 * There are a few cases we're interested in :
5442 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5443 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5444 * directions, so let's simply disable both analysers.
5445 * - HTTP_MSG_CLOSED on the response only means we must abort the
5446 * request.
5447 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5448 * with server-close mode means we've completed one request and we
5449 * must re-initialize the server connection.
5450 */
5451
5452 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5453 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5454 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5455 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005456 s->req.analysers = 0;
5457 channel_auto_close(&s->req);
5458 channel_auto_read(&s->req);
5459 s->res.analysers = 0;
5460 channel_auto_close(&s->res);
5461 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005462 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005463 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005464 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005465 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005466 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005467 s->res.analysers = 0;
5468 channel_auto_close(&s->res);
5469 channel_auto_read(&s->res);
5470 s->req.analysers = 0;
5471 channel_abort(&s->req);
5472 channel_auto_close(&s->req);
5473 channel_auto_read(&s->req);
5474 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005475 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005476 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5477 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005478 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005479 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5480 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5481 /* server-close/keep-alive: terminate this transaction,
5482 * possibly killing the server connection and reinitialize
5483 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005484 */
5485 http_end_txn_clean_session(s);
5486 }
5487
Willy Tarreau610ecce2010-01-04 21:15:02 +01005488 return txn->req.msg_state != old_req_state ||
5489 txn->rsp.msg_state != old_res_state;
5490}
5491
Willy Tarreaud98cf932009-12-27 22:54:55 +01005492/* This function is an analyser which forwards request body (including chunk
5493 * sizes if any). It is called as soon as we must forward, even if we forward
5494 * zero byte. The only situation where it must not be called is when we're in
5495 * tunnel mode and we want to forward till the close. It's used both to forward
5496 * remaining data and to resync after end of body. It expects the msg_state to
5497 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005498 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005499 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005500 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005501 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005502int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005503{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005504 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005505 struct http_txn *txn = s->txn;
5506 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005507
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005508 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5509 return 0;
5510
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005511 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005512 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005513 /* Output closed while we were sending data. We must abort and
5514 * wake the other side up.
5515 */
5516 msg->msg_state = HTTP_MSG_ERROR;
5517 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005518 return 1;
5519 }
5520
Willy Tarreaud98cf932009-12-27 22:54:55 +01005521 /* Note that we don't have to send 100-continue back because we don't
5522 * need the data to complete our job, and it's up to the server to
5523 * decide whether to return 100, 417 or anything else in return of
5524 * an "Expect: 100-continue" header.
5525 */
5526
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005527 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005528 /* we have msg->sov which points to the first byte of message
5529 * body, and req->buf.p still points to the beginning of the
5530 * message. We forward the headers now, as we don't need them
5531 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005532 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005533 b_adv(req->buf, msg->sov);
5534 msg->next -= msg->sov;
5535 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005536
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005537 /* The previous analysers guarantee that the state is somewhere
5538 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5539 * msg->next are always correct.
5540 */
5541 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5542 if (msg->flags & HTTP_MSGF_TE_CHNK)
5543 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5544 else
5545 msg->msg_state = HTTP_MSG_DATA;
5546 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005547 }
5548
Willy Tarreau7ba23542014-04-17 21:50:00 +02005549 /* Some post-connect processing might want us to refrain from starting to
5550 * forward data. Currently, the only reason for this is "balance url_param"
5551 * whichs need to parse/process the request after we've enabled forwarding.
5552 */
5553 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005554 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005555 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005556 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005557 goto missing_data;
5558 }
5559 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5560 }
5561
Willy Tarreau80a92c02014-03-12 10:41:13 +01005562 /* in most states, we should abort in case of early close */
5563 channel_auto_close(req);
5564
Willy Tarreauefdf0942014-04-24 20:08:57 +02005565 if (req->to_forward) {
5566 /* We can't process the buffer's contents yet */
5567 req->flags |= CF_WAKE_WRITE;
5568 goto missing_data;
5569 }
5570
Willy Tarreaud98cf932009-12-27 22:54:55 +01005571 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005572 if (msg->msg_state == HTTP_MSG_DATA) {
5573 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005574 /* we may have some pending data starting at req->buf->p */
5575 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005576 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005577 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005578 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005579 msg->next += msg->chunk_len;
5580 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005581
5582 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005583 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005584 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005585 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005586 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005587 }
5588 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005589 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005590 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005591 * TRAILERS state.
5592 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005593 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005594
Willy Tarreau54d23df2012-10-25 19:04:45 +02005595 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005596 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005597 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005598 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005599 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005600 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005601 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005602 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005603 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005604 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005605 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005606 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005607 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005608
5609 if (ret == 0)
5610 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005611 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005612 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005613 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005614 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005615 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005616 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005617 /* we're in MSG_CHUNK_SIZE now */
5618 }
5619 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005620 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005621
5622 if (ret == 0)
5623 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005624 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005625 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005626 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005627 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005628 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005629 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005630 /* we're in HTTP_MSG_DONE now */
5631 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005632 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005633 int old_state = msg->msg_state;
5634
Willy Tarreau610ecce2010-01-04 21:15:02 +01005635 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005636
5637 /* we may have some pending data starting at req->buf->p
5638 * such as last chunk of data or trailers.
5639 */
5640 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005641 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005642 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005643 msg->next = 0;
5644
Willy Tarreau4fe41902010-06-07 22:27:41 +02005645 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005646 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5647 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005648 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005649 if (http_resync_states(s)) {
5650 /* some state changes occurred, maybe the analyser
5651 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005652 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005653 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005654 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005655 /* request errors are most likely due to
5656 * the server aborting the transfer.
5657 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005658 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005659 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005660 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005661 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005662 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005663 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005664 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005665 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005666
5667 /* If "option abortonclose" is set on the backend, we
5668 * want to monitor the client's connection and forward
5669 * any shutdown notification to the server, which will
5670 * decide whether to close or to go on processing the
5671 * request.
5672 */
5673 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005674 channel_auto_read(req);
5675 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005676 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005677 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005678 /* POST requests may require to read extra CRLF
5679 * sent by broken browsers and which could cause
5680 * an RST to be sent upon close on some systems
5681 * (eg: Linux).
5682 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005683 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005684 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005685
Willy Tarreau610ecce2010-01-04 21:15:02 +01005686 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005687 }
5688 }
5689
Willy Tarreaud98cf932009-12-27 22:54:55 +01005690 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005691 /* we may have some pending data starting at req->buf->p */
5692 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005693 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005694 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5695
Willy Tarreaubed410e2014-04-22 08:19:34 +02005696 msg->next = 0;
5697 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5698
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005699 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005700 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005701 if (!(s->flags & SF_ERR_MASK))
5702 s->flags |= SF_ERR_CLICL;
5703 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005704 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005705 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005706 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005707 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005708 }
5709
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005710 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005711 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005712 if (objt_server(s->target))
5713 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005714
5715 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005716 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005717
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005718 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005719 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005720 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005721
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005722 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005723 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005724 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005725 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005726 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005727
Willy Tarreau5c620922011-05-11 19:56:11 +02005728 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005729 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005730 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005731 * modes are already handled by the stream sock layer. We must not do
5732 * this in content-length mode because it could present the MSG_MORE
5733 * flag with the last block of forwarded data, which would cause an
5734 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005735 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005736 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005737 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005738
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005739 return 0;
5740
Willy Tarreaud98cf932009-12-27 22:54:55 +01005741 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005742 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005743 if (sess->listener->counters)
5744 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005745
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005746 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005747 /* we may have some pending data starting at req->buf->p */
5748 b_adv(req->buf, msg->next);
5749 msg->next = 0;
5750
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005751 txn->req.msg_state = HTTP_MSG_ERROR;
5752 if (txn->status) {
5753 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005754 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005755 } else {
5756 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005757 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005758 }
5759 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005760 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005761
Willy Tarreaue7dff022015-04-03 01:14:29 +02005762 if (!(s->flags & SF_ERR_MASK))
5763 s->flags |= SF_ERR_PRXCOND;
5764 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005765 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005766 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005767 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005768 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005769 }
5770 return 0;
5771
5772 aborted_xfer:
5773 txn->req.msg_state = HTTP_MSG_ERROR;
5774 if (txn->status) {
5775 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005776 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005777 } else {
5778 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005779 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005780 }
5781 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005782 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005783
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005784 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005785 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005786 if (objt_server(s->target))
5787 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005788
Willy Tarreaue7dff022015-04-03 01:14:29 +02005789 if (!(s->flags & SF_ERR_MASK))
5790 s->flags |= SF_ERR_SRVCL;
5791 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005792 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005793 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005794 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005795 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005796 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005797 return 0;
5798}
5799
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005800/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5801 * processing can continue on next analysers, or zero if it either needs more
5802 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005803 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005804 * when it has nothing left to do, and may remove any analyser when it wants to
5805 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005806 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005807int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005808{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005809 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005810 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005811 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005812 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005813 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005814 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005815 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005816
Willy Tarreau87b09662015-04-03 00:22:06 +02005817 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 +02005818 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005819 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005820 rep,
5821 rep->rex, rep->wex,
5822 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005823 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005824 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005825
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005826 /*
5827 * Now parse the partial (or complete) lines.
5828 * We will check the response syntax, and also join multi-line
5829 * headers. An index of all the lines will be elaborated while
5830 * parsing.
5831 *
5832 * For the parsing, we use a 28 states FSM.
5833 *
5834 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005835 * rep->buf->p = beginning of response
5836 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5837 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005838 * msg->eol = end of current header or line (LF or CRLF)
5839 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005840 */
5841
Willy Tarreau628c40c2014-04-24 19:11:26 +02005842 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005843 /* There's a protected area at the end of the buffer for rewriting
5844 * purposes. We don't want to start to parse the request if the
5845 * protected area is affected, because we may have to move processed
5846 * data later, which is much more complicated.
5847 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005848 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005849 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005850 /* some data has still not left the buffer, wake us once that's done */
5851 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5852 goto abort_response;
5853 channel_dont_close(rep);
5854 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005855 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005856 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005857 }
5858
Willy Tarreau379357a2013-06-08 12:55:46 +02005859 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5860 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5861 buffer_slow_realign(rep->buf);
5862
Willy Tarreau9b28e032012-10-12 23:49:43 +02005863 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005864 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005865 }
5866
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005867 /* 1: we might have to print this header in debug mode */
5868 if (unlikely((global.mode & MODE_DEBUG) &&
5869 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005870 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005871 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005872
Willy Tarreau9b28e032012-10-12 23:49:43 +02005873 sol = rep->buf->p;
5874 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005875 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005876
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005877 sol += hdr_idx_first_pos(&txn->hdr_idx);
5878 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005879
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005880 while (cur_idx) {
5881 eol = sol + txn->hdr_idx.v[cur_idx].len;
5882 debug_hdr("srvhdr", s, sol, eol);
5883 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5884 cur_idx = txn->hdr_idx.v[cur_idx].next;
5885 }
5886 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005887
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005888 /*
5889 * Now we quickly check if we have found a full valid response.
5890 * If not so, we check the FD and buffer states before leaving.
5891 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005892 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005893 * responses are checked first.
5894 *
5895 * Depending on whether the client is still there or not, we
5896 * may send an error response back or not. Note that normally
5897 * we should only check for HTTP status there, and check I/O
5898 * errors somewhere else.
5899 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005900
Willy Tarreau655dce92009-11-08 13:10:58 +01005901 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005902 /* Invalid response */
5903 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5904 /* we detected a parsing error. We want to archive this response
5905 * in the dedicated proxy area for later troubleshooting.
5906 */
5907 hdr_response_bad:
5908 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005909 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005910
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005911 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005912 if (objt_server(s->target)) {
5913 objt_server(s->target)->counters.failed_resp++;
5914 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005915 }
Willy Tarreau64648412010-03-05 10:41:54 +01005916 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005917 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005918 rep->analysers = 0;
5919 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005920 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005921 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005922 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005923
Willy Tarreaue7dff022015-04-03 01:14:29 +02005924 if (!(s->flags & SF_ERR_MASK))
5925 s->flags |= SF_ERR_PRXCOND;
5926 if (!(s->flags & SF_FINST_MASK))
5927 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005928
5929 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005930 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005931
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005932 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005933 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005934 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005935 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005936 goto hdr_response_bad;
5937 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005938
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005939 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005940 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005941 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005942 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005943 else if (txn->flags & TX_NOT_FIRST)
5944 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005945
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005946 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005947 if (objt_server(s->target)) {
5948 objt_server(s->target)->counters.failed_resp++;
5949 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005950 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005951
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005952 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005953 rep->analysers = 0;
5954 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005955 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005956 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005957 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005958
Willy Tarreaue7dff022015-04-03 01:14:29 +02005959 if (!(s->flags & SF_ERR_MASK))
5960 s->flags |= SF_ERR_SRVCL;
5961 if (!(s->flags & SF_FINST_MASK))
5962 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005963 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005964 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005965
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005966 /* read timeout : return a 504 to the client. */
5967 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005968 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005969 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005970 else if (txn->flags & TX_NOT_FIRST)
5971 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005972
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005973 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005974 if (objt_server(s->target)) {
5975 objt_server(s->target)->counters.failed_resp++;
5976 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005977 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005978
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005979 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005980 rep->analysers = 0;
5981 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005982 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005983 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005984 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005985
Willy Tarreaue7dff022015-04-03 01:14:29 +02005986 if (!(s->flags & SF_ERR_MASK))
5987 s->flags |= SF_ERR_SRVTO;
5988 if (!(s->flags & SF_FINST_MASK))
5989 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005990 return 0;
5991 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005992
Willy Tarreauf003d372012-11-26 13:35:37 +01005993 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005994 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005995 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005996 s->be->be_counters.cli_aborts++;
5997 if (objt_server(s->target))
5998 objt_server(s->target)->counters.cli_aborts++;
5999
6000 rep->analysers = 0;
6001 channel_auto_close(rep);
6002
6003 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006004 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006005 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006006
Willy Tarreaue7dff022015-04-03 01:14:29 +02006007 if (!(s->flags & SF_ERR_MASK))
6008 s->flags |= SF_ERR_CLICL;
6009 if (!(s->flags & SF_FINST_MASK))
6010 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006011
Willy Tarreau87b09662015-04-03 00:22:06 +02006012 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006013 return 0;
6014 }
6015
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006016 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006017 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006018 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006019 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006020 else if (txn->flags & TX_NOT_FIRST)
6021 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006022
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006023 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006024 if (objt_server(s->target)) {
6025 objt_server(s->target)->counters.failed_resp++;
6026 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006027 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006028
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006029 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006030 rep->analysers = 0;
6031 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006032 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006033 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006034 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006035
Willy Tarreaue7dff022015-04-03 01:14:29 +02006036 if (!(s->flags & SF_ERR_MASK))
6037 s->flags |= SF_ERR_SRVCL;
6038 if (!(s->flags & SF_FINST_MASK))
6039 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006040 return 0;
6041 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006042
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006043 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006044 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006045 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006046 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006047 else if (txn->flags & TX_NOT_FIRST)
6048 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006049
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006050 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006051 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006052 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006053
Willy Tarreaue7dff022015-04-03 01:14:29 +02006054 if (!(s->flags & SF_ERR_MASK))
6055 s->flags |= SF_ERR_CLICL;
6056 if (!(s->flags & SF_FINST_MASK))
6057 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006058
Willy Tarreau87b09662015-04-03 00:22:06 +02006059 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006060 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006061 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006062
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006063 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006064 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006065 return 0;
6066 }
6067
6068 /* More interesting part now : we know that we have a complete
6069 * response which at least looks like HTTP. We have an indicator
6070 * of each header's length, so we can parse them quickly.
6071 */
6072
6073 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006074 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006075
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006076 /*
6077 * 1: get the status code
6078 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006079 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006080 if (n < 1 || n > 5)
6081 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006082 /* when the client triggers a 4xx from the server, it's most often due
6083 * to a missing object or permission. These events should be tracked
6084 * because if they happen often, it may indicate a brute force or a
6085 * vulnerability scan.
6086 */
6087 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006088 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006089
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006090 if (objt_server(s->target))
6091 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006092
Willy Tarreau91852eb2015-05-01 13:26:00 +02006093 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6094 * exactly one digit "." one digit. This check may be disabled using
6095 * option accept-invalid-http-response.
6096 */
6097 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6098 if (msg->sl.st.v_l != 8) {
6099 msg->err_pos = 0;
6100 goto hdr_response_bad;
6101 }
6102
6103 if (rep->buf->p[4] != '/' ||
6104 !isdigit((unsigned char)rep->buf->p[5]) ||
6105 rep->buf->p[6] != '.' ||
6106 !isdigit((unsigned char)rep->buf->p[7])) {
6107 msg->err_pos = 4;
6108 goto hdr_response_bad;
6109 }
6110 }
6111
Willy Tarreau5b154472009-12-21 20:11:07 +01006112 /* check if the response is HTTP/1.1 or above */
6113 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006114 ((rep->buf->p[5] > '1') ||
6115 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006116 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006117
6118 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006119 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 +01006120
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006121 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006122 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006123
Willy Tarreau9b28e032012-10-12 23:49:43 +02006124 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006125
Willy Tarreau39650402010-03-15 19:44:39 +01006126 /* Adjust server's health based on status code. Note: status codes 501
6127 * and 505 are triggered on demand by client request, so we must not
6128 * count them as server failures.
6129 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006130 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006131 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006132 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006133 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006134 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006135 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006136
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006137 /*
6138 * 2: check for cacheability.
6139 */
6140
6141 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006142 case 100:
6143 /*
6144 * We may be facing a 100-continue response, in which case this
6145 * is not the right response, and we're waiting for the next one.
6146 * Let's allow this response to go to the client and wait for the
6147 * next one.
6148 */
6149 hdr_idx_init(&txn->hdr_idx);
6150 msg->next -= channel_forward(rep, msg->next);
6151 msg->msg_state = HTTP_MSG_RPBEFORE;
6152 txn->status = 0;
6153 s->logs.t_data = -1; /* was not a response yet */
6154 goto next_one;
6155
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006156 case 200:
6157 case 203:
6158 case 206:
6159 case 300:
6160 case 301:
6161 case 410:
6162 /* RFC2616 @13.4:
6163 * "A response received with a status code of
6164 * 200, 203, 206, 300, 301 or 410 MAY be stored
6165 * by a cache (...) unless a cache-control
6166 * directive prohibits caching."
6167 *
6168 * RFC2616 @9.5: POST method :
6169 * "Responses to this method are not cacheable,
6170 * unless the response includes appropriate
6171 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006172 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006173 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006174 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006175 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6176 break;
6177 default:
6178 break;
6179 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006180
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006181 /*
6182 * 3: we may need to capture headers
6183 */
6184 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006185 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006186 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006187 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006188
Willy Tarreau557f1992015-05-01 10:05:17 +02006189 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6190 * by RFC7230#3.3.3 :
6191 *
6192 * The length of a message body is determined by one of the following
6193 * (in order of precedence):
6194 *
6195 * 1. Any response to a HEAD request and any response with a 1xx
6196 * (Informational), 204 (No Content), or 304 (Not Modified) status
6197 * code is always terminated by the first empty line after the
6198 * header fields, regardless of the header fields present in the
6199 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006200 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006201 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6202 * the connection will become a tunnel immediately after the empty
6203 * line that concludes the header fields. A client MUST ignore any
6204 * Content-Length or Transfer-Encoding header fields received in
6205 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006206 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006207 * 3. If a Transfer-Encoding header field is present and the chunked
6208 * transfer coding (Section 4.1) is the final encoding, the message
6209 * body length is determined by reading and decoding the chunked
6210 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006211 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006212 * If a Transfer-Encoding header field is present in a response and
6213 * the chunked transfer coding is not the final encoding, the
6214 * message body length is determined by reading the connection until
6215 * it is closed by the server. If a Transfer-Encoding header field
6216 * is present in a request and the chunked transfer coding is not
6217 * the final encoding, the message body length cannot be determined
6218 * reliably; the server MUST respond with the 400 (Bad Request)
6219 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006220 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006221 * If a message is received with both a Transfer-Encoding and a
6222 * Content-Length header field, the Transfer-Encoding overrides the
6223 * Content-Length. Such a message might indicate an attempt to
6224 * perform request smuggling (Section 9.5) or response splitting
6225 * (Section 9.4) and ought to be handled as an error. A sender MUST
6226 * remove the received Content-Length field prior to forwarding such
6227 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006228 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006229 * 4. If a message is received without Transfer-Encoding and with
6230 * either multiple Content-Length header fields having differing
6231 * field-values or a single Content-Length header field having an
6232 * invalid value, then the message framing is invalid and the
6233 * recipient MUST treat it as an unrecoverable error. If this is a
6234 * request message, the server MUST respond with a 400 (Bad Request)
6235 * status code and then close the connection. If this is a response
6236 * message received by a proxy, the proxy MUST close the connection
6237 * to the server, discard the received response, and send a 502 (Bad
6238 * Gateway) response to the client. If this is a response message
6239 * received by a user agent, the user agent MUST close the
6240 * connection to the server and discard the received response.
6241 *
6242 * 5. If a valid Content-Length header field is present without
6243 * Transfer-Encoding, its decimal value defines the expected message
6244 * body length in octets. If the sender closes the connection or
6245 * the recipient times out before the indicated number of octets are
6246 * received, the recipient MUST consider the message to be
6247 * incomplete and close the connection.
6248 *
6249 * 6. If this is a request message and none of the above are true, then
6250 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006251 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006252 * 7. Otherwise, this is a response message without a declared message
6253 * body length, so the message body length is determined by the
6254 * number of octets received prior to the server closing the
6255 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006256 */
6257
6258 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006259 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006260 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006261 * FIXME: should we parse anyway and return an error on chunked encoding ?
6262 */
6263 if (txn->meth == HTTP_METH_HEAD ||
6264 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006265 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006266 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006267 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006268 goto skip_content_length;
6269 }
6270
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006271 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006272 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006273 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006274 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006275 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6276 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006277 /* bad transfer-encoding (chunked followed by something else) */
6278 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006279 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006280 break;
6281 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006282 }
6283
Willy Tarreau1c913912015-04-30 10:57:51 +02006284 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006285 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006286 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006287 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6288 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6289 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006290 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006291 signed long long cl;
6292
Willy Tarreauad14f752011-09-02 20:33:27 +02006293 if (!ctx.vlen) {
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 Tarreauad14f752011-09-02 20:33:27 +02006298 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &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; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006301 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006302
Willy Tarreauad14f752011-09-02 20:33:27 +02006303 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006304 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006305 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006306 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006307
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006308 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006309 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006310 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006311 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006312
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006313 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006314 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006315 }
6316
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006317 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006318 select_compression_response_header(s, rep->buf);
6319
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006320skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006321 /* Now we have to check if we need to modify the Connection header.
6322 * This is more difficult on the response than it is on the request,
6323 * because we can have two different HTTP versions and we don't know
6324 * how the client will interprete a response. For instance, let's say
6325 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6326 * HTTP/1.1 response without any header. Maybe it will bound itself to
6327 * HTTP/1.0 because it only knows about it, and will consider the lack
6328 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6329 * the lack of header as a keep-alive. Thus we will use two flags
6330 * indicating how a request MAY be understood by the client. In case
6331 * of multiple possibilities, we'll fix the header to be explicit. If
6332 * ambiguous cases such as both close and keepalive are seen, then we
6333 * will fall back to explicit close. Note that we won't take risks with
6334 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006335 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006336 */
6337
Willy Tarreaudc008c52010-02-01 16:20:08 +01006338 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6339 txn->status == 101)) {
6340 /* Either we've established an explicit tunnel, or we're
6341 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006342 * to understand the next protocols. We have to switch to tunnel
6343 * mode, so that we transfer the request and responses then let
6344 * this protocol pass unmodified. When we later implement specific
6345 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006346 * header which contains information about that protocol for
6347 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006348 */
6349 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6350 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006351 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6352 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006353 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006354 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006355 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006356
Willy Tarreau70dffda2014-01-30 03:07:23 +01006357 /* this situation happens when combining pretend-keepalive with httpclose. */
6358 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006359 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006360 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006361 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6362
Willy Tarreau60466522010-01-18 19:08:45 +01006363 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006364 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006365 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6366 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006367
Willy Tarreau60466522010-01-18 19:08:45 +01006368 /* now adjust header transformations depending on current state */
6369 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6370 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6371 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006372 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006373 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006374 }
Willy Tarreau60466522010-01-18 19:08:45 +01006375 else { /* SCL / KAL */
6376 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006377 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006378 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006379 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006380
Willy Tarreau60466522010-01-18 19:08:45 +01006381 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006382 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006383
Willy Tarreau60466522010-01-18 19:08:45 +01006384 /* Some keep-alive responses are converted to Server-close if
6385 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006386 */
Willy Tarreau60466522010-01-18 19:08:45 +01006387 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6388 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006389 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006390 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006391 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006392 }
6393
Willy Tarreau7959a552013-09-23 16:44:27 +02006394 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006395 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006396
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006397 /* end of job, return OK */
6398 rep->analysers &= ~an_bit;
6399 rep->analyse_exp = TICK_ETERNITY;
6400 channel_auto_close(rep);
6401 return 1;
6402
6403 abort_keep_alive:
6404 /* A keep-alive request to the server failed on a network error.
6405 * The client is required to retry. We need to close without returning
6406 * any other information so that the client retries.
6407 */
6408 txn->status = 0;
6409 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006410 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006411 channel_auto_close(rep);
6412 s->logs.logwait = 0;
6413 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006414 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006415 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006416 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006417 return 0;
6418}
6419
6420/* This function performs all the processing enabled for the current response.
6421 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006422 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006423 * other functions. It works like process_request (see indications above).
6424 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006425int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006426{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006427 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006428 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006429 struct http_msg *msg = &txn->rsp;
6430 struct proxy *cur_proxy;
6431 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006432 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006433
Willy Tarreau87b09662015-04-03 00:22:06 +02006434 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 +02006435 now_ms, __FUNCTION__,
6436 s,
6437 rep,
6438 rep->rex, rep->wex,
6439 rep->flags,
6440 rep->buf->i,
6441 rep->analysers);
6442
6443 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6444 return 0;
6445
Willy Tarreau70730dd2014-04-24 18:06:27 +02006446 /* The stats applet needs to adjust the Connection header but we don't
6447 * apply any filter there.
6448 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006449 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6450 rep->analysers &= ~an_bit;
6451 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006452 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006453 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006454
Willy Tarreau58975672014-04-24 21:13:57 +02006455 /*
6456 * We will have to evaluate the filters.
6457 * As opposed to version 1.2, now they will be evaluated in the
6458 * filters order and not in the header order. This means that
6459 * each filter has to be validated among all headers.
6460 *
6461 * Filters are tried with ->be first, then with ->fe if it is
6462 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006463 *
6464 * Maybe we are in resume condiion. In this case I choose the
6465 * "struct proxy" which contains the rule list matching the resume
6466 * pointer. If none of theses "struct proxy" match, I initialise
6467 * the process with the first one.
6468 *
6469 * In fact, I check only correspondance betwwen the current list
6470 * pointer and the ->fe rule list. If it doesn't match, I initialize
6471 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006472 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006473 if (s->current_rule_list == &sess->fe->http_res_rules)
6474 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006475 else
6476 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006477 while (1) {
6478 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006479
Willy Tarreau58975672014-04-24 21:13:57 +02006480 /* evaluate http-response rules */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006481 if (ret == HTTP_RULE_RES_CONT)
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006482 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006483
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006484 /* we need to be called again. */
6485 if (ret == HTTP_RULE_RES_YIELD) {
6486 channel_dont_close(rep);
6487 return 0;
6488 }
6489
Willy Tarreau58975672014-04-24 21:13:57 +02006490 /* try headers filters */
6491 if (rule_set->rsp_exp != NULL) {
6492 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6493 return_bad_resp:
6494 if (objt_server(s->target)) {
6495 objt_server(s->target)->counters.failed_resp++;
6496 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006497 }
Willy Tarreau58975672014-04-24 21:13:57 +02006498 s->be->be_counters.failed_resp++;
6499 return_srv_prx_502:
6500 rep->analysers = 0;
6501 txn->status = 502;
6502 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006503 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006504 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006505 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006506 if (!(s->flags & SF_ERR_MASK))
6507 s->flags |= SF_ERR_PRXCOND;
6508 if (!(s->flags & SF_FINST_MASK))
6509 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006510 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006511 }
Willy Tarreau58975672014-04-24 21:13:57 +02006512 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006513
Willy Tarreau58975672014-04-24 21:13:57 +02006514 /* has the response been denied ? */
6515 if (txn->flags & TX_SVDENY) {
6516 if (objt_server(s->target))
6517 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006518
Willy Tarreau58975672014-04-24 21:13:57 +02006519 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006520 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006521 if (sess->listener->counters)
6522 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006523
Willy Tarreau58975672014-04-24 21:13:57 +02006524 goto return_srv_prx_502;
6525 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006526
Willy Tarreau58975672014-04-24 21:13:57 +02006527 /* add response headers from the rule sets in the same order */
6528 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006529 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006530 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006531 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006532 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006533 ret = acl_pass(ret);
6534 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6535 ret = !ret;
6536 if (!ret)
6537 continue;
6538 }
6539 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6540 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006541 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006542
Willy Tarreau58975672014-04-24 21:13:57 +02006543 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006544 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006545 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006546 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006547 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006548
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006549 /* After this point, this anayzer can't return yield, so we can
6550 * remove the bit corresponding to this analyzer from the list.
6551 *
6552 * Note that the intermediate returns and goto found previously
6553 * reset the analyzers.
6554 */
6555 rep->analysers &= ~an_bit;
6556 rep->analyse_exp = TICK_ETERNITY;
6557
Willy Tarreau58975672014-04-24 21:13:57 +02006558 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006559 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006560 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006561
Willy Tarreau58975672014-04-24 21:13:57 +02006562 /*
6563 * Now check for a server cookie.
6564 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006565 if (s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name ||
Willy Tarreau58975672014-04-24 21:13:57 +02006566 (s->be->options & PR_O_CHK_CACHE))
6567 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006568
Willy Tarreau58975672014-04-24 21:13:57 +02006569 /*
6570 * Check for cache-control or pragma headers if required.
6571 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006572 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 +02006573 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006574
Willy Tarreau58975672014-04-24 21:13:57 +02006575 /*
6576 * Add server cookie in the response if needed
6577 */
6578 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6579 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006580 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006581 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6582 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6583 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6584 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6585 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006586 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006587 /* the server is known, it's not the one the client requested, or the
6588 * cookie's last seen date needs to be refreshed. We have to
6589 * insert a set-cookie here, except if we want to insert only on POST
6590 * requests and this one isn't. Note that servers which don't have cookies
6591 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006592 */
Willy Tarreau58975672014-04-24 21:13:57 +02006593 if (!objt_server(s->target)->cookie) {
6594 chunk_printf(&trash,
6595 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6596 s->be->cookie_name);
6597 }
6598 else {
6599 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006600
Willy Tarreau58975672014-04-24 21:13:57 +02006601 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6602 /* emit last_date, which is mandatory */
6603 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6604 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6605 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006606
Willy Tarreau58975672014-04-24 21:13:57 +02006607 if (s->be->cookie_maxlife) {
6608 /* emit first_date, which is either the original one or
6609 * the current date.
6610 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006611 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006612 s30tob64(txn->cookie_first_date ?
6613 txn->cookie_first_date >> 2 :
6614 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006615 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006616 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006617 }
Willy Tarreau58975672014-04-24 21:13:57 +02006618 chunk_appendf(&trash, "; path=/");
6619 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006620
Willy Tarreau58975672014-04-24 21:13:57 +02006621 if (s->be->cookie_domain)
6622 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006623
Willy Tarreau58975672014-04-24 21:13:57 +02006624 if (s->be->ck_opts & PR_CK_HTTPONLY)
6625 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006626
Willy Tarreau58975672014-04-24 21:13:57 +02006627 if (s->be->ck_opts & PR_CK_SECURE)
6628 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006629
Willy Tarreau58975672014-04-24 21:13:57 +02006630 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6631 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006632
Willy Tarreau58975672014-04-24 21:13:57 +02006633 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006634 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006635 /* the server did not change, only the date was updated */
6636 txn->flags |= TX_SCK_UPDATED;
6637 else
6638 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006639
Willy Tarreau58975672014-04-24 21:13:57 +02006640 /* Here, we will tell an eventual cache on the client side that we don't
6641 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6642 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6643 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006644 */
Willy Tarreau58975672014-04-24 21:13:57 +02006645 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006646
Willy Tarreau58975672014-04-24 21:13:57 +02006647 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006648
Willy Tarreau58975672014-04-24 21:13:57 +02006649 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6650 "Cache-control: private", 22) < 0))
6651 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006652 }
Willy Tarreau58975672014-04-24 21:13:57 +02006653 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006654
Willy Tarreau58975672014-04-24 21:13:57 +02006655 /*
6656 * Check if result will be cacheable with a cookie.
6657 * We'll block the response if security checks have caught
6658 * nasty things such as a cacheable cookie.
6659 */
6660 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6661 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6662 (s->be->options & PR_O_CHK_CACHE)) {
6663 /* we're in presence of a cacheable response containing
6664 * a set-cookie header. We'll block it as requested by
6665 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006666 */
Willy Tarreau58975672014-04-24 21:13:57 +02006667 if (objt_server(s->target))
6668 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006669
Willy Tarreau58975672014-04-24 21:13:57 +02006670 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006671 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006672 if (sess->listener->counters)
6673 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006674
Willy Tarreau58975672014-04-24 21:13:57 +02006675 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6676 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6677 send_log(s->be, LOG_ALERT,
6678 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6679 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6680 goto return_srv_prx_502;
6681 }
Willy Tarreau03945942009-12-22 16:50:27 +01006682
Willy Tarreau70730dd2014-04-24 18:06:27 +02006683 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006684 /*
6685 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6686 * If an "Upgrade" token is found, the header is left untouched in order
6687 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006688 * if anything but "Upgrade" is present in the Connection header. We don't
6689 * want to touch any 101 response either since it's switching to another
6690 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006691 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006692 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006693 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006694 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006695 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6696 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006697
Willy Tarreau58975672014-04-24 21:13:57 +02006698 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6699 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6700 /* we want a keep-alive response here. Keep-alive header
6701 * required if either side is not 1.1.
6702 */
6703 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6704 want_flags |= TX_CON_KAL_SET;
6705 }
6706 else {
6707 /* we want a close response here. Close header required if
6708 * the server is 1.1, regardless of the client.
6709 */
6710 if (msg->flags & HTTP_MSGF_VER_11)
6711 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006712 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006713
Willy Tarreau58975672014-04-24 21:13:57 +02006714 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6715 http_change_connection_header(txn, msg, want_flags);
6716 }
6717
6718 skip_header_mangling:
6719 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6720 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6721 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006722
Willy Tarreau58975672014-04-24 21:13:57 +02006723 /* if the user wants to log as soon as possible, without counting
6724 * bytes from the server, then this is the right moment. We have
6725 * to temporarily assign bytes_out to log what we currently have.
6726 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006727 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006728 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6729 s->logs.bytes_out = txn->rsp.eoh;
6730 s->do_log(s);
6731 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006732 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006733 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006734}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006735
Willy Tarreaud98cf932009-12-27 22:54:55 +01006736/* This function is an analyser which forwards response body (including chunk
6737 * sizes if any). It is called as soon as we must forward, even if we forward
6738 * zero byte. The only situation where it must not be called is when we're in
6739 * tunnel mode and we want to forward till the close. It's used both to forward
6740 * remaining data and to resync after end of body. It expects the msg_state to
6741 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006742 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006743 *
6744 * It is capable of compressing response data both in content-length mode and
6745 * in chunked mode. The state machines follows different flows depending on
6746 * whether content-length and chunked modes are used, since there are no
6747 * trailers in content-length :
6748 *
6749 * chk-mode cl-mode
6750 * ,----- BODY -----.
6751 * / \
6752 * V size > 0 V chk-mode
6753 * .--> SIZE -------------> DATA -------------> CRLF
6754 * | | size == 0 | last byte |
6755 * | v final crlf v inspected |
6756 * | TRAILERS -----------> DONE |
6757 * | |
6758 * `----------------------------------------------'
6759 *
6760 * Compression only happens in the DATA state, and must be flushed in final
6761 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6762 * is performed at once on final states for all bytes parsed, or when leaving
6763 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006764 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006765int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006766{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006767 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006768 struct http_txn *txn = s->txn;
6769 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006770 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006771 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006772 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006773
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006774 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6775 return 0;
6776
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006777 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006778 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006779 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006780 /* Output closed while we were sending data. We must abort and
6781 * wake the other side up.
6782 */
6783 msg->msg_state = HTTP_MSG_ERROR;
6784 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006785 return 1;
6786 }
6787
Willy Tarreau4fe41902010-06-07 22:27:41 +02006788 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006789 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006790
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006791 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006792 /* we have msg->sov which points to the first byte of message
6793 * body, and res->buf.p still points to the beginning of the
6794 * message. We forward the headers now, as we don't need them
6795 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006796 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006797 b_adv(res->buf, msg->sov);
6798 msg->next -= msg->sov;
6799 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006800
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006801 /* The previous analysers guarantee that the state is somewhere
6802 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6803 * msg->next are always correct.
6804 */
6805 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6806 if (msg->flags & HTTP_MSGF_TE_CHNK)
6807 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6808 else
6809 msg->msg_state = HTTP_MSG_DATA;
6810 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006811 }
6812
Willy Tarreauefdf0942014-04-24 20:08:57 +02006813 if (res->to_forward) {
6814 /* We can't process the buffer's contents yet */
6815 res->flags |= CF_WAKE_WRITE;
6816 goto missing_data;
6817 }
6818
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006819 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6820 /* We need a compression buffer in the DATA state to put the
6821 * output of compressed data, and in CRLF state to let the
6822 * TRAILERS state finish the job of removing the trailing CRLF.
6823 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006824 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006825 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006826 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006827 goto aborted_xfer; /* no memory */
6828 }
6829
6830 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006831 if (ret < 0) {
6832 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006833 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006834 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006835 compressing = 1;
6836 }
6837
Willy Tarreaud98cf932009-12-27 22:54:55 +01006838 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006839 switch (msg->msg_state - HTTP_MSG_DATA) {
6840 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006841 /* we may have some pending data starting at res->buf->p */
6842 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006843 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006844 if (ret < 0)
6845 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006846
Willy Tarreaud5a67832014-04-21 10:54:27 +02006847 if (msg->chunk_len) {
6848 /* input empty or output full */
6849 if (res->buf->i > msg->next)
6850 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006851 goto missing_data;
6852 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006853 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006854 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006855 if (msg->chunk_len > res->buf->i - msg->next) {
6856 /* output full */
6857 res->flags |= CF_WAKE_WRITE;
6858 goto missing_data;
6859 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006860 msg->next += msg->chunk_len;
6861 msg->chunk_len = 0;
6862 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006863
6864 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006865 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006866 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006867 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006868 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006869 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006870 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006871 /* fall through for HTTP_MSG_CHUNK_CRLF */
6872
6873 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6874 /* we want the CRLF after the data */
6875
6876 ret = http_skip_chunk_crlf(msg);
6877 if (ret == 0)
6878 goto missing_data;
6879 else if (ret < 0) {
6880 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006881 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006882 goto return_bad_res;
6883 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006884 /* we're in MSG_CHUNK_SIZE now, fall through */
6885
6886 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006887 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006888 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006889 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006890 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006891
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006892 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006893 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006894 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006895 else if (ret < 0) {
6896 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006897 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006898 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006899 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006900 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006901 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006902
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006903 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006904 if (unlikely(compressing)) {
6905 /* we need to flush output contents before syncing FSMs */
6906 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6907 compressing = 0;
6908 }
6909
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006910 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006911 if (ret == 0)
6912 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006913 else if (ret < 0) {
6914 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006915 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006916 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006917 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006918 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006919
6920 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006921 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006922 if (unlikely(compressing)) {
6923 /* we need to flush output contents before syncing FSMs */
6924 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6925 compressing = 0;
6926 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006927
Willy Tarreauc623c172014-04-18 09:53:50 +02006928 /* we may have some pending data starting at res->buf->p
6929 * such as a last chunk of data or trailers.
6930 */
6931 b_adv(res->buf, msg->next);
6932 msg->next = 0;
6933
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006934 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006935 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006936 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6937 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006938 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006939
Willy Tarreau610ecce2010-01-04 21:15:02 +01006940 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006941 /* some state changes occurred, maybe the analyser
6942 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006943 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006944 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006945 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006946 /* response errors are most likely due to
6947 * the client aborting the transfer.
6948 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006949 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006950 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006951 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006952 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006953 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006954 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006955 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006956 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006957 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006958 }
6959 }
6960
Willy Tarreaud98cf932009-12-27 22:54:55 +01006961 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006962 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006963 if (unlikely(compressing)) {
6964 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006965 compressing = 0;
6966 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006967
Willy Tarreauc623c172014-04-18 09:53:50 +02006968 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6969 b_adv(res->buf, msg->next);
6970 msg->next = 0;
6971 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6972 }
6973
Willy Tarreauf003d372012-11-26 13:35:37 +01006974 if (res->flags & CF_SHUTW)
6975 goto aborted_xfer;
6976
6977 /* stop waiting for data if the input is closed before the end. If the
6978 * client side was already closed, it means that the client has aborted,
6979 * so we don't want to count this as a server abort. Otherwise it's a
6980 * server abort.
6981 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006982 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006983 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006984 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006985 if (!(s->flags & SF_ERR_MASK))
6986 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006987 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006988 if (objt_server(s->target))
6989 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006990 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006991 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006992
Willy Tarreau40dba092010-03-04 18:14:51 +01006993 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006994 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006995 goto return_bad_res;
6996
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006997 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006998 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006999 * Similarly, with keep-alive on the client side, we don't want to forward a
7000 * close.
7001 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007002 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007003 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7004 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007005 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007006
Willy Tarreau5c620922011-05-11 19:56:11 +02007007 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007008 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007009 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007010 * modes are already handled by the stream sock layer. We must not do
7011 * this in content-length mode because it could present the MSG_MORE
7012 * flag with the last block of forwarded data, which would cause an
7013 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007014 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007015 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007016 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007017
Willy Tarreau87b09662015-04-03 00:22:06 +02007018 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007019 return 0;
7020
Willy Tarreau40dba092010-03-04 18:14:51 +01007021 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007022 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007023 if (objt_server(s->target))
7024 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007025
7026 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007027 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007028 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007029 compressing = 0;
7030 }
7031
Willy Tarreauc623c172014-04-18 09:53:50 +02007032 /* we may have some pending data starting at res->buf->p */
7033 if (s->comp_algo == NULL) {
7034 b_adv(res->buf, msg->next);
7035 msg->next = 0;
7036 }
7037
Willy Tarreaud98cf932009-12-27 22:54:55 +01007038 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007039 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007040 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007041 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007042 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007043 if (objt_server(s->target))
7044 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007045
Willy Tarreaue7dff022015-04-03 01:14:29 +02007046 if (!(s->flags & SF_ERR_MASK))
7047 s->flags |= SF_ERR_PRXCOND;
7048 if (!(s->flags & SF_FINST_MASK))
7049 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007050 return 0;
7051
7052 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007053 if (unlikely(compressing)) {
7054 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7055 compressing = 0;
7056 }
7057
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007058 txn->rsp.msg_state = HTTP_MSG_ERROR;
7059 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007060 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007061 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007062 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007063
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007064 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007065 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007066 if (objt_server(s->target))
7067 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007068
Willy Tarreaue7dff022015-04-03 01:14:29 +02007069 if (!(s->flags & SF_ERR_MASK))
7070 s->flags |= SF_ERR_CLICL;
7071 if (!(s->flags & SF_FINST_MASK))
7072 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007073 return 0;
7074}
7075
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007076/* Iterate the same filter through all request headers.
7077 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007078 * Since it can manage the switch to another backend, it updates the per-proxy
7079 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007080 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007081int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007082{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007083 char *cur_ptr, *cur_end, *cur_next;
7084 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007085 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007086 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007087 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007088
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007089 last_hdr = 0;
7090
Willy Tarreau9b28e032012-10-12 23:49:43 +02007091 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007092 old_idx = 0;
7093
7094 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007095 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007096 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007097 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007098 (exp->action == ACT_ALLOW ||
7099 exp->action == ACT_DENY ||
7100 exp->action == ACT_TARPIT))
7101 return 0;
7102
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007103 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007104 if (!cur_idx)
7105 break;
7106
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007107 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007108 cur_ptr = cur_next;
7109 cur_end = cur_ptr + cur_hdr->len;
7110 cur_next = cur_end + cur_hdr->cr + 1;
7111
7112 /* Now we have one header between cur_ptr and cur_end,
7113 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007114 */
7115
Willy Tarreau15a53a42015-01-21 13:39:42 +01007116 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007117 switch (exp->action) {
7118 case ACT_SETBE:
7119 /* It is not possible to jump a second time.
7120 * FIXME: should we return an HTTP/500 here so that
7121 * the admin knows there's a problem ?
7122 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007123 if (s->be != strm_fe(s))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007124 break;
7125
7126 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02007127 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007128 last_hdr = 1;
7129 break;
7130
7131 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007132 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007133 last_hdr = 1;
7134 break;
7135
7136 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007137 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007138 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007139 break;
7140
7141 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007142 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007143 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007144 break;
7145
7146 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007147 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7148 if (trash.len < 0)
7149 return -1;
7150
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007151 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007152 /* FIXME: if the user adds a newline in the replacement, the
7153 * index will not be recalculated for now, and the new line
7154 * will not be counted as a new header.
7155 */
7156
7157 cur_end += delta;
7158 cur_next += delta;
7159 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007160 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007161 break;
7162
7163 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007164 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007165 cur_next += delta;
7166
Willy Tarreaufa355d42009-11-29 18:12:29 +01007167 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007168 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7169 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007170 cur_hdr->len = 0;
7171 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007172 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007173 break;
7174
7175 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007176 }
7177
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007178 /* keep the link from this header to next one in case of later
7179 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007180 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007181 old_idx = cur_idx;
7182 }
7183 return 0;
7184}
7185
7186
7187/* Apply the filter to the request line.
7188 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7189 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007190 * Since it can manage the switch to another backend, it updates the per-proxy
7191 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007192 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007193int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007194{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007195 char *cur_ptr, *cur_end;
7196 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007197 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007198 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007199
Willy Tarreau3d300592007-03-18 18:34:41 +01007200 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007201 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007202 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007203 (exp->action == ACT_ALLOW ||
7204 exp->action == ACT_DENY ||
7205 exp->action == ACT_TARPIT))
7206 return 0;
7207 else if (exp->action == ACT_REMOVE)
7208 return 0;
7209
7210 done = 0;
7211
Willy Tarreau9b28e032012-10-12 23:49:43 +02007212 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007213 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007214
7215 /* Now we have the request line between cur_ptr and cur_end */
7216
Willy Tarreau15a53a42015-01-21 13:39:42 +01007217 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007218 switch (exp->action) {
7219 case ACT_SETBE:
7220 /* It is not possible to jump a second time.
7221 * FIXME: should we return an HTTP/500 here so that
7222 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007223 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007224 if (s->be != strm_fe(s))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007225 break;
7226
7227 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02007228 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007229 done = 1;
7230 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007231
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007232 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007233 txn->flags |= TX_CLALLOW;
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_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007238 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007239 done = 1;
7240 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007241
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007242 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007243 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007244 done = 1;
7245 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007247 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007248 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7249 if (trash.len < 0)
7250 return -1;
7251
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007252 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007253 /* FIXME: if the user adds a newline in the replacement, the
7254 * index will not be recalculated for now, and the new line
7255 * will not be counted as a new header.
7256 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007257
Willy Tarreaufa355d42009-11-29 18:12:29 +01007258 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007259 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007260 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007261 HTTP_MSG_RQMETH,
7262 cur_ptr, cur_end + 1,
7263 NULL, NULL);
7264 if (unlikely(!cur_end))
7265 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007266
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007267 /* we have a full request and we know that we have either a CR
7268 * or an LF at <ptr>.
7269 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007270 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7271 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007272 /* there is no point trying this regex on headers */
7273 return 1;
7274 }
7275 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007276 return done;
7277}
Willy Tarreau97de6242006-12-27 17:18:38 +01007278
Willy Tarreau58f10d72006-12-04 02:26:12 +01007279
Willy Tarreau58f10d72006-12-04 02:26:12 +01007280
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007281/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007282 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007283 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007284 * unparsable request. Since it can manage the switch to another backend, it
7285 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007286 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007287int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007288{
Willy Tarreau192252e2015-04-04 01:47:55 +02007289 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007290 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007291 struct hdr_exp *exp;
7292
7293 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007294 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007295
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007296 /*
7297 * The interleaving of transformations and verdicts
7298 * makes it difficult to decide to continue or stop
7299 * the evaluation.
7300 */
7301
Willy Tarreau6c123b12010-01-28 20:22:06 +01007302 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7303 break;
7304
Willy Tarreau3d300592007-03-18 18:34:41 +01007305 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007306 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007307 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007308 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007309
7310 /* if this filter had a condition, evaluate it now and skip to
7311 * next filter if the condition does not match.
7312 */
7313 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007314 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007315 ret = acl_pass(ret);
7316 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7317 ret = !ret;
7318
7319 if (!ret)
7320 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007321 }
7322
7323 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007324 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007325 if (unlikely(ret < 0))
7326 return -1;
7327
7328 if (likely(ret == 0)) {
7329 /* The filter did not match the request, it can be
7330 * iterated through all headers.
7331 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007332 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7333 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007334 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007335 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007336 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007337}
7338
7339
Willy Tarreaua15645d2007-03-18 16:22:39 +01007340
Willy Tarreau58f10d72006-12-04 02:26:12 +01007341/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007342 * Try to retrieve the server associated to the appsession.
Willy Tarreau87b09662015-04-03 00:22:06 +02007343 * If the server is found, it's assigned to the stream.
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007344 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007345void manage_client_side_appsession(struct stream *s, const char *buf, int len) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02007346 struct http_txn *txn = s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007347 appsess *asession = NULL;
7348 char *sessid_temp = NULL;
7349
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007350 if (len > s->be->appsession_len) {
7351 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007352 }
7353
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007354 if (s->be->options2 & PR_O2_AS_REQL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007355 /* request-learn option is enabled : store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007356 if (txn->sessid != NULL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007357 /* free previously allocated memory as we don't need the stream id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007358 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007359 }
7360
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007361 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007362 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
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007367 memcpy(txn->sessid, buf, len);
7368 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007369 }
7370
7371 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7372 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007373 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007374 return;
7375 }
7376
Cyril Bontéb21570a2009-11-29 20:04:48 +01007377 memcpy(sessid_temp, buf, len);
7378 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007379
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007380 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007381 /* free previously allocated memory */
7382 pool_free2(apools.sessid, sessid_temp);
7383
7384 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007385 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7386 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007387 asession->request_count++;
7388
7389 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007390 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007391
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007392 while (srv) {
7393 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007394 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007395 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007396 (s->flags & SF_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007397 /* we found the server and it's usable */
7398 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007399 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007400 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007401 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007402
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007403 break;
7404 } else {
7405 txn->flags &= ~TX_CK_MASK;
7406 txn->flags |= TX_CK_DOWN;
7407 }
7408 }
7409 srv = srv->next;
7410 }
7411 }
7412 }
7413}
7414
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007415/* Find the end of a cookie value contained between <s> and <e>. It works the
7416 * same way as with headers above except that the semi-colon also ends a token.
7417 * See RFC2965 for more information. Note that it requires a valid header to
7418 * return a valid result.
7419 */
7420char *find_cookie_value_end(char *s, const char *e)
7421{
7422 int quoted, qdpair;
7423
7424 quoted = qdpair = 0;
7425 for (; s < e; s++) {
7426 if (qdpair) qdpair = 0;
7427 else if (quoted) {
7428 if (*s == '\\') qdpair = 1;
7429 else if (*s == '"') quoted = 0;
7430 }
7431 else if (*s == '"') quoted = 1;
7432 else if (*s == ',' || *s == ';') return s;
7433 }
7434 return s;
7435}
7436
7437/* Delete a value in a header between delimiters <from> and <next> in buffer
7438 * <buf>. The number of characters displaced is returned, and the pointer to
7439 * the first delimiter is updated if required. The function tries as much as
7440 * possible to respect the following principles :
7441 * - replace <from> delimiter by the <next> one unless <from> points to a
7442 * colon, in which case <next> is simply removed
7443 * - set exactly one space character after the new first delimiter, unless
7444 * there are not enough characters in the block being moved to do so.
7445 * - remove unneeded spaces before the previous delimiter and after the new
7446 * one.
7447 *
7448 * It is the caller's responsibility to ensure that :
7449 * - <from> points to a valid delimiter or the colon ;
7450 * - <next> points to a valid delimiter or the final CR/LF ;
7451 * - there are non-space chars before <from> ;
7452 * - there is a CR/LF at or after <next>.
7453 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007454int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007455{
7456 char *prev = *from;
7457
7458 if (*prev == ':') {
7459 /* We're removing the first value, preserve the colon and add a
7460 * space if possible.
7461 */
7462 if (!http_is_crlf[(unsigned char)*next])
7463 next++;
7464 prev++;
7465 if (prev < next)
7466 *prev++ = ' ';
7467
7468 while (http_is_spht[(unsigned char)*next])
7469 next++;
7470 } else {
7471 /* Remove useless spaces before the old delimiter. */
7472 while (http_is_spht[(unsigned char)*(prev-1)])
7473 prev--;
7474 *from = prev;
7475
7476 /* copy the delimiter and if possible a space if we're
7477 * not at the end of the line.
7478 */
7479 if (!http_is_crlf[(unsigned char)*next]) {
7480 *prev++ = *next++;
7481 if (prev + 1 < next)
7482 *prev++ = ' ';
7483 while (http_is_spht[(unsigned char)*next])
7484 next++;
7485 }
7486 }
7487 return buffer_replace2(buf, prev, next, NULL, 0);
7488}
7489
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007490/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007491 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007492 * desirable to call it only when needed. This code is quite complex because
7493 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7494 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007495 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007496void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007497{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007498 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007499 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007500 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007501 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007502 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7503 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007504
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007505 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007506 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007507 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007508
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007509 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007510 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007511 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007512
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007513 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007514 hdr_beg = hdr_next;
7515 hdr_end = hdr_beg + cur_hdr->len;
7516 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007517
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007518 /* We have one full header between hdr_beg and hdr_end, and the
7519 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007520 * "Cookie:" headers.
7521 */
7522
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007523 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007524 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007525 old_idx = cur_idx;
7526 continue;
7527 }
7528
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007529 del_from = NULL; /* nothing to be deleted */
7530 preserve_hdr = 0; /* assume we may kill the whole header */
7531
Willy Tarreau58f10d72006-12-04 02:26:12 +01007532 /* Now look for cookies. Conforming to RFC2109, we have to support
7533 * attributes whose name begin with a '$', and associate them with
7534 * the right cookie, if we want to delete this cookie.
7535 * So there are 3 cases for each cookie read :
7536 * 1) it's a special attribute, beginning with a '$' : ignore it.
7537 * 2) it's a server id cookie that we *MAY* want to delete : save
7538 * some pointers on it (last semi-colon, beginning of cookie...)
7539 * 3) it's an application cookie : we *MAY* have to delete a previous
7540 * "special" cookie.
7541 * At the end of loop, if a "special" cookie remains, we may have to
7542 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007543 * *MUST* delete it.
7544 *
7545 * Note: RFC2965 is unclear about the processing of spaces around
7546 * the equal sign in the ATTR=VALUE form. A careful inspection of
7547 * the RFC explicitly allows spaces before it, and not within the
7548 * tokens (attrs or values). An inspection of RFC2109 allows that
7549 * too but section 10.1.3 lets one think that spaces may be allowed
7550 * after the equal sign too, resulting in some (rare) buggy
7551 * implementations trying to do that. So let's do what servers do.
7552 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7553 * allowed quoted strings in values, with any possible character
7554 * after a backslash, including control chars and delimitors, which
7555 * causes parsing to become ambiguous. Browsers also allow spaces
7556 * within values even without quotes.
7557 *
7558 * We have to keep multiple pointers in order to support cookie
7559 * removal at the beginning, middle or end of header without
7560 * corrupting the header. All of these headers are valid :
7561 *
7562 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7563 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7564 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7565 * | | | | | | | | |
7566 * | | | | | | | | hdr_end <--+
7567 * | | | | | | | +--> next
7568 * | | | | | | +----> val_end
7569 * | | | | | +-----------> val_beg
7570 * | | | | +--------------> equal
7571 * | | | +----------------> att_end
7572 * | | +---------------------> att_beg
7573 * | +--------------------------> prev
7574 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007575 */
7576
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007577 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7578 /* Iterate through all cookies on this line */
7579
7580 /* find att_beg */
7581 att_beg = prev + 1;
7582 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7583 att_beg++;
7584
7585 /* find att_end : this is the first character after the last non
7586 * space before the equal. It may be equal to hdr_end.
7587 */
7588 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007589
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007590 while (equal < hdr_end) {
7591 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007592 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007593 if (http_is_spht[(unsigned char)*equal++])
7594 continue;
7595 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007596 }
7597
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007598 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7599 * is between <att_beg> and <equal>, both may be identical.
7600 */
7601
7602 /* look for end of cookie if there is an equal sign */
7603 if (equal < hdr_end && *equal == '=') {
7604 /* look for the beginning of the value */
7605 val_beg = equal + 1;
7606 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7607 val_beg++;
7608
7609 /* find the end of the value, respecting quotes */
7610 next = find_cookie_value_end(val_beg, hdr_end);
7611
7612 /* make val_end point to the first white space or delimitor after the value */
7613 val_end = next;
7614 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7615 val_end--;
7616 } else {
7617 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007618 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007619
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007620 /* We have nothing to do with attributes beginning with '$'. However,
7621 * they will automatically be removed if a header before them is removed,
7622 * since they're supposed to be linked together.
7623 */
7624 if (*att_beg == '$')
7625 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007626
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007627 /* Ignore cookies with no equal sign */
7628 if (equal == next) {
7629 /* This is not our cookie, so we must preserve it. But if we already
7630 * scheduled another cookie for removal, we cannot remove the
7631 * complete header, but we can remove the previous block itself.
7632 */
7633 preserve_hdr = 1;
7634 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007635 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007636 val_end += delta;
7637 next += delta;
7638 hdr_end += delta;
7639 hdr_next += delta;
7640 cur_hdr->len += delta;
7641 http_msg_move_end(&txn->req, delta);
7642 prev = del_from;
7643 del_from = NULL;
7644 }
7645 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007646 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007647
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007648 /* if there are spaces around the equal sign, we need to
7649 * strip them otherwise we'll get trouble for cookie captures,
7650 * or even for rewrites. Since this happens extremely rarely,
7651 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007652 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007653 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7654 int stripped_before = 0;
7655 int stripped_after = 0;
7656
7657 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007658 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007659 equal += stripped_before;
7660 val_beg += stripped_before;
7661 }
7662
7663 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007664 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007665 val_beg += stripped_after;
7666 stripped_before += stripped_after;
7667 }
7668
7669 val_end += stripped_before;
7670 next += stripped_before;
7671 hdr_end += stripped_before;
7672 hdr_next += stripped_before;
7673 cur_hdr->len += stripped_before;
7674 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007675 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007676 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007677
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007678 /* First, let's see if we want to capture this cookie. We check
7679 * that we don't already have a client side cookie, because we
7680 * can only capture one. Also as an optimisation, we ignore
7681 * cookies shorter than the declared name.
7682 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007683 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7684 (val_end - att_beg >= sess->fe->capture_namelen) &&
7685 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007686 int log_len = val_end - att_beg;
7687
7688 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7689 Alert("HTTP logging : out of memory.\n");
7690 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007691 if (log_len > sess->fe->capture_len)
7692 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007693 memcpy(txn->cli_cookie, att_beg, log_len);
7694 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007695 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007696 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007697
Willy Tarreaubca99692010-10-06 19:25:55 +02007698 /* Persistence cookies in passive, rewrite or insert mode have the
7699 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007700 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007701 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007702 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007703 * For cookies in prefix mode, the form is :
7704 *
7705 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007706 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007707 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7708 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7709 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007710 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007711
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007712 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7713 * have the server ID between val_beg and delim, and the original cookie between
7714 * delim+1 and val_end. Otherwise, delim==val_end :
7715 *
7716 * Cookie: NAME=SRV; # in all but prefix modes
7717 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7718 * | || || | |+-> next
7719 * | || || | +--> val_end
7720 * | || || +---------> delim
7721 * | || |+------------> val_beg
7722 * | || +-------------> att_end = equal
7723 * | |+-----------------> att_beg
7724 * | +------------------> prev
7725 * +-------------------------> hdr_beg
7726 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007727
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007728 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007729 for (delim = val_beg; delim < val_end; delim++)
7730 if (*delim == COOKIE_DELIM)
7731 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007732 } else {
7733 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007734 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007735 /* Now check if the cookie contains a date field, which would
7736 * appear after a vertical bar ('|') just after the server name
7737 * and before the delimiter.
7738 */
7739 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7740 if (vbar1) {
7741 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007742 * right is the last seen date. It is a base64 encoded
7743 * 30-bit value representing the UNIX date since the
7744 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007745 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007746 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007747 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007748 if (val_end - vbar1 >= 5) {
7749 val = b64tos30(vbar1);
7750 if (val > 0)
7751 txn->cookie_last_date = val << 2;
7752 }
7753 /* look for a second vertical bar */
7754 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7755 if (vbar1 && (val_end - vbar1 > 5)) {
7756 val = b64tos30(vbar1 + 1);
7757 if (val > 0)
7758 txn->cookie_first_date = val << 2;
7759 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007760 }
7761 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007762
Willy Tarreauf64d1412010-10-07 20:06:11 +02007763 /* if the cookie has an expiration date and the proxy wants to check
7764 * it, then we do that now. We first check if the cookie is too old,
7765 * then only if it has expired. We detect strict overflow because the
7766 * time resolution here is not great (4 seconds). Cookies with dates
7767 * in the future are ignored if their offset is beyond one day. This
7768 * allows an admin to fix timezone issues without expiring everyone
7769 * and at the same time avoids keeping unwanted side effects for too
7770 * long.
7771 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007772 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7773 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007774 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007775 txn->flags &= ~TX_CK_MASK;
7776 txn->flags |= TX_CK_OLD;
7777 delim = val_beg; // let's pretend we have not found the cookie
7778 txn->cookie_first_date = 0;
7779 txn->cookie_last_date = 0;
7780 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007781 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7782 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007783 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007784 txn->flags &= ~TX_CK_MASK;
7785 txn->flags |= TX_CK_EXPIRED;
7786 delim = val_beg; // let's pretend we have not found the cookie
7787 txn->cookie_first_date = 0;
7788 txn->cookie_last_date = 0;
7789 }
7790
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007791 /* Here, we'll look for the first running server which supports the cookie.
7792 * This allows to share a same cookie between several servers, for example
7793 * to dedicate backup servers to specific servers only.
7794 * However, to prevent clients from sticking to cookie-less backup server
7795 * when they have incidentely learned an empty cookie, we simply ignore
7796 * empty cookies and mark them as invalid.
7797 * The same behaviour is applied when persistence must be ignored.
7798 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007799 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007800 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007801
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007802 while (srv) {
7803 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7804 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007805 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007806 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007807 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007808 /* we found the server and we can use it */
7809 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007810 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007811 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007812 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007813 break;
7814 } else {
7815 /* we found a server, but it's down,
7816 * mark it as such and go on in case
7817 * another one is available.
7818 */
7819 txn->flags &= ~TX_CK_MASK;
7820 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007821 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007822 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007823 srv = srv->next;
7824 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007825
Willy Tarreauf64d1412010-10-07 20:06:11 +02007826 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007827 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007828 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007829 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007830 txn->flags |= TX_CK_UNUSED;
7831 else
7832 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007833 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007834
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007835 /* depending on the cookie mode, we may have to either :
7836 * - delete the complete cookie if we're in insert+indirect mode, so that
7837 * the server never sees it ;
7838 * - remove the server id from the cookie value, and tag the cookie as an
7839 * application cookie so that it does not get accidentely removed later,
7840 * if we're in cookie prefix mode
7841 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007842 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007843 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007844
Willy Tarreau9b28e032012-10-12 23:49:43 +02007845 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007846 val_end += delta;
7847 next += delta;
7848 hdr_end += delta;
7849 hdr_next += delta;
7850 cur_hdr->len += delta;
7851 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007852
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007853 del_from = NULL;
7854 preserve_hdr = 1; /* we want to keep this cookie */
7855 }
7856 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007857 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007858 del_from = prev;
7859 }
7860 } else {
7861 /* This is not our cookie, so we must preserve it. But if we already
7862 * scheduled another cookie for removal, we cannot remove the
7863 * complete header, but we can remove the previous block itself.
7864 */
7865 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007866
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007867 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007868 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007869 if (att_beg >= del_from)
7870 att_beg += delta;
7871 if (att_end >= del_from)
7872 att_end += delta;
7873 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007874 val_end += delta;
7875 next += delta;
7876 hdr_end += delta;
7877 hdr_next += delta;
7878 cur_hdr->len += delta;
7879 http_msg_move_end(&txn->req, delta);
7880 prev = del_from;
7881 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007882 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007883 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007884
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007885 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007886 if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007887 int cmp_len, value_len;
7888 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007889
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007890 if (s->be->options2 & PR_O2_AS_PFX) {
7891 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7892 value_begin = att_beg + s->be->appsession_name_len;
7893 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007894 } else {
7895 cmp_len = att_end - att_beg;
7896 value_begin = val_beg;
7897 value_len = val_end - val_beg;
7898 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007899
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007900 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007901 if (cmp_len == s->be->appsession_name_len &&
7902 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7903 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007904 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007905 }
7906
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007907 /* continue with next cookie on this header line */
7908 att_beg = next;
7909 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007910
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007911 /* There are no more cookies on this line.
7912 * We may still have one (or several) marked for deletion at the
7913 * end of the line. We must do this now in two ways :
7914 * - if some cookies must be preserved, we only delete from the
7915 * mark to the end of line ;
7916 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007917 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007918 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007919 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007920 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007921 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007922 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007923 cur_hdr->len += delta;
7924 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007925 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007926
7927 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007928 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7929 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007930 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007931 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007932 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007933 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007934 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007935 }
7936
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007937 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007938 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007939 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007940}
7941
7942
Willy Tarreaua15645d2007-03-18 16:22:39 +01007943/* Iterate the same filter through all response headers contained in <rtr>.
7944 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7945 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007946int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007947{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007948 char *cur_ptr, *cur_end, *cur_next;
7949 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007950 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007951 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007952 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007953
7954 last_hdr = 0;
7955
Willy Tarreau9b28e032012-10-12 23:49:43 +02007956 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007957 old_idx = 0;
7958
7959 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007960 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007961 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007962 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007963 (exp->action == ACT_ALLOW ||
7964 exp->action == ACT_DENY))
7965 return 0;
7966
7967 cur_idx = txn->hdr_idx.v[old_idx].next;
7968 if (!cur_idx)
7969 break;
7970
7971 cur_hdr = &txn->hdr_idx.v[cur_idx];
7972 cur_ptr = cur_next;
7973 cur_end = cur_ptr + cur_hdr->len;
7974 cur_next = cur_end + cur_hdr->cr + 1;
7975
7976 /* Now we have one header between cur_ptr and cur_end,
7977 * and the next header starts at cur_next.
7978 */
7979
Willy Tarreau15a53a42015-01-21 13:39:42 +01007980 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007981 switch (exp->action) {
7982 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007983 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007984 last_hdr = 1;
7985 break;
7986
7987 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007988 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007989 last_hdr = 1;
7990 break;
7991
7992 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007993 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7994 if (trash.len < 0)
7995 return -1;
7996
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007997 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007998 /* FIXME: if the user adds a newline in the replacement, the
7999 * index will not be recalculated for now, and the new line
8000 * will not be counted as a new header.
8001 */
8002
8003 cur_end += delta;
8004 cur_next += delta;
8005 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008006 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008007 break;
8008
8009 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008010 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008011 cur_next += delta;
8012
Willy Tarreaufa355d42009-11-29 18:12:29 +01008013 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008014 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8015 txn->hdr_idx.used--;
8016 cur_hdr->len = 0;
8017 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008018 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008019 break;
8020
8021 }
8022 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023
8024 /* keep the link from this header to next one in case of later
8025 * removal of next header.
8026 */
8027 old_idx = cur_idx;
8028 }
8029 return 0;
8030}
8031
8032
8033/* Apply the filter to the status line in the response buffer <rtr>.
8034 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8035 * or -1 if a replacement resulted in an invalid status line.
8036 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008037int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008039 char *cur_ptr, *cur_end;
8040 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008041 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008042 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008043
8044
Willy Tarreau3d300592007-03-18 18:34:41 +01008045 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008046 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008047 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008048 (exp->action == ACT_ALLOW ||
8049 exp->action == ACT_DENY))
8050 return 0;
8051 else if (exp->action == ACT_REMOVE)
8052 return 0;
8053
8054 done = 0;
8055
Willy Tarreau9b28e032012-10-12 23:49:43 +02008056 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008057 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058
8059 /* Now we have the status line between cur_ptr and cur_end */
8060
Willy Tarreau15a53a42015-01-21 13:39:42 +01008061 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008062 switch (exp->action) {
8063 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008064 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008065 done = 1;
8066 break;
8067
8068 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008069 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008070 done = 1;
8071 break;
8072
8073 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008074 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8075 if (trash.len < 0)
8076 return -1;
8077
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008078 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008079 /* FIXME: if the user adds a newline in the replacement, the
8080 * index will not be recalculated for now, and the new line
8081 * will not be counted as a new header.
8082 */
8083
Willy Tarreaufa355d42009-11-29 18:12:29 +01008084 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008085 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008086 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008087 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008088 cur_ptr, cur_end + 1,
8089 NULL, NULL);
8090 if (unlikely(!cur_end))
8091 return -1;
8092
8093 /* we have a full respnse and we know that we have either a CR
8094 * or an LF at <ptr>.
8095 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008096 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008097 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008098 /* there is no point trying this regex on headers */
8099 return 1;
8100 }
8101 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008102 return done;
8103}
8104
8105
8106
8107/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008108 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008109 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8110 * unparsable response.
8111 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008112int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008113{
Willy Tarreau192252e2015-04-04 01:47:55 +02008114 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008115 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008116 struct hdr_exp *exp;
8117
8118 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008119 int ret;
8120
8121 /*
8122 * The interleaving of transformations and verdicts
8123 * makes it difficult to decide to continue or stop
8124 * the evaluation.
8125 */
8126
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008127 if (txn->flags & TX_SVDENY)
8128 break;
8129
Willy Tarreau3d300592007-03-18 18:34:41 +01008130 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008131 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8132 exp->action == ACT_PASS)) {
8133 exp = exp->next;
8134 continue;
8135 }
8136
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008137 /* if this filter had a condition, evaluate it now and skip to
8138 * next filter if the condition does not match.
8139 */
8140 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008141 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008142 ret = acl_pass(ret);
8143 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8144 ret = !ret;
8145 if (!ret)
8146 continue;
8147 }
8148
Willy Tarreaua15645d2007-03-18 16:22:39 +01008149 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008150 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008151 if (unlikely(ret < 0))
8152 return -1;
8153
8154 if (likely(ret == 0)) {
8155 /* The filter did not match the response, it can be
8156 * iterated through all headers.
8157 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008158 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8159 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008160 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008161 }
8162 return 0;
8163}
8164
8165
Willy Tarreaua15645d2007-03-18 16:22:39 +01008166/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008167 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008168 * desirable to call it only when needed. This function is also used when we
8169 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008170 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008171void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008172{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008173 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008174 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008175 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008176 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008177 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008178 char *hdr_beg, *hdr_end, *hdr_next;
8179 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008180
Willy Tarreaua15645d2007-03-18 16:22:39 +01008181 /* Iterate through the headers.
8182 * we start with the start line.
8183 */
8184 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008185 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008186
8187 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8188 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008189 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008190
8191 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008192 hdr_beg = hdr_next;
8193 hdr_end = hdr_beg + cur_hdr->len;
8194 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008195
Willy Tarreau24581ba2010-08-31 22:39:35 +02008196 /* We have one full header between hdr_beg and hdr_end, and the
8197 * next header starts at hdr_next. We're only interested in
8198 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008199 */
8200
Willy Tarreau24581ba2010-08-31 22:39:35 +02008201 is_cookie2 = 0;
8202 prev = hdr_beg + 10;
8203 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008204 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008205 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8206 if (!val) {
8207 old_idx = cur_idx;
8208 continue;
8209 }
8210 is_cookie2 = 1;
8211 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008212 }
8213
Willy Tarreau24581ba2010-08-31 22:39:35 +02008214 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8215 * <prev> points to the colon.
8216 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008217 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008218
Willy Tarreau24581ba2010-08-31 22:39:35 +02008219 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8220 * check-cache is enabled) and we are not interested in checking
8221 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008222 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008223 if (s->be->cookie_name == NULL &&
8224 s->be->appsession_name == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008225 sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008226 return;
8227
Willy Tarreau24581ba2010-08-31 22:39:35 +02008228 /* OK so now we know we have to process this response cookie.
8229 * The format of the Set-Cookie header is slightly different
8230 * from the format of the Cookie header in that it does not
8231 * support the comma as a cookie delimiter (thus the header
8232 * cannot be folded) because the Expires attribute described in
8233 * the original Netscape's spec may contain an unquoted date
8234 * with a comma inside. We have to live with this because
8235 * many browsers don't support Max-Age and some browsers don't
8236 * support quoted strings. However the Set-Cookie2 header is
8237 * clean.
8238 *
8239 * We have to keep multiple pointers in order to support cookie
8240 * removal at the beginning, middle or end of header without
8241 * corrupting the header (in case of set-cookie2). A special
8242 * pointer, <scav> points to the beginning of the set-cookie-av
8243 * fields after the first semi-colon. The <next> pointer points
8244 * either to the end of line (set-cookie) or next unquoted comma
8245 * (set-cookie2). All of these headers are valid :
8246 *
8247 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8248 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8249 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8250 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8251 * | | | | | | | | | |
8252 * | | | | | | | | +-> next hdr_end <--+
8253 * | | | | | | | +------------> scav
8254 * | | | | | | +--------------> val_end
8255 * | | | | | +--------------------> val_beg
8256 * | | | | +----------------------> equal
8257 * | | | +------------------------> att_end
8258 * | | +----------------------------> att_beg
8259 * | +------------------------------> prev
8260 * +-----------------------------------------> hdr_beg
8261 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008262
Willy Tarreau24581ba2010-08-31 22:39:35 +02008263 for (; prev < hdr_end; prev = next) {
8264 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008265
Willy Tarreau24581ba2010-08-31 22:39:35 +02008266 /* find att_beg */
8267 att_beg = prev + 1;
8268 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8269 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008270
Willy Tarreau24581ba2010-08-31 22:39:35 +02008271 /* find att_end : this is the first character after the last non
8272 * space before the equal. It may be equal to hdr_end.
8273 */
8274 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008275
Willy Tarreau24581ba2010-08-31 22:39:35 +02008276 while (equal < hdr_end) {
8277 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8278 break;
8279 if (http_is_spht[(unsigned char)*equal++])
8280 continue;
8281 att_end = equal;
8282 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008283
Willy Tarreau24581ba2010-08-31 22:39:35 +02008284 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8285 * is between <att_beg> and <equal>, both may be identical.
8286 */
8287
8288 /* look for end of cookie if there is an equal sign */
8289 if (equal < hdr_end && *equal == '=') {
8290 /* look for the beginning of the value */
8291 val_beg = equal + 1;
8292 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8293 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008294
Willy Tarreau24581ba2010-08-31 22:39:35 +02008295 /* find the end of the value, respecting quotes */
8296 next = find_cookie_value_end(val_beg, hdr_end);
8297
8298 /* make val_end point to the first white space or delimitor after the value */
8299 val_end = next;
8300 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8301 val_end--;
8302 } else {
8303 /* <equal> points to next comma, semi-colon or EOL */
8304 val_beg = val_end = next = equal;
8305 }
8306
8307 if (next < hdr_end) {
8308 /* Set-Cookie2 supports multiple cookies, and <next> points to
8309 * a colon or semi-colon before the end. So skip all attr-value
8310 * pairs and look for the next comma. For Set-Cookie, since
8311 * commas are permitted in values, skip to the end.
8312 */
8313 if (is_cookie2)
8314 next = find_hdr_value_end(next, hdr_end);
8315 else
8316 next = hdr_end;
8317 }
8318
8319 /* Now everything is as on the diagram above */
8320
8321 /* Ignore cookies with no equal sign */
8322 if (equal == val_end)
8323 continue;
8324
8325 /* If there are spaces around the equal sign, we need to
8326 * strip them otherwise we'll get trouble for cookie captures,
8327 * or even for rewrites. Since this happens extremely rarely,
8328 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008329 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008330 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8331 int stripped_before = 0;
8332 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008333
Willy Tarreau24581ba2010-08-31 22:39:35 +02008334 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008335 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008336 equal += stripped_before;
8337 val_beg += stripped_before;
8338 }
8339
8340 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008341 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008342 val_beg += stripped_after;
8343 stripped_before += stripped_after;
8344 }
8345
8346 val_end += stripped_before;
8347 next += stripped_before;
8348 hdr_end += stripped_before;
8349 hdr_next += stripped_before;
8350 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008351 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008352 }
8353
8354 /* First, let's see if we want to capture this cookie. We check
8355 * that we don't already have a server side cookie, because we
8356 * can only capture one. Also as an optimisation, we ignore
8357 * cookies shorter than the declared name.
8358 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008359 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008360 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008361 (val_end - att_beg >= sess->fe->capture_namelen) &&
8362 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008363 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008364 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008365 Alert("HTTP logging : out of memory.\n");
8366 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008367 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008368 if (log_len > sess->fe->capture_len)
8369 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008370 memcpy(txn->srv_cookie, att_beg, log_len);
8371 txn->srv_cookie[log_len] = 0;
8372 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008373 }
8374
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008375 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008376 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008377 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008378 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8379 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008380 /* assume passive cookie by default */
8381 txn->flags &= ~TX_SCK_MASK;
8382 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008383
8384 /* If the cookie is in insert mode on a known server, we'll delete
8385 * this occurrence because we'll insert another one later.
8386 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008387 * a direct access.
8388 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008389 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008390 /* The "preserve" flag was set, we don't want to touch the
8391 * server's cookie.
8392 */
8393 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008394 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008395 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008396 /* this cookie must be deleted */
8397 if (*prev == ':' && next == hdr_end) {
8398 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008399 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008400 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8401 txn->hdr_idx.used--;
8402 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008403 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008404 hdr_next += delta;
8405 http_msg_move_end(&txn->rsp, delta);
8406 /* note: while both invalid now, <next> and <hdr_end>
8407 * are still equal, so the for() will stop as expected.
8408 */
8409 } else {
8410 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008411 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008412 next = prev;
8413 hdr_end += delta;
8414 hdr_next += delta;
8415 cur_hdr->len += delta;
8416 http_msg_move_end(&txn->rsp, delta);
8417 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008418 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008419 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008420 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008421 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008422 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008423 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008424 * with this server since we know it.
8425 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008426 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008427 next += delta;
8428 hdr_end += delta;
8429 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008430 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008431 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008432
Willy Tarreauf1348312010-10-07 15:54:11 +02008433 txn->flags &= ~TX_SCK_MASK;
8434 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008435 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008436 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008437 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008438 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008439 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008440 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008441 next += delta;
8442 hdr_end += delta;
8443 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008444 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008445 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008446
Willy Tarreau827aee92011-03-10 16:55:02 +01008447 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008448 txn->flags &= ~TX_SCK_MASK;
8449 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008450 }
8451 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008452 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008453 else if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008454 int cmp_len, value_len;
8455 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008456
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008457 if (s->be->options2 & PR_O2_AS_PFX) {
8458 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8459 value_begin = att_beg + s->be->appsession_name_len;
8460 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008461 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008462 cmp_len = att_end - att_beg;
8463 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008464 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008465 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008466
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008467 if ((cmp_len == s->be->appsession_name_len) &&
8468 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008469 /* free a possibly previously allocated memory */
8470 pool_free2(apools.sessid, txn->sessid);
8471
Willy Tarreau87b09662015-04-03 00:22:06 +02008472 /* Store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008473 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008474 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008475 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008476 return;
8477 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008478 memcpy(txn->sessid, value_begin, value_len);
8479 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008480 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008481 }
8482 /* that's done for this cookie, check the next one on the same
8483 * line when next != hdr_end (only if is_cookie2).
8484 */
8485 }
8486 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008487 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008488 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008489
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008490 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008491 appsess *asession = NULL;
8492 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008493 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008494 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008495 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008496 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8497 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008498 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008499 return;
8500 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008501 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8502
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008503 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8504 Alert("Not enough Memory process_srv():asession->sessid: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 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008509 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8510 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008511
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008512 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008513 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008514 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008515 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8516 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008517 return;
8518 }
8519 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008520 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008521
8522 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008523 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008524 }
8525
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008526 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008527 asession->request_count++;
8528 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008529}
8530
8531
Willy Tarreaua15645d2007-03-18 16:22:39 +01008532/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008533 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008534 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008535void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008536{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008537 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008538 char *p1, *p2;
8539
8540 char *cur_ptr, *cur_end, *cur_next;
8541 int cur_idx;
8542
Willy Tarreau5df51872007-11-25 16:20:08 +01008543 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008544 return;
8545
8546 /* Iterate through the headers.
8547 * we start with the start line.
8548 */
8549 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008550 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008551
8552 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8553 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008554 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008555
8556 cur_hdr = &txn->hdr_idx.v[cur_idx];
8557 cur_ptr = cur_next;
8558 cur_end = cur_ptr + cur_hdr->len;
8559 cur_next = cur_end + cur_hdr->cr + 1;
8560
8561 /* We have one full header between cur_ptr and cur_end, and the
8562 * next header starts at cur_next. We're only interested in
8563 * "Cookie:" headers.
8564 */
8565
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008566 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8567 if (val) {
8568 if ((cur_end - (cur_ptr + val) >= 8) &&
8569 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8570 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8571 return;
8572 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008573 }
8574
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008575 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8576 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008577 continue;
8578
8579 /* OK, right now we know we have a cache-control header at cur_ptr */
8580
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008581 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008582
8583 if (p1 >= cur_end) /* no more info */
8584 continue;
8585
8586 /* p1 is at the beginning of the value */
8587 p2 = p1;
8588
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008589 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008590 p2++;
8591
8592 /* we have a complete value between p1 and p2 */
8593 if (p2 < cur_end && *p2 == '=') {
8594 /* we have something of the form no-cache="set-cookie" */
8595 if ((cur_end - p1 >= 21) &&
8596 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8597 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008598 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008599 continue;
8600 }
8601
8602 /* OK, so we know that either p2 points to the end of string or to a comma */
8603 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008604 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008605 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8606 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8607 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008608 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008609 return;
8610 }
8611
8612 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008613 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008614 continue;
8615 }
8616 }
8617}
8618
8619
Willy Tarreau58f10d72006-12-04 02:26:12 +01008620/*
8621 * Try to retrieve a known appsession in the URI, then the associated server.
Willy Tarreau87b09662015-04-03 00:22:06 +02008622 * If the server is found, it's assigned to the stream.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008623 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008624void get_srv_from_appsession(struct stream *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008625{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008626 char *end_params, *first_param, *cur_param, *next_param;
8627 char separator;
8628 int value_len;
8629
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008630 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008631
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008632 if (s->be->appsession_name == NULL ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02008633 (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 +01008634 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008635 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008636
Cyril Bontéb21570a2009-11-29 20:04:48 +01008637 first_param = NULL;
8638 switch (mode) {
8639 case PR_O2_AS_M_PP:
8640 first_param = memchr(begin, ';', len);
8641 break;
8642 case PR_O2_AS_M_QS:
8643 first_param = memchr(begin, '?', len);
8644 break;
8645 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008646
Cyril Bontéb21570a2009-11-29 20:04:48 +01008647 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008648 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008649 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008650
Cyril Bontéb21570a2009-11-29 20:04:48 +01008651 switch (mode) {
8652 case PR_O2_AS_M_PP:
8653 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8654 end_params = (char *) begin + len;
8655 }
8656 separator = ';';
8657 break;
8658 case PR_O2_AS_M_QS:
8659 end_params = (char *) begin + len;
8660 separator = '&';
8661 break;
8662 default:
8663 /* unknown mode, shouldn't happen */
8664 return;
8665 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008666
Cyril Bontéb21570a2009-11-29 20:04:48 +01008667 cur_param = next_param = end_params;
8668 while (cur_param > first_param) {
8669 cur_param--;
8670 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8671 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008672 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8673 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8674 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008675 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008676 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8677 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008678 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008679 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008680 }
8681 break;
8682 }
8683 next_param = cur_param;
8684 }
8685 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008686#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008687 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008688 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008689#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008690}
8691
Willy Tarreaub2513902006-12-17 14:52:38 +01008692/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008693 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008694 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008695 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008696 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008697 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008698 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008699 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008700 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008701int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008702{
8703 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008704 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008705 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008706
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008707 if (!uri_auth)
8708 return 0;
8709
Cyril Bonté70be45d2010-10-12 00:14:35 +02008710 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008711 return 0;
8712
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008713 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008714 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008715 return 0;
8716
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008717 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008718 return 0;
8719
Willy Tarreaub2513902006-12-17 14:52:38 +01008720 return 1;
8721}
8722
Willy Tarreau4076a152009-04-02 15:18:36 +02008723/*
8724 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008725 * By default it tries to report the error position as msg->err_pos. However if
8726 * this one is not set, it will then report msg->next, which is the last known
8727 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008728 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008729 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008730void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008731 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008732 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008733{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008734 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008735 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008736 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008737
Willy Tarreau9b28e032012-10-12 23:49:43 +02008738 es->len = MIN(chn->buf->i, sizeof(es->buf));
8739 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008740 len1 = MIN(len1, es->len);
8741 len2 = es->len - len1; /* remaining data if buffer wraps */
8742
Willy Tarreau9b28e032012-10-12 23:49:43 +02008743 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008744 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008745 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008746
Willy Tarreau4076a152009-04-02 15:18:36 +02008747 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008748 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008749 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008750 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008751
Willy Tarreau4076a152009-04-02 15:18:36 +02008752 es->when = date; // user-visible date
8753 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008754 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008755 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008756 if (objt_conn(sess->origin))
8757 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008758 else
8759 memset(&es->src, 0, sizeof(es->src));
8760
Willy Tarreau078272e2010-12-12 12:46:33 +01008761 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008762 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008763 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008764 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008765 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008766 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008767 es->b_out = chn->buf->o;
8768 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008769 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008770 es->m_clen = msg->chunk_len;
8771 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008772}
Willy Tarreaub2513902006-12-17 14:52:38 +01008773
Willy Tarreau294c4732011-12-16 21:35:50 +01008774/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8775 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8776 * performed over the whole headers. Otherwise it must contain a valid header
8777 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8778 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8779 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8780 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008781 * -1. The value fetch stops at commas, so this function is suited for use with
8782 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008783 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008784 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008785unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008786 struct hdr_idx *idx, int occ,
8787 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008788{
Willy Tarreau294c4732011-12-16 21:35:50 +01008789 struct hdr_ctx local_ctx;
8790 char *ptr_hist[MAX_HDR_HISTORY];
8791 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008792 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008793 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008794
Willy Tarreau294c4732011-12-16 21:35:50 +01008795 if (!ctx) {
8796 local_ctx.idx = 0;
8797 ctx = &local_ctx;
8798 }
8799
Willy Tarreaubce70882009-09-07 11:51:47 +02008800 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008801 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008802 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008803 occ--;
8804 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008805 *vptr = ctx->line + ctx->val;
8806 *vlen = ctx->vlen;
8807 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008808 }
8809 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008810 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008811 }
8812
8813 /* negative occurrence, we scan all the list then walk back */
8814 if (-occ > MAX_HDR_HISTORY)
8815 return 0;
8816
Willy Tarreau294c4732011-12-16 21:35:50 +01008817 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008818 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008819 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8820 len_hist[hist_ptr] = ctx->vlen;
8821 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008822 hist_ptr = 0;
8823 found++;
8824 }
8825 if (-occ > found)
8826 return 0;
8827 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008828 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8829 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8830 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008831 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008832 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008833 if (hist_ptr >= MAX_HDR_HISTORY)
8834 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008835 *vptr = ptr_hist[hist_ptr];
8836 *vlen = len_hist[hist_ptr];
8837 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008838}
8839
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008840/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8841 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8842 * performed over the whole headers. Otherwise it must contain a valid header
8843 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8844 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8845 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8846 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8847 * -1. This function differs from http_get_hdr() in that it only returns full
8848 * line header values and does not stop at commas.
8849 * The return value is 0 if nothing was found, or non-zero otherwise.
8850 */
8851unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8852 struct hdr_idx *idx, int occ,
8853 struct hdr_ctx *ctx, char **vptr, int *vlen)
8854{
8855 struct hdr_ctx local_ctx;
8856 char *ptr_hist[MAX_HDR_HISTORY];
8857 int len_hist[MAX_HDR_HISTORY];
8858 unsigned int hist_ptr;
8859 int found;
8860
8861 if (!ctx) {
8862 local_ctx.idx = 0;
8863 ctx = &local_ctx;
8864 }
8865
8866 if (occ >= 0) {
8867 /* search from the beginning */
8868 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8869 occ--;
8870 if (occ <= 0) {
8871 *vptr = ctx->line + ctx->val;
8872 *vlen = ctx->vlen;
8873 return 1;
8874 }
8875 }
8876 return 0;
8877 }
8878
8879 /* negative occurrence, we scan all the list then walk back */
8880 if (-occ > MAX_HDR_HISTORY)
8881 return 0;
8882
8883 found = hist_ptr = 0;
8884 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8885 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8886 len_hist[hist_ptr] = ctx->vlen;
8887 if (++hist_ptr >= MAX_HDR_HISTORY)
8888 hist_ptr = 0;
8889 found++;
8890 }
8891 if (-occ > found)
8892 return 0;
8893 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8894 * find occurrence -occ, so we have to check [hist_ptr+occ].
8895 */
8896 hist_ptr += occ;
8897 if (hist_ptr >= MAX_HDR_HISTORY)
8898 hist_ptr -= MAX_HDR_HISTORY;
8899 *vptr = ptr_hist[hist_ptr];
8900 *vlen = len_hist[hist_ptr];
8901 return 1;
8902}
8903
Willy Tarreaubaaee002006-06-26 02:48:02 +02008904/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008905 * Print a debug line with a header. Always stop at the first CR or LF char,
8906 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8907 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008908 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008909void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008910{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008911 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008912 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008913
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008914 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008915 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008916 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008917 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 +02008918
8919 for (max = 0; start + max < end; max++)
8920 if (start[max] == '\r' || start[max] == '\n')
8921 break;
8922
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008923 UBOUND(max, trash.size - trash.len - 3);
8924 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8925 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008926 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008927}
8928
Willy Tarreaueee5b512015-04-03 23:46:31 +02008929
8930/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8931 * The hdr_idx is allocated as well. In case of allocation failure, everything
8932 * allocated is freed and NULL is returned. Otherwise the new transaction is
8933 * assigned to the stream and returned.
8934 */
8935struct http_txn *http_alloc_txn(struct stream *s)
8936{
8937 struct http_txn *txn = s->txn;
8938
8939 if (txn)
8940 return txn;
8941
8942 txn = pool_alloc2(pool2_http_txn);
8943 if (!txn)
8944 return txn;
8945
8946 txn->hdr_idx.size = global.tune.max_http_hdr;
8947 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8948 if (!txn->hdr_idx.v) {
8949 pool_free2(pool2_http_txn, txn);
8950 return NULL;
8951 }
8952
8953 s->txn = txn;
8954 return txn;
8955}
8956
Willy Tarreau0937bc42009-12-22 15:03:09 +01008957/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008958 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008959 * the required fields are properly allocated and that we only need to (re)init
8960 * them. This should be used before processing any new request.
8961 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008962void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008963{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008964 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008965 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008966
8967 txn->flags = 0;
8968 txn->status = -1;
8969
Willy Tarreauf64d1412010-10-07 20:06:11 +02008970 txn->cookie_first_date = 0;
8971 txn->cookie_last_date = 0;
8972
Willy Tarreaueee5b512015-04-03 23:46:31 +02008973 txn->sessid = NULL;
8974 txn->srv_cookie = NULL;
8975 txn->cli_cookie = NULL;
8976 txn->uri = NULL;
8977
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008978 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008979 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008980 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008981 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008982 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008983 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008984 txn->req.chunk_len = 0LL;
8985 txn->req.body_len = 0LL;
8986 txn->rsp.chunk_len = 0LL;
8987 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008988 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8989 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008990 txn->req.chn = &s->req;
8991 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008992
8993 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008994
8995 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8996 if (fe->options2 & PR_O2_REQBUG_OK)
8997 txn->req.err_pos = -1; /* let buggy requests pass */
8998
Willy Tarreau0937bc42009-12-22 15:03:09 +01008999 if (txn->hdr_idx.v)
9000 hdr_idx_init(&txn->hdr_idx);
9001}
9002
9003/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02009004void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01009005{
Willy Tarreaueee5b512015-04-03 23:46:31 +02009006 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009007 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01009008
Willy Tarreau75195602014-03-11 15:48:55 +01009009 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02009010 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01009011 s->comp_algo->end(&s->comp_ctx);
9012 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02009013 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01009014
Willy Tarreau0937bc42009-12-22 15:03:09 +01009015 /* these ones will have been dynamically allocated */
9016 pool_free2(pool2_requri, txn->uri);
9017 pool_free2(pool2_capture, txn->cli_cookie);
9018 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01009019 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01009020 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009021
William Lallemanda73203e2012-03-12 12:48:57 +01009022 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01009023 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009024 txn->uri = NULL;
9025 txn->srv_cookie = NULL;
9026 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01009027
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009028 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01009029 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02009030 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009031 pool_free2(h->pool, s->req_cap[h->index]);
9032 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01009033 }
9034
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009035 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01009036 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02009037 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009038 pool_free2(h->pool, s->res_cap[h->index]);
9039 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01009040 }
9041
Willy Tarreau0937bc42009-12-22 15:03:09 +01009042}
9043
9044/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02009045void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01009046{
9047 http_end_txn(s);
9048 http_init_txn(s);
9049
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01009050 /* reinitialise the current rule list pointer to NULL. We are sure that
9051 * any rulelist match the NULL pointer.
9052 */
9053 s->current_rule_list = NULL;
9054
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009055 s->be = strm_fe(s);
9056 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02009057 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02009058 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01009059 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009060 /* re-init store persistence */
9061 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01009062 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009063
Willy Tarreau0937bc42009-12-22 15:03:09 +01009064 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009065
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009066 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01009067
Willy Tarreau739cfba2010-01-25 23:11:14 +01009068 /* We must trim any excess data from the response buffer, because we
9069 * may have blocked an invalid response from a server that we don't
9070 * want to accidentely forward once we disable the analysers, nor do
9071 * we want those data to come along with next response. A typical
9072 * example of such data would be from a buggy server responding to
9073 * a HEAD with some data, or sending more than the advertised
9074 * content-length.
9075 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009076 if (unlikely(s->res.buf->i))
9077 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009078
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009079 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009080 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009081
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009082 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009083 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009084
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009085 s->req.rex = TICK_ETERNITY;
9086 s->req.wex = TICK_ETERNITY;
9087 s->req.analyse_exp = TICK_ETERNITY;
9088 s->res.rex = TICK_ETERNITY;
9089 s->res.wex = TICK_ETERNITY;
9090 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009091}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009092
Sasha Pachev218f0642014-06-16 12:05:59 -06009093void free_http_res_rules(struct list *r)
9094{
9095 struct http_res_rule *tr, *pr;
9096
9097 list_for_each_entry_safe(pr, tr, r, list) {
9098 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009099 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009100 free(pr);
9101 }
9102}
9103
9104void free_http_req_rules(struct list *r)
9105{
Willy Tarreauff011f22011-01-06 17:51:27 +01009106 struct http_req_rule *tr, *pr;
9107
9108 list_for_each_entry_safe(pr, tr, r, list) {
9109 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01009110 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009111 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009112
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009113 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009114 free(pr);
9115 }
9116}
9117
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009118/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01009119struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9120{
9121 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009122 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009123 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009124 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009125
9126 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
9127 if (!rule) {
9128 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009129 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009130 }
9131
CJ Ess108b1dd2015-04-07 12:03:37 -04009132 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009133 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01009134 rule->action = HTTP_REQ_ACT_ALLOW;
9135 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009136 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009137 int code;
9138 int hc;
9139
Willy Tarreauff011f22011-01-06 17:51:27 +01009140 rule->action = HTTP_REQ_ACT_DENY;
9141 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009142 if (strcmp(args[cur_arg], "deny_status") == 0) {
9143 cur_arg++;
9144 if (!args[cur_arg]) {
9145 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9146 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9147 goto out_err;
9148 }
9149
9150 code = atol(args[cur_arg]);
9151 cur_arg++;
9152 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9153 if (http_err_codes[hc] == code) {
9154 rule->deny_status = hc;
9155 break;
9156 }
9157 }
9158
9159 if (hc >= HTTP_ERR_SIZE) {
9160 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9161 file, linenum, code);
9162 }
9163 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009164 } else if (!strcmp(args[0], "tarpit")) {
9165 rule->action = HTTP_REQ_ACT_TARPIT;
9166 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009167 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01009168 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009169 cur_arg = 1;
9170
9171 while(*args[cur_arg]) {
9172 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009173 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009174 cur_arg+=2;
9175 continue;
9176 } else
9177 break;
9178 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009179 } else if (!strcmp(args[0], "set-nice")) {
9180 rule->action = HTTP_REQ_ACT_SET_NICE;
9181 cur_arg = 1;
9182
9183 if (!*args[cur_arg] ||
9184 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9185 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9186 file, linenum, args[0]);
9187 goto out_err;
9188 }
9189 rule->arg.nice = atoi(args[cur_arg]);
9190 if (rule->arg.nice < -1024)
9191 rule->arg.nice = -1024;
9192 else if (rule->arg.nice > 1024)
9193 rule->arg.nice = 1024;
9194 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009195 } else if (!strcmp(args[0], "set-tos")) {
9196#ifdef IP_TOS
9197 char *err;
9198 rule->action = HTTP_REQ_ACT_SET_TOS;
9199 cur_arg = 1;
9200
9201 if (!*args[cur_arg] ||
9202 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9203 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9204 file, linenum, args[0]);
9205 goto out_err;
9206 }
9207
9208 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9209 if (err && *err != '\0') {
9210 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9211 file, linenum, err, args[0]);
9212 goto out_err;
9213 }
9214 cur_arg++;
9215#else
9216 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9217 goto out_err;
9218#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009219 } else if (!strcmp(args[0], "set-mark")) {
9220#ifdef SO_MARK
9221 char *err;
9222 rule->action = HTTP_REQ_ACT_SET_MARK;
9223 cur_arg = 1;
9224
9225 if (!*args[cur_arg] ||
9226 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9227 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9228 file, linenum, args[0]);
9229 goto out_err;
9230 }
9231
9232 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9233 if (err && *err != '\0') {
9234 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9235 file, linenum, err, args[0]);
9236 goto out_err;
9237 }
9238 cur_arg++;
9239 global.last_checks |= LSTCHK_NETADM;
9240#else
9241 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9242 goto out_err;
9243#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009244 } else if (!strcmp(args[0], "set-log-level")) {
9245 rule->action = HTTP_REQ_ACT_SET_LOGL;
9246 cur_arg = 1;
9247
9248 if (!*args[cur_arg] ||
9249 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9250 bad_log_level:
9251 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9252 file, linenum, args[0]);
9253 goto out_err;
9254 }
9255 if (strcmp(args[cur_arg], "silent") == 0)
9256 rule->arg.loglevel = -1;
9257 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9258 goto bad_log_level;
9259 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009260 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9261 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9262 cur_arg = 1;
9263
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009264 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9265 (*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 +01009266 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9267 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009268 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009269 }
9270
9271 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9272 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9273 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009274
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009275 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009276 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009277 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9278 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009279 free(proxy->conf.lfs_file);
9280 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9281 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009282 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009283 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9284 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009285 cur_arg = 1;
9286
9287 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009288 (*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 -06009289 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9290 file, linenum, args[0]);
9291 goto out_err;
9292 }
9293
9294 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9295 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9296 LIST_INIT(&rule->arg.hdr_add.fmt);
9297
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009298 error = NULL;
9299 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9300 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9301 args[cur_arg + 1], error);
9302 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009303 goto out_err;
9304 }
9305
9306 proxy->conf.args.ctx = ARGC_HRQ;
9307 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9308 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9309 file, linenum);
9310
9311 free(proxy->conf.lfs_file);
9312 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9313 proxy->conf.lfs_line = proxy->conf.args.line;
9314 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009315 } else if (strcmp(args[0], "del-header") == 0) {
9316 rule->action = HTTP_REQ_ACT_DEL_HDR;
9317 cur_arg = 1;
9318
9319 if (!*args[cur_arg] ||
9320 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9321 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9322 file, linenum, args[0]);
9323 goto out_err;
9324 }
9325
9326 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9327 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9328
9329 proxy->conf.args.ctx = ARGC_HRQ;
9330 free(proxy->conf.lfs_file);
9331 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9332 proxy->conf.lfs_line = proxy->conf.args.line;
9333 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009334 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9335 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009336 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009337 struct sample_expr *expr;
9338 unsigned int where;
9339 char *err = NULL;
9340
9341 cur_arg = 1;
9342 proxy->conf.args.ctx = ARGC_TRK;
9343
9344 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9345 if (!expr) {
9346 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9347 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9348 free(err);
9349 goto out_err;
9350 }
9351
9352 where = 0;
9353 if (proxy->cap & PR_CAP_FE)
9354 where |= SMP_VAL_FE_HRQ_HDR;
9355 if (proxy->cap & PR_CAP_BE)
9356 where |= SMP_VAL_BE_HRQ_HDR;
9357
9358 if (!(expr->fetch->val & where)) {
9359 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9360 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9361 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9362 args[cur_arg-1], sample_src_names(expr->fetch->use));
9363 free(expr);
9364 goto out_err;
9365 }
9366
9367 if (strcmp(args[cur_arg], "table") == 0) {
9368 cur_arg++;
9369 if (!args[cur_arg]) {
9370 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9371 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9372 free(expr);
9373 goto out_err;
9374 }
9375 /* we copy the table name for now, it will be resolved later */
9376 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9377 cur_arg++;
9378 }
9379 rule->act_prm.trk_ctr.expr = expr;
9380 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009381 } else if (strcmp(args[0], "redirect") == 0) {
9382 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009383 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009384
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009385 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009386 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9387 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9388 goto out_err;
9389 }
9390
9391 /* this redirect rule might already contain a parsed condition which
9392 * we'll pass to the http-request rule.
9393 */
9394 rule->action = HTTP_REQ_ACT_REDIR;
9395 rule->arg.redir = redir;
9396 rule->cond = redir->cond;
9397 redir->cond = NULL;
9398 cur_arg = 2;
9399 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009400 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9401 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9402 rule->action = HTTP_REQ_ACT_ADD_ACL;
9403 /*
9404 * '+ 8' for 'add-acl('
9405 * '- 9' for 'add-acl(' + trailing ')'
9406 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009407 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009408
9409 cur_arg = 1;
9410
9411 if (!*args[cur_arg] ||
9412 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9413 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9414 file, linenum, args[0]);
9415 goto out_err;
9416 }
9417
9418 LIST_INIT(&rule->arg.map.key);
9419 proxy->conf.args.ctx = ARGC_HRQ;
9420 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9421 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9422 file, linenum);
9423 free(proxy->conf.lfs_file);
9424 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9425 proxy->conf.lfs_line = proxy->conf.args.line;
9426 cur_arg += 1;
9427 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9428 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9429 rule->action = HTTP_REQ_ACT_DEL_ACL;
9430 /*
9431 * '+ 8' for 'del-acl('
9432 * '- 9' for 'del-acl(' + trailing ')'
9433 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009434 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009435
9436 cur_arg = 1;
9437
9438 if (!*args[cur_arg] ||
9439 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9440 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9441 file, linenum, args[0]);
9442 goto out_err;
9443 }
9444
9445 LIST_INIT(&rule->arg.map.key);
9446 proxy->conf.args.ctx = ARGC_HRQ;
9447 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9448 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9449 file, linenum);
9450 free(proxy->conf.lfs_file);
9451 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9452 proxy->conf.lfs_line = proxy->conf.args.line;
9453 cur_arg += 1;
9454 } else if (strncmp(args[0], "del-map", 7) == 0) {
9455 /* http-request del-map(<reference (map name)>) <key pattern> */
9456 rule->action = HTTP_REQ_ACT_DEL_MAP;
9457 /*
9458 * '+ 8' for 'del-map('
9459 * '- 9' for 'del-map(' + trailing ')'
9460 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009461 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009462
9463 cur_arg = 1;
9464
9465 if (!*args[cur_arg] ||
9466 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9467 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9468 file, linenum, args[0]);
9469 goto out_err;
9470 }
9471
9472 LIST_INIT(&rule->arg.map.key);
9473 proxy->conf.args.ctx = ARGC_HRQ;
9474 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9475 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9476 file, linenum);
9477 free(proxy->conf.lfs_file);
9478 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9479 proxy->conf.lfs_line = proxy->conf.args.line;
9480 cur_arg += 1;
9481 } else if (strncmp(args[0], "set-map", 7) == 0) {
9482 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9483 rule->action = HTTP_REQ_ACT_SET_MAP;
9484 /*
9485 * '+ 8' for 'set-map('
9486 * '- 9' for 'set-map(' + trailing ')'
9487 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009488 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009489
9490 cur_arg = 1;
9491
9492 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9493 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9494 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9495 file, linenum, args[0]);
9496 goto out_err;
9497 }
9498
9499 LIST_INIT(&rule->arg.map.key);
9500 LIST_INIT(&rule->arg.map.value);
9501 proxy->conf.args.ctx = ARGC_HRQ;
9502
9503 /* key pattern */
9504 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9505 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9506 file, linenum);
9507
9508 /* value pattern */
9509 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9510 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9511 file, linenum);
9512 free(proxy->conf.lfs_file);
9513 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9514 proxy->conf.lfs_line = proxy->conf.args.line;
9515
9516 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009517 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9518 char *errmsg = NULL;
9519 cur_arg = 1;
9520 /* try in the module list */
9521 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9522 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9523 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9524 free(errmsg);
9525 goto out_err;
9526 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009527 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009528 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 +01009529 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009530 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009531 }
9532
9533 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9534 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009535 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009536
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009537 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9538 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9539 file, linenum, args[0], errmsg);
9540 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009541 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009542 }
9543 rule->cond = cond;
9544 }
9545 else if (*args[cur_arg]) {
9546 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9547 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9548 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009549 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009550 }
9551
9552 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009553 out_err:
9554 free(rule);
9555 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009556}
9557
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009558/* parse an "http-respose" rule */
9559struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9560{
9561 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009562 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009563 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009564 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009565
9566 rule = calloc(1, sizeof(*rule));
9567 if (!rule) {
9568 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9569 goto out_err;
9570 }
9571
9572 if (!strcmp(args[0], "allow")) {
9573 rule->action = HTTP_RES_ACT_ALLOW;
9574 cur_arg = 1;
9575 } else if (!strcmp(args[0], "deny")) {
9576 rule->action = HTTP_RES_ACT_DENY;
9577 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009578 } else if (!strcmp(args[0], "set-nice")) {
9579 rule->action = HTTP_RES_ACT_SET_NICE;
9580 cur_arg = 1;
9581
9582 if (!*args[cur_arg] ||
9583 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9584 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9585 file, linenum, args[0]);
9586 goto out_err;
9587 }
9588 rule->arg.nice = atoi(args[cur_arg]);
9589 if (rule->arg.nice < -1024)
9590 rule->arg.nice = -1024;
9591 else if (rule->arg.nice > 1024)
9592 rule->arg.nice = 1024;
9593 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009594 } else if (!strcmp(args[0], "set-tos")) {
9595#ifdef IP_TOS
9596 char *err;
9597 rule->action = HTTP_RES_ACT_SET_TOS;
9598 cur_arg = 1;
9599
9600 if (!*args[cur_arg] ||
9601 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9602 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9603 file, linenum, args[0]);
9604 goto out_err;
9605 }
9606
9607 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9608 if (err && *err != '\0') {
9609 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9610 file, linenum, err, args[0]);
9611 goto out_err;
9612 }
9613 cur_arg++;
9614#else
9615 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9616 goto out_err;
9617#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009618 } else if (!strcmp(args[0], "set-mark")) {
9619#ifdef SO_MARK
9620 char *err;
9621 rule->action = HTTP_RES_ACT_SET_MARK;
9622 cur_arg = 1;
9623
9624 if (!*args[cur_arg] ||
9625 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9626 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9627 file, linenum, args[0]);
9628 goto out_err;
9629 }
9630
9631 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9632 if (err && *err != '\0') {
9633 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9634 file, linenum, err, args[0]);
9635 goto out_err;
9636 }
9637 cur_arg++;
9638 global.last_checks |= LSTCHK_NETADM;
9639#else
9640 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9641 goto out_err;
9642#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009643 } else if (!strcmp(args[0], "set-log-level")) {
9644 rule->action = HTTP_RES_ACT_SET_LOGL;
9645 cur_arg = 1;
9646
9647 if (!*args[cur_arg] ||
9648 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9649 bad_log_level:
9650 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9651 file, linenum, args[0]);
9652 goto out_err;
9653 }
9654 if (strcmp(args[cur_arg], "silent") == 0)
9655 rule->arg.loglevel = -1;
9656 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9657 goto bad_log_level;
9658 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009659 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9660 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9661 cur_arg = 1;
9662
9663 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9664 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9665 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9666 file, linenum, args[0]);
9667 goto out_err;
9668 }
9669
9670 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9671 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9672 LIST_INIT(&rule->arg.hdr_add.fmt);
9673
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009674 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009675 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009676 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9677 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009678 free(proxy->conf.lfs_file);
9679 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9680 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009681 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009682 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009683 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009684 cur_arg = 1;
9685
9686 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009687 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9688 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009689 file, linenum, args[0]);
9690 goto out_err;
9691 }
9692
9693 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9694 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9695 LIST_INIT(&rule->arg.hdr_add.fmt);
9696
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009697 error = NULL;
9698 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9699 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9700 args[cur_arg + 1], error);
9701 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009702 goto out_err;
9703 }
9704
9705 proxy->conf.args.ctx = ARGC_HRQ;
9706 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9707 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9708 file, linenum);
9709
9710 free(proxy->conf.lfs_file);
9711 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9712 proxy->conf.lfs_line = proxy->conf.args.line;
9713 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009714 } else if (strcmp(args[0], "del-header") == 0) {
9715 rule->action = HTTP_RES_ACT_DEL_HDR;
9716 cur_arg = 1;
9717
9718 if (!*args[cur_arg] ||
9719 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9720 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9721 file, linenum, args[0]);
9722 goto out_err;
9723 }
9724
9725 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9726 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9727
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009728 proxy->conf.args.ctx = ARGC_HRS;
9729 free(proxy->conf.lfs_file);
9730 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9731 proxy->conf.lfs_line = proxy->conf.args.line;
9732 cur_arg += 1;
9733 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9734 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9735 rule->action = HTTP_RES_ACT_ADD_ACL;
9736 /*
9737 * '+ 8' for 'add-acl('
9738 * '- 9' for 'add-acl(' + trailing ')'
9739 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009740 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009741
9742 cur_arg = 1;
9743
9744 if (!*args[cur_arg] ||
9745 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9746 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9747 file, linenum, args[0]);
9748 goto out_err;
9749 }
9750
9751 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009752 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009753 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9754 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9755 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009756 free(proxy->conf.lfs_file);
9757 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9758 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009759
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009760 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009761 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9762 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9763 rule->action = HTTP_RES_ACT_DEL_ACL;
9764 /*
9765 * '+ 8' for 'del-acl('
9766 * '- 9' for 'del-acl(' + trailing ')'
9767 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009768 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009769
9770 cur_arg = 1;
9771
9772 if (!*args[cur_arg] ||
9773 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9774 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9775 file, linenum, args[0]);
9776 goto out_err;
9777 }
9778
9779 LIST_INIT(&rule->arg.map.key);
9780 proxy->conf.args.ctx = ARGC_HRS;
9781 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9782 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9783 file, linenum);
9784 free(proxy->conf.lfs_file);
9785 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9786 proxy->conf.lfs_line = proxy->conf.args.line;
9787 cur_arg += 1;
9788 } else if (strncmp(args[0], "del-map", 7) == 0) {
9789 /* http-response del-map(<reference (map name)>) <key pattern> */
9790 rule->action = HTTP_RES_ACT_DEL_MAP;
9791 /*
9792 * '+ 8' for 'del-map('
9793 * '- 9' for 'del-map(' + trailing ')'
9794 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009795 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009796
9797 cur_arg = 1;
9798
9799 if (!*args[cur_arg] ||
9800 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9801 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9802 file, linenum, args[0]);
9803 goto out_err;
9804 }
9805
9806 LIST_INIT(&rule->arg.map.key);
9807 proxy->conf.args.ctx = ARGC_HRS;
9808 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9809 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9810 file, linenum);
9811 free(proxy->conf.lfs_file);
9812 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9813 proxy->conf.lfs_line = proxy->conf.args.line;
9814 cur_arg += 1;
9815 } else if (strncmp(args[0], "set-map", 7) == 0) {
9816 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9817 rule->action = HTTP_RES_ACT_SET_MAP;
9818 /*
9819 * '+ 8' for 'set-map('
9820 * '- 9' for 'set-map(' + trailing ')'
9821 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009822 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009823
9824 cur_arg = 1;
9825
9826 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9827 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9828 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9829 file, linenum, args[0]);
9830 goto out_err;
9831 }
9832
9833 LIST_INIT(&rule->arg.map.key);
9834 LIST_INIT(&rule->arg.map.value);
9835
9836 proxy->conf.args.ctx = ARGC_HRS;
9837
9838 /* key pattern */
9839 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9840 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9841 file, linenum);
9842
9843 /* value pattern */
9844 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9845 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9846 file, linenum);
9847
9848 free(proxy->conf.lfs_file);
9849 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9850 proxy->conf.lfs_line = proxy->conf.args.line;
9851
9852 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009853 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9854 char *errmsg = NULL;
9855 cur_arg = 1;
9856 /* try in the module list */
9857 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9858 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9859 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9860 free(errmsg);
9861 goto out_err;
9862 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009863 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009864 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 +02009865 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9866 goto out_err;
9867 }
9868
9869 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9870 struct acl_cond *cond;
9871 char *errmsg = NULL;
9872
9873 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9874 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9875 file, linenum, args[0], errmsg);
9876 free(errmsg);
9877 goto out_err;
9878 }
9879 rule->cond = cond;
9880 }
9881 else if (*args[cur_arg]) {
9882 Alert("parsing [%s:%d]: 'http-response %s' expects"
9883 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9884 file, linenum, args[0], args[cur_arg]);
9885 goto out_err;
9886 }
9887
9888 return rule;
9889 out_err:
9890 free(rule);
9891 return NULL;
9892}
9893
Willy Tarreau4baae242012-12-27 12:00:31 +01009894/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009895 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9896 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009897 */
9898struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009899 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009900{
9901 struct redirect_rule *rule;
9902 int cur_arg;
9903 int type = REDIRECT_TYPE_NONE;
9904 int code = 302;
9905 const char *destination = NULL;
9906 const char *cookie = NULL;
9907 int cookie_set = 0;
9908 unsigned int flags = REDIRECT_FLAG_NONE;
9909 struct acl_cond *cond = NULL;
9910
9911 cur_arg = 0;
9912 while (*(args[cur_arg])) {
9913 if (strcmp(args[cur_arg], "location") == 0) {
9914 if (!*args[cur_arg + 1])
9915 goto missing_arg;
9916
9917 type = REDIRECT_TYPE_LOCATION;
9918 cur_arg++;
9919 destination = args[cur_arg];
9920 }
9921 else if (strcmp(args[cur_arg], "prefix") == 0) {
9922 if (!*args[cur_arg + 1])
9923 goto missing_arg;
9924
9925 type = REDIRECT_TYPE_PREFIX;
9926 cur_arg++;
9927 destination = args[cur_arg];
9928 }
9929 else if (strcmp(args[cur_arg], "scheme") == 0) {
9930 if (!*args[cur_arg + 1])
9931 goto missing_arg;
9932
9933 type = REDIRECT_TYPE_SCHEME;
9934 cur_arg++;
9935 destination = args[cur_arg];
9936 }
9937 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9938 if (!*args[cur_arg + 1])
9939 goto missing_arg;
9940
9941 cur_arg++;
9942 cookie = args[cur_arg];
9943 cookie_set = 1;
9944 }
9945 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9946 if (!*args[cur_arg + 1])
9947 goto missing_arg;
9948
9949 cur_arg++;
9950 cookie = args[cur_arg];
9951 cookie_set = 0;
9952 }
9953 else if (strcmp(args[cur_arg], "code") == 0) {
9954 if (!*args[cur_arg + 1])
9955 goto missing_arg;
9956
9957 cur_arg++;
9958 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009959 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009960 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009961 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009962 args[cur_arg - 1], args[cur_arg]);
9963 return NULL;
9964 }
9965 }
9966 else if (!strcmp(args[cur_arg],"drop-query")) {
9967 flags |= REDIRECT_FLAG_DROP_QS;
9968 }
9969 else if (!strcmp(args[cur_arg],"append-slash")) {
9970 flags |= REDIRECT_FLAG_APPEND_SLASH;
9971 }
9972 else if (strcmp(args[cur_arg], "if") == 0 ||
9973 strcmp(args[cur_arg], "unless") == 0) {
9974 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9975 if (!cond) {
9976 memprintf(errmsg, "error in condition: %s", *errmsg);
9977 return NULL;
9978 }
9979 break;
9980 }
9981 else {
9982 memprintf(errmsg,
9983 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9984 args[cur_arg]);
9985 return NULL;
9986 }
9987 cur_arg++;
9988 }
9989
9990 if (type == REDIRECT_TYPE_NONE) {
9991 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9992 return NULL;
9993 }
9994
9995 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9996 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009997 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009998
9999 if (!use_fmt) {
10000 /* old-style static redirect rule */
10001 rule->rdr_str = strdup(destination);
10002 rule->rdr_len = strlen(destination);
10003 }
10004 else {
10005 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010006
10007 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10008 * if prefix == "/", we don't want to add anything, otherwise it
10009 * makes it hard for the user to configure a self-redirection.
10010 */
Godbachd9722032014-12-18 15:44:58 +080010011 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010012 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +010010013 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +010010014 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10015 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010016 free(curproxy->conf.lfs_file);
10017 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10018 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010019 }
10020 }
10021
Willy Tarreau4baae242012-12-27 12:00:31 +010010022 if (cookie) {
10023 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10024 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10025 */
10026 rule->cookie_len = strlen(cookie);
10027 if (cookie_set) {
10028 rule->cookie_str = malloc(rule->cookie_len + 10);
10029 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10030 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10031 rule->cookie_len += 9;
10032 } else {
10033 rule->cookie_str = malloc(rule->cookie_len + 21);
10034 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10035 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10036 rule->cookie_len += 20;
10037 }
10038 }
10039 rule->type = type;
10040 rule->code = code;
10041 rule->flags = flags;
10042 LIST_INIT(&rule->list);
10043 return rule;
10044
10045 missing_arg:
10046 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10047 return NULL;
10048}
10049
Willy Tarreau8797c062007-05-07 00:55:35 +020010050/************************************************************************/
10051/* The code below is dedicated to ACL parsing and matching */
10052/************************************************************************/
10053
10054
Willy Tarreau14174bc2012-04-16 14:34:04 +020010055/* This function ensures that the prerequisites for an L7 fetch are ready,
10056 * which means that a request or response is ready. If some data is missing,
10057 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010058 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10059 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010060 *
10061 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010062 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10063 * decide whether or not an HTTP message is present ;
10064 * 0 if the requested data cannot be fetched or if it is certain that
10065 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010066 * 1 if an HTTP message is ready
10067 */
10068static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010069smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010070 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010071{
Willy Tarreau192252e2015-04-04 01:47:55 +020010072 struct http_txn *txn;
10073 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010074
Willy Tarreaueee5b512015-04-03 23:46:31 +020010075 /* Note: this function may only be used from places where
10076 * http_init_txn() has already been done, and implies that <s>,
10077 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10078 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010079 */
10080
Willy Tarreau192252e2015-04-04 01:47:55 +020010081 if (!s)
10082 return 0;
10083 txn = s->txn;
10084
10085 if (!txn)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010086 return 0;
Willy Tarreau192252e2015-04-04 01:47:55 +020010087 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010088
10089 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +020010090 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010091
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010092 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010093 /* If the buffer does not leave enough free space at the end,
10094 * we must first realign it.
10095 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010096 if (s->req.buf->p > s->req.buf->data &&
10097 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10098 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010099
Willy Tarreau14174bc2012-04-16 14:34:04 +020010100 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010101 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010102 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010103
10104 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010105 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010106 http_msg_analyzer(msg, &txn->hdr_idx);
10107
10108 /* Still no valid request ? */
10109 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010110 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010111 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010112 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010113 }
10114 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010115 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010116 return 0;
10117 }
10118
10119 /* OK we just got a valid HTTP request. We have some minor
10120 * preparation to perform so that further checks can rely
10121 * on HTTP tests.
10122 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010123
10124 /* If the request was parsed but was too large, we must absolutely
10125 * return an error so that it is not processed. At the moment this
10126 * cannot happen, but if the parsers are to change in the future,
10127 * we want this check to be maintained.
10128 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010129 if (unlikely(s->req.buf->i + s->req.buf->p >
10130 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010131 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +020010132 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010133 return 1;
10134 }
10135
Willy Tarreau9b28e032012-10-12 23:49:43 +020010136 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010137 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010138 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010139
Willy Tarreau506d0502013-07-06 13:29:24 +020010140 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10141 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010142 }
10143
Willy Tarreau506d0502013-07-06 13:29:24 +020010144 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010145 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010146 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010147
10148 /* otherwise everything's ready for the request */
10149 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010150 else {
10151 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010152 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10153 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010154 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010155 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010156 }
10157
10158 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +020010159 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010160 return 1;
10161}
Willy Tarreau8797c062007-05-07 00:55:35 +020010162
Willy Tarreau6c616e02014-06-25 16:56:41 +020010163/* Note: these functinos *do* modify the sample. Even in case of success, at
10164 * least the type and uint value are modified.
10165 */
Willy Tarreauc0239e02012-04-16 14:42:55 +020010166#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau15e91e12015-04-04 00:52:09 +020010167 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 +020010168
Willy Tarreau24e32d82012-04-23 23:55:44 +020010169#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau15e91e12015-04-04 00:52:09 +020010170 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 +020010171
Willy Tarreau8797c062007-05-07 00:55:35 +020010172
10173/* 1. Check on METHOD
10174 * We use the pre-parsed method if it is known, and store its number as an
10175 * integer. If it is unknown, we use the pointer and the length.
10176 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010177static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010178{
10179 int len, meth;
10180
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010181 len = strlen(text);
10182 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010183
10184 pattern->val.i = meth;
10185 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010186 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010187 pattern->len = len;
10188 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010189 else {
10190 pattern->ptr.str = NULL;
10191 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010192 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010193 return 1;
10194}
10195
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010196/* This function fetches the method of current HTTP request and stores
10197 * it in the global pattern struct as a chunk. There are two possibilities :
10198 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10199 * in <len> and <ptr> is NULL ;
10200 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10201 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010202 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010203 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010204static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010205smp_fetch_meth(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010206 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010207{
10208 int meth;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010209 struct http_txn *txn = strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010210
Willy Tarreau24e32d82012-04-23 23:55:44 +020010211 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010212
Willy Tarreau8797c062007-05-07 00:55:35 +020010213 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010214 smp->type = SMP_T_METH;
10215 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010216 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010217 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10218 /* ensure the indexes are not affected */
10219 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010220 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010221 smp->data.meth.str.len = txn->req.sl.rq.m_l;
10222 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010223 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010224 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010225 return 1;
10226}
10227
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010228/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010229static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010230{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010231 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010232 struct pattern_list *lst;
10233 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010234
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010235 list_for_each_entry(lst, &expr->patterns, list) {
10236 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010237
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010238 /* well-known method */
10239 if (pattern->val.i != HTTP_METH_OTHER) {
10240 if (smp->data.meth.meth == pattern->val.i)
10241 return pattern;
10242 else
10243 continue;
10244 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010245
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010246 /* Other method, we must compare the strings */
10247 if (pattern->len != smp->data.meth.str.len)
10248 continue;
10249
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010250 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010251 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10252 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010253 return pattern;
10254 }
10255 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010256}
10257
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010258static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010259smp_fetch_rqver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010260 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010261{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010262 struct http_txn *txn = strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010263 char *ptr;
10264 int len;
10265
Willy Tarreauc0239e02012-04-16 14:42:55 +020010266 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010267
Willy Tarreau8797c062007-05-07 00:55:35 +020010268 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010269 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010270
10271 while ((len-- > 0) && (*ptr++ != '/'));
10272 if (len <= 0)
10273 return 0;
10274
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010275 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010276 smp->data.str.str = ptr;
10277 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010278
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010279 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010280 return 1;
10281}
10282
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010283static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010284smp_fetch_stver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010285 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010286{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010287 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010288 char *ptr;
10289 int len;
10290
Willy Tarreauc0239e02012-04-16 14:42:55 +020010291 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010292
Willy Tarreau15e91e12015-04-04 00:52:09 +020010293 txn = strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010294 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10295 return 0;
10296
Willy Tarreau8797c062007-05-07 00:55:35 +020010297 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010298 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010299
10300 while ((len-- > 0) && (*ptr++ != '/'));
10301 if (len <= 0)
10302 return 0;
10303
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010304 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010305 smp->data.str.str = ptr;
10306 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010307
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010308 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010309 return 1;
10310}
10311
10312/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010313static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010314smp_fetch_stcode(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010315 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010316{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010317 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010318 char *ptr;
10319 int len;
10320
Willy Tarreauc0239e02012-04-16 14:42:55 +020010321 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010322
Willy Tarreau15e91e12015-04-04 00:52:09 +020010323 txn = strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010324 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10325 return 0;
10326
Willy Tarreau8797c062007-05-07 00:55:35 +020010327 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010328 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010329
Willy Tarreauf853c462012-04-23 18:53:56 +020010330 smp->type = SMP_T_UINT;
10331 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010332 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010333 return 1;
10334}
10335
10336/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010337static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010338smp_fetch_url(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010339 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010340{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010341 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010342
Willy Tarreauc0239e02012-04-16 14:42:55 +020010343 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010344
Willy Tarreau15e91e12015-04-04 00:52:09 +020010345 txn = strm->txn;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010346 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010347 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010348 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010349 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010350 return 1;
10351}
10352
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010353static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010354smp_fetch_url_ip(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010355 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010356{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010357 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010358 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010359
Willy Tarreauc0239e02012-04-16 14:42:55 +020010360 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010361
Willy Tarreau15e91e12015-04-04 00:52:09 +020010362 txn = strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010363 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 +020010364 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010365 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010366
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010367 smp->type = SMP_T_IPV4;
10368 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010369 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010370 return 1;
10371}
10372
10373static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010374smp_fetch_url_port(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010375 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010376{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010377 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010378 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010379
Willy Tarreauc0239e02012-04-16 14:42:55 +020010380 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010381
Willy Tarreau15e91e12015-04-04 00:52:09 +020010382 txn = strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010383 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 +020010384 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10385 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010386
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010387 smp->type = SMP_T_UINT;
10388 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010389 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010390 return 1;
10391}
10392
Willy Tarreau185b5c42012-04-26 15:11:51 +020010393/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10394 * Accepts an optional argument of type string containing the header field name,
10395 * and an optional argument of type signed or unsigned integer to request an
10396 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010397 * headers are considered from the first one. It does not stop on commas and
10398 * returns full lines instead (useful for User-Agent or Date for example).
10399 */
10400static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010401smp_fetch_fhdr(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010402 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010403{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010404 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010405 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010406 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010407 int occ = 0;
10408 const char *name_str = NULL;
10409 int name_len = 0;
10410
10411 if (!ctx) {
10412 /* first call */
10413 ctx = &static_hdr_ctx;
10414 ctx->idx = 0;
10415 smp->ctx.a[0] = ctx;
10416 }
10417
10418 if (args) {
10419 if (args[0].type != ARGT_STR)
10420 return 0;
10421 name_str = args[0].data.str.str;
10422 name_len = args[0].data.str.len;
10423
10424 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10425 occ = args[1].data.uint;
10426 }
10427
10428 CHECK_HTTP_MESSAGE_FIRST();
10429
Willy Tarreau15e91e12015-04-04 00:52:09 +020010430 idx = &strm->txn->hdr_idx;
10431 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10432
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010433 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10434 /* search for header from the beginning */
10435 ctx->idx = 0;
10436
10437 if (!occ && !(opt & SMP_OPT_ITERATE))
10438 /* no explicit occurrence and single fetch => last header by default */
10439 occ = -1;
10440
10441 if (!occ)
10442 /* prepare to report multiple occurrences for ACL fetches */
10443 smp->flags |= SMP_F_NOT_LAST;
10444
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010445 smp->type = SMP_T_STR;
10446 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010447 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10448 return 1;
10449
10450 smp->flags &= ~SMP_F_NOT_LAST;
10451 return 0;
10452}
10453
10454/* 6. Check on HTTP header count. The number of occurrences is returned.
10455 * Accepts exactly 1 argument of type string. It does not stop on commas and
10456 * returns full lines instead (useful for User-Agent or Date for example).
10457 */
10458static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010459smp_fetch_fhdr_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010460 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010461{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010462 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010463 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010464 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010465 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010466 const char *name = NULL;
10467 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010468
Willy Tarreau601a4d12015-04-01 19:16:09 +020010469 if (args && args->type == ARGT_STR) {
10470 name = args->data.str.str;
10471 len = args->data.str.len;
10472 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010473
10474 CHECK_HTTP_MESSAGE_FIRST();
10475
Willy Tarreau15e91e12015-04-04 00:52:09 +020010476 idx = &strm->txn->hdr_idx;
10477 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10478
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010479 ctx.idx = 0;
10480 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010481 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010482 cnt++;
10483
10484 smp->type = SMP_T_UINT;
10485 smp->data.uint = cnt;
10486 smp->flags = SMP_F_VOL_HDR;
10487 return 1;
10488}
10489
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010490static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010491smp_fetch_hdr_names(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010492 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010493{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010494 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010495 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010496 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010497 struct chunk *temp;
10498 char del = ',';
10499
10500 if (args && args->type == ARGT_STR)
10501 del = *args[0].data.str.str;
10502
10503 CHECK_HTTP_MESSAGE_FIRST();
10504
Willy Tarreau15e91e12015-04-04 00:52:09 +020010505 idx = &strm->txn->hdr_idx;
10506 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10507
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010508 temp = get_trash_chunk();
10509
10510 ctx.idx = 0;
10511 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10512 if (temp->len)
10513 temp->str[temp->len++] = del;
10514 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10515 temp->len += ctx.del;
10516 }
10517
10518 smp->type = SMP_T_STR;
10519 smp->data.str.str = temp->str;
10520 smp->data.str.len = temp->len;
10521 smp->flags = SMP_F_VOL_HDR;
10522 return 1;
10523}
10524
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010525/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10526 * Accepts an optional argument of type string containing the header field name,
10527 * and an optional argument of type signed or unsigned integer to request an
10528 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010529 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010530 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010531static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010532smp_fetch_hdr(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010533 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010534{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010535 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010536 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010537 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010538 int occ = 0;
10539 const char *name_str = NULL;
10540 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010541
Willy Tarreaua890d072013-04-02 12:01:06 +020010542 if (!ctx) {
10543 /* first call */
10544 ctx = &static_hdr_ctx;
10545 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010546 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010547 }
10548
Willy Tarreau185b5c42012-04-26 15:11:51 +020010549 if (args) {
10550 if (args[0].type != ARGT_STR)
10551 return 0;
10552 name_str = args[0].data.str.str;
10553 name_len = args[0].data.str.len;
10554
10555 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10556 occ = args[1].data.uint;
10557 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010558
Willy Tarreaue333ec92012-04-16 16:26:40 +020010559 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010560
Willy Tarreau15e91e12015-04-04 00:52:09 +020010561 idx = &strm->txn->hdr_idx;
10562 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10563
Willy Tarreau185b5c42012-04-26 15:11:51 +020010564 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010565 /* search for header from the beginning */
10566 ctx->idx = 0;
10567
Willy Tarreau185b5c42012-04-26 15:11:51 +020010568 if (!occ && !(opt & SMP_OPT_ITERATE))
10569 /* no explicit occurrence and single fetch => last header by default */
10570 occ = -1;
10571
10572 if (!occ)
10573 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010574 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010575
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010576 smp->type = SMP_T_STR;
10577 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010578 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 +020010579 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010580
Willy Tarreau37406352012-04-23 16:16:37 +020010581 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010582 return 0;
10583}
10584
Willy Tarreauc11416f2007-06-17 16:58:38 +020010585/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010586 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010587 */
10588static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010589smp_fetch_hdr_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010590 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010591{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010592 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010593 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010594 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010595 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010596 const char *name = NULL;
10597 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010598
Willy Tarreau601a4d12015-04-01 19:16:09 +020010599 if (args && args->type == ARGT_STR) {
10600 name = args->data.str.str;
10601 len = args->data.str.len;
10602 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010603
Willy Tarreaue333ec92012-04-16 16:26:40 +020010604 CHECK_HTTP_MESSAGE_FIRST();
10605
Willy Tarreau15e91e12015-04-04 00:52:09 +020010606 idx = &strm->txn->hdr_idx;
10607 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10608
Willy Tarreau33a7e692007-06-10 19:45:56 +020010609 ctx.idx = 0;
10610 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010611 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010612 cnt++;
10613
Willy Tarreauf853c462012-04-23 18:53:56 +020010614 smp->type = SMP_T_UINT;
10615 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010616 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010617 return 1;
10618}
10619
Willy Tarreau185b5c42012-04-26 15:11:51 +020010620/* Fetch an HTTP header's integer value. The integer value is returned. It
10621 * takes a mandatory argument of type string and an optional one of type int
10622 * to designate a specific occurrence. It returns an unsigned integer, which
10623 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010624 */
10625static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010626smp_fetch_hdr_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010627 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010628{
Willy Tarreau192252e2015-04-04 01:47:55 +020010629 int ret = smp_fetch_hdr(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010630
Willy Tarreauf853c462012-04-23 18:53:56 +020010631 if (ret > 0) {
10632 smp->type = SMP_T_UINT;
10633 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10634 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010635
Willy Tarreaud53e2422012-04-16 17:21:11 +020010636 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010637}
10638
Cyril Bonté69fa9922012-10-25 00:01:06 +020010639/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10640 * and an optional one of type int to designate a specific occurrence.
10641 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010642 */
10643static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010644smp_fetch_hdr_ip(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010645 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010646{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010647 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010648
Willy Tarreau192252e2015-04-04 01:47:55 +020010649 while ((ret = smp_fetch_hdr(px, sess, strm, opt, args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010650 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10651 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010652 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010653 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010654 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010655 if (smp->data.str.len < temp->size - 1) {
10656 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10657 temp->str[smp->data.str.len] = '\0';
10658 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10659 smp->type = SMP_T_IPV6;
10660 break;
10661 }
10662 }
10663 }
10664
Willy Tarreaud53e2422012-04-16 17:21:11 +020010665 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010666 if (!(smp->flags & SMP_F_NOT_LAST))
10667 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010668 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010669 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010670}
10671
Willy Tarreau737b0c12007-06-10 21:28:46 +020010672/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10673 * the first '/' after the possible hostname, and ends before the possible '?'.
10674 */
10675static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010676smp_fetch_path(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010677 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010678{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010679 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010680 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010681
Willy Tarreauc0239e02012-04-16 14:42:55 +020010682 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010683
Willy Tarreau15e91e12015-04-04 00:52:09 +020010684 txn = strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010685 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010686 ptr = http_get_path(txn);
10687 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010688 return 0;
10689
10690 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010691 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010692 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010693
10694 while (ptr < end && *ptr != '?')
10695 ptr++;
10696
Willy Tarreauf853c462012-04-23 18:53:56 +020010697 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010698 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010699 return 1;
10700}
10701
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010702/* This produces a concatenation of the first occurrence of the Host header
10703 * followed by the path component if it begins with a slash ('/'). This means
10704 * that '*' will not be added, resulting in exactly the first Host entry.
10705 * If no Host header is found, then the path is returned as-is. The returned
10706 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010707 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010708 */
10709static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010710smp_fetch_base(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010711 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010712{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010713 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010714 char *ptr, *end, *beg;
10715 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010716 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010717
10718 CHECK_HTTP_MESSAGE_FIRST();
10719
Willy Tarreau15e91e12015-04-04 00:52:09 +020010720 txn = strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010721 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010722 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreau192252e2015-04-04 01:47:55 +020010723 return smp_fetch_path(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010724
10725 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010726 temp = get_trash_chunk();
10727 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010728 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010729 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010730 smp->data.str.len = ctx.vlen;
10731
10732 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010733 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010734 beg = http_get_path(txn);
10735 if (!beg)
10736 beg = end;
10737
10738 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10739
10740 if (beg < ptr && *beg == '/') {
10741 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10742 smp->data.str.len += ptr - beg;
10743 }
10744
10745 smp->flags = SMP_F_VOL_1ST;
10746 return 1;
10747}
10748
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010749/* This produces a 32-bit hash of the concatenation of the first occurrence of
10750 * the Host header followed by the path component if it begins with a slash ('/').
10751 * This means that '*' will not be added, resulting in exactly the first Host
10752 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010753 * is hashed using the path hash followed by a full avalanche hash and provides a
10754 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010755 * high-traffic sites without having to store whole paths.
10756 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010757int
Willy Tarreau192252e2015-04-04 01:47:55 +020010758smp_fetch_base32(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010759 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010760{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010761 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010762 struct hdr_ctx ctx;
10763 unsigned int hash = 0;
10764 char *ptr, *beg, *end;
10765 int len;
10766
10767 CHECK_HTTP_MESSAGE_FIRST();
10768
Willy Tarreau15e91e12015-04-04 00:52:09 +020010769 txn = strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010770 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010771 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010772 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10773 ptr = ctx.line + ctx.val;
10774 len = ctx.vlen;
10775 while (len--)
10776 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10777 }
10778
10779 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010780 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010781 beg = http_get_path(txn);
10782 if (!beg)
10783 beg = end;
10784
10785 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10786
10787 if (beg < ptr && *beg == '/') {
10788 while (beg < ptr)
10789 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10790 }
10791 hash = full_hash(hash);
10792
10793 smp->type = SMP_T_UINT;
10794 smp->data.uint = hash;
10795 smp->flags = SMP_F_VOL_1ST;
10796 return 1;
10797}
10798
Willy Tarreau4a550602012-12-09 14:53:32 +010010799/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010800 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10801 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10802 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010803 * that in environments where IPv6 is insignificant, truncating the output to
10804 * 8 bytes would still work.
10805 */
10806static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010807smp_fetch_base32_src(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010808 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010809{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010810 struct chunk *temp;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020010811 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010812
10813 if (!cli_conn)
10814 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010815
Willy Tarreau192252e2015-04-04 01:47:55 +020010816 if (!smp_fetch_base32(px, sess, strm, opt, args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010817 return 0;
10818
Willy Tarreau47ca5452012-12-23 20:22:19 +010010819 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010820 *(unsigned int *)temp->str = htonl(smp->data.uint);
10821 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010822
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010823 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010824 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010825 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010826 temp->len += 4;
10827 break;
10828 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010829 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010830 temp->len += 16;
10831 break;
10832 default:
10833 return 0;
10834 }
10835
10836 smp->data.str = *temp;
10837 smp->type = SMP_T_BIN;
10838 return 1;
10839}
10840
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010841/* Extracts the query string, which comes after the question mark '?'. If no
10842 * question mark is found, nothing is returned. Otherwise it returns a sample
10843 * of type string carrying the whole query string.
10844 */
10845static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010846smp_fetch_query(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010847 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010848{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010849 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010850 char *ptr, *end;
10851
10852 CHECK_HTTP_MESSAGE_FIRST();
10853
Willy Tarreau15e91e12015-04-04 00:52:09 +020010854 txn = strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010855 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10856 end = ptr + txn->req.sl.rq.u_l;
10857
10858 /* look up the '?' */
10859 do {
10860 if (ptr == end)
10861 return 0;
10862 } while (*ptr++ != '?');
10863
10864 smp->type = SMP_T_STR;
10865 smp->data.str.str = ptr;
10866 smp->data.str.len = end - ptr;
10867 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10868 return 1;
10869}
10870
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010871static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010872smp_fetch_proto_http(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010873 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010874{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010875 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10876 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10877 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010878
Willy Tarreau24e32d82012-04-23 23:55:44 +020010879 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010880
Willy Tarreauf853c462012-04-23 18:53:56 +020010881 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010882 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010883 return 1;
10884}
10885
Willy Tarreau7f18e522010-10-22 20:04:13 +020010886/* return a valid test if the current request is the first one on the connection */
10887static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010888smp_fetch_http_first_req(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)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010890{
Willy Tarreauf853c462012-04-23 18:53:56 +020010891 smp->type = SMP_T_BOOL;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010892 smp->data.uint = !(strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010893 return 1;
10894}
10895
Willy Tarreau34db1082012-04-19 17:16:54 +020010896/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010897static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010898smp_fetch_http_auth(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010899 const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010900{
10901
Willy Tarreau24e32d82012-04-23 23:55:44 +020010902 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010903 return 0;
10904
Willy Tarreauc0239e02012-04-16 14:42:55 +020010905 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010906
Willy Tarreau15e91e12015-04-04 00:52:09 +020010907 if (!get_http_auth(strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010908 return 0;
10909
Willy Tarreauf853c462012-04-23 18:53:56 +020010910 smp->type = SMP_T_BOOL;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010911 smp->data.uint = check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010912 return 1;
10913}
Willy Tarreau8797c062007-05-07 00:55:35 +020010914
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010915/* Accepts exactly 1 argument of type userlist */
10916static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010917smp_fetch_http_auth_grp(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010918 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010919{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010920 if (!args || args->type != ARGT_USR)
10921 return 0;
10922
10923 CHECK_HTTP_MESSAGE_FIRST();
10924
Willy Tarreau15e91e12015-04-04 00:52:09 +020010925 if (!get_http_auth(strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010926 return 0;
10927
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010928 /* if the user does not belong to the userlist or has a wrong password,
10929 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010930 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010931 */
Willy Tarreau15e91e12015-04-04 00:52:09 +020010932 if (!check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010933 return 0;
10934
10935 /* pat_match_auth() will need the user list */
10936 smp->ctx.a[0] = args->data.usr;
10937
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010938 smp->type = SMP_T_STR;
10939 smp->flags = SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010940 smp->data.str.str = strm->txn->auth.user;
10941 smp->data.str.len = strlen(strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010942
10943 return 1;
10944}
10945
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010946/* Try to find the next occurrence of a cookie name in a cookie header value.
10947 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10948 * the cookie value is returned into *value and *value_l, and the function
10949 * returns a pointer to the next pointer to search from if the value was found.
10950 * Otherwise if the cookie was not found, NULL is returned and neither value
10951 * nor value_l are touched. The input <hdr> string should first point to the
10952 * header's value, and the <hdr_end> pointer must point to the first character
10953 * not part of the value. <list> must be non-zero if value may represent a list
10954 * of values (cookie headers). This makes it faster to abort parsing when no
10955 * list is expected.
10956 */
10957static char *
10958extract_cookie_value(char *hdr, const char *hdr_end,
10959 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010960 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010961{
10962 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10963 char *next;
10964
10965 /* we search at least a cookie name followed by an equal, and more
10966 * generally something like this :
10967 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10968 */
10969 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10970 /* Iterate through all cookies on this line */
10971
10972 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10973 att_beg++;
10974
10975 /* find att_end : this is the first character after the last non
10976 * space before the equal. It may be equal to hdr_end.
10977 */
10978 equal = att_end = att_beg;
10979
10980 while (equal < hdr_end) {
10981 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10982 break;
10983 if (http_is_spht[(unsigned char)*equal++])
10984 continue;
10985 att_end = equal;
10986 }
10987
10988 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10989 * is between <att_beg> and <equal>, both may be identical.
10990 */
10991
10992 /* look for end of cookie if there is an equal sign */
10993 if (equal < hdr_end && *equal == '=') {
10994 /* look for the beginning of the value */
10995 val_beg = equal + 1;
10996 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10997 val_beg++;
10998
10999 /* find the end of the value, respecting quotes */
11000 next = find_cookie_value_end(val_beg, hdr_end);
11001
11002 /* make val_end point to the first white space or delimitor after the value */
11003 val_end = next;
11004 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
11005 val_end--;
11006 } else {
11007 val_beg = val_end = next = equal;
11008 }
11009
11010 /* We have nothing to do with attributes beginning with '$'. However,
11011 * they will automatically be removed if a header before them is removed,
11012 * since they're supposed to be linked together.
11013 */
11014 if (*att_beg == '$')
11015 continue;
11016
11017 /* Ignore cookies with no equal sign */
11018 if (equal == next)
11019 continue;
11020
11021 /* Now we have the cookie name between att_beg and att_end, and
11022 * its value between val_beg and val_end.
11023 */
11024
11025 if (att_end - att_beg == cookie_name_l &&
11026 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11027 /* let's return this value and indicate where to go on from */
11028 *value = val_beg;
11029 *value_l = val_end - val_beg;
11030 return next + 1;
11031 }
11032
11033 /* Set-Cookie headers only have the name in the first attr=value part */
11034 if (!list)
11035 break;
11036 }
11037
11038 return NULL;
11039}
11040
William Lallemanda43ba4e2014-01-28 18:14:25 +010011041/* Fetch a captured HTTP request header. The index is the position of
11042 * the "capture" option in the configuration file
11043 */
11044static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011045smp_fetch_capture_header_req(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011046 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011047{
Willy Tarreaud0d8da92015-04-04 02:10:38 +020011048 struct proxy *fe = strm_fe(strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011049 int idx;
11050
11051 if (!args || args->type != ARGT_UINT)
11052 return 0;
11053
11054 idx = args->data.uint;
11055
Willy Tarreau15e91e12015-04-04 00:52:09 +020011056 if (idx > (fe->nb_req_cap - 1) || strm->req_cap == NULL || strm->req_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011057 return 0;
11058
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011059 smp->type = SMP_T_STR;
11060 smp->flags |= SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011061 smp->data.str.str = strm->req_cap[idx];
11062 smp->data.str.len = strlen(strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011063
11064 return 1;
11065}
11066
11067/* Fetch a captured HTTP response header. The index is the position of
11068 * the "capture" option in the configuration file
11069 */
11070static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011071smp_fetch_capture_header_res(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011072 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011073{
Willy Tarreaud0d8da92015-04-04 02:10:38 +020011074 struct proxy *fe = strm_fe(strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011075 int idx;
11076
11077 if (!args || args->type != ARGT_UINT)
11078 return 0;
11079
11080 idx = args->data.uint;
11081
Willy Tarreau15e91e12015-04-04 00:52:09 +020011082 if (idx > (fe->nb_rsp_cap - 1) || strm->res_cap == NULL || strm->res_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011083 return 0;
11084
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011085 smp->type = SMP_T_STR;
11086 smp->flags |= SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011087 smp->data.str.str = strm->res_cap[idx];
11088 smp->data.str.len = strlen(strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011089
11090 return 1;
11091}
11092
William Lallemand65ad6e12014-01-31 15:08:02 +010011093/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11094static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011095smp_fetch_capture_req_method(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011096 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011097{
11098 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011099 struct http_txn *txn = strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011100 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011101
Willy Tarreau15e91e12015-04-04 00:52:09 +020011102 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011103 return 0;
11104
William Lallemand96a77852014-02-05 00:30:02 +010011105 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011106
William Lallemand96a77852014-02-05 00:30:02 +010011107 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11108 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011109
William Lallemand96a77852014-02-05 00:30:02 +010011110 temp = get_trash_chunk();
11111 temp->str = txn->uri;
11112 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011113 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011114 smp->type = SMP_T_STR;
11115 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011116
11117 return 1;
11118
11119}
11120
11121/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11122static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011123smp_fetch_capture_req_uri(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011124 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011125{
11126 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011127 struct http_txn *txn = strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011128 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011129
Willy Tarreau15e91e12015-04-04 00:52:09 +020011130 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011131 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011132
William Lallemand65ad6e12014-01-31 15:08:02 +010011133 ptr = txn->uri;
11134
11135 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11136 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011137
William Lallemand65ad6e12014-01-31 15:08:02 +010011138 if (!*ptr)
11139 return 0;
11140
11141 ptr++; /* skip the space */
11142
11143 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011144 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011145 if (!ptr)
11146 return 0;
11147 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11148 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011149
11150 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010011151 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011152 smp->type = SMP_T_STR;
11153 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011154
11155 return 1;
11156}
11157
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011158/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11159 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11160 */
11161static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011162smp_fetch_capture_req_ver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011163 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011164{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011165 struct http_txn *txn = strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011166
Willy Tarreau15e91e12015-04-04 00:52:09 +020011167 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011168 return 0;
11169
11170 if (txn->req.flags & HTTP_MSGF_VER_11)
11171 smp->data.str.str = "HTTP/1.1";
11172 else
11173 smp->data.str.str = "HTTP/1.0";
11174
11175 smp->data.str.len = 8;
11176 smp->type = SMP_T_STR;
11177 smp->flags = SMP_F_CONST;
11178 return 1;
11179
11180}
11181
11182/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11183 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11184 */
11185static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011186smp_fetch_capture_res_ver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011187 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011188{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011189 struct http_txn *txn = strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011190
Willy Tarreau15e91e12015-04-04 00:52:09 +020011191 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011192 return 0;
11193
11194 if (txn->rsp.flags & HTTP_MSGF_VER_11)
11195 smp->data.str.str = "HTTP/1.1";
11196 else
11197 smp->data.str.str = "HTTP/1.0";
11198
11199 smp->data.str.len = 8;
11200 smp->type = SMP_T_STR;
11201 smp->flags = SMP_F_CONST;
11202 return 1;
11203
11204}
11205
William Lallemand65ad6e12014-01-31 15:08:02 +010011206
Willy Tarreaue333ec92012-04-16 16:26:40 +020011207/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011208 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011209 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011210 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011211 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011212 * Accepts exactly 1 argument of type string. If the input options indicate
11213 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011214 * The returned sample is of type CSTR. Can be used to parse cookies in other
11215 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011216 */
Willy Tarreau192252e2015-04-04 01:47:55 +020011217int smp_fetch_cookie(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011218 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011219{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011220 struct http_txn *txn;
11221 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011222 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011223 const struct http_msg *msg;
11224 const char *hdr_name;
11225 int hdr_name_len;
11226 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011227 int occ = 0;
11228 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011229
Willy Tarreau24e32d82012-04-23 23:55:44 +020011230 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011231 return 0;
11232
Willy Tarreaua890d072013-04-02 12:01:06 +020011233 if (!ctx) {
11234 /* first call */
11235 ctx = &static_hdr_ctx;
11236 ctx->idx = 0;
11237 smp->ctx.a[2] = ctx;
11238 }
11239
Willy Tarreaue333ec92012-04-16 16:26:40 +020011240 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011241
Willy Tarreau15e91e12015-04-04 00:52:09 +020011242 txn = strm->txn;
11243 idx = &strm->txn->hdr_idx;
11244
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011245 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011246 msg = &txn->req;
11247 hdr_name = "Cookie";
11248 hdr_name_len = 6;
11249 } else {
11250 msg = &txn->rsp;
11251 hdr_name = "Set-Cookie";
11252 hdr_name_len = 10;
11253 }
11254
Willy Tarreau28376d62012-04-26 21:26:10 +020011255 if (!occ && !(opt & SMP_OPT_ITERATE))
11256 /* no explicit occurrence and single fetch => last cookie by default */
11257 occ = -1;
11258
11259 /* OK so basically here, either we want only one value and it's the
11260 * last one, or we want to iterate over all of them and we fetch the
11261 * next one.
11262 */
11263
Willy Tarreau9b28e032012-10-12 23:49:43 +020011264 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011265 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011266 /* search for the header from the beginning, we must first initialize
11267 * the search parameters.
11268 */
Willy Tarreau37406352012-04-23 16:16:37 +020011269 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011270 ctx->idx = 0;
11271 }
11272
Willy Tarreau28376d62012-04-26 21:26:10 +020011273 smp->flags |= SMP_F_VOL_HDR;
11274
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011275 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011276 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11277 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011278 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11279 goto out;
11280
Willy Tarreau24e32d82012-04-23 23:55:44 +020011281 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011282 continue;
11283
Willy Tarreau37406352012-04-23 16:16:37 +020011284 smp->ctx.a[0] = ctx->line + ctx->val;
11285 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011286 }
11287
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011288 smp->type = SMP_T_STR;
11289 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011290 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011291 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011292 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011293 &smp->data.str.str,
11294 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011295 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011296 found = 1;
11297 if (occ >= 0) {
11298 /* one value was returned into smp->data.str.{str,len} */
11299 smp->flags |= SMP_F_NOT_LAST;
11300 return 1;
11301 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011302 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011303 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011304 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011305 /* all cookie headers and values were scanned. If we're looking for the
11306 * last occurrence, we may return it now.
11307 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011308 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011309 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011310 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011311}
11312
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011313/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011314 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011315 * multiple cookies may be parsed on the same line. The returned sample is of
11316 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011317 */
11318static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011319smp_fetch_cookie_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011320 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011321{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011322 struct http_txn *txn;
11323 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011324 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011325 const struct http_msg *msg;
11326 const char *hdr_name;
11327 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011328 int cnt;
11329 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011330 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011331
Willy Tarreau24e32d82012-04-23 23:55:44 +020011332 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011333 return 0;
11334
Willy Tarreaue333ec92012-04-16 16:26:40 +020011335 CHECK_HTTP_MESSAGE_FIRST();
11336
Willy Tarreau15e91e12015-04-04 00:52:09 +020011337 txn = strm->txn;
11338 idx = &strm->txn->hdr_idx;
11339
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011340 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011341 msg = &txn->req;
11342 hdr_name = "Cookie";
11343 hdr_name_len = 6;
11344 } else {
11345 msg = &txn->rsp;
11346 hdr_name = "Set-Cookie";
11347 hdr_name_len = 10;
11348 }
11349
Willy Tarreau9b28e032012-10-12 23:49:43 +020011350 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011351 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011352 ctx.idx = 0;
11353 cnt = 0;
11354
11355 while (1) {
11356 /* Note: val_beg == NULL every time we need to fetch a new header */
11357 if (!val_beg) {
11358 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11359 break;
11360
Willy Tarreau24e32d82012-04-23 23:55:44 +020011361 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011362 continue;
11363
11364 val_beg = ctx.line + ctx.val;
11365 val_end = val_beg + ctx.vlen;
11366 }
11367
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011368 smp->type = SMP_T_STR;
11369 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011370 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011371 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011372 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011373 &smp->data.str.str,
11374 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011375 cnt++;
11376 }
11377 }
11378
Willy Tarreaub169eba2013-12-16 15:14:43 +010011379 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011380 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011381 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011382 return 1;
11383}
11384
Willy Tarreau51539362012-05-08 12:46:28 +020011385/* Fetch an cookie's integer value. The integer value is returned. It
11386 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11387 */
11388static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011389smp_fetch_cookie_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011390 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011391{
Willy Tarreau192252e2015-04-04 01:47:55 +020011392 int ret = smp_fetch_cookie(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011393
11394 if (ret > 0) {
11395 smp->type = SMP_T_UINT;
11396 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11397 }
11398
11399 return ret;
11400}
11401
Willy Tarreau8797c062007-05-07 00:55:35 +020011402/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011403/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011404/************************************************************************/
11405
David Cournapeau16023ee2010-12-23 20:55:41 +090011406/*
11407 * Given a path string and its length, find the position of beginning of the
11408 * query string. Returns NULL if no query string is found in the path.
11409 *
11410 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11411 *
11412 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11413 */
bedis4c75cca2012-10-05 08:38:24 +020011414static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011415{
11416 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011417
bedis4c75cca2012-10-05 08:38:24 +020011418 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011419 return p ? p + 1 : NULL;
11420}
11421
bedis4c75cca2012-10-05 08:38:24 +020011422static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011423{
bedis4c75cca2012-10-05 08:38:24 +020011424 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011425}
11426
11427/*
11428 * Given a url parameter, find the starting position of the first occurence,
11429 * or NULL if the parameter is not found.
11430 *
11431 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11432 * the function will return query_string+8.
11433 */
11434static char*
11435find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011436 char* url_param_name, size_t url_param_name_l,
11437 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011438{
11439 char *pos, *last;
11440
11441 pos = query_string;
11442 last = query_string + query_string_l - url_param_name_l - 1;
11443
11444 while (pos <= last) {
11445 if (pos[url_param_name_l] == '=') {
11446 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11447 return pos;
11448 pos += url_param_name_l + 1;
11449 }
bedis4c75cca2012-10-05 08:38:24 +020011450 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011451 pos++;
11452 pos++;
11453 }
11454 return NULL;
11455}
11456
11457/*
11458 * Given a url parameter name, returns its value and size into *value and
11459 * *value_l respectively, and returns non-zero. If the parameter is not found,
11460 * zero is returned and value/value_l are not touched.
11461 */
11462static int
11463find_url_param_value(char* path, size_t path_l,
11464 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011465 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011466{
11467 char *query_string, *qs_end;
11468 char *arg_start;
11469 char *value_start, *value_end;
11470
bedis4c75cca2012-10-05 08:38:24 +020011471 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011472 if (!query_string)
11473 return 0;
11474
11475 qs_end = path + path_l;
11476 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011477 url_param_name, url_param_name_l,
11478 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011479 if (!arg_start)
11480 return 0;
11481
11482 value_start = arg_start + url_param_name_l + 1;
11483 value_end = value_start;
11484
bedis4c75cca2012-10-05 08:38:24 +020011485 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011486 value_end++;
11487
11488 *value = value_start;
11489 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011490 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011491}
11492
11493static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011494smp_fetch_url_param(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011495 const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011496{
bedis4c75cca2012-10-05 08:38:24 +020011497 char delim = '?';
Willy Tarreau15e91e12015-04-04 00:52:09 +020011498 struct http_msg *msg;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011499
bedis4c75cca2012-10-05 08:38:24 +020011500 if (!args || args[0].type != ARGT_STR ||
11501 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011502 return 0;
11503
11504 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011505
Willy Tarreau15e91e12015-04-04 00:52:09 +020011506 msg = &strm->txn->req;
11507
bedis4c75cca2012-10-05 08:38:24 +020011508 if (args[1].type)
11509 delim = *args[1].data.str.str;
11510
Willy Tarreau9b28e032012-10-12 23:49:43 +020011511 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011512 args->data.str.str, args->data.str.len,
11513 &smp->data.str.str, &smp->data.str.len,
11514 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011515 return 0;
11516
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011517 smp->type = SMP_T_STR;
11518 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011519 return 1;
11520}
11521
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011522/* Return the signed integer value for the specified url parameter (see url_param
11523 * above).
11524 */
11525static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011526smp_fetch_url_param_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011527 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011528{
Willy Tarreau192252e2015-04-04 01:47:55 +020011529 int ret = smp_fetch_url_param(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011530
11531 if (ret > 0) {
11532 smp->type = SMP_T_UINT;
11533 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11534 }
11535
11536 return ret;
11537}
11538
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011539/* This produces a 32-bit hash of the concatenation of the first occurrence of
11540 * the Host header followed by the path component if it begins with a slash ('/').
11541 * This means that '*' will not be added, resulting in exactly the first Host
11542 * entry. If no Host header is found, then the path is used. The resulting value
11543 * is hashed using the url hash followed by a full avalanche hash and provides a
11544 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11545 * high-traffic sites without having to store whole paths.
11546 * this differs from the base32 functions in that it includes the url parameters
11547 * as well as the path
11548 */
11549static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011550smp_fetch_url32(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011551 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011552{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011553 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011554 struct hdr_ctx ctx;
11555 unsigned int hash = 0;
11556 char *ptr, *beg, *end;
11557 int len;
11558
11559 CHECK_HTTP_MESSAGE_FIRST();
11560
Willy Tarreau15e91e12015-04-04 00:52:09 +020011561 txn = strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011562 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011563 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011564 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11565 ptr = ctx.line + ctx.val;
11566 len = ctx.vlen;
11567 while (len--)
11568 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11569 }
11570
11571 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011572 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 +000011573 beg = http_get_path(txn);
11574 if (!beg)
11575 beg = end;
11576
11577 for (ptr = beg; ptr < end ; ptr++);
11578
11579 if (beg < ptr && *beg == '/') {
11580 while (beg < ptr)
11581 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11582 }
11583 hash = full_hash(hash);
11584
11585 smp->type = SMP_T_UINT;
11586 smp->data.uint = hash;
11587 smp->flags = SMP_F_VOL_1ST;
11588 return 1;
11589}
11590
11591/* This concatenates the source address with the 32-bit hash of the Host and
11592 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11593 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11594 * on the source address length. The URL hash is stored before the address so
11595 * that in environments where IPv6 is insignificant, truncating the output to
11596 * 8 bytes would still work.
11597 */
11598static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011599smp_fetch_url32_src(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011600 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011601{
11602 struct chunk *temp;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020011603 struct connection *cli_conn = objt_conn(sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011604
Willy Tarreau192252e2015-04-04 01:47:55 +020011605 if (!smp_fetch_url32(px, sess, strm, opt, args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011606 return 0;
11607
11608 temp = get_trash_chunk();
11609 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11610 temp->len += sizeof(smp->data.uint);
11611
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011612 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011613 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011614 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011615 temp->len += 4;
11616 break;
11617 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011618 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011619 temp->len += 16;
11620 break;
11621 default:
11622 return 0;
11623 }
11624
11625 smp->data.str = *temp;
11626 smp->type = SMP_T_BIN;
11627 return 1;
11628}
11629
Willy Tarreau185b5c42012-04-26 15:11:51 +020011630/* This function is used to validate the arguments passed to any "hdr" fetch
11631 * keyword. These keywords support an optional positive or negative occurrence
11632 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11633 * is assumed that the types are already the correct ones. Returns 0 on error,
11634 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11635 * error message in case of error, that the caller is responsible for freeing.
11636 * The initial location must either be freeable or NULL.
11637 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011638int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011639{
11640 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011641 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011642 return 0;
11643 }
11644 return 1;
11645}
11646
Willy Tarreau276fae92013-07-25 14:36:01 +020011647/* takes an UINT value on input supposed to represent the time since EPOCH,
11648 * adds an optional offset found in args[0] and emits a string representing
11649 * the date in RFC-1123/5322 format.
11650 */
Willy Tarreau87b09662015-04-03 00:22:06 +020011651static int sample_conv_http_date(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011652 struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011653{
11654 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11655 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11656 struct chunk *temp;
11657 struct tm *tm;
11658 time_t curr_date = smp->data.uint;
11659
11660 /* add offset */
11661 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11662 curr_date += args[0].data.sint;
11663
11664 tm = gmtime(&curr_date);
11665
11666 temp = get_trash_chunk();
11667 temp->len = snprintf(temp->str, temp->size - temp->len,
11668 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11669 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11670 tm->tm_hour, tm->tm_min, tm->tm_sec);
11671
11672 smp->data.str = *temp;
11673 smp->type = SMP_T_STR;
11674 return 1;
11675}
11676
Thierry FOURNIERad903512014-04-11 17:51:01 +020011677/* Match language range with language tag. RFC2616 14.4:
11678 *
11679 * A language-range matches a language-tag if it exactly equals
11680 * the tag, or if it exactly equals a prefix of the tag such
11681 * that the first tag character following the prefix is "-".
11682 *
11683 * Return 1 if the strings match, else return 0.
11684 */
11685static inline int language_range_match(const char *range, int range_len,
11686 const char *tag, int tag_len)
11687{
11688 const char *end = range + range_len;
11689 const char *tend = tag + tag_len;
11690 while (range < end) {
11691 if (*range == '-' && tag == tend)
11692 return 1;
11693 if (*range != *tag || tag == tend)
11694 return 0;
11695 range++;
11696 tag++;
11697 }
11698 /* Return true only if the last char of the tag is matched. */
11699 return tag == tend;
11700}
11701
11702/* Arguments: The list of expected value, the number of parts returned and the separator */
Willy Tarreau87b09662015-04-03 00:22:06 +020011703static int sample_conv_q_prefered(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011704 struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011705{
11706 const char *al = smp->data.str.str;
11707 const char *end = al + smp->data.str.len;
11708 const char *token;
11709 int toklen;
11710 int qvalue;
11711 const char *str;
11712 const char *w;
11713 int best_q = 0;
11714
11715 /* Set the constant to the sample, because the output of the
11716 * function will be peek in the constant configuration string.
11717 */
11718 smp->flags |= SMP_F_CONST;
11719 smp->data.str.size = 0;
11720 smp->data.str.str = "";
11721 smp->data.str.len = 0;
11722
11723 /* Parse the accept language */
11724 while (1) {
11725
11726 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011727 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011728 al++;
11729 if (al >= end)
11730 break;
11731
11732 /* Start of the fisrt word. */
11733 token = al;
11734
11735 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011736 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011737 al++;
11738 if (al == token)
11739 goto expect_comma;
11740
11741 /* Length of the token. */
11742 toklen = al - token;
11743 qvalue = 1000;
11744
11745 /* Check if the token exists in the list. If the token not exists,
11746 * jump to the next token.
11747 */
11748 str = args[0].data.str.str;
11749 w = str;
11750 while (1) {
11751 if (*str == ';' || *str == '\0') {
11752 if (language_range_match(token, toklen, w, str-w))
11753 goto look_for_q;
11754 if (*str == '\0')
11755 goto expect_comma;
11756 w = str + 1;
11757 }
11758 str++;
11759 }
11760 goto expect_comma;
11761
11762look_for_q:
11763
11764 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011765 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011766 al++;
11767 if (al >= end)
11768 goto process_value;
11769
11770 /* If ',' is found, process the result */
11771 if (*al == ',')
11772 goto process_value;
11773
11774 /* If the character is different from ';', look
11775 * for the end of the header part in best effort.
11776 */
11777 if (*al != ';')
11778 goto expect_comma;
11779
11780 /* Assumes that the char is ';', now expect "q=". */
11781 al++;
11782
11783 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011784 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011785 al++;
11786 if (al >= end)
11787 goto process_value;
11788
11789 /* Expect 'q'. If no 'q', continue in best effort */
11790 if (*al != 'q')
11791 goto process_value;
11792 al++;
11793
11794 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011795 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011796 al++;
11797 if (al >= end)
11798 goto process_value;
11799
11800 /* Expect '='. If no '=', continue in best effort */
11801 if (*al != '=')
11802 goto process_value;
11803 al++;
11804
11805 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011806 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011807 al++;
11808 if (al >= end)
11809 goto process_value;
11810
11811 /* Parse the q value. */
11812 qvalue = parse_qvalue(al, &al);
11813
11814process_value:
11815
11816 /* If the new q value is the best q value, then store the associated
11817 * language in the response. If qvalue is the biggest value (1000),
11818 * break the process.
11819 */
11820 if (qvalue > best_q) {
11821 smp->data.str.str = (char *)w;
11822 smp->data.str.len = str - w;
11823 if (qvalue >= 1000)
11824 break;
11825 best_q = qvalue;
11826 }
11827
11828expect_comma:
11829
11830 /* Expect comma or end. If the end is detected, quit the loop. */
11831 while (al < end && *al != ',')
11832 al++;
11833 if (al >= end)
11834 break;
11835
11836 /* Comma is found, jump it and restart the analyzer. */
11837 al++;
11838 }
11839
11840 /* Set default value if required. */
11841 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11842 smp->data.str.str = args[1].data.str.str;
11843 smp->data.str.len = args[1].data.str.len;
11844 }
11845
11846 /* Return true only if a matching language was found. */
11847 return smp->data.str.len != 0;
11848}
11849
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011850/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011851 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011852 * the relevant part of the request line accordingly. Then it updates various
11853 * pointers to the next elements which were moved, and the total buffer length.
11854 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011855 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11856 * error, though this can be revisited when this code is finally exploited.
11857 *
11858 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11859 * query string and 3 to replace uri.
11860 *
11861 * In query string case, the mark question '?' must be set at the start of the
11862 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011863 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011864int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011865 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011866{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011867 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011868 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011869 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011870 int delta;
11871
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011872 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011873 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011874 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011875 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11876
11877 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011878 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011879 txn->req.sl.rq.m_l += delta;
11880 txn->req.sl.rq.u += delta;
11881 txn->req.sl.rq.v += delta;
11882 break;
11883
11884 case 1: // path
11885 cur_ptr = http_get_path(txn);
11886 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011887 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011888
11889 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011890 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 +010011891 cur_end++;
11892
11893 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011894 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011895 txn->req.sl.rq.u_l += delta;
11896 txn->req.sl.rq.v += delta;
11897 break;
11898
11899 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011900 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011901 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011902 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11903 while (cur_ptr < cur_end && *cur_ptr != '?')
11904 cur_ptr++;
11905
11906 /* skip the question mark or indicate that we must insert it
11907 * (but only if the format string is not empty then).
11908 */
11909 if (cur_ptr < cur_end)
11910 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011911 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011912 offset = 0;
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 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011921 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011922 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11923
11924 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011925 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011926 txn->req.sl.rq.u_l += delta;
11927 txn->req.sl.rq.v += delta;
11928 break;
11929
11930 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011931 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011932 }
11933
11934 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011935 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011936 txn->req.sl.rq.l += delta;
11937 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011938 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011939 return 0;
11940}
11941
11942/* This function executes one of the set-{method,path,query,uri} actions. It
11943 * builds a string in the trash from the specified format string. It finds
11944 * the action to be performed in p[2], previously filled by function
11945 * parse_set_req_line(). The replacement action is excuted by the function
11946 * http_action_set_req_line_exec(). It always returns 1. If an error occurs
11947 * the action is canceled, but the rule processing continue.
11948 */
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011949int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011950{
11951 chunk_reset(&trash);
11952
11953 /* If we have to create a query string, prepare a '?'. */
11954 if (*(int *)&rule->arg.act.p[2] == 2)
11955 trash.str[trash.len++] = '?';
11956 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
11957
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011958 http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s);
Thierry FOURNIER01c30122015-03-14 14:14:47 +010011959 return 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011960}
11961
11962/* parse an http-request action among :
11963 * set-method
11964 * set-path
11965 * set-query
11966 * set-uri
11967 *
11968 * All of them accept a single argument of type string representing a log-format.
11969 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11970 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
11971 * It returns 0 on success, < 0 on error.
11972 */
11973int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
11974{
11975 int cur_arg = *orig_arg;
11976
11977 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
11978
11979 switch (args[0][4]) {
11980 case 'm' :
11981 *(int *)&rule->arg.act.p[2] = 0;
11982 rule->action_ptr = http_action_set_req_line;
11983 break;
11984 case 'p' :
11985 *(int *)&rule->arg.act.p[2] = 1;
11986 rule->action_ptr = http_action_set_req_line;
11987 break;
11988 case 'q' :
11989 *(int *)&rule->arg.act.p[2] = 2;
11990 rule->action_ptr = http_action_set_req_line;
11991 break;
11992 case 'u' :
11993 *(int *)&rule->arg.act.p[2] = 3;
11994 rule->action_ptr = http_action_set_req_line;
11995 break;
11996 default:
11997 memprintf(err, "internal error: unhandled action '%s'", args[0]);
11998 return -1;
11999 }
12000
12001 if (!*args[cur_arg] ||
12002 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12003 memprintf(err, "expects exactly 1 argument <format>");
12004 return -1;
12005 }
12006
12007 LIST_INIT((struct list *)&rule->arg.act.p[0]);
12008 proxy->conf.args.ctx = ARGC_HRQ;
12009 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
12010 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12011 proxy->conf.args.file, proxy->conf.args.line);
12012
12013 (*orig_arg)++;
12014 return 0;
12015}
12016
William Lallemand73025dd2014-04-24 14:38:37 +020012017/*
12018 * Return the struct http_req_action_kw associated to a keyword.
12019 */
12020struct http_req_action_kw *action_http_req_custom(const char *kw)
12021{
12022 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
12023 struct http_req_action_kw_list *kw_list;
12024 int i;
12025
12026 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
12027 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
12028 if (!strcmp(kw, kw_list->kw[i].kw))
12029 return &kw_list->kw[i];
12030 }
12031 }
12032 }
12033 return NULL;
12034}
12035
12036/*
12037 * Return the struct http_res_action_kw associated to a keyword.
12038 */
12039struct http_res_action_kw *action_http_res_custom(const char *kw)
12040{
12041 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
12042 struct http_res_action_kw_list *kw_list;
12043 int i;
12044
12045 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
12046 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
12047 if (!strcmp(kw, kw_list->kw[i].kw))
12048 return &kw_list->kw[i];
12049 }
12050 }
12051 }
12052 return NULL;
12053}
12054
Willy Tarreau4a568972010-05-12 08:08:50 +020012055/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012056/* All supported ACL keywords must be declared here. */
12057/************************************************************************/
12058
12059/* Note: must not be declared <const> as its list will be overwritten.
12060 * Please take care of keeping this list alphabetically sorted.
12061 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012062static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012063 { "base", "base", PAT_MATCH_STR },
12064 { "base_beg", "base", PAT_MATCH_BEG },
12065 { "base_dir", "base", PAT_MATCH_DIR },
12066 { "base_dom", "base", PAT_MATCH_DOM },
12067 { "base_end", "base", PAT_MATCH_END },
12068 { "base_len", "base", PAT_MATCH_LEN },
12069 { "base_reg", "base", PAT_MATCH_REG },
12070 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012071
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012072 { "cook", "req.cook", PAT_MATCH_STR },
12073 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12074 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12075 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12076 { "cook_end", "req.cook", PAT_MATCH_END },
12077 { "cook_len", "req.cook", PAT_MATCH_LEN },
12078 { "cook_reg", "req.cook", PAT_MATCH_REG },
12079 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012080
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012081 { "hdr", "req.hdr", PAT_MATCH_STR },
12082 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12083 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12084 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12085 { "hdr_end", "req.hdr", PAT_MATCH_END },
12086 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12087 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12088 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012089
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012090 /* these two declarations uses strings with list storage (in place
12091 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12092 * and delete functions are relative to the list management. The parse
12093 * and match method are related to the corresponding fetch methods. This
12094 * is very particular ACL declaration mode.
12095 */
12096 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12097 { "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 +020012098
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012099 { "path", "path", PAT_MATCH_STR },
12100 { "path_beg", "path", PAT_MATCH_BEG },
12101 { "path_dir", "path", PAT_MATCH_DIR },
12102 { "path_dom", "path", PAT_MATCH_DOM },
12103 { "path_end", "path", PAT_MATCH_END },
12104 { "path_len", "path", PAT_MATCH_LEN },
12105 { "path_reg", "path", PAT_MATCH_REG },
12106 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012107
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012108 { "req_ver", "req.ver", PAT_MATCH_STR },
12109 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012110
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012111 { "scook", "res.cook", PAT_MATCH_STR },
12112 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12113 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12114 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12115 { "scook_end", "res.cook", PAT_MATCH_END },
12116 { "scook_len", "res.cook", PAT_MATCH_LEN },
12117 { "scook_reg", "res.cook", PAT_MATCH_REG },
12118 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012119
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012120 { "shdr", "res.hdr", PAT_MATCH_STR },
12121 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12122 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12123 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12124 { "shdr_end", "res.hdr", PAT_MATCH_END },
12125 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12126 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12127 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012128
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012129 { "url", "url", PAT_MATCH_STR },
12130 { "url_beg", "url", PAT_MATCH_BEG },
12131 { "url_dir", "url", PAT_MATCH_DIR },
12132 { "url_dom", "url", PAT_MATCH_DOM },
12133 { "url_end", "url", PAT_MATCH_END },
12134 { "url_len", "url", PAT_MATCH_LEN },
12135 { "url_reg", "url", PAT_MATCH_REG },
12136 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012137
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012138 { "urlp", "urlp", PAT_MATCH_STR },
12139 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12140 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12141 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12142 { "urlp_end", "urlp", PAT_MATCH_END },
12143 { "urlp_len", "urlp", PAT_MATCH_LEN },
12144 { "urlp_reg", "urlp", PAT_MATCH_REG },
12145 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012146
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012147 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012148}};
12149
12150/************************************************************************/
12151/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012152/************************************************************************/
12153/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012154static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012155 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012156 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12157 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12158
Willy Tarreau87b09662015-04-03 00:22:06 +020012159 /* capture are allocated and are permanent in the stream */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012160 { "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 +020012161
12162 /* retrieve these captures from the HTTP logs */
12163 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12164 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12165 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12166
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012167 { "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 +020012168 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012169
Willy Tarreau409bcde2013-01-08 00:31:00 +010012170 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12171 * are only here to match the ACL's name, are request-only and are used
12172 * for ACL compatibility only.
12173 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012174 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12175 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012176 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12177 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12178
12179 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12180 * only here to match the ACL's name, are request-only and are used for
12181 * ACL compatibility only.
12182 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012183 { "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 +010012184 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12185 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
12186 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12187
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012188 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012189 { "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 +010012190 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012191 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012192 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012193 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012194
12195 /* HTTP protocol on the request path */
12196 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012197 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012198
12199 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012200 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12201 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012202
12203 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012204 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12205 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012206
Willy Tarreau18ed2562013-01-14 15:56:36 +010012207 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012208 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012209 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12210 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12211
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012212 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012213 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012214 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012215 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12216 { "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 +010012217 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012218 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12219
12220 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012221 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012222 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12223 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12224
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012225 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012226 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012227 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012228 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12229 { "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 +010012230 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012231 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12232
Willy Tarreau409bcde2013-01-08 00:31:00 +010012233 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012234 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012235 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12236 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012237 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012238
12239 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012240 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012241 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12242 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
12243 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12244
12245 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012246 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012247 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12248 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012249 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
12250 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012251 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12252 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012253 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12254 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012255}};
12256
Willy Tarreau8797c062007-05-07 00:55:35 +020012257
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012258/************************************************************************/
12259/* All supported converter keywords must be declared here. */
12260/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012261/* Note: must not be declared <const> as its list will be overwritten */
12262static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020012263 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
12264 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012265 { NULL, NULL, 0, 0, 0 },
12266}};
12267
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012268/************************************************************************/
12269/* All supported http-request action keywords must be declared here. */
12270/************************************************************************/
12271struct http_req_action_kw_list http_req_actions = {
12272 .scope = "http",
12273 .kw = {
12274 { "set-method", parse_set_req_line },
12275 { "set-path", parse_set_req_line },
12276 { "set-query", parse_set_req_line },
12277 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012278 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012279 }
12280};
12281
Willy Tarreau8797c062007-05-07 00:55:35 +020012282__attribute__((constructor))
12283static void __http_protocol_init(void)
12284{
12285 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012286 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012287 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012288 http_req_keywords_register(&http_req_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012289}
12290
12291
Willy Tarreau58f10d72006-12-04 02:26:12 +010012292/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012293 * Local variables:
12294 * c-indent-level: 8
12295 * c-basic-offset: 8
12296 * End:
12297 */