blob: d20225a921325f13f32b7292470b5a8f405f8967 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020064#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau522d6c02009-12-06 18:49:18 +010069const char HTTP_100[] =
70 "HTTP/1.1 100 Continue\r\n\r\n";
71
72const struct chunk http_100_chunk = {
73 .str = (char *)&HTTP_100,
74 .len = sizeof(HTTP_100)-1
75};
76
Willy Tarreaua9679ac2010-01-03 17:32:57 +010077/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040096
97/* same as 302 except that the browser MUST retry with the same method */
98const char *HTTP_307 =
99 "HTTP/1.1 307 Temporary Redirect\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Content-length: 0\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
104/* same as 301 except that the browser MUST retry with the same method */
105const char *HTTP_308 =
106 "HTTP/1.1 308 Permanent Redirect\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau844a7e72010-01-31 21:46:18 +0100120const char *HTTP_407_fmt =
121 "HTTP/1.0 407 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
124 "Content-Type: text/html\r\n"
125 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800127 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400134 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400136 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100137 [HTTP_ERR_500] = 500,
138 [HTTP_ERR_502] = 502,
139 [HTTP_ERR_503] = 503,
140 [HTTP_ERR_504] = 504,
141};
142
Willy Tarreau80587432006-12-24 17:47:20 +0100143static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200144 [HTTP_ERR_200] =
145 "HTTP/1.0 200 OK\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
151
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100153 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
159
160 [HTTP_ERR_403] =
161 "HTTP/1.0 403 Forbidden\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
167
CJ Ess108b1dd2015-04-07 12:03:37 -0400168 [HTTP_ERR_405] =
169 "HTTP/1.0 405 Method Not Allowed\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
175
Willy Tarreau0f772532006-12-23 20:51:41 +0100176 [HTTP_ERR_408] =
177 "HTTP/1.0 408 Request Time-out\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
183
CJ Ess108b1dd2015-04-07 12:03:37 -0400184 [HTTP_ERR_429] =
185 "HTTP/1.0 429 Too Many Requests\r\n"
186 "Cache-Control: no-cache\r\n"
187 "Connection: close\r\n"
188 "Content-Type: text/html\r\n"
189 "\r\n"
190 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
191
Willy Tarreau0f772532006-12-23 20:51:41 +0100192 [HTTP_ERR_500] =
193 "HTTP/1.0 500 Server Error\r\n"
194 "Cache-Control: no-cache\r\n"
195 "Connection: close\r\n"
196 "Content-Type: text/html\r\n"
197 "\r\n"
198 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
199
200 [HTTP_ERR_502] =
201 "HTTP/1.0 502 Bad Gateway\r\n"
202 "Cache-Control: no-cache\r\n"
203 "Connection: close\r\n"
204 "Content-Type: text/html\r\n"
205 "\r\n"
206 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
207
208 [HTTP_ERR_503] =
209 "HTTP/1.0 503 Service Unavailable\r\n"
210 "Cache-Control: no-cache\r\n"
211 "Connection: close\r\n"
212 "Content-Type: text/html\r\n"
213 "\r\n"
214 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
215
216 [HTTP_ERR_504] =
217 "HTTP/1.0 504 Gateway Time-out\r\n"
218 "Cache-Control: no-cache\r\n"
219 "Connection: close\r\n"
220 "Content-Type: text/html\r\n"
221 "\r\n"
222 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
223
224};
225
Cyril Bonté19979e12012-04-04 12:57:21 +0200226/* status codes available for the stats admin page (strictly 4 chars length) */
227const char *stat_status_codes[STAT_STATUS_SIZE] = {
228 [STAT_STATUS_DENY] = "DENY",
229 [STAT_STATUS_DONE] = "DONE",
230 [STAT_STATUS_ERRP] = "ERRP",
231 [STAT_STATUS_EXCD] = "EXCD",
232 [STAT_STATUS_NONE] = "NONE",
233 [STAT_STATUS_PART] = "PART",
234 [STAT_STATUS_UNKN] = "UNKN",
235};
236
237
William Lallemand73025dd2014-04-24 14:38:37 +0200238/* List head of all known action keywords for "http-request" */
239struct http_req_action_kw_list http_req_keywords = {
240 .list = LIST_HEAD_INIT(http_req_keywords.list)
241};
242
243/* List head of all known action keywords for "http-response" */
244struct http_res_action_kw_list http_res_keywords = {
245 .list = LIST_HEAD_INIT(http_res_keywords.list)
246};
247
Willy Tarreau80587432006-12-24 17:47:20 +0100248/* We must put the messages here since GCC cannot initialize consts depending
249 * on strlen().
250 */
251struct chunk http_err_chunks[HTTP_ERR_SIZE];
252
Willy Tarreaua890d072013-04-02 12:01:06 +0200253/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
254static struct hdr_ctx static_hdr_ctx;
255
Willy Tarreau42250582007-04-01 01:30:43 +0200256#define FD_SETS_ARE_BITFIELDS
257#ifdef FD_SETS_ARE_BITFIELDS
258/*
259 * This map is used with all the FD_* macros to check whether a particular bit
260 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
261 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
262 * byte should be encoded. Be careful to always pass bytes from 0 to 255
263 * exclusively to the macros.
264 */
265fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
266fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100267fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200268
269#else
270#error "Check if your OS uses bitfields for fd_sets"
271#endif
272
Willy Tarreau87b09662015-04-03 00:22:06 +0200273static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200274
Willy Tarreau80587432006-12-24 17:47:20 +0100275void init_proto_http()
276{
Willy Tarreau42250582007-04-01 01:30:43 +0200277 int i;
278 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100279 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200280
Willy Tarreau80587432006-12-24 17:47:20 +0100281 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
282 if (!http_err_msgs[msg]) {
283 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
284 abort();
285 }
286
287 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
288 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
289 }
Willy Tarreau42250582007-04-01 01:30:43 +0200290
291 /* initialize the log header encoding map : '{|}"#' should be encoded with
292 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
293 * URL encoding only requires '"', '#' to be encoded as well as non-
294 * printable characters above.
295 */
296 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
297 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100298 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200299 for (i = 0; i < 32; i++) {
300 FD_SET(i, hdr_encode_map);
301 FD_SET(i, url_encode_map);
302 }
303 for (i = 127; i < 256; i++) {
304 FD_SET(i, hdr_encode_map);
305 FD_SET(i, url_encode_map);
306 }
307
308 tmp = "\"#{|}";
309 while (*tmp) {
310 FD_SET(*tmp, hdr_encode_map);
311 tmp++;
312 }
313
314 tmp = "\"#";
315 while (*tmp) {
316 FD_SET(*tmp, url_encode_map);
317 tmp++;
318 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200319
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100320 /* initialize the http header encoding map. The draft httpbis define the
321 * header content as:
322 *
323 * HTTP-message = start-line
324 * *( header-field CRLF )
325 * CRLF
326 * [ message-body ]
327 * header-field = field-name ":" OWS field-value OWS
328 * field-value = *( field-content / obs-fold )
329 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
330 * obs-fold = CRLF 1*( SP / HTAB )
331 * field-vchar = VCHAR / obs-text
332 * VCHAR = %x21-7E
333 * obs-text = %x80-FF
334 *
335 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
336 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
337 * "obs-fold" is volontary forgotten because haproxy remove this.
338 */
339 memset(http_encode_map, 0, sizeof(http_encode_map));
340 for (i = 0x00; i <= 0x08; i++)
341 FD_SET(i, http_encode_map);
342 for (i = 0x0a; i <= 0x1f; i++)
343 FD_SET(i, http_encode_map);
344 FD_SET(0x7f, http_encode_map);
345
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200346 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200347 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200348 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100349 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100350}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351
Willy Tarreau53b6c742006-12-17 13:37:46 +0100352/*
353 * We have 26 list of methods (1 per first letter), each of which can have
354 * up to 3 entries (2 valid, 1 null).
355 */
356struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100357 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100358 int len;
359 const char text[8];
360};
361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100362const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100363 ['C' - 'A'] = {
364 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
365 },
366 ['D' - 'A'] = {
367 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
368 },
369 ['G' - 'A'] = {
370 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
371 },
372 ['H' - 'A'] = {
373 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
374 },
375 ['P' - 'A'] = {
376 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
377 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
378 },
379 ['T' - 'A'] = {
380 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
381 },
382 /* rest is empty like this :
383 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
384 */
385};
386
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100387const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
388 [HTTP_METH_NONE] = { "", 0 },
389 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
390 [HTTP_METH_GET] = { "GET", 3 },
391 [HTTP_METH_HEAD] = { "HEAD", 4 },
392 [HTTP_METH_POST] = { "POST", 4 },
393 [HTTP_METH_PUT] = { "PUT", 3 },
394 [HTTP_METH_DELETE] = { "DELETE", 6 },
395 [HTTP_METH_TRACE] = { "TRACE", 5 },
396 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
397};
398
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100399/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200400 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100401 * RFC2616 for those chars.
402 */
403
404const char http_is_spht[256] = {
405 [' '] = 1, ['\t'] = 1,
406};
407
408const char http_is_crlf[256] = {
409 ['\r'] = 1, ['\n'] = 1,
410};
411
412const char http_is_lws[256] = {
413 [' '] = 1, ['\t'] = 1,
414 ['\r'] = 1, ['\n'] = 1,
415};
416
417const char http_is_sep[256] = {
418 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
419 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
420 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
421 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
422 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
423};
424
425const char http_is_ctl[256] = {
426 [0 ... 31] = 1,
427 [127] = 1,
428};
429
430/*
431 * A token is any ASCII char that is neither a separator nor a CTL char.
432 * Do not overwrite values in assignment since gcc-2.95 will not handle
433 * them correctly. Instead, define every non-CTL char's status.
434 */
435const char http_is_token[256] = {
436 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
437 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
438 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
439 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
440 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
441 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
442 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
443 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
444 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
445 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
446 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
447 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
448 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
449 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
450 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
451 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
452 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
453 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
454 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
455 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
456 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
457 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
458 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
459 ['|'] = 1, ['}'] = 0, ['~'] = 1,
460};
461
462
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100463/*
464 * An http ver_token is any ASCII which can be found in an HTTP version,
465 * which includes 'H', 'T', 'P', '/', '.' and any digit.
466 */
467const char http_is_ver_token[256] = {
468 ['.'] = 1, ['/'] = 1,
469 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
470 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100471 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100472};
473
474
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100475/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100476 * Adds a header and its CRLF at the tail of the message's buffer, just before
477 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100478 * The header is also automatically added to the index <hdr_idx>, and the end
479 * of headers is automatically adjusted. The number of bytes added is returned
480 * on success, otherwise <0 is returned indicating an error.
481 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100482int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100483{
484 int bytes, len;
485
486 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200487 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100488 if (!bytes)
489 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100490 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100491 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
492}
493
494/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100495 * Adds a header and its CRLF at the tail of the message's buffer, just before
496 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100497 * the buffer is only opened and the space reserved, but nothing is copied.
498 * The header is also automatically added to the index <hdr_idx>, and the end
499 * of headers is automatically adjusted. The number of bytes added is returned
500 * on success, otherwise <0 is returned indicating an error.
501 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100502int http_header_add_tail2(struct http_msg *msg,
503 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100504{
505 int bytes;
506
Willy Tarreau9b28e032012-10-12 23:49:43 +0200507 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100508 if (!bytes)
509 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100510 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100511 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
512}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200513
514/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100515 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
516 * If so, returns the position of the first non-space character relative to
517 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
518 * to return a pointer to the place after the first space. Returns 0 if the
519 * header name does not match. Checks are case-insensitive.
520 */
521int http_header_match2(const char *hdr, const char *end,
522 const char *name, int len)
523{
524 const char *val;
525
526 if (hdr + len >= end)
527 return 0;
528 if (hdr[len] != ':')
529 return 0;
530 if (strncasecmp(hdr, name, len) != 0)
531 return 0;
532 val = hdr + len + 1;
533 while (val < end && HTTP_IS_SPHT(*val))
534 val++;
535 if ((val >= end) && (len + 2 <= end - hdr))
536 return len + 2; /* we may replace starting from second space */
537 return val - hdr;
538}
539
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200540/* Find the first or next occurrence of header <name> in message buffer <sol>
541 * using headers index <idx>, and return it in the <ctx> structure. This
542 * structure holds everything necessary to use the header and find next
543 * occurrence. If its <idx> member is 0, the header is searched from the
544 * beginning. Otherwise, the next occurrence is returned. The function returns
545 * 1 when it finds a value, and 0 when there is no more. It is very similar to
546 * http_find_header2() except that it is designed to work with full-line headers
547 * whose comma is not a delimiter but is part of the syntax. As a special case,
548 * if ctx->val is NULL when searching for a new values of a header, the current
549 * header is rescanned. This allows rescanning after a header deletion.
550 */
551int http_find_full_header2(const char *name, int len,
552 char *sol, struct hdr_idx *idx,
553 struct hdr_ctx *ctx)
554{
555 char *eol, *sov;
556 int cur_idx, old_idx;
557
558 cur_idx = ctx->idx;
559 if (cur_idx) {
560 /* We have previously returned a header, let's search another one */
561 sol = ctx->line;
562 eol = sol + idx->v[cur_idx].len;
563 goto next_hdr;
564 }
565
566 /* first request for this header */
567 sol += hdr_idx_first_pos(idx);
568 old_idx = 0;
569 cur_idx = hdr_idx_first_idx(idx);
570 while (cur_idx) {
571 eol = sol + idx->v[cur_idx].len;
572
573 if (len == 0) {
574 /* No argument was passed, we want any header.
575 * To achieve this, we simply build a fake request. */
576 while (sol + len < eol && sol[len] != ':')
577 len++;
578 name = sol;
579 }
580
581 if ((len < eol - sol) &&
582 (sol[len] == ':') &&
583 (strncasecmp(sol, name, len) == 0)) {
584 ctx->del = len;
585 sov = sol + len + 1;
586 while (sov < eol && http_is_lws[(unsigned char)*sov])
587 sov++;
588
589 ctx->line = sol;
590 ctx->prev = old_idx;
591 ctx->idx = cur_idx;
592 ctx->val = sov - sol;
593 ctx->tws = 0;
594 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
595 eol--;
596 ctx->tws++;
597 }
598 ctx->vlen = eol - sov;
599 return 1;
600 }
601 next_hdr:
602 sol = eol + idx->v[cur_idx].cr + 1;
603 old_idx = cur_idx;
604 cur_idx = idx->v[cur_idx].next;
605 }
606 return 0;
607}
608
Willy Tarreauc90dc232015-02-20 13:51:36 +0100609/* Find the first or next header field in message buffer <sol> using headers
610 * index <idx>, and return it in the <ctx> structure. This structure holds
611 * everything necessary to use the header and find next occurrence. If its
612 * <idx> member is 0, the first header is retrieved. Otherwise, the next
613 * occurrence is returned. The function returns 1 when it finds a value, and
614 * 0 when there is no more. It is equivalent to http_find_full_header2() with
615 * no header name.
616 */
617int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
618{
619 char *eol, *sov;
620 int cur_idx, old_idx;
621 int len;
622
623 cur_idx = ctx->idx;
624 if (cur_idx) {
625 /* We have previously returned a header, let's search another one */
626 sol = ctx->line;
627 eol = sol + idx->v[cur_idx].len;
628 goto next_hdr;
629 }
630
631 /* first request for this header */
632 sol += hdr_idx_first_pos(idx);
633 old_idx = 0;
634 cur_idx = hdr_idx_first_idx(idx);
635 while (cur_idx) {
636 eol = sol + idx->v[cur_idx].len;
637
638 len = 0;
639 while (1) {
640 if (len >= eol - sol)
641 goto next_hdr;
642 if (sol[len] == ':')
643 break;
644 len++;
645 }
646
647 ctx->del = len;
648 sov = sol + len + 1;
649 while (sov < eol && http_is_lws[(unsigned char)*sov])
650 sov++;
651
652 ctx->line = sol;
653 ctx->prev = old_idx;
654 ctx->idx = cur_idx;
655 ctx->val = sov - sol;
656 ctx->tws = 0;
657
658 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
659 eol--;
660 ctx->tws++;
661 }
662 ctx->vlen = eol - sov;
663 return 1;
664
665 next_hdr:
666 sol = eol + idx->v[cur_idx].cr + 1;
667 old_idx = cur_idx;
668 cur_idx = idx->v[cur_idx].next;
669 }
670 return 0;
671}
672
Willy Tarreau68085d82010-01-18 14:54:04 +0100673/* Find the end of the header value contained between <s> and <e>. See RFC2616,
674 * par 2.2 for more information. Note that it requires a valid header to return
675 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200676 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100677char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200678{
679 int quoted, qdpair;
680
681 quoted = qdpair = 0;
682 for (; s < e; s++) {
683 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200684 else if (quoted) {
685 if (*s == '\\') qdpair = 1;
686 else if (*s == '"') quoted = 0;
687 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200688 else if (*s == '"') quoted = 1;
689 else if (*s == ',') return s;
690 }
691 return s;
692}
693
694/* Find the first or next occurrence of header <name> in message buffer <sol>
695 * using headers index <idx>, and return it in the <ctx> structure. This
696 * structure holds everything necessary to use the header and find next
697 * occurrence. If its <idx> member is 0, the header is searched from the
698 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100699 * 1 when it finds a value, and 0 when there is no more. It is designed to work
700 * with headers defined as comma-separated lists. As a special case, if ctx->val
701 * is NULL when searching for a new values of a header, the current header is
702 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200703 */
704int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100705 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200706 struct hdr_ctx *ctx)
707{
Willy Tarreau68085d82010-01-18 14:54:04 +0100708 char *eol, *sov;
709 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200710
Willy Tarreau68085d82010-01-18 14:54:04 +0100711 cur_idx = ctx->idx;
712 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200713 /* We have previously returned a value, let's search
714 * another one on the same line.
715 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200716 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200717 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100718 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200719 eol = sol + idx->v[cur_idx].len;
720
721 if (sov >= eol)
722 /* no more values in this header */
723 goto next_hdr;
724
Willy Tarreau68085d82010-01-18 14:54:04 +0100725 /* values remaining for this header, skip the comma but save it
726 * for later use (eg: for header deletion).
727 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200728 sov++;
729 while (sov < eol && http_is_lws[(unsigned char)*sov])
730 sov++;
731
732 goto return_hdr;
733 }
734
735 /* first request for this header */
736 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100737 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200738 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200739 while (cur_idx) {
740 eol = sol + idx->v[cur_idx].len;
741
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200742 if (len == 0) {
743 /* No argument was passed, we want any header.
744 * To achieve this, we simply build a fake request. */
745 while (sol + len < eol && sol[len] != ':')
746 len++;
747 name = sol;
748 }
749
Willy Tarreau33a7e692007-06-10 19:45:56 +0200750 if ((len < eol - sol) &&
751 (sol[len] == ':') &&
752 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100753 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200754 sov = sol + len + 1;
755 while (sov < eol && http_is_lws[(unsigned char)*sov])
756 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100757
Willy Tarreau33a7e692007-06-10 19:45:56 +0200758 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100759 ctx->prev = old_idx;
760 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200761 ctx->idx = cur_idx;
762 ctx->val = sov - sol;
763
764 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200765 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200766 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200767 eol--;
768 ctx->tws++;
769 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200770 ctx->vlen = eol - sov;
771 return 1;
772 }
773 next_hdr:
774 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100775 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200776 cur_idx = idx->v[cur_idx].next;
777 }
778 return 0;
779}
780
781int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100782 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200783 struct hdr_ctx *ctx)
784{
785 return http_find_header2(name, strlen(name), sol, idx, ctx);
786}
787
Willy Tarreau68085d82010-01-18 14:54:04 +0100788/* Remove one value of a header. This only works on a <ctx> returned by one of
789 * the http_find_header functions. The value is removed, as well as surrounding
790 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100791 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100792 * message <msg>. The new index is returned. If it is zero, it means there is
793 * no more header, so any processing may stop. The ctx is always left in a form
794 * that can be handled by http_find_header2() to find next occurrence.
795 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100796int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100797{
798 int cur_idx = ctx->idx;
799 char *sol = ctx->line;
800 struct hdr_idx_elem *hdr;
801 int delta, skip_comma;
802
803 if (!cur_idx)
804 return 0;
805
806 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200807 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100808 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200809 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100810 http_msg_move_end(msg, delta);
811 idx->used--;
812 hdr->len = 0; /* unused entry */
813 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100814 if (idx->tail == ctx->idx)
815 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100816 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100817 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100818 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200819 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100820 return ctx->idx;
821 }
822
823 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200824 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
825 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100826 */
827
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200828 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200829 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200830 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100831 NULL, 0);
832 hdr->len += delta;
833 http_msg_move_end(msg, delta);
834 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200835 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100836 return ctx->idx;
837}
838
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100839/* This function handles a server error at the stream interface level. The
840 * stream interface is assumed to be already in a closed state. An optional
841 * message is copied into the input buffer, and an HTTP status code stored.
842 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100843 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200845static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100846 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100848 channel_auto_read(si_oc(si));
849 channel_abort(si_oc(si));
850 channel_auto_close(si_oc(si));
851 channel_erase(si_oc(si));
852 channel_auto_close(si_ic(si));
853 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100854 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200855 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100856 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200857 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200858 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200859 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200860 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200861 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200862}
863
Willy Tarreau87b09662015-04-03 00:22:06 +0200864/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100865 * and message.
866 */
867
Willy Tarreau87b09662015-04-03 00:22:06 +0200868struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100869{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200870 if (s->be->errmsg[msgnum].str)
871 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200872 else if (strm_fe(s)->errmsg[msgnum].str)
873 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100874 else
875 return &http_err_chunks[msgnum];
876}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200877
Willy Tarreau53b6c742006-12-17 13:37:46 +0100878/*
879 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
880 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
881 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100882enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100883{
884 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100885 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100886
887 m = ((unsigned)*str - 'A');
888
889 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100890 for (h = http_methods[m]; h->len > 0; h++) {
891 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100892 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100893 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100894 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100895 };
896 return HTTP_METH_OTHER;
897 }
898 return HTTP_METH_NONE;
899
900}
901
Willy Tarreau21d2af32008-02-14 20:25:24 +0100902/* Parse the URI from the given transaction (which is assumed to be in request
903 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
904 * It is returned otherwise.
905 */
906static char *
907http_get_path(struct http_txn *txn)
908{
909 char *ptr, *end;
910
Willy Tarreau9b28e032012-10-12 23:49:43 +0200911 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100912 end = ptr + txn->req.sl.rq.u_l;
913
914 if (ptr >= end)
915 return NULL;
916
917 /* RFC2616, par. 5.1.2 :
918 * Request-URI = "*" | absuri | abspath | authority
919 */
920
921 if (*ptr == '*')
922 return NULL;
923
924 if (isalpha((unsigned char)*ptr)) {
925 /* this is a scheme as described by RFC3986, par. 3.1 */
926 ptr++;
927 while (ptr < end &&
928 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
929 ptr++;
930 /* skip '://' */
931 if (ptr == end || *ptr++ != ':')
932 return NULL;
933 if (ptr == end || *ptr++ != '/')
934 return NULL;
935 if (ptr == end || *ptr++ != '/')
936 return NULL;
937 }
938 /* skip [user[:passwd]@]host[:[port]] */
939
940 while (ptr < end && *ptr != '/')
941 ptr++;
942
943 if (ptr == end)
944 return NULL;
945
946 /* OK, we got the '/' ! */
947 return ptr;
948}
949
William Lallemand65ad6e12014-01-31 15:08:02 +0100950/* Parse the URI from the given string and look for the "/" beginning the PATH.
951 * If not found, return NULL. It is returned otherwise.
952 */
953static char *
954http_get_path_from_string(char *str)
955{
956 char *ptr = str;
957
958 /* RFC2616, par. 5.1.2 :
959 * Request-URI = "*" | absuri | abspath | authority
960 */
961
962 if (*ptr == '*')
963 return NULL;
964
965 if (isalpha((unsigned char)*ptr)) {
966 /* this is a scheme as described by RFC3986, par. 3.1 */
967 ptr++;
968 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
969 ptr++;
970 /* skip '://' */
971 if (*ptr == '\0' || *ptr++ != ':')
972 return NULL;
973 if (*ptr == '\0' || *ptr++ != '/')
974 return NULL;
975 if (*ptr == '\0' || *ptr++ != '/')
976 return NULL;
977 }
978 /* skip [user[:passwd]@]host[:[port]] */
979
980 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
981 ptr++;
982
983 if (*ptr == '\0' || *ptr == ' ')
984 return NULL;
985
986 /* OK, we got the '/' ! */
987 return ptr;
988}
989
Willy Tarreau71241ab2012-12-27 11:30:54 +0100990/* Returns a 302 for a redirectable request that reaches a server working in
991 * in redirect mode. This may only be called just after the stream interface
992 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
993 * follow normal proxy processing. NOTE: this function is designed to support
994 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100995 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200996void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100997{
998 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100999 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001000 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001001 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001002
1003 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001004 trash.len = strlen(HTTP_302);
1005 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001007 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001008
Willy Tarreauefb453c2008-10-26 20:49:47 +01001009 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001010 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001011 return;
1012
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001013 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001014 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001015 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1016 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001017 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001018
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001019 /* 3: add the request URI. Since it was already forwarded, we need
1020 * to temporarily rewind the buffer.
1021 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001022 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001023 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001024
Willy Tarreauefb453c2008-10-26 20:49:47 +01001025 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001026 len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001027
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001028 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001029
Willy Tarreauefb453c2008-10-26 20:49:47 +01001030 if (!path)
1031 return;
1032
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001033 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001034 return;
1035
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001036 memcpy(trash.str + trash.len, path, len);
1037 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001038
1039 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001040 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1041 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001042 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001043 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1044 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001045 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001046
1047 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001048 si_shutr(si);
1049 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001050 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001051 si->state = SI_ST_CLO;
1052
1053 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001054 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001055
1056 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001057 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001058 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001059}
1060
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001061/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001062 * that the server side is closed. Note that err_type is actually a
1063 * bitmask, where almost only aborts may be cumulated with other
1064 * values. We consider that aborted operations are more important
1065 * than timeouts or errors due to the fact that nobody else in the
1066 * logs might explain incomplete retries. All others should avoid
1067 * being cumulated. It should normally not be possible to have multiple
1068 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001069 * Note that connection errors appearing on the second request of a keep-alive
1070 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001071 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001072void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001073{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001074 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001075
1076 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001077 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001078 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001079 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001080 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001081 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001082 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001083 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001084 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001085 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001087 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001088 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001089 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001090 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001091 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001092 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001093 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001094 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1095 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001096 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001097 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001098 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001099 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001100 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001101 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001102 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001103 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001104}
1105
Willy Tarreau42250582007-04-01 01:30:43 +02001106extern const char sess_term_cond[8];
1107extern const char sess_fin_state[8];
1108extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001109struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001110struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001111struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001112struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113
Willy Tarreau117f59e2007-03-04 18:17:17 +01001114/*
1115 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001116 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001117 */
1118void capture_headers(char *som, struct hdr_idx *idx,
1119 char **cap, struct cap_hdr *cap_hdr)
1120{
1121 char *eol, *sol, *col, *sov;
1122 int cur_idx;
1123 struct cap_hdr *h;
1124 int len;
1125
1126 sol = som + hdr_idx_first_pos(idx);
1127 cur_idx = hdr_idx_first_idx(idx);
1128
1129 while (cur_idx) {
1130 eol = sol + idx->v[cur_idx].len;
1131
1132 col = sol;
1133 while (col < eol && *col != ':')
1134 col++;
1135
1136 sov = col + 1;
1137 while (sov < eol && http_is_lws[(unsigned char)*sov])
1138 sov++;
1139
1140 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001141 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001142 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1143 if (cap[h->index] == NULL)
1144 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001145 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001146
1147 if (cap[h->index] == NULL) {
1148 Alert("HTTP capture : out of memory.\n");
1149 continue;
1150 }
1151
1152 len = eol - sov;
1153 if (len > h->len)
1154 len = h->len;
1155
1156 memcpy(cap[h->index], sov, len);
1157 cap[h->index][len]=0;
1158 }
1159 }
1160 sol = eol + idx->v[cur_idx].cr + 1;
1161 cur_idx = idx->v[cur_idx].next;
1162 }
1163}
1164
1165
Willy Tarreau42250582007-04-01 01:30:43 +02001166/* either we find an LF at <ptr> or we jump to <bad>.
1167 */
1168#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1169
1170/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1171 * otherwise to <http_msg_ood> with <state> set to <st>.
1172 */
1173#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1174 ptr++; \
1175 if (likely(ptr < end)) \
1176 goto good; \
1177 else { \
1178 state = (st); \
1179 goto http_msg_ood; \
1180 } \
1181 } while (0)
1182
1183
Willy Tarreaubaaee002006-06-26 02:48:02 +02001184/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001185 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001186 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1187 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1188 * will give undefined results.
1189 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1190 * and that msg->sol points to the beginning of the response.
1191 * If a complete line is found (which implies that at least one CR or LF is
1192 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1193 * returned indicating an incomplete line (which does not mean that parts have
1194 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1195 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1196 * upon next call.
1197 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001198 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001199 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1200 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001201 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001202 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001203const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001204 enum ht_state state, const char *ptr, const char *end,
1205 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001206{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001207 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001208
Willy Tarreau8973c702007-01-21 23:58:29 +01001209 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001210 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001211 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001212 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001213 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1214
1215 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001216 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001217 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1218 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001219 state = HTTP_MSG_ERROR;
1220 break;
1221
Willy Tarreau8973c702007-01-21 23:58:29 +01001222 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001223 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001224 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001225 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001226 goto http_msg_rpcode;
1227 }
1228 if (likely(HTTP_IS_SPHT(*ptr)))
1229 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1230 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001231 state = HTTP_MSG_ERROR;
1232 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001233
Willy Tarreau8973c702007-01-21 23:58:29 +01001234 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001235 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001236 if (likely(!HTTP_IS_LWS(*ptr)))
1237 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1238
1239 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001240 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001241 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1242 }
1243
1244 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001245 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001246 http_msg_rsp_reason:
1247 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001248 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001249 msg->sl.st.r_l = 0;
1250 goto http_msg_rpline_eol;
1251
Willy Tarreau8973c702007-01-21 23:58:29 +01001252 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001253 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001254 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001255 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001256 goto http_msg_rpreason;
1257 }
1258 if (likely(HTTP_IS_SPHT(*ptr)))
1259 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1260 /* so it's a CR/LF, so there is no reason phrase */
1261 goto http_msg_rsp_reason;
1262
Willy Tarreau8973c702007-01-21 23:58:29 +01001263 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001264 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001265 if (likely(!HTTP_IS_CRLF(*ptr)))
1266 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001267 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 http_msg_rpline_eol:
1269 /* We have seen the end of line. Note that we do not
1270 * necessarily have the \n yet, but at least we know that we
1271 * have EITHER \r OR \n, otherwise the response would not be
1272 * complete. We can then record the response length and return
1273 * to the caller which will be able to register it.
1274 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001275 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 return ptr;
1277
Willy Tarreau8973c702007-01-21 23:58:29 +01001278 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001279#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001280 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1281 exit(1);
1282#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001283 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 }
1285
1286 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001287 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001288 if (ret_state)
1289 *ret_state = state;
1290 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001291 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001292 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001293}
1294
Willy Tarreau8973c702007-01-21 23:58:29 +01001295/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001296 * This function parses a request line between <ptr> and <end>, starting with
1297 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1298 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1299 * will give undefined results.
1300 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1301 * and that msg->sol points to the beginning of the request.
1302 * If a complete line is found (which implies that at least one CR or LF is
1303 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1304 * returned indicating an incomplete line (which does not mean that parts have
1305 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1306 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1307 * upon next call.
1308 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001309 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1311 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001312 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001313 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001314const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001315 enum ht_state state, const char *ptr, const char *end,
1316 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001317{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001318 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001322 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001323 if (likely(HTTP_IS_TOKEN(*ptr)))
1324 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001327 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1329 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001330
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 if (likely(HTTP_IS_CRLF(*ptr))) {
1332 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001333 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001334 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001335 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001336 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001337 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001338 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001339 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 msg->sl.rq.v_l = 0;
1341 goto http_msg_rqline_eol;
1342 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001343 state = HTTP_MSG_ERROR;
1344 break;
1345
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001346 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001347 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001348 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001349 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 goto http_msg_rquri;
1351 }
1352 if (likely(HTTP_IS_SPHT(*ptr)))
1353 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1354 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1355 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001357 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001358 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001359 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001363 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1365 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001366
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001367 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001368 /* non-ASCII chars are forbidden unless option
1369 * accept-invalid-http-request is enabled in the frontend.
1370 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001371 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001372 if (msg->err_pos < -1)
1373 goto invalid_char;
1374 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001375 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001376 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1377 }
1378
1379 if (likely(HTTP_IS_CRLF(*ptr))) {
1380 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1381 goto http_msg_req09_uri_e;
1382 }
1383
1384 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001385 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001386 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001387 state = HTTP_MSG_ERROR;
1388 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001391 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001393 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 goto http_msg_rqver;
1395 }
1396 if (likely(HTTP_IS_SPHT(*ptr)))
1397 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1398 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1399 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001402 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001403 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001405
1406 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001407 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001408 http_msg_rqline_eol:
1409 /* We have seen the end of line. Note that we do not
1410 * necessarily have the \n yet, but at least we know that we
1411 * have EITHER \r OR \n, otherwise the request would not be
1412 * complete. We can then record the request length and return
1413 * to the caller which will be able to register it.
1414 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001415 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001416 return ptr;
1417 }
1418
1419 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001420 state = HTTP_MSG_ERROR;
1421 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001424#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1426 exit(1);
1427#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001428 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001430
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001432 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 if (ret_state)
1434 *ret_state = state;
1435 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001436 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001439
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001440/*
1441 * Returns the data from Authorization header. Function may be called more
1442 * than once so data is stored in txn->auth_data. When no header is found
1443 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001444 * searching again for something we are unable to find anyway. However, if
1445 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001446 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001447 */
1448
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001449/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1450 * set according to global.tune.bufsize.
1451 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001452char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001453
1454int
Willy Tarreau87b09662015-04-03 00:22:06 +02001455get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001456{
1457
Willy Tarreaueee5b512015-04-03 23:46:31 +02001458 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001459 struct chunk auth_method;
1460 struct hdr_ctx ctx;
1461 char *h, *p;
1462 int len;
1463
1464#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001465 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001466#endif
1467
1468 if (txn->auth.method == HTTP_AUTH_WRONG)
1469 return 0;
1470
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001471 txn->auth.method = HTTP_AUTH_WRONG;
1472
1473 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001474
1475 if (txn->flags & TX_USE_PX_CONN) {
1476 h = "Proxy-Authorization";
1477 len = strlen(h);
1478 } else {
1479 h = "Authorization";
1480 len = strlen(h);
1481 }
1482
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001483 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001484 return 0;
1485
1486 h = ctx.line + ctx.val;
1487
1488 p = memchr(h, ' ', ctx.vlen);
1489 if (!p || p == h)
1490 return 0;
1491
1492 chunk_initlen(&auth_method, h, 0, p-h);
1493 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1494
1495 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1496
1497 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001498 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001499
1500 if (len < 0)
1501 return 0;
1502
1503
1504 get_http_auth_buff[len] = '\0';
1505
1506 p = strchr(get_http_auth_buff, ':');
1507
1508 if (!p)
1509 return 0;
1510
1511 txn->auth.user = get_http_auth_buff;
1512 *p = '\0';
1513 txn->auth.pass = p+1;
1514
1515 txn->auth.method = HTTP_AUTH_BASIC;
1516 return 1;
1517 }
1518
1519 return 0;
1520}
1521
Willy Tarreau58f10d72006-12-04 02:26:12 +01001522
Willy Tarreau8973c702007-01-21 23:58:29 +01001523/*
1524 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001525 * depending on the initial msg->msg_state. The caller is responsible for
1526 * ensuring that the message does not wrap. The function can be preempted
1527 * everywhere when data are missing and recalled at the exact same location
1528 * with no information loss. The message may even be realigned between two
1529 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001530 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001531 * fields. Note that msg->sol will be initialized after completing the first
1532 * state, so that none of the msg pointers has to be initialized prior to the
1533 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001534 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001535void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536{
Willy Tarreau3770f232013-12-07 00:01:53 +01001537 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001539 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001540
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001541 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001542 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001543 ptr = buf->p + msg->next;
1544 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 if (unlikely(ptr >= end))
1547 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001548
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001550 /*
1551 * First, states that are specific to the response only.
1552 * We check them first so that request and headers are
1553 * closer to each other (accessed more often).
1554 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001555 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001556 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001557 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001558 /* we have a start of message, but we have to check
1559 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001560 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001561 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001562 if (unlikely(ptr != buf->p)) {
1563 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001564 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001565 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001566 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001567 }
Willy Tarreau26927362012-05-18 23:22:52 +02001568 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001569 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001570 hdr_idx_init(idx);
1571 state = HTTP_MSG_RPVER;
1572 goto http_msg_rpver;
1573 }
1574
1575 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1576 goto http_msg_invalid;
1577
1578 if (unlikely(*ptr == '\n'))
1579 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1580 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1581 /* stop here */
1582
Willy Tarreau8973c702007-01-21 23:58:29 +01001583 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001584 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001585 EXPECT_LF_HERE(ptr, http_msg_invalid);
1586 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1587 /* stop here */
1588
Willy Tarreau8973c702007-01-21 23:58:29 +01001589 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001590 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001591 case HTTP_MSG_RPVER_SP:
1592 case HTTP_MSG_RPCODE:
1593 case HTTP_MSG_RPCODE_SP:
1594 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001595 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001596 state, ptr, end,
1597 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001598 if (unlikely(!ptr))
1599 return;
1600
1601 /* we have a full response and we know that we have either a CR
1602 * or an LF at <ptr>.
1603 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001604 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1605
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001606 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001607 if (likely(*ptr == '\r'))
1608 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1609 goto http_msg_rpline_end;
1610
Willy Tarreau8973c702007-01-21 23:58:29 +01001611 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001612 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001613 /* msg->sol must point to the first of CR or LF. */
1614 EXPECT_LF_HERE(ptr, http_msg_invalid);
1615 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1616 /* stop here */
1617
1618 /*
1619 * Second, states that are specific to the request only
1620 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001622 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001624 /* we have a start of message, but we have to check
1625 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001626 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001627 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001628 if (likely(ptr != buf->p)) {
1629 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001630 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001631 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001632 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001633 }
Willy Tarreau26927362012-05-18 23:22:52 +02001634 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001635 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001636 state = HTTP_MSG_RQMETH;
1637 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1641 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 if (unlikely(*ptr == '\n'))
1644 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1645 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001646 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001648 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001649 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001650 EXPECT_LF_HERE(ptr, http_msg_invalid);
1651 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001652 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001655 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 case HTTP_MSG_RQMETH_SP:
1657 case HTTP_MSG_RQURI:
1658 case HTTP_MSG_RQURI_SP:
1659 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001660 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001661 state, ptr, end,
1662 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 if (unlikely(!ptr))
1664 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 /* we have a full request and we know that we have either a CR
1667 * or an LF at <ptr>.
1668 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001670
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001671 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001672 if (likely(*ptr == '\r'))
1673 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001675
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001677 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001678 /* check for HTTP/0.9 request : no version information available.
1679 * msg->sol must point to the first of CR or LF.
1680 */
1681 if (unlikely(msg->sl.rq.v_l == 0))
1682 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001683
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 EXPECT_LF_HERE(ptr, http_msg_invalid);
1685 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001686 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001687
Willy Tarreau8973c702007-01-21 23:58:29 +01001688 /*
1689 * Common states below
1690 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001691 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001692 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001693 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 if (likely(!HTTP_IS_CRLF(*ptr))) {
1695 goto http_msg_hdr_name;
1696 }
1697
1698 if (likely(*ptr == '\r'))
1699 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1700 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001701
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001702 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001703 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 /* assumes msg->sol points to the first char */
1705 if (likely(HTTP_IS_TOKEN(*ptr)))
1706 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001707
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001708 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001710
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001711 if (likely(msg->err_pos < -1) || *ptr == '\n')
1712 goto http_msg_invalid;
1713
1714 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001715 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001716
1717 /* and we still accept this non-token character */
1718 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001719
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001721 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001722 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 if (likely(HTTP_IS_SPHT(*ptr)))
1724 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001725
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001727 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001728
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 if (likely(!HTTP_IS_CRLF(*ptr))) {
1730 goto http_msg_hdr_val;
1731 }
1732
1733 if (likely(*ptr == '\r'))
1734 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1735 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001736
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001737 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001738 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 EXPECT_LF_HERE(ptr, http_msg_invalid);
1740 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001741
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001743 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 if (likely(HTTP_IS_SPHT(*ptr))) {
1745 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001746 for (; buf->p + msg->sov < ptr; msg->sov++)
1747 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 goto http_msg_hdr_l1_sp;
1749 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001750 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001751 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 goto http_msg_complete_header;
1753
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001755 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001756 /* assumes msg->sol points to the first char, and msg->sov
1757 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 */
1759 if (likely(!HTTP_IS_CRLF(*ptr)))
1760 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001761
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001762 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 /* Note: we could also copy eol into ->eoh so that we have the
1764 * real header end in case it ends with lots of LWS, but is this
1765 * really needed ?
1766 */
1767 if (likely(*ptr == '\r'))
1768 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1769 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001770
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001772 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 EXPECT_LF_HERE(ptr, http_msg_invalid);
1774 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001775
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001777 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1779 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001780 for (; buf->p + msg->eol < ptr; msg->eol++)
1781 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001782 goto http_msg_hdr_val;
1783 }
1784 http_msg_complete_header:
1785 /*
1786 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001787 * Assumes msg->sol points to the first char, msg->sov points
1788 * to the first character of the value and msg->eol to the
1789 * first CR or LF so we know how the line ends. We insert last
1790 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001792 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 idx, idx->tail) < 0))
1794 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001795
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001796 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 if (likely(!HTTP_IS_CRLF(*ptr))) {
1798 goto http_msg_hdr_name;
1799 }
1800
1801 if (likely(*ptr == '\r'))
1802 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1803 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001806 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001807 /* Assumes msg->sol points to the first of either CR or LF.
1808 * Sets ->sov and ->next to the total header length, ->eoh to
1809 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1810 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811 EXPECT_LF_HERE(ptr, http_msg_invalid);
1812 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001813 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001814 msg->eoh = msg->sol;
1815 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001816 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001817 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001818 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001819
1820 case HTTP_MSG_ERROR:
1821 /* this may only happen if we call http_msg_analyser() twice with an error */
1822 break;
1823
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001825#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1827 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001828#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001829 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 }
1831 http_msg_ood:
1832 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001833 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001834 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001836
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001837 http_msg_invalid:
1838 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001839 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001840 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001841 return;
1842}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001843
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001844/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1845 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1846 * nothing is done and 1 is returned.
1847 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001848static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001849{
1850 int delta;
1851 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001852 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001853
1854 if (msg->sl.rq.v_l != 0)
1855 return 1;
1856
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001857 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1858 if (txn->meth != HTTP_METH_GET)
1859 return 0;
1860
Willy Tarreau9b28e032012-10-12 23:49:43 +02001861 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001862 delta = 0;
1863
1864 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001865 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1866 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001867 }
1868 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001869 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001870 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001871 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001872 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001873 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001874 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001875 NULL, NULL);
1876 if (unlikely(!cur_end))
1877 return 0;
1878
1879 /* we have a full HTTP/1.0 request now and we know that
1880 * we have either a CR or an LF at <ptr>.
1881 */
1882 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1883 return 1;
1884}
1885
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001886/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001887 * and "keep-alive" values. If we already know that some headers may safely
1888 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001889 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1890 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001891 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001892 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1893 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1894 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001895 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001896 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001897void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001898{
Willy Tarreau5b154472009-12-21 20:11:07 +01001899 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001900 const char *hdr_val = "Connection";
1901 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001902
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001903 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001904 return;
1905
Willy Tarreau88d349d2010-01-25 12:15:43 +01001906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection";
1908 hdr_len = 16;
1909 }
1910
Willy Tarreau5b154472009-12-21 20:11:07 +01001911 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001912 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001913 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001914 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1915 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001916 if (to_del & 2)
1917 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001918 else
1919 txn->flags |= TX_CON_KAL_SET;
1920 }
1921 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1922 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001923 if (to_del & 1)
1924 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001925 else
1926 txn->flags |= TX_CON_CLO_SET;
1927 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001928 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1929 txn->flags |= TX_HDR_CONN_UPG;
1930 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001931 }
1932
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001933 txn->flags |= TX_HDR_CONN_PRS;
1934 return;
1935}
Willy Tarreau5b154472009-12-21 20:11:07 +01001936
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001937/* Apply desired changes on the Connection: header. Values may be removed and/or
1938 * added depending on the <wanted> flags, which are exclusively composed of
1939 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1940 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1941 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001942void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001943{
1944 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001945 const char *hdr_val = "Connection";
1946 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001947
1948 ctx.idx = 0;
1949
Willy Tarreau88d349d2010-01-25 12:15:43 +01001950
1951 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1952 hdr_val = "Proxy-Connection";
1953 hdr_len = 16;
1954 }
1955
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001956 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001957 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001958 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1959 if (wanted & TX_CON_KAL_SET)
1960 txn->flags |= TX_CON_KAL_SET;
1961 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001962 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001963 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001964 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1965 if (wanted & TX_CON_CLO_SET)
1966 txn->flags |= TX_CON_CLO_SET;
1967 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001968 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001969 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001970 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001971
1972 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1973 return;
1974
1975 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1976 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001977 hdr_val = "Connection: close";
1978 hdr_len = 17;
1979 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1980 hdr_val = "Proxy-Connection: close";
1981 hdr_len = 23;
1982 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001983 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001984 }
1985
1986 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1987 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001988 hdr_val = "Connection: keep-alive";
1989 hdr_len = 22;
1990 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1991 hdr_val = "Proxy-Connection: keep-alive";
1992 hdr_len = 28;
1993 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001994 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001995 }
1996 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001997}
1998
Willy Tarreauc24715e2014-04-17 15:21:20 +02001999/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2000 * the first byte of data after the chunk size, so that we know we can forward
2001 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2002 * the chunk size. That way it is always possible to differentiate between the
2003 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002004 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002005 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002006 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002007static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002008{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002009 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002010 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002011 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002012 const char *end = buf->data + buf->size;
2013 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002014 unsigned int chunk = 0;
2015
2016 /* The chunk size is in the following form, though we are only
2017 * interested in the size and CRLF :
2018 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2019 */
2020 while (1) {
2021 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002022 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002023 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002024 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002025 if (c < 0) /* not a hex digit anymore */
2026 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002027 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002028 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002029 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002030 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002031 chunk = (chunk << 4) + c;
2032 }
2033
Willy Tarreaud98cf932009-12-27 22:54:55 +01002034 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002035 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002036 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037
2038 while (http_is_spht[(unsigned char)*ptr]) {
2039 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002040 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002041 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002042 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002043 }
2044
Willy Tarreaud98cf932009-12-27 22:54:55 +01002045 /* Up to there, we know that at least one byte is present at *ptr. Check
2046 * for the end of chunk size.
2047 */
2048 while (1) {
2049 if (likely(HTTP_IS_CRLF(*ptr))) {
2050 /* we now have a CR or an LF at ptr */
2051 if (likely(*ptr == '\r')) {
2052 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002053 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002054 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002055 return 0;
2056 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002057
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002059 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002061 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002062 /* done */
2063 break;
2064 }
2065 else if (*ptr == ';') {
2066 /* chunk extension, ends at next CRLF */
2067 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002068 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002069 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002070 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002071
2072 while (!HTTP_IS_CRLF(*ptr)) {
2073 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002074 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002075 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002076 return 0;
2077 }
2078 /* we have a CRLF now, loop above */
2079 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002080 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002081 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002082 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002083 }
2084
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002086 * which may or may not be present. We save that into ->next,
2087 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002088 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002089 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002090 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002091 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002092 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002093 msg->chunk_len = chunk;
2094 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002096 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002097 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002098 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002099 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002100}
2101
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002102/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002103 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002104 * the trailers is found, it is automatically scheduled to be forwarded,
2105 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2106 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002107 * except maybe msg->next if it could parse some lines, and returns zero.
2108 * If a parse error is encountered, the function returns < 0 and does not
2109 * change anything except maybe msg->next. Note that the message must
2110 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002111 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002112 * forwarding, and that msg->next exactly matches the length of trailers
2113 * already parsed and not forwarded. It is also important to note that this
2114 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002115 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002116static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002117{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002118 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002119
Willy Tarreaua458b672012-03-05 11:17:50 +01002120 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002121 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002122 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002123 const char *ptr = b_ptr(buf, msg->next);
2124 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002125 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002126
2127 /* scan current line and stop at LF or CRLF */
2128 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002129 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002130 return 0;
2131
2132 if (*ptr == '\n') {
2133 if (!p1)
2134 p1 = ptr;
2135 p2 = ptr;
2136 break;
2137 }
2138
2139 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002140 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002141 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002142 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002143 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002144 p1 = ptr;
2145 }
2146
2147 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002148 if (ptr >= buf->data + buf->size)
2149 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002150 }
2151
2152 /* after LF; point to beginning of next line */
2153 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002154 if (p2 >= buf->data + buf->size)
2155 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002156
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002157 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002158 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002159 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002160
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002161 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002162 /* LF/CRLF at beginning of line => end of trailers at p2.
2163 * Everything was scheduled for forwarding, there's nothing
2164 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002165 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002166 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002167 msg->msg_state = HTTP_MSG_DONE;
2168 return 1;
2169 }
2170 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002171 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002172 }
2173}
2174
Willy Tarreauc24715e2014-04-17 15:21:20 +02002175/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2176 * or a possible LF alone at the end of a chunk. It automatically adjusts
2177 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002178 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002179 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2180 * not enough data are available, the function does not change anything and
2181 * returns zero. If a parse error is encountered, the function returns < 0 and
2182 * does not change anything. Note: this function is designed to parse wrapped
2183 * CRLF at the end of the buffer.
2184 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002185static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002186{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002187 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002188 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002189 int bytes;
2190
2191 /* NB: we'll check data availabilty at the end. It's not a
2192 * problem because whatever we match first will be checked
2193 * against the correct length.
2194 */
2195 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002196 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 if (*ptr == '\r') {
2198 bytes++;
2199 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002200 if (ptr >= buf->data + buf->size)
2201 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002202 }
2203
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002204 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002205 return 0;
2206
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002207 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002208 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002209 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002210 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002211
2212 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002213 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002214 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002215 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002216 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002217 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2218 return 1;
2219}
Willy Tarreau5b154472009-12-21 20:11:07 +01002220
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002221/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2222 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2223 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2224 * Unparsable qvalues return 1000 as "q=1.000".
2225 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002226int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002227{
2228 int q = 1000;
2229
Willy Tarreau506c69a2014-07-08 00:59:48 +02002230 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002231 goto out;
2232 q = (*qvalue++ - '0') * 1000;
2233
2234 if (*qvalue++ != '.')
2235 goto out;
2236
Willy Tarreau506c69a2014-07-08 00:59:48 +02002237 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002238 goto out;
2239 q += (*qvalue++ - '0') * 100;
2240
Willy Tarreau506c69a2014-07-08 00:59:48 +02002241 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002242 goto out;
2243 q += (*qvalue++ - '0') * 10;
2244
Willy Tarreau506c69a2014-07-08 00:59:48 +02002245 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002246 goto out;
2247 q += (*qvalue++ - '0') * 1;
2248 out:
2249 if (q > 1000)
2250 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002251 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002252 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002253 return q;
2254}
William Lallemand82fe75c2012-10-23 10:25:10 +02002255
2256/*
2257 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002258 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002259int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002260{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002261 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002262 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002263 struct hdr_ctx ctx;
2264 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002265 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002266
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002267 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2268 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002269 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2270 */
2271 ctx.idx = 0;
2272 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2273 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002274 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2275 (ctx.vlen < 31 ||
2276 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2277 ctx.line[ctx.val + 30] < '6' ||
2278 (ctx.line[ctx.val + 30] == '6' &&
2279 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2280 s->comp_algo = NULL;
2281 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002282 }
2283
William Lallemand82fe75c2012-10-23 10:25:10 +02002284 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002285 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002286 int best_q = 0;
2287
William Lallemand82fe75c2012-10-23 10:25:10 +02002288 ctx.idx = 0;
2289 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002290 const char *qval;
2291 int q;
2292 int toklen;
2293
2294 /* try to isolate the token from the optional q-value */
2295 toklen = 0;
2296 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2297 toklen++;
2298
2299 qval = ctx.line + ctx.val + toklen;
2300 while (1) {
2301 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2302 qval++;
2303
2304 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2305 qval = NULL;
2306 break;
2307 }
2308 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002309
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002310 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2311 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002312
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002313 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2314 qval = NULL;
2315 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002316 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002317 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2318 break;
2319
2320 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2321 qval++;
2322 }
2323
2324 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002325 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002326
2327 if (q <= best_q)
2328 continue;
2329
2330 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2331 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002332 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002333 s->comp_algo = comp_algo;
2334 best_q = q;
2335 break;
2336 }
2337 }
2338 }
2339 }
2340
2341 /* remove all occurrences of the header when "compression offload" is set */
2342 if (s->comp_algo) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002343 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002344 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2345 ctx.idx = 0;
2346 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2347 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002348 }
2349 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002350 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002351 }
2352
2353 /* identity is implicit does not require headers */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002354 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002355 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002356 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002357 s->comp_algo = comp_algo;
2358 return 1;
2359 }
2360 }
2361 }
2362
2363 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002364 return 0;
2365}
2366
2367/*
2368 * Selects a comression algorithm depending of the server response.
2369 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002370int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002371{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002372 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002373 struct http_msg *msg = &txn->rsp;
2374 struct hdr_ctx ctx;
2375 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002376
2377 /* no common compression algorithm was found in request header */
2378 if (s->comp_algo == NULL)
2379 goto fail;
2380
2381 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002382 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002383 goto fail;
2384
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002385 /* compress 200,201,202,203 responses only */
2386 if ((txn->status != 200) &&
2387 (txn->status != 201) &&
2388 (txn->status != 202) &&
2389 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002390 goto fail;
2391
William Lallemand82fe75c2012-10-23 10:25:10 +02002392 /* Content-Length is null */
2393 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2394 goto fail;
2395
2396 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002397 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002398 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2399 goto fail;
2400
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002401 /* no compression when Cache-Control: no-transform is present in the message */
2402 ctx.idx = 0;
2403 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2404 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2405 goto fail;
2406 }
2407
William Lallemand82fe75c2012-10-23 10:25:10 +02002408 comp_type = NULL;
2409
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002410 /* we don't want to compress multipart content-types, nor content-types that are
2411 * not listed in the "compression type" directive if any. If no content-type was
2412 * found but configuration requires one, we don't compress either. Backend has
2413 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002414 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002415 ctx.idx = 0;
2416 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2417 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2418 goto fail;
2419
2420 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002421 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002422 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002423 if (ctx.vlen >= comp_type->name_len &&
2424 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002425 /* this Content-Type should be compressed */
2426 break;
2427 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002428 /* this Content-Type should not be compressed */
2429 if (comp_type == NULL)
2430 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002431 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002432 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002433 else { /* no content-type header */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002434 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002435 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002436 }
2437
William Lallemandd85f9172012-11-09 17:05:39 +01002438 /* limit compression rate */
2439 if (global.comp_rate_lim > 0)
2440 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2441 goto fail;
2442
William Lallemand072a2bf2012-11-20 17:01:01 +01002443 /* limit cpu usage */
2444 if (idle_pct < compress_min_idle)
2445 goto fail;
2446
William Lallemand4c49fae2012-11-07 15:00:23 +01002447 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002448 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002449 goto fail;
2450
Willy Tarreaue7dff022015-04-03 01:14:29 +02002451 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002452
William Lallemand82fe75c2012-10-23 10:25:10 +02002453 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002454 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002455 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2456 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2457
2458 /* add Transfer-Encoding header */
2459 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2460 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2461
2462 /*
2463 * Add Content-Encoding header when it's not identity encoding.
2464 * RFC 2616 : Identity encoding: This content-coding is used only in the
2465 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2466 * header.
2467 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002468 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002469 trash.len = 18;
2470 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002471 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2472 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002473 trash.str[trash.len] = '\0';
2474 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002475 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002476 return 1;
2477
2478fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002479 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002480 return 0;
2481}
2482
Willy Tarreau87b09662015-04-03 00:22:06 +02002483void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002484{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002485 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002486 int tmp = TX_CON_WANT_KAL;
2487
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002488 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2489 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002490 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2491 tmp = TX_CON_WANT_TUN;
2492
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002493 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002494 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2495 tmp = TX_CON_WANT_TUN;
2496 }
2497
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002498 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002499 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2500 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002501 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002502 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2503 tmp = TX_CON_WANT_CLO;
2504 else
2505 tmp = TX_CON_WANT_SCL;
2506 }
2507
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002508 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002509 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2510 tmp = TX_CON_WANT_CLO;
2511
2512 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2513 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2514
2515 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2516 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2517 /* parse the Connection header and possibly clean it */
2518 int to_del = 0;
2519 if ((msg->flags & HTTP_MSGF_VER_11) ||
2520 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002521 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002522 to_del |= 2; /* remove "keep-alive" */
2523 if (!(msg->flags & HTTP_MSGF_VER_11))
2524 to_del |= 1; /* remove "close" */
2525 http_parse_connection_header(txn, msg, to_del);
2526 }
2527
2528 /* check if client or config asks for explicit close in KAL/SCL */
2529 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2530 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2531 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2532 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2533 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002534 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002535 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2536}
William Lallemand82fe75c2012-10-23 10:25:10 +02002537
Willy Tarreaud787e662009-07-07 10:14:51 +02002538/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2539 * processing can continue on next analysers, or zero if it either needs more
2540 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002541 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002542 * when it has nothing left to do, and may remove any analyser when it wants to
2543 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002544 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002545int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002546{
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 /*
2548 * We will parse the partial (or complete) lines.
2549 * We will check the request syntax, and also join multi-line
2550 * headers. An index of all the lines will be elaborated while
2551 * parsing.
2552 *
2553 * For the parsing, we use a 28 states FSM.
2554 *
2555 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002556 * req->buf->p = beginning of request
2557 * req->buf->p + msg->eoh = end of processed headers / start of current one
2558 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002559 * msg->eol = end of current header or line (LF or CRLF)
2560 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002561 *
2562 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002563 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002564 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2565 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002566 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002567
Willy Tarreau59234e92008-11-30 23:51:27 +01002568 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002569 int use_close_only;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002570 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002571 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002573 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002574
Willy Tarreau87b09662015-04-03 00:22:06 +02002575 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002576 now_ms, __FUNCTION__,
2577 s,
2578 req,
2579 req->rex, req->wex,
2580 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002581 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002582 req->analysers);
2583
Willy Tarreau52a0c602009-08-16 22:45:38 +02002584 /* we're speaking HTTP here, so let's speak HTTP to the client */
2585 s->srv_error = http_return_srv_error;
2586
Willy Tarreau83e3af02009-12-28 17:39:57 +01002587 /* There's a protected area at the end of the buffer for rewriting
2588 * purposes. We don't want to start to parse the request if the
2589 * protected area is affected, because we may have to move processed
2590 * data later, which is much more complicated.
2591 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002592 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002593 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002594 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002595 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002596 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002597 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002598 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002599 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002600 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002601 return 0;
2602 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002603 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2604 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2605 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002606 }
2607
Willy Tarreau065e8332010-01-08 00:30:20 +01002608 /* Note that we have the same problem with the response ; we
2609 * may want to send a redirect, error or anything which requires
2610 * some spare space. So we'll ensure that we have at least
2611 * maxrewrite bytes available in the response buffer before
2612 * processing that one. This will only affect pipelined
2613 * keep-alive requests.
2614 */
2615 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002616 unlikely(!channel_is_rewritable(&s->res) ||
2617 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2618 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2619 if (s->res.buf->o) {
2620 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002621 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002622 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002623 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002624 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2625 s->res.flags |= CF_WAKE_WRITE;
2626 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002627 return 0;
2628 }
2629 }
2630
Willy Tarreau9b28e032012-10-12 23:49:43 +02002631 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002632 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002633 }
2634
Willy Tarreau59234e92008-11-30 23:51:27 +01002635 /* 1: we might have to print this header in debug mode */
2636 if (unlikely((global.mode & MODE_DEBUG) &&
2637 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002638 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002639 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002640
Willy Tarreau9b28e032012-10-12 23:49:43 +02002641 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002642 /* this is a bit complex : in case of error on the request line,
2643 * we know that rq.l is still zero, so we display only the part
2644 * up to the end of the line (truncated by debug_hdr).
2645 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002646 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002647 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002648
Willy Tarreau59234e92008-11-30 23:51:27 +01002649 sol += hdr_idx_first_pos(&txn->hdr_idx);
2650 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002651
Willy Tarreau59234e92008-11-30 23:51:27 +01002652 while (cur_idx) {
2653 eol = sol + txn->hdr_idx.v[cur_idx].len;
2654 debug_hdr("clihdr", s, sol, eol);
2655 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2656 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002657 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002658 }
2659
Willy Tarreau58f10d72006-12-04 02:26:12 +01002660
Willy Tarreau59234e92008-11-30 23:51:27 +01002661 /*
2662 * Now we quickly check if we have found a full valid request.
2663 * If not so, we check the FD and buffer states before leaving.
2664 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002665 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002666 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002667 * on a keep-alive stream, if we encounter and error, close, t/o,
2668 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002669 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002670 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002671 * Last, we may increase some tracked counters' http request errors on
2672 * the cases that are deliberately the client's fault. For instance,
2673 * a timeout or connection reset is not counted as an error. However
2674 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002675 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002676
Willy Tarreau655dce92009-11-08 13:10:58 +01002677 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002678 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002679 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002680 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002681 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002682 stream_inc_http_req_ctr(s);
2683 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002684 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002686 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002687
Willy Tarreau59234e92008-11-30 23:51:27 +01002688 /* 1: Since we are in header mode, if there's no space
2689 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002690 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002691 * must terminate it now.
2692 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002693 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002694 /* FIXME: check if URI is set and return Status
2695 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002696 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002697 stream_inc_http_req_ctr(s);
2698 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002699 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002700 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002701 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002702 goto return_bad_req;
2703 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002704
Willy Tarreau59234e92008-11-30 23:51:27 +01002705 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002706 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002707 if (!(s->flags & SF_ERR_MASK))
2708 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002709
Willy Tarreaufcffa692010-01-10 14:21:19 +01002710 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002711 goto failed_keep_alive;
2712
Willy Tarreau59234e92008-11-30 23:51:27 +01002713 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002714 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002715 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002716 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002717 }
2718
Willy Tarreaudc979f22012-12-04 10:39:01 +01002719 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002720 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002721 msg->msg_state = HTTP_MSG_ERROR;
2722 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002723
Willy Tarreau87b09662015-04-03 00:22:06 +02002724 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002725 proxy_inc_fe_req_ctr(sess->fe);
2726 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002727 if (sess->listener->counters)
2728 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002729
Willy Tarreaue7dff022015-04-03 01:14:29 +02002730 if (!(s->flags & SF_FINST_MASK))
2731 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 return 0;
2733 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002734
Willy Tarreau59234e92008-11-30 23:51:27 +01002735 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002736 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002737 if (!(s->flags & SF_ERR_MASK))
2738 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002739
Willy Tarreaufcffa692010-01-10 14:21:19 +01002740 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002741 goto failed_keep_alive;
2742
Willy Tarreau59234e92008-11-30 23:51:27 +01002743 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002744 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002745 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002746 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002747 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002748 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002749 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002750 msg->msg_state = HTTP_MSG_ERROR;
2751 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002752
Willy Tarreau87b09662015-04-03 00:22:06 +02002753 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002754 proxy_inc_fe_req_ctr(sess->fe);
2755 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002756 if (sess->listener->counters)
2757 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002758
Willy Tarreaue7dff022015-04-03 01:14:29 +02002759 if (!(s->flags & SF_FINST_MASK))
2760 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002761 return 0;
2762 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002763
Willy Tarreau59234e92008-11-30 23:51:27 +01002764 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002765 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002766 if (!(s->flags & SF_ERR_MASK))
2767 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002768
Willy Tarreaufcffa692010-01-10 14:21:19 +01002769 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002770 goto failed_keep_alive;
2771
Willy Tarreau4076a152009-04-02 15:18:36 +02002772 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002773 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002774 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002775 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002776 msg->msg_state = HTTP_MSG_ERROR;
2777 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002778
Willy Tarreau87b09662015-04-03 00:22:06 +02002779 stream_inc_http_err_ctr(s);
2780 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002781 proxy_inc_fe_req_ctr(sess->fe);
2782 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002783 if (sess->listener->counters)
2784 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002785
Willy Tarreaue7dff022015-04-03 01:14:29 +02002786 if (!(s->flags & SF_FINST_MASK))
2787 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002788 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002789 }
2790
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002791 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002792 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002793 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002794#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002795 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2796 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002797 /* We need more data, we have to re-enable quick-ack in case we
2798 * previously disabled it, otherwise we might cause the client
2799 * to delay next data.
2800 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002801 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002802 }
2803#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002804
Willy Tarreaufcffa692010-01-10 14:21:19 +01002805 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2806 /* If the client starts to talk, let's fall back to
2807 * request timeout processing.
2808 */
2809 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002810 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002811 }
2812
Willy Tarreau59234e92008-11-30 23:51:27 +01002813 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002814 if (!tick_isset(req->analyse_exp)) {
2815 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2816 (txn->flags & TX_WAIT_NEXT_RQ) &&
2817 tick_isset(s->be->timeout.httpka))
2818 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2819 else
2820 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2821 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002822
Willy Tarreau59234e92008-11-30 23:51:27 +01002823 /* we're not ready yet */
2824 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002825
2826 failed_keep_alive:
2827 /* Here we process low-level errors for keep-alive requests. In
2828 * short, if the request is not the first one and it experiences
2829 * a timeout, read error or shutdown, we just silently close so
2830 * that the client can try again.
2831 */
2832 txn->status = 0;
2833 msg->msg_state = HTTP_MSG_RQBEFORE;
2834 req->analysers = 0;
2835 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002836 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002837 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002838 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002839 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002840 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002841
Willy Tarreaud787e662009-07-07 10:14:51 +02002842 /* OK now we have a complete HTTP request with indexed headers. Let's
2843 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002844 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002845 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002846 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002847 * byte after the last LF. msg->sov points to the first byte of data.
2848 * msg->eol cannot be trusted because it may have been left uninitialized
2849 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002850 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002851
Willy Tarreau87b09662015-04-03 00:22:06 +02002852 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002853 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002854
Willy Tarreaub16a5742010-01-10 14:46:16 +01002855 if (txn->flags & TX_WAIT_NEXT_RQ) {
2856 /* kill the pending keep-alive timeout */
2857 txn->flags &= ~TX_WAIT_NEXT_RQ;
2858 req->analyse_exp = TICK_ETERNITY;
2859 }
2860
2861
Willy Tarreaud787e662009-07-07 10:14:51 +02002862 /* Maybe we found in invalid header name while we were configured not
2863 * to block on that, so we have to capture it now.
2864 */
2865 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002866 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002867
Willy Tarreau59234e92008-11-30 23:51:27 +01002868 /*
2869 * 1: identify the method
2870 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002871 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002872
2873 /* we can make use of server redirect on GET and HEAD */
2874 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002875 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002876
Willy Tarreau59234e92008-11-30 23:51:27 +01002877 /*
2878 * 2: check if the URI matches the monitor_uri.
2879 * We have to do this for every request which gets in, because
2880 * the monitor-uri is defined by the frontend.
2881 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002882 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2883 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002884 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002885 sess->fe->monitor_uri,
2886 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002887 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002888 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002889 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002890 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002891
Willy Tarreaue7dff022015-04-03 01:14:29 +02002892 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002893 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002894
Willy Tarreau59234e92008-11-30 23:51:27 +01002895 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002896 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002897 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002898
Willy Tarreau59234e92008-11-30 23:51:27 +01002899 ret = acl_pass(ret);
2900 if (cond->pol == ACL_COND_UNLESS)
2901 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002902
Willy Tarreau59234e92008-11-30 23:51:27 +01002903 if (ret) {
2904 /* we fail this request, let's return 503 service unavail */
2905 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002906 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002907 if (!(s->flags & SF_ERR_MASK))
2908 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002909 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002910 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002911 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002912
Willy Tarreau59234e92008-11-30 23:51:27 +01002913 /* nothing to fail, let's reply normaly */
2914 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01002915 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002916 if (!(s->flags & SF_ERR_MASK))
2917 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002918 goto return_prx_cond;
2919 }
2920
2921 /*
2922 * 3: Maybe we have to copy the original REQURI for the logs ?
2923 * Note: we cannot log anymore if the request has been
2924 * classified as invalid.
2925 */
2926 if (unlikely(s->logs.logwait & LW_REQ)) {
2927 /* we have a complete HTTP request that we must log */
2928 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2929 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002930
Willy Tarreau59234e92008-11-30 23:51:27 +01002931 if (urilen >= REQURI_LEN)
2932 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002933 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002934 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002935
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002936 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002937 s->do_log(s);
2938 } else {
2939 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002940 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002941 }
Willy Tarreau06619262006-12-17 08:37:22 +01002942
Willy Tarreau59234e92008-11-30 23:51:27 +01002943 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002944 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002945 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002946
Willy Tarreau5b154472009-12-21 20:11:07 +01002947 /* ... and check if the request is HTTP/1.1 or above */
2948 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002949 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2950 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2951 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002952 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002953
2954 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002955 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002956
Willy Tarreau88d349d2010-01-25 12:15:43 +01002957 /* if the frontend has "option http-use-proxy-header", we'll check if
2958 * we have what looks like a proxied connection instead of a connection,
2959 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2960 * Note that this is *not* RFC-compliant, however browsers and proxies
2961 * happen to do that despite being non-standard :-(
2962 * We consider that a request not beginning with either '/' or '*' is
2963 * a proxied connection, which covers both "scheme://location" and
2964 * CONNECT ip:port.
2965 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002966 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002967 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002968 txn->flags |= TX_USE_PX_CONN;
2969
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002970 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002971 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002972
Willy Tarreau59234e92008-11-30 23:51:27 +01002973 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002974 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002975 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002976 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002977
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002978 /* 6: determine the transfer-length.
2979 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2980 * the presence of a message-body in a REQUEST and its transfer length
2981 * must be determined that way (in order of precedence) :
2982 * 1. The presence of a message-body in a request is signaled by the
2983 * inclusion of a Content-Length or Transfer-Encoding header field
2984 * in the request's header fields. When a request message contains
2985 * both a message-body of non-zero length and a method that does
2986 * not define any semantics for that request message-body, then an
2987 * origin server SHOULD either ignore the message-body or respond
2988 * with an appropriate error message (e.g., 413). A proxy or
2989 * gateway, when presented the same request, SHOULD either forward
2990 * the request inbound with the message- body or ignore the
2991 * message-body when determining a response.
2992 *
2993 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2994 * and the "chunked" transfer-coding (Section 6.2) is used, the
2995 * transfer-length is defined by the use of this transfer-coding.
2996 * If a Transfer-Encoding header field is present and the "chunked"
2997 * transfer-coding is not present, the transfer-length is defined
2998 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002999 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003000 * 3. If a Content-Length header field is present, its decimal value in
3001 * OCTETs represents both the entity-length and the transfer-length.
3002 * If a message is received with both a Transfer-Encoding header
3003 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003004 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003005 * 4. By the server closing the connection. (Closing the connection
3006 * cannot be used to indicate the end of a request body, since that
3007 * would leave no possibility for the server to send back a response.)
3008 *
3009 * Whenever a transfer-coding is applied to a message-body, the set of
3010 * transfer-codings MUST include "chunked", unless the message indicates
3011 * it is terminated by closing the connection. When the "chunked"
3012 * transfer-coding is used, it MUST be the last transfer-coding applied
3013 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003014 */
3015
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003016 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003017 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003018 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003019 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003020 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003021 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003022 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3023 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003024 /* bad transfer-encoding (chunked followed by something else) */
3025 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003026 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003027 break;
3028 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003029 }
3030
Willy Tarreau32b47f42009-10-18 20:55:02 +02003031 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003032 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003033 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003034 signed long long cl;
3035
Willy Tarreauad14f752011-09-02 20:33:27 +02003036 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003037 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003038 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003039 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003040
Willy Tarreauad14f752011-09-02 20:33:27 +02003041 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003042 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003043 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003044 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003045
Willy Tarreauad14f752011-09-02 20:33:27 +02003046 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003047 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003048 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003049 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003050
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003051 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003052 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003053 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003054 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003055
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003056 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003057 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003058 }
3059
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003060 /* bodyless requests have a known length */
3061 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003062 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003063
Willy Tarreau179085c2014-04-28 16:48:56 +02003064 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3065 * only change if both the request and the config reference something else.
3066 * Option httpclose by itself sets tunnel mode where headers are mangled.
3067 * However, if another mode is set, it will affect it (eg: server-close/
3068 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3069 * if FE and BE have the same settings (common). The method consists in
3070 * checking if options changed between the two calls (implying that either
3071 * one is non-null, or one of them is non-null and we are there for the first
3072 * time.
3073 */
3074 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003075 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003076 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003077
Willy Tarreaud787e662009-07-07 10:14:51 +02003078 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003079 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003080 req->analyse_exp = TICK_ETERNITY;
3081 return 1;
3082
3083 return_bad_req:
3084 /* We centralize bad requests processing here */
3085 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3086 /* we detected a parsing error. We want to archive this request
3087 * in the dedicated proxy area for later troubleshooting.
3088 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003089 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003090 }
3091
3092 txn->req.msg_state = HTTP_MSG_ERROR;
3093 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003094 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003095
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003096 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003097 if (sess->listener->counters)
3098 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003099
3100 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003101 if (!(s->flags & SF_ERR_MASK))
3102 s->flags |= SF_ERR_PRXCOND;
3103 if (!(s->flags & SF_FINST_MASK))
3104 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003105
3106 req->analysers = 0;
3107 req->analyse_exp = TICK_ETERNITY;
3108 return 0;
3109}
3110
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003111
Willy Tarreau347a35d2013-11-22 17:51:09 +01003112/* This function prepares an applet to handle the stats. It can deal with the
3113 * "100-continue" expectation, check that admin rules are met for POST requests,
3114 * and program a response message if something was unexpected. It cannot fail
3115 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003116 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003117 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003118 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003119 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003120int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003121{
3122 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003123 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003124 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003125 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003126 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003127 struct uri_auth *uri_auth = s->be->uri_auth;
3128 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003129 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003130
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003131 appctx = si_appctx(si);
3132 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3133 appctx->st1 = appctx->st2 = 0;
3134 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3135 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003136 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003137 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003138
3139 uri = msg->chn->buf->p + msg->sl.rq.u;
3140 lookup = uri + uri_auth->uri_len;
3141
3142 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3143 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003144 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003145 break;
3146 }
3147 }
3148
3149 if (uri_auth->refresh) {
3150 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3151 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003152 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003153 break;
3154 }
3155 }
3156 }
3157
3158 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3159 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003160 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003161 break;
3162 }
3163 }
3164
3165 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3166 if (memcmp(h, ";st=", 4) == 0) {
3167 int i;
3168 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003169 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003170 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3171 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003172 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003173 break;
3174 }
3175 }
3176 break;
3177 }
3178 }
3179
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003180 appctx->ctx.stats.scope_str = 0;
3181 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003182 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3183 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3184 int itx = 0;
3185 const char *h2;
3186 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3187 const char *err;
3188
3189 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3190 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003191 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003192 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3193 itx++;
3194 h++;
3195 }
3196
3197 if (itx > STAT_SCOPE_TXT_MAXLEN)
3198 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003199 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003200
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003201 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003202 memcpy(scope_txt, h2, itx);
3203 scope_txt[itx] = '\0';
3204 err = invalid_char(scope_txt);
3205 if (err) {
3206 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003207 appctx->ctx.stats.scope_str = 0;
3208 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003209 }
3210 break;
3211 }
3212 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003213
3214 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003215 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003216 int ret = 1;
3217
3218 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003219 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 +01003220 ret = acl_pass(ret);
3221 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3222 ret = !ret;
3223 }
3224
3225 if (ret) {
3226 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003227 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003228 break;
3229 }
3230 }
3231
3232 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003233 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003234 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003235 /* we'll need the request body, possibly after sending 100-continue */
3236 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003237 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003238 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003239 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003240 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3241 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003242 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003243 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003244 else {
3245 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003246 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003247 }
3248
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003249 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003250 return 1;
3251}
3252
Lukas Tribus67db8df2013-06-23 17:37:13 +02003253/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3254 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3255 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003256void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003257{
3258#ifdef IP_TOS
3259 if (from.ss_family == AF_INET)
3260 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3261#endif
3262#ifdef IPV6_TCLASS
3263 if (from.ss_family == AF_INET6) {
3264 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3265 /* v4-mapped addresses need IP_TOS */
3266 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3267 else
3268 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3269 }
3270#endif
3271}
3272
Willy Tarreau87b09662015-04-03 00:22:06 +02003273int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003274 const char* name, unsigned int name_len,
3275 const char *str, struct my_regex *re,
3276 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003277{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003278 struct hdr_ctx ctx;
3279 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003280 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003281 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3282 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003283 struct chunk *output = get_trash_chunk();
3284
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003285 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003286
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003287 /* Choose the header browsing function. */
3288 switch (action) {
3289 case HTTP_REQ_ACT_REPLACE_VAL:
3290 case HTTP_RES_ACT_REPLACE_VAL:
3291 http_find_hdr_func = http_find_header2;
3292 break;
3293 case HTTP_REQ_ACT_REPLACE_HDR:
3294 case HTTP_RES_ACT_REPLACE_HDR:
3295 http_find_hdr_func = http_find_full_header2;
3296 break;
3297 default: /* impossible */
3298 return -1;
3299 }
3300
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003301 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3302 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003303 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003304 char *val = ctx.line + ctx.val;
3305 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003306
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003307 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3308 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003309
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003310 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003311 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003312 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003313
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003314 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003315
3316 hdr->len += delta;
3317 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003318
3319 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003320 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003321 }
3322
3323 return 0;
3324}
3325
Willy Tarreau87b09662015-04-03 00:22:06 +02003326static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003327 const char* name, unsigned int name_len,
3328 struct list *fmt, struct my_regex *re,
3329 int action)
3330{
3331 struct chunk *replace = get_trash_chunk();
3332
3333 replace->len = build_logline(s, replace->str, replace->size, fmt);
3334 if (replace->len >= replace->size - 1)
3335 return -1;
3336
3337 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3338}
3339
Willy Tarreau87b09662015-04-03 00:22:06 +02003340/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003341 * transaction <txn>. Returns the verdict of the first rule that prevents
3342 * further processing of the request (auth, deny, ...), and defaults to
3343 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3344 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3345 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003346 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003347enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003348http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003349{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003350 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003351 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003352 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003353 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003354 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003355 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003356
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003357 /* If "the current_rule_list" match the executed rule list, we are in
3358 * resume condition. If a resume is needed it is always in the action
3359 * and never in the ACL or converters. In this case, we initialise the
3360 * current rule, and go to the action execution point.
3361 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003362 if (s->current_rule) {
3363 rule = s->current_rule;
3364 s->current_rule = NULL;
3365 if (s->current_rule_list == rules)
3366 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003367 }
3368 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003369
Willy Tarreauff011f22011-01-06 17:51:27 +01003370 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003371 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003372 continue;
3373
Willy Tarreau96257ec2012-12-27 10:46:37 +01003374 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003375 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003376 int ret;
3377
Willy Tarreau192252e2015-04-04 01:47:55 +02003378 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003379 ret = acl_pass(ret);
3380
Willy Tarreauff011f22011-01-06 17:51:27 +01003381 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003382 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003383
3384 if (!ret) /* condition not matched */
3385 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003386 }
3387
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003388resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003389 switch (rule->action) {
3390 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003391 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003392
3393 case HTTP_REQ_ACT_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003394 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003395 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003396
Willy Tarreauccbcc372012-12-27 12:37:57 +01003397 case HTTP_REQ_ACT_TARPIT:
3398 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003399 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003400 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003401
Willy Tarreau96257ec2012-12-27 10:46:37 +01003402 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003403 /* Auth might be performed on regular http-req rules as well as on stats */
3404 auth_realm = rule->arg.auth.realm;
3405 if (!auth_realm) {
3406 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3407 auth_realm = STATS_DEFAULT_REALM;
3408 else
3409 auth_realm = px->id;
3410 }
3411 /* send 401/407 depending on whether we use a proxy or not. We still
3412 * count one error, because normal browsing won't significantly
3413 * increase the counter but brute force attempts will.
3414 */
3415 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3416 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3417 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003418 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003419 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003420
Willy Tarreau81499eb2012-12-27 12:19:02 +01003421 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003422 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3423 return HTTP_RULE_RES_BADREQ;
3424 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003425
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003426 case HTTP_REQ_ACT_SET_NICE:
3427 s->task->nice = rule->arg.nice;
3428 break;
3429
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003430 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003431 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003432 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003433 break;
3434
Willy Tarreau51347ed2013-06-11 19:34:13 +02003435 case HTTP_REQ_ACT_SET_MARK:
3436#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003437 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003438 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003439#endif
3440 break;
3441
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003442 case HTTP_REQ_ACT_SET_LOGL:
3443 s->logs.level = rule->arg.loglevel;
3444 break;
3445
Sasha Pachev218f0642014-06-16 12:05:59 -06003446 case HTTP_REQ_ACT_REPLACE_HDR:
3447 case HTTP_REQ_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003448 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3449 rule->arg.hdr_add.name_len,
3450 &rule->arg.hdr_add.fmt,
3451 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003452 return HTTP_RULE_RES_BADREQ;
3453 break;
3454
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003455 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003456 ctx.idx = 0;
3457 /* remove all occurrences of the header */
3458 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3459 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3460 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003461 }
Willy Tarreau85603282015-01-21 20:39:27 +01003462 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003463
Willy Tarreau85603282015-01-21 20:39:27 +01003464 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003465 case HTTP_REQ_ACT_ADD_HDR:
3466 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3467 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3468 trash.len = rule->arg.hdr_add.name_len;
3469 trash.str[trash.len++] = ':';
3470 trash.str[trash.len++] = ' ';
3471 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 +01003472
3473 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3474 /* remove all occurrences of the header */
3475 ctx.idx = 0;
3476 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3477 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3478 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3479 }
3480 }
3481
Willy Tarreau96257ec2012-12-27 10:46:37 +01003482 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3483 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003484
3485 case HTTP_REQ_ACT_DEL_ACL:
3486 case HTTP_REQ_ACT_DEL_MAP: {
3487 struct pat_ref *ref;
3488 char *key;
3489 int len;
3490
3491 /* collect reference */
3492 ref = pat_ref_lookup(rule->arg.map.ref);
3493 if (!ref)
3494 continue;
3495
3496 /* collect key */
3497 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3498 key = trash.str;
3499 key[len] = '\0';
3500
3501 /* perform update */
3502 /* returned code: 1=ok, 0=ko */
3503 pat_ref_delete(ref, key);
3504
3505 break;
3506 }
3507
3508 case HTTP_REQ_ACT_ADD_ACL: {
3509 struct pat_ref *ref;
3510 char *key;
3511 struct chunk *trash_key;
3512 int len;
3513
3514 trash_key = get_trash_chunk();
3515
3516 /* collect reference */
3517 ref = pat_ref_lookup(rule->arg.map.ref);
3518 if (!ref)
3519 continue;
3520
3521 /* collect key */
3522 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3523 key = trash_key->str;
3524 key[len] = '\0';
3525
3526 /* perform update */
3527 /* add entry only if it does not already exist */
3528 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003529 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003530
3531 break;
3532 }
3533
3534 case HTTP_REQ_ACT_SET_MAP: {
3535 struct pat_ref *ref;
3536 char *key, *value;
3537 struct chunk *trash_key, *trash_value;
3538 int len;
3539
3540 trash_key = get_trash_chunk();
3541 trash_value = get_trash_chunk();
3542
3543 /* collect reference */
3544 ref = pat_ref_lookup(rule->arg.map.ref);
3545 if (!ref)
3546 continue;
3547
3548 /* collect key */
3549 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3550 key = trash_key->str;
3551 key[len] = '\0';
3552
3553 /* collect value */
3554 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3555 value = trash_value->str;
3556 value[len] = '\0';
3557
3558 /* perform update */
3559 if (pat_ref_find_elt(ref, key) != NULL)
3560 /* update entry if it exists */
3561 pat_ref_set(ref, key, value, NULL);
3562 else
3563 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003564 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003565
3566 break;
3567 }
William Lallemand73025dd2014-04-24 14:38:37 +02003568
3569 case HTTP_REQ_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003570 if (!rule->action_ptr(rule, px, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003571 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003572 return HTTP_RULE_RES_YIELD;
3573 }
William Lallemand73025dd2014-04-24 14:38:37 +02003574 break;
3575
3576 case HTTP_REQ_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003577 rule->action_ptr(rule, px, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003578 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003579
3580 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3581 /* Note: only the first valid tracking parameter of each
3582 * applies.
3583 */
3584
3585 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3586 struct stktable *t;
3587 struct stksess *ts;
3588 struct stktable_key *key;
3589 void *ptr;
3590
3591 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau192252e2015-04-04 01:47:55 +02003592 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 +02003593
3594 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003595 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003596
3597 /* let's count a new HTTP request as it's the first time we do it */
3598 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3599 if (ptr)
3600 stktable_data_cast(ptr, http_req_cnt)++;
3601
3602 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3603 if (ptr)
3604 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3605 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3606
3607 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003608 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003609 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3610 }
3611 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003612 }
3613 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003614
3615 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003616 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003617}
3618
Willy Tarreau71241ab2012-12-27 11:30:54 +01003619
Willy Tarreau87b09662015-04-03 00:22:06 +02003620/* Executes the http-response rules <rules> for stream <s>, proxy <px> and
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003621 * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD
3622 * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the
3623 * evaluation of next rule list. If *STOP is returned, the process must stop
3624 * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny
3625 * rule. If *YIELD is returned, the czller must call again the function with
3626 * the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003627 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003628static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003629http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003630{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003631 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003632 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003633 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003634 struct http_res_rule *rule;
3635 struct hdr_ctx ctx;
3636
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003637 /* If "the current_rule_list" match the executed rule list, we are in
3638 * resume condition. If a resume is needed it is always in the action
3639 * and never in the ACL or converters. In this case, we initialise the
3640 * current rule, and go to the action execution point.
3641 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003642 if (s->current_rule) {
3643 rule = s->current_rule;
3644 s->current_rule = NULL;
3645 if (s->current_rule_list == rules)
3646 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003647 }
3648 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003649
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003650 list_for_each_entry(rule, rules, list) {
3651 if (rule->action >= HTTP_RES_ACT_MAX)
3652 continue;
3653
3654 /* check optional condition */
3655 if (rule->cond) {
3656 int ret;
3657
Willy Tarreau192252e2015-04-04 01:47:55 +02003658 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003659 ret = acl_pass(ret);
3660
3661 if (rule->cond->pol == ACL_COND_UNLESS)
3662 ret = !ret;
3663
3664 if (!ret) /* condition not matched */
3665 continue;
3666 }
3667
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003668resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003669 switch (rule->action) {
3670 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003671 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003672
3673 case HTTP_RES_ACT_DENY:
3674 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003675 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003676
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003677 case HTTP_RES_ACT_SET_NICE:
3678 s->task->nice = rule->arg.nice;
3679 break;
3680
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003681 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003682 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003683 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003684 break;
3685
Willy Tarreau51347ed2013-06-11 19:34:13 +02003686 case HTTP_RES_ACT_SET_MARK:
3687#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003688 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003689 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003690#endif
3691 break;
3692
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003693 case HTTP_RES_ACT_SET_LOGL:
3694 s->logs.level = rule->arg.loglevel;
3695 break;
3696
Sasha Pachev218f0642014-06-16 12:05:59 -06003697 case HTTP_RES_ACT_REPLACE_HDR:
3698 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003699 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3700 rule->arg.hdr_add.name_len,
3701 &rule->arg.hdr_add.fmt,
3702 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003703 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003704 break;
3705
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003706 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003707 ctx.idx = 0;
3708 /* remove all occurrences of the header */
3709 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3710 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3711 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3712 }
Willy Tarreau85603282015-01-21 20:39:27 +01003713 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003714
Willy Tarreau85603282015-01-21 20:39:27 +01003715 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003716 case HTTP_RES_ACT_ADD_HDR:
3717 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3718 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3719 trash.len = rule->arg.hdr_add.name_len;
3720 trash.str[trash.len++] = ':';
3721 trash.str[trash.len++] = ' ';
3722 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 +01003723
3724 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3725 /* remove all occurrences of the header */
3726 ctx.idx = 0;
3727 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3728 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3729 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3730 }
3731 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003732 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3733 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003734
3735 case HTTP_RES_ACT_DEL_ACL:
3736 case HTTP_RES_ACT_DEL_MAP: {
3737 struct pat_ref *ref;
3738 char *key;
3739 int len;
3740
3741 /* collect reference */
3742 ref = pat_ref_lookup(rule->arg.map.ref);
3743 if (!ref)
3744 continue;
3745
3746 /* collect key */
3747 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3748 key = trash.str;
3749 key[len] = '\0';
3750
3751 /* perform update */
3752 /* returned code: 1=ok, 0=ko */
3753 pat_ref_delete(ref, key);
3754
3755 break;
3756 }
3757
3758 case HTTP_RES_ACT_ADD_ACL: {
3759 struct pat_ref *ref;
3760 char *key;
3761 struct chunk *trash_key;
3762 int len;
3763
3764 trash_key = get_trash_chunk();
3765
3766 /* collect reference */
3767 ref = pat_ref_lookup(rule->arg.map.ref);
3768 if (!ref)
3769 continue;
3770
3771 /* collect key */
3772 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3773 key = trash_key->str;
3774 key[len] = '\0';
3775
3776 /* perform update */
3777 /* check if the entry already exists */
3778 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003779 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003780
3781 break;
3782 }
3783
3784 case HTTP_RES_ACT_SET_MAP: {
3785 struct pat_ref *ref;
3786 char *key, *value;
3787 struct chunk *trash_key, *trash_value;
3788 int len;
3789
3790 trash_key = get_trash_chunk();
3791 trash_value = get_trash_chunk();
3792
3793 /* collect reference */
3794 ref = pat_ref_lookup(rule->arg.map.ref);
3795 if (!ref)
3796 continue;
3797
3798 /* collect key */
3799 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3800 key = trash_key->str;
3801 key[len] = '\0';
3802
3803 /* collect value */
3804 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3805 value = trash_value->str;
3806 value[len] = '\0';
3807
3808 /* perform update */
3809 if (pat_ref_find_elt(ref, key) != NULL)
3810 /* update entry if it exists */
3811 pat_ref_set(ref, key, value, NULL);
3812 else
3813 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003814 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003815
3816 break;
3817 }
William Lallemand73025dd2014-04-24 14:38:37 +02003818
3819 case HTTP_RES_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003820 if (!rule->action_ptr(rule, px, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003821 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003822 return HTTP_RULE_RES_YIELD;
3823 }
William Lallemand73025dd2014-04-24 14:38:37 +02003824 break;
3825
3826 case HTTP_RES_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003827 rule->action_ptr(rule, px, s);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003828 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003829 }
3830 }
3831
3832 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003833 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003834}
3835
3836
Willy Tarreau71241ab2012-12-27 11:30:54 +01003837/* Perform an HTTP redirect based on the information in <rule>. The function
3838 * returns non-zero on success, or zero in case of a, irrecoverable error such
3839 * as too large a request to build a valid response.
3840 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003841static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003842{
3843 struct http_msg *msg = &txn->req;
3844 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003845 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003846
3847 /* build redirect message */
3848 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003849 case 308:
3850 msg_fmt = HTTP_308;
3851 break;
3852 case 307:
3853 msg_fmt = HTTP_307;
3854 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003855 case 303:
3856 msg_fmt = HTTP_303;
3857 break;
3858 case 301:
3859 msg_fmt = HTTP_301;
3860 break;
3861 case 302:
3862 default:
3863 msg_fmt = HTTP_302;
3864 break;
3865 }
3866
3867 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3868 return 0;
3869
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003870 location = trash.str + trash.len;
3871
Willy Tarreau71241ab2012-12-27 11:30:54 +01003872 switch(rule->type) {
3873 case REDIRECT_TYPE_SCHEME: {
3874 const char *path;
3875 const char *host;
3876 struct hdr_ctx ctx;
3877 int pathlen;
3878 int hostlen;
3879
3880 host = "";
3881 hostlen = 0;
3882 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003883 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003884 host = ctx.line + ctx.val;
3885 hostlen = ctx.vlen;
3886 }
3887
3888 path = http_get_path(txn);
3889 /* build message using path */
3890 if (path) {
3891 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3892 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3893 int qs = 0;
3894 while (qs < pathlen) {
3895 if (path[qs] == '?') {
3896 pathlen = qs;
3897 break;
3898 }
3899 qs++;
3900 }
3901 }
3902 } else {
3903 path = "/";
3904 pathlen = 1;
3905 }
3906
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003907 if (rule->rdr_str) { /* this is an old "redirect" rule */
3908 /* check if we can add scheme + "://" + host + path */
3909 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3910 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003911
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003912 /* add scheme */
3913 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3914 trash.len += rule->rdr_len;
3915 }
3916 else {
3917 /* add scheme with executing log format */
3918 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003919
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003920 /* check if we can add scheme + "://" + host + path */
3921 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3922 return 0;
3923 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003924 /* add "://" */
3925 memcpy(trash.str + trash.len, "://", 3);
3926 trash.len += 3;
3927
3928 /* add host */
3929 memcpy(trash.str + trash.len, host, hostlen);
3930 trash.len += hostlen;
3931
3932 /* add path */
3933 memcpy(trash.str + trash.len, path, pathlen);
3934 trash.len += pathlen;
3935
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003936 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003937 if (trash.len && trash.str[trash.len - 1] != '/' &&
3938 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3939 if (trash.len > trash.size - 5)
3940 return 0;
3941 trash.str[trash.len] = '/';
3942 trash.len++;
3943 }
3944
3945 break;
3946 }
3947 case REDIRECT_TYPE_PREFIX: {
3948 const char *path;
3949 int pathlen;
3950
3951 path = http_get_path(txn);
3952 /* build message using path */
3953 if (path) {
3954 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3955 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3956 int qs = 0;
3957 while (qs < pathlen) {
3958 if (path[qs] == '?') {
3959 pathlen = qs;
3960 break;
3961 }
3962 qs++;
3963 }
3964 }
3965 } else {
3966 path = "/";
3967 pathlen = 1;
3968 }
3969
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003970 if (rule->rdr_str) { /* this is an old "redirect" rule */
3971 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3972 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003973
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003974 /* add prefix. Note that if prefix == "/", we don't want to
3975 * add anything, otherwise it makes it hard for the user to
3976 * configure a self-redirection.
3977 */
3978 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3979 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3980 trash.len += rule->rdr_len;
3981 }
3982 }
3983 else {
3984 /* add prefix with executing log format */
3985 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3986
3987 /* Check length */
3988 if (trash.len + pathlen > trash.size - 4)
3989 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003990 }
3991
3992 /* add path */
3993 memcpy(trash.str + trash.len, path, pathlen);
3994 trash.len += pathlen;
3995
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003996 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003997 if (trash.len && trash.str[trash.len - 1] != '/' &&
3998 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3999 if (trash.len > trash.size - 5)
4000 return 0;
4001 trash.str[trash.len] = '/';
4002 trash.len++;
4003 }
4004
4005 break;
4006 }
4007 case REDIRECT_TYPE_LOCATION:
4008 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004009 if (rule->rdr_str) { /* this is an old "redirect" rule */
4010 if (trash.len + rule->rdr_len > trash.size - 4)
4011 return 0;
4012
4013 /* add location */
4014 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4015 trash.len += rule->rdr_len;
4016 }
4017 else {
4018 /* add location with executing log format */
4019 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004020
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004021 /* Check left length */
4022 if (trash.len > trash.size - 4)
4023 return 0;
4024 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004025 break;
4026 }
4027
4028 if (rule->cookie_len) {
4029 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4030 trash.len += 14;
4031 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4032 trash.len += rule->cookie_len;
4033 memcpy(trash.str + trash.len, "\r\n", 2);
4034 trash.len += 2;
4035 }
4036
4037 /* add end of headers and the keep-alive/close status.
4038 * We may choose to set keep-alive if the Location begins
4039 * with a slash, because the client will come back to the
4040 * same server.
4041 */
4042 txn->status = rule->code;
4043 /* let's log the request time */
4044 s->logs.tv_request = now;
4045
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004046 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004047 (msg->flags & HTTP_MSGF_XFER_LEN) &&
4048 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
4049 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4050 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4051 /* keep-alive possible */
4052 if (!(msg->flags & HTTP_MSGF_VER_11)) {
4053 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4054 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4055 trash.len += 30;
4056 } else {
4057 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4058 trash.len += 24;
4059 }
4060 }
4061 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4062 trash.len += 4;
4063 bo_inject(txn->rsp.chn, trash.str, trash.len);
4064 /* "eat" the request */
4065 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02004066 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004067 msg->sov = 0;
4068 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004069 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004070 txn->req.msg_state = HTTP_MSG_CLOSED;
4071 txn->rsp.msg_state = HTTP_MSG_DONE;
4072 } else {
4073 /* keep-alive not possible */
4074 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4075 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4076 trash.len += 29;
4077 } else {
4078 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4079 trash.len += 23;
4080 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004081 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004082 txn->req.chn->analysers = 0;
4083 }
4084
Willy Tarreaue7dff022015-04-03 01:14:29 +02004085 if (!(s->flags & SF_ERR_MASK))
4086 s->flags |= SF_ERR_LOCAL;
4087 if (!(s->flags & SF_FINST_MASK))
4088 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004089
4090 return 1;
4091}
4092
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004093/* This stream analyser runs all HTTP request processing which is common to
4094 * frontends and backends, which means blocking ACLs, filters, connection-close,
4095 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004096 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004097 * either needs more data or wants to immediately abort the request (eg: deny,
4098 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004099 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004100int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004101{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004102 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004103 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004104 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004105 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004106 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004107 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004108
Willy Tarreau655dce92009-11-08 13:10:58 +01004109 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004110 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004111 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004112 }
4113
Willy Tarreau87b09662015-04-03 00:22:06 +02004114 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 +02004115 now_ms, __FUNCTION__,
4116 s,
4117 req,
4118 req->rex, req->wex,
4119 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004120 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004121 req->analysers);
4122
Willy Tarreau65410832014-04-28 21:25:43 +02004123 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004124 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004125
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004126 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004127 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004128 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004129
Willy Tarreau0b748332014-04-29 00:13:29 +02004130 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004131 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4132 goto return_prx_yield;
4133
Willy Tarreau0b748332014-04-29 00:13:29 +02004134 case HTTP_RULE_RES_CONT:
4135 case HTTP_RULE_RES_STOP: /* nothing to do */
4136 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004137
Willy Tarreau0b748332014-04-29 00:13:29 +02004138 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4139 if (txn->flags & TX_CLTARPIT)
4140 goto tarpit;
4141 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004142
Willy Tarreau0b748332014-04-29 00:13:29 +02004143 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4144 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004145
Willy Tarreau0b748332014-04-29 00:13:29 +02004146 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004147 goto done;
4148
Willy Tarreau0b748332014-04-29 00:13:29 +02004149 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4150 goto return_bad_req;
4151 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004152 }
4153
Willy Tarreau52542592014-04-28 18:33:26 +02004154 /* OK at this stage, we know that the request was accepted according to
4155 * the http-request rules, we can check for the stats. Note that the
4156 * URI is detected *before* the req* rules in order not to be affected
4157 * by a possible reqrep, while they are processed *after* so that a
4158 * reqdeny can still block them. This clearly needs to change in 1.6!
4159 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004160 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004161 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004162 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004163 txn->status = 500;
4164 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004165 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004166
Willy Tarreaue7dff022015-04-03 01:14:29 +02004167 if (!(s->flags & SF_ERR_MASK))
4168 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004169 goto return_prx_cond;
4170 }
4171
4172 /* parse the whole stats request and extract the relevant information */
4173 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004174 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004175 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004176
Willy Tarreau0b748332014-04-29 00:13:29 +02004177 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4178 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004179
Willy Tarreau0b748332014-04-29 00:13:29 +02004180 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4181 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004182 }
4183
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004184 /* evaluate the req* rules except reqadd */
4185 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004186 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004187 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004188
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004189 if (txn->flags & TX_CLDENY)
4190 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004191
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004192 if (txn->flags & TX_CLTARPIT)
4193 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004194 }
Willy Tarreau06619262006-12-17 08:37:22 +01004195
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004196 /* add request headers from the rule sets in the same order */
4197 list_for_each_entry(wl, &px->req_add, list) {
4198 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004199 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004200 ret = acl_pass(ret);
4201 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4202 ret = !ret;
4203 if (!ret)
4204 continue;
4205 }
4206
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004207 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004208 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004209 }
4210
Willy Tarreau52542592014-04-28 18:33:26 +02004211
4212 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004213 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004214 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004215 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4216 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004217
Willy Tarreaue7dff022015-04-03 01:14:29 +02004218 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4219 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4220 if (!(s->flags & SF_FINST_MASK))
4221 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004222
Willy Tarreau70730dd2014-04-24 18:06:27 +02004223 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004224 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004225 select_compression_request_header(s, req->buf);
4226
4227 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004228 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004229 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004230 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004231
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004232 /* check whether we have some ACLs set to redirect this request */
4233 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004234 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004235 int ret;
4236
Willy Tarreau192252e2015-04-04 01:47:55 +02004237 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004238 ret = acl_pass(ret);
4239 if (rule->cond->pol == ACL_COND_UNLESS)
4240 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004241 if (!ret)
4242 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004243 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004244 if (!http_apply_redirect_rule(rule, s, txn))
4245 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004246 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004247 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004248
Willy Tarreau2be39392010-01-03 17:24:51 +01004249 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4250 * If this happens, then the data will not come immediately, so we must
4251 * send all what we have without waiting. Note that due to the small gain
4252 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004253 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004254 * itself once used.
4255 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004256 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004257
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004258 done: /* done with this analyser, continue with next ones that the calling
4259 * points will have set, if any.
4260 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004261 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004262 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4263 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004264 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004265
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004266 tarpit:
4267 /* When a connection is tarpitted, we use the tarpit timeout,
4268 * which may be the same as the connect timeout if unspecified.
4269 * If unset, then set it to zero because we really want it to
4270 * eventually expire. We build the tarpit as an analyser.
4271 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004272 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004273
4274 /* wipe the request out so that we can drop the connection early
4275 * if the client closes first.
4276 */
4277 channel_dont_connect(req);
4278 req->analysers = 0; /* remove switching rules etc... */
4279 req->analysers |= AN_REQ_HTTP_TARPIT;
4280 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4281 if (!req->analyse_exp)
4282 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004283 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004284 sess->fe->fe_counters.denied_req++;
4285 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004286 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004287 if (sess->listener->counters)
4288 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004289 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004290
4291 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004292 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004293 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004294 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004295 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004296 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004297 sess->fe->fe_counters.denied_req++;
4298 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004299 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004300 if (sess->listener->counters)
4301 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004302 goto return_prx_cond;
4303
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004304 return_bad_req:
4305 /* We centralize bad requests processing here */
4306 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4307 /* we detected a parsing error. We want to archive this request
4308 * in the dedicated proxy area for later troubleshooting.
4309 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004310 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004311 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004312
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004313 txn->req.msg_state = HTTP_MSG_ERROR;
4314 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004315 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004316
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004317 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004318 if (sess->listener->counters)
4319 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004320
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004321 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004322 if (!(s->flags & SF_ERR_MASK))
4323 s->flags |= SF_ERR_PRXCOND;
4324 if (!(s->flags & SF_FINST_MASK))
4325 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004326
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004327 req->analysers = 0;
4328 req->analyse_exp = TICK_ETERNITY;
4329 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004330
4331 return_prx_yield:
4332 channel_dont_connect(req);
4333 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004334}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004335
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004336/* This function performs all the processing enabled for the current request.
4337 * It returns 1 if the processing can continue on next analysers, or zero if it
4338 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004339 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004340 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004341int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004342{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004343 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004344 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004345 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004346 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004347
Willy Tarreau655dce92009-11-08 13:10:58 +01004348 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004349 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004350 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004351 return 0;
4352 }
4353
Willy Tarreau87b09662015-04-03 00:22:06 +02004354 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 +02004355 now_ms, __FUNCTION__,
4356 s,
4357 req,
4358 req->rex, req->wex,
4359 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004360 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004361 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004362
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004363 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004364 select_compression_request_header(s, req->buf);
4365
Willy Tarreau59234e92008-11-30 23:51:27 +01004366 /*
4367 * Right now, we know that we have processed the entire headers
4368 * and that unwanted requests have been filtered out. We can do
4369 * whatever we want with the remaining request. Also, now we
4370 * may have separate values for ->fe, ->be.
4371 */
Willy Tarreau06619262006-12-17 08:37:22 +01004372
Willy Tarreau59234e92008-11-30 23:51:27 +01004373 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004374 * If HTTP PROXY is set we simply get remote server address parsing
4375 * incoming request. Note that this requires that a connection is
4376 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004377 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004378 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004379 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004380 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004381
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004382 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau350f4872014-11-28 14:42:25 +01004383 if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004384 txn->req.msg_state = HTTP_MSG_ERROR;
4385 txn->status = 500;
4386 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004387 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004388
Willy Tarreaue7dff022015-04-03 01:14:29 +02004389 if (!(s->flags & SF_ERR_MASK))
4390 s->flags |= SF_ERR_RESOURCE;
4391 if (!(s->flags & SF_FINST_MASK))
4392 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004393
4394 return 0;
4395 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004396
4397 path = http_get_path(txn);
4398 url2sa(req->buf->p + msg->sl.rq.u,
4399 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004400 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004401 /* if the path was found, we have to remove everything between
4402 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4403 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4404 * u_l characters by a single "/".
4405 */
4406 if (path) {
4407 char *cur_ptr = req->buf->p;
4408 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4409 int delta;
4410
4411 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4412 http_msg_move_end(&txn->req, delta);
4413 cur_end += delta;
4414 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4415 goto return_bad_req;
4416 }
4417 else {
4418 char *cur_ptr = req->buf->p;
4419 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4420 int delta;
4421
4422 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4423 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4424 http_msg_move_end(&txn->req, delta);
4425 cur_end += delta;
4426 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4427 goto return_bad_req;
4428 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004429 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004430
Willy Tarreau59234e92008-11-30 23:51:27 +01004431 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004432 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004433 * Note that doing so might move headers in the request, but
4434 * the fields will stay coherent and the URI will not move.
4435 * This should only be performed in the backend.
4436 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004437 if ((s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004438 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4439 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004440
Willy Tarreau59234e92008-11-30 23:51:27 +01004441 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004442 * 8: the appsession cookie was looked up very early in 1.2,
4443 * so let's do the same now.
4444 */
4445
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004446 /* It needs to look into the URI unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004447 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004448 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 +01004449 }
4450
William Lallemanda73203e2012-03-12 12:48:57 +01004451 /* add unique-id if "header-unique-id" is specified */
4452
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004453 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004454 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4455 goto return_bad_req;
4456 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004457 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004458 }
William Lallemanda73203e2012-03-12 12:48:57 +01004459
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004460 if (sess->fe->header_unique_id && s->unique_id) {
4461 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004462 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004463 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004464 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004465 goto return_bad_req;
4466 }
4467
Cyril Bontéb21570a2009-11-29 20:04:48 +01004468 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004469 * 9: add X-Forwarded-For if either the frontend or the backend
4470 * asks for it.
4471 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004472 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004473 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004474 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4475 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4476 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004477 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004478 /* The header is set to be added only if none is present
4479 * and we found it, so don't do anything.
4480 */
4481 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004482 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004483 /* Add an X-Forwarded-For header unless the source IP is
4484 * in the 'except' network range.
4485 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004486 if ((!sess->fe->except_mask.s_addr ||
4487 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4488 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004489 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004490 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004491 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004492 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004493 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004494 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004495
4496 /* Note: we rely on the backend to get the header name to be used for
4497 * x-forwarded-for, because the header is really meant for the backends.
4498 * However, if the backend did not specify any option, we have to rely
4499 * on the frontend's header name.
4500 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004501 if (s->be->fwdfor_hdr_len) {
4502 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004503 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004504 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004505 len = sess->fe->fwdfor_hdr_len;
4506 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004507 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004508 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 +01004509
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004510 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004511 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004512 }
4513 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004514 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004515 /* FIXME: for the sake of completeness, we should also support
4516 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004517 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004518 int len;
4519 char pn[INET6_ADDRSTRLEN];
4520 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004521 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004522 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004523
Willy Tarreau59234e92008-11-30 23:51:27 +01004524 /* Note: we rely on the backend to get the header name to be used for
4525 * x-forwarded-for, because the header is really meant for the backends.
4526 * However, if the backend did not specify any option, we have to rely
4527 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004528 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004529 if (s->be->fwdfor_hdr_len) {
4530 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004531 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004532 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004533 len = sess->fe->fwdfor_hdr_len;
4534 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004535 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004536 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004537
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004538 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004539 goto return_bad_req;
4540 }
4541 }
4542
4543 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004544 * 10: add X-Original-To if either the frontend or the backend
4545 * asks for it.
4546 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004547 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004548
4549 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004550 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004551 /* Add an X-Original-To header unless the destination IP is
4552 * in the 'except' network range.
4553 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004554 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004555
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004556 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004557 ((!sess->fe->except_mask_to.s_addr ||
4558 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4559 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004560 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004561 (((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 +02004562 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004563 int len;
4564 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004565 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004566
4567 /* Note: we rely on the backend to get the header name to be used for
4568 * x-original-to, because the header is really meant for the backends.
4569 * However, if the backend did not specify any option, we have to rely
4570 * on the frontend's header name.
4571 */
4572 if (s->be->orgto_hdr_len) {
4573 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004574 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004575 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004576 len = sess->fe->orgto_hdr_len;
4577 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004578 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004579 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 +02004580
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004581 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004582 goto return_bad_req;
4583 }
4584 }
4585 }
4586
Willy Tarreau50fc7772012-11-11 22:19:57 +01004587 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4588 * If an "Upgrade" token is found, the header is left untouched in order not to have
4589 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4590 * "Upgrade" is present in the Connection header.
4591 */
4592 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4593 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004594 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004595 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004596 unsigned int want_flags = 0;
4597
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004598 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004599 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004600 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004601 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004602 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004603 want_flags |= TX_CON_CLO_SET;
4604 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004605 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004606 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004607 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004608 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004609 want_flags |= TX_CON_KAL_SET;
4610 }
4611
4612 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004613 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004614 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004615
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004616
Willy Tarreau522d6c02009-12-06 18:49:18 +01004617 /* If we have no server assigned yet and we're balancing on url_param
4618 * with a POST request, we may be interested in checking the body for
4619 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004620 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004621 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004622 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004623 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004624 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004625 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004626 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004627
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004628 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004629 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004630#ifdef TCP_QUICKACK
4631 /* We expect some data from the client. Unless we know for sure
4632 * we already have a full request, we have to re-enable quick-ack
4633 * in case we previously disabled it, otherwise we might cause
4634 * the client to delay further data.
4635 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004636 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004637 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004638 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004639 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004640 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004641#endif
4642 }
Willy Tarreau03945942009-12-22 16:50:27 +01004643
Willy Tarreau59234e92008-11-30 23:51:27 +01004644 /*************************************************************
4645 * OK, that's finished for the headers. We have done what we *
4646 * could. Let's switch to the DATA state. *
4647 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004648 req->analyse_exp = TICK_ETERNITY;
4649 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004650
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004651 /* if the server closes the connection, we want to immediately react
4652 * and close the socket to save packets and syscalls.
4653 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004654 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004655 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004656
Willy Tarreau59234e92008-11-30 23:51:27 +01004657 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004658 /* OK let's go on with the BODY now */
4659 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004660
Willy Tarreau59234e92008-11-30 23:51:27 +01004661 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004662 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004663 /* we detected a parsing error. We want to archive this request
4664 * in the dedicated proxy area for later troubleshooting.
4665 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004666 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004667 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004668
Willy Tarreau59234e92008-11-30 23:51:27 +01004669 txn->req.msg_state = HTTP_MSG_ERROR;
4670 txn->status = 400;
4671 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004672 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004673
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004674 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004675 if (sess->listener->counters)
4676 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004677
Willy Tarreaue7dff022015-04-03 01:14:29 +02004678 if (!(s->flags & SF_ERR_MASK))
4679 s->flags |= SF_ERR_PRXCOND;
4680 if (!(s->flags & SF_FINST_MASK))
4681 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004682 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004683}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004684
Willy Tarreau60b85b02008-11-30 23:28:40 +01004685/* This function is an analyser which processes the HTTP tarpit. It always
4686 * returns zero, at the beginning because it prevents any other processing
4687 * from occurring, and at the end because it terminates the request.
4688 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004689int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004690{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004691 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004692
4693 /* This connection is being tarpitted. The CLIENT side has
4694 * already set the connect expiration date to the right
4695 * timeout. We just have to check that the client is still
4696 * there and that the timeout has not expired.
4697 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004698 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004699 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004700 !tick_is_expired(req->analyse_exp, now_ms))
4701 return 0;
4702
4703 /* We will set the queue timer to the time spent, just for
4704 * logging purposes. We fake a 500 server error, so that the
4705 * attacker will not suspect his connection has been tarpitted.
4706 * It will not cause trouble to the logs because we can exclude
4707 * the tarpitted connections by filtering on the 'PT' status flags.
4708 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004709 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4710
4711 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004712 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004713 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004714
4715 req->analysers = 0;
4716 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004717
Willy Tarreaue7dff022015-04-03 01:14:29 +02004718 if (!(s->flags & SF_ERR_MASK))
4719 s->flags |= SF_ERR_PRXCOND;
4720 if (!(s->flags & SF_FINST_MASK))
4721 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004722 return 0;
4723}
4724
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004725/* This function is an analyser which waits for the HTTP request body. It waits
4726 * for either the buffer to be full, or the full advertised contents to have
4727 * reached the buffer. It must only be called after the standard HTTP request
4728 * processing has occurred, because it expects the request to be parsed and will
4729 * look for the Expect header. It may send a 100-Continue interim response. It
4730 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4731 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4732 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004733 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004734int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004735{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004736 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004737 struct http_txn *txn = s->txn;
4738 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004739
4740 /* We have to parse the HTTP request body to find any required data.
4741 * "balance url_param check_post" should have been the only way to get
4742 * into this. We were brought here after HTTP header analysis, so all
4743 * related structures are ready.
4744 */
4745
Willy Tarreau890988f2014-04-10 11:59:33 +02004746 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4747 /* This is the first call */
4748 if (msg->msg_state < HTTP_MSG_BODY)
4749 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004750
Willy Tarreau890988f2014-04-10 11:59:33 +02004751 if (msg->msg_state < HTTP_MSG_100_SENT) {
4752 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4753 * send an HTTP/1.1 100 Continue intermediate response.
4754 */
4755 if (msg->flags & HTTP_MSGF_VER_11) {
4756 struct hdr_ctx ctx;
4757 ctx.idx = 0;
4758 /* Expect is allowed in 1.1, look for it */
4759 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4760 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004761 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004762 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004763 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004764 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004765 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004766
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004767 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004768 * req->buf->p still points to the beginning of the message. We
4769 * must save the body in msg->next because it survives buffer
4770 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004771 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004772 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004773
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004774 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004775 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4776 else
4777 msg->msg_state = HTTP_MSG_DATA;
4778 }
4779
Willy Tarreau890988f2014-04-10 11:59:33 +02004780 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4781 /* We're in content-length mode, we just have to wait for enough data. */
4782 if (req->buf->i - msg->sov < msg->body_len)
4783 goto missing_data;
4784
4785 /* OK we have everything we need now */
4786 goto http_end;
4787 }
4788
4789 /* OK here we're parsing a chunked-encoded message */
4790
Willy Tarreau522d6c02009-12-06 18:49:18 +01004791 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004792 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004793 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004794 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004795 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004796 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004797
Willy Tarreau115acb92009-12-26 13:56:06 +01004798 if (!ret)
4799 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004800 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004801 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004802 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004803 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004804 }
4805
Willy Tarreaud98cf932009-12-27 22:54:55 +01004806 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004807 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004808 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004809 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004810 if (msg->msg_state == HTTP_MSG_TRAILERS)
4811 goto http_end;
4812
Willy Tarreau226071e2014-04-10 11:55:45 +02004813 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004814 goto http_end;
4815
4816 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004817 /* we get here if we need to wait for more data. If the buffer is full,
4818 * we have the maximum we can expect.
4819 */
4820 if (buffer_full(req->buf, global.tune.maxrewrite))
4821 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004822
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004823 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004824 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004825 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004826
Willy Tarreaue7dff022015-04-03 01:14:29 +02004827 if (!(s->flags & SF_ERR_MASK))
4828 s->flags |= SF_ERR_CLITO;
4829 if (!(s->flags & SF_FINST_MASK))
4830 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004831 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004832 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004833
4834 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004835 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004836 /* Not enough data. We'll re-use the http-request
4837 * timeout here. Ideally, we should set the timeout
4838 * relative to the accept() date. We just set the
4839 * request timeout once at the beginning of the
4840 * request.
4841 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004842 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004843 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004844 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004845 return 0;
4846 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004847
4848 http_end:
4849 /* The situation will not evolve, so let's give up on the analysis. */
4850 s->logs.tv_request = now; /* update the request timer to reflect full request */
4851 req->analysers &= ~an_bit;
4852 req->analyse_exp = TICK_ETERNITY;
4853 return 1;
4854
4855 return_bad_req: /* let's centralize all bad requests */
4856 txn->req.msg_state = HTTP_MSG_ERROR;
4857 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004858 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004859
Willy Tarreaue7dff022015-04-03 01:14:29 +02004860 if (!(s->flags & SF_ERR_MASK))
4861 s->flags |= SF_ERR_PRXCOND;
4862 if (!(s->flags & SF_FINST_MASK))
4863 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004864
Willy Tarreau522d6c02009-12-06 18:49:18 +01004865 return_err_msg:
4866 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004867 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004868 if (sess->listener->counters)
4869 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004870 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004871}
4872
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004873/* send a server's name with an outgoing request over an established connection.
4874 * Note: this function is designed to be called once the request has been scheduled
4875 * for being forwarded. This is the reason why it rewinds the buffer before
4876 * proceeding.
4877 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004878int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004879
4880 struct hdr_ctx ctx;
4881
Mark Lamourinec2247f02012-01-04 13:02:01 -05004882 char *hdr_name = be->server_id_hdr_name;
4883 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004884 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004885 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004886 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004887
William Lallemandd9e90662012-01-30 17:27:17 +01004888 ctx.idx = 0;
4889
Willy Tarreau211cdec2014-04-17 20:18:08 +02004890 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004891 if (old_o) {
4892 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004893 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004894 txn->req.next += old_o;
4895 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004896 }
4897
Willy Tarreau9b28e032012-10-12 23:49:43 +02004898 old_i = chn->buf->i;
4899 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 -05004900 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004901 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004902 }
4903
4904 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004905 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004906 memcpy(hdr_val, hdr_name, hdr_name_len);
4907 hdr_val += hdr_name_len;
4908 *hdr_val++ = ':';
4909 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004910 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4911 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004912
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004913 if (old_o) {
4914 /* If this was a forwarded request, we must readjust the amount of
4915 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004916 * variations. Note that the current state is >= HTTP_MSG_BODY,
4917 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004918 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004919 old_o += chn->buf->i - old_i;
4920 b_adv(chn->buf, old_o);
4921 txn->req.next -= old_o;
4922 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004923 }
4924
Mark Lamourinec2247f02012-01-04 13:02:01 -05004925 return 0;
4926}
4927
Willy Tarreau610ecce2010-01-04 21:15:02 +01004928/* Terminate current transaction and prepare a new one. This is very tricky
4929 * right now but it works.
4930 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004931void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004932{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004933 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004934 struct proxy *fe = strm_fe(s);
Willy Tarreau068621e2013-12-23 15:11:25 +01004935
Willy Tarreau610ecce2010-01-04 21:15:02 +01004936 /* FIXME: We need a more portable way of releasing a backend's and a
4937 * server's connections. We need a safer way to reinitialize buffer
4938 * flags. We also need a more accurate method for computing per-request
4939 * data.
4940 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004941
Willy Tarreau4213a112013-12-15 10:25:42 +01004942 /* unless we're doing keep-alive, we want to quickly close the connection
4943 * to the server.
4944 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004945 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004946 !si_conn_ready(&s->si[1])) {
4947 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4948 si_shutr(&s->si[1]);
4949 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004950 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004951
Willy Tarreaue7dff022015-04-03 01:14:29 +02004952 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004953 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004954 if (unlikely(s->srv_conn))
4955 sess_change_server(s, NULL);
4956 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004957
4958 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004959 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004960
Willy Tarreaueee5b512015-04-03 23:46:31 +02004961 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004962 int n;
4963
Willy Tarreaueee5b512015-04-03 23:46:31 +02004964 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004965 if (n < 1 || n > 5)
4966 n = 0;
4967
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004968 if (fe->mode == PR_MODE_HTTP) {
4969 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02004970 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004971 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004972 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004973 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004974 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004975 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004976 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02004977 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004978 s->be->be_counters.p.http.comp_rsp++;
4979 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004980 }
4981
4982 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004983 s->logs.bytes_in -= s->req.buf->i;
4984 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004985
4986 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004987 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004988 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004989 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004990 s->do_log(s);
4991 }
4992
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004993 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004994 stream_stop_content_counters(s);
4995 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004996
Willy Tarreau610ecce2010-01-04 21:15:02 +01004997 s->logs.accept_date = date; /* user-visible date for logging */
4998 s->logs.tv_accept = now; /* corrected date for internal use */
4999 tv_zero(&s->logs.tv_request);
5000 s->logs.t_queue = -1;
5001 s->logs.t_connect = -1;
5002 s->logs.t_data = -1;
5003 s->logs.t_close = 0;
5004 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5005 s->logs.srv_queue_size = 0; /* we will get this number soon */
5006
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005007 s->logs.bytes_in = s->req.total = s->req.buf->i;
5008 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005009
5010 if (s->pend_pos)
5011 pendconn_free(s->pend_pos);
5012
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005013 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005014 if (s->flags & SF_CURR_SESS) {
5015 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005016 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005017 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005018 if (may_dequeue_tasks(objt_server(s->target), s->be))
5019 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005020 }
5021
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005022 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005023
Willy Tarreau4213a112013-12-15 10:25:42 +01005024 /* only release our endpoint if we don't intend to reuse the
5025 * connection.
5026 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005027 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005028 !si_conn_ready(&s->si[1])) {
5029 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005030 }
5031
Willy Tarreau350f4872014-11-28 14:42:25 +01005032 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5033 s->si[1].err_type = SI_ET_NONE;
5034 s->si[1].conn_retries = 0; /* used for logging too */
5035 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005036 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 +01005037 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);
5038 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 +02005039 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5040 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5041 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005042
Willy Tarreaueee5b512015-04-03 23:46:31 +02005043 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005044 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005045 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005046
5047 if (prev_status == 401 || prev_status == 407) {
5048 /* In HTTP keep-alive mode, if we receive a 401, we still have
5049 * a chance of being able to send the visitor again to the same
5050 * server over the same connection. This is required by some
5051 * broken protocols such as NTLM, and anyway whenever there is
5052 * an opportunity for sending the challenge to the proper place,
5053 * it's better to do it (at least it helps with debugging).
5054 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005055 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01005056 }
5057
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005058 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005059 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005060
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005061 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005062 s->req.flags |= CF_NEVER_WAIT;
5063 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005064 }
5065
Willy Tarreau610ecce2010-01-04 21:15:02 +01005066 /* if the request buffer is not empty, it means we're
5067 * about to process another request, so send pending
5068 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005069 * Just don't do this if the buffer is close to be full,
5070 * because the request will wait for it to flush a little
5071 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005072 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005073 if (s->req.buf->i) {
5074 if (s->res.buf->o &&
5075 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5076 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5077 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005078 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005079
5080 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005081 channel_auto_read(&s->req);
5082 channel_auto_close(&s->req);
5083 channel_auto_read(&s->res);
5084 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005085
Willy Tarreau27375622013-12-17 00:00:28 +01005086 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau350f4872014-11-28 14:42:25 +01005087 si_idle_conn(&s->si[1]);
Willy Tarreau27375622013-12-17 00:00:28 +01005088
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005089 s->req.analysers = strm_li(s)->analysers;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005090 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005091}
5092
5093
5094/* This function updates the request state machine according to the response
5095 * state machine and buffer flags. It returns 1 if it changes anything (flag
5096 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5097 * it is only used to find when a request/response couple is complete. Both
5098 * this function and its equivalent should loop until both return zero. It
5099 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5100 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005101int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005102{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005103 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005104 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005105 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005106 unsigned int old_state = txn->req.msg_state;
5107
Willy Tarreau610ecce2010-01-04 21:15:02 +01005108 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5109 return 0;
5110
5111 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005112 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005113 * We can shut the read side unless we want to abort_on_close,
5114 * or we have a POST request. The issue with POST requests is
5115 * that some browsers still send a CRLF after the request, and
5116 * this CRLF must be read so that it does not remain in the kernel
5117 * buffers, otherwise a close could cause an RST on some systems
5118 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005119 * Note that if we're using keep-alive on the client side, we'd
5120 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005121 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005122 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005123 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005124 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5125 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5126 !(s->be->options & PR_O_ABRT_CLOSE) &&
5127 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005128 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005129
Willy Tarreau40f151a2012-12-20 12:10:09 +01005130 /* if the server closes the connection, we want to immediately react
5131 * and close the socket to save packets and syscalls.
5132 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005133 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005134
Willy Tarreau610ecce2010-01-04 21:15:02 +01005135 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5136 goto wait_other_side;
5137
5138 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5139 /* The server has not finished to respond, so we
5140 * don't want to move in order not to upset it.
5141 */
5142 goto wait_other_side;
5143 }
5144
5145 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5146 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005147 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005148 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005149 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005150 goto wait_other_side;
5151 }
5152
5153 /* When we get here, it means that both the request and the
5154 * response have finished receiving. Depending on the connection
5155 * mode, we'll have to wait for the last bytes to leave in either
5156 * direction, and sometimes for a close to be effective.
5157 */
5158
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005159 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5160 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005161 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5162 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005163 }
5164 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5165 /* Option forceclose is set, or either side wants to close,
5166 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005167 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005168 * once both states are CLOSED.
5169 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005170 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5171 channel_shutr_now(chn);
5172 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005173 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005174 }
5175 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005176 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5177 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005178 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005179 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5180 channel_auto_read(chn);
5181 txn->req.msg_state = HTTP_MSG_TUNNEL;
5182 chn->flags |= CF_NEVER_WAIT;
5183 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005184 }
5185
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005186 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005187 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005188 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005189
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005190 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005191 txn->req.msg_state = HTTP_MSG_CLOSING;
5192 goto http_msg_closing;
5193 }
5194 else {
5195 txn->req.msg_state = HTTP_MSG_CLOSED;
5196 goto http_msg_closed;
5197 }
5198 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005199 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005200 }
5201
5202 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5203 http_msg_closing:
5204 /* nothing else to forward, just waiting for the output buffer
5205 * to be empty and for the shutw_now to take effect.
5206 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005207 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005208 txn->req.msg_state = HTTP_MSG_CLOSED;
5209 goto http_msg_closed;
5210 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005211 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005212 txn->req.msg_state = HTTP_MSG_ERROR;
5213 goto wait_other_side;
5214 }
5215 }
5216
5217 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5218 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005219 /* see above in MSG_DONE why we only do this in these states */
5220 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5221 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5222 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005223 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005224 goto wait_other_side;
5225 }
5226
5227 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005228 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005229}
5230
5231
5232/* This function updates the response state machine according to the request
5233 * state machine and buffer flags. It returns 1 if it changes anything (flag
5234 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5235 * it is only used to find when a request/response couple is complete. Both
5236 * this function and its equivalent should loop until both return zero. It
5237 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5238 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005239int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005240{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005241 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005242 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005243 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005244 unsigned int old_state = txn->rsp.msg_state;
5245
Willy Tarreau610ecce2010-01-04 21:15:02 +01005246 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5247 return 0;
5248
5249 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5250 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005251 * still monitor the server connection for a possible close
5252 * while the request is being uploaded, so we don't disable
5253 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005254 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005255 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005256
5257 if (txn->req.msg_state == HTTP_MSG_ERROR)
5258 goto wait_other_side;
5259
5260 if (txn->req.msg_state < HTTP_MSG_DONE) {
5261 /* The client seems to still be sending data, probably
5262 * because we got an error response during an upload.
5263 * We have the choice of either breaking the connection
5264 * or letting it pass through. Let's do the later.
5265 */
5266 goto wait_other_side;
5267 }
5268
5269 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5270 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005271 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005272 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005273 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005274 goto wait_other_side;
5275 }
5276
5277 /* When we get here, it means that both the request and the
5278 * response have finished receiving. Depending on the connection
5279 * mode, we'll have to wait for the last bytes to leave in either
5280 * direction, and sometimes for a close to be effective.
5281 */
5282
5283 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5284 /* Server-close mode : shut read and wait for the request
5285 * side to close its output buffer. The caller will detect
5286 * when we're in DONE and the other is in CLOSED and will
5287 * catch that for the final cleanup.
5288 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005289 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5290 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005291 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005292 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5293 /* Option forceclose is set, or either side wants to close,
5294 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005295 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005296 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005297 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005298 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5299 channel_shutr_now(chn);
5300 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005301 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005302 }
5303 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005304 /* The last possible modes are keep-alive and tunnel. Tunnel will
5305 * need to forward remaining data. Keep-alive will need to monitor
5306 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005307 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005308 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005309 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005310 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5311 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005312 }
5313
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005314 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005315 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005316 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005317 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5318 goto http_msg_closing;
5319 }
5320 else {
5321 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5322 goto http_msg_closed;
5323 }
5324 }
5325 goto wait_other_side;
5326 }
5327
5328 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5329 http_msg_closing:
5330 /* nothing else to forward, just waiting for the output buffer
5331 * to be empty and for the shutw_now to take effect.
5332 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005333 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005334 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5335 goto http_msg_closed;
5336 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005337 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005338 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005339 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005340 if (objt_server(s->target))
5341 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005342 goto wait_other_side;
5343 }
5344 }
5345
5346 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5347 http_msg_closed:
5348 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005349 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005350 channel_auto_close(chn);
5351 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005352 goto wait_other_side;
5353 }
5354
5355 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005356 /* We force the response to leave immediately if we're waiting for the
5357 * other side, since there is no pending shutdown to push it out.
5358 */
5359 if (!channel_is_empty(chn))
5360 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005361 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005362}
5363
5364
5365/* Resync the request and response state machines. Return 1 if either state
5366 * changes.
5367 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005368int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005369{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005370 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005371 int old_req_state = txn->req.msg_state;
5372 int old_res_state = txn->rsp.msg_state;
5373
Willy Tarreau610ecce2010-01-04 21:15:02 +01005374 http_sync_req_state(s);
5375 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005376 if (!http_sync_res_state(s))
5377 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005378 if (!http_sync_req_state(s))
5379 break;
5380 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005381
Willy Tarreau610ecce2010-01-04 21:15:02 +01005382 /* OK, both state machines agree on a compatible state.
5383 * There are a few cases we're interested in :
5384 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5385 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5386 * directions, so let's simply disable both analysers.
5387 * - HTTP_MSG_CLOSED on the response only means we must abort the
5388 * request.
5389 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5390 * with server-close mode means we've completed one request and we
5391 * must re-initialize the server connection.
5392 */
5393
5394 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5395 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5396 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5397 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005398 s->req.analysers = 0;
5399 channel_auto_close(&s->req);
5400 channel_auto_read(&s->req);
5401 s->res.analysers = 0;
5402 channel_auto_close(&s->res);
5403 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005404 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005405 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005406 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005407 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005408 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005409 s->res.analysers = 0;
5410 channel_auto_close(&s->res);
5411 channel_auto_read(&s->res);
5412 s->req.analysers = 0;
5413 channel_abort(&s->req);
5414 channel_auto_close(&s->req);
5415 channel_auto_read(&s->req);
5416 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005417 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005418 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5419 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005420 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005421 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5422 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5423 /* server-close/keep-alive: terminate this transaction,
5424 * possibly killing the server connection and reinitialize
5425 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005426 */
5427 http_end_txn_clean_session(s);
5428 }
5429
Willy Tarreau610ecce2010-01-04 21:15:02 +01005430 return txn->req.msg_state != old_req_state ||
5431 txn->rsp.msg_state != old_res_state;
5432}
5433
Willy Tarreaud98cf932009-12-27 22:54:55 +01005434/* This function is an analyser which forwards request body (including chunk
5435 * sizes if any). It is called as soon as we must forward, even if we forward
5436 * zero byte. The only situation where it must not be called is when we're in
5437 * tunnel mode and we want to forward till the close. It's used both to forward
5438 * remaining data and to resync after end of body. It expects the msg_state to
5439 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005440 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005441 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005442 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005443 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005444int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005445{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005446 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005447 struct http_txn *txn = s->txn;
5448 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005449
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005450 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5451 return 0;
5452
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005453 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005454 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005455 /* Output closed while we were sending data. We must abort and
5456 * wake the other side up.
5457 */
5458 msg->msg_state = HTTP_MSG_ERROR;
5459 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005460 return 1;
5461 }
5462
Willy Tarreaud98cf932009-12-27 22:54:55 +01005463 /* Note that we don't have to send 100-continue back because we don't
5464 * need the data to complete our job, and it's up to the server to
5465 * decide whether to return 100, 417 or anything else in return of
5466 * an "Expect: 100-continue" header.
5467 */
5468
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005469 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005470 /* we have msg->sov which points to the first byte of message
5471 * body, and req->buf.p still points to the beginning of the
5472 * message. We forward the headers now, as we don't need them
5473 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005474 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005475 b_adv(req->buf, msg->sov);
5476 msg->next -= msg->sov;
5477 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005478
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005479 /* The previous analysers guarantee that the state is somewhere
5480 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5481 * msg->next are always correct.
5482 */
5483 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5484 if (msg->flags & HTTP_MSGF_TE_CHNK)
5485 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5486 else
5487 msg->msg_state = HTTP_MSG_DATA;
5488 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005489 }
5490
Willy Tarreau7ba23542014-04-17 21:50:00 +02005491 /* Some post-connect processing might want us to refrain from starting to
5492 * forward data. Currently, the only reason for this is "balance url_param"
5493 * whichs need to parse/process the request after we've enabled forwarding.
5494 */
5495 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005496 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005497 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005498 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005499 goto missing_data;
5500 }
5501 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5502 }
5503
Willy Tarreau80a92c02014-03-12 10:41:13 +01005504 /* in most states, we should abort in case of early close */
5505 channel_auto_close(req);
5506
Willy Tarreauefdf0942014-04-24 20:08:57 +02005507 if (req->to_forward) {
5508 /* We can't process the buffer's contents yet */
5509 req->flags |= CF_WAKE_WRITE;
5510 goto missing_data;
5511 }
5512
Willy Tarreaud98cf932009-12-27 22:54:55 +01005513 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005514 if (msg->msg_state == HTTP_MSG_DATA) {
5515 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005516 /* we may have some pending data starting at req->buf->p */
5517 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005518 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005519 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005520 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005521 msg->next += msg->chunk_len;
5522 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005523
5524 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005525 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005526 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005527 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005528 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005529 }
5530 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005531 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005532 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005533 * TRAILERS state.
5534 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005535 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005536
Willy Tarreau54d23df2012-10-25 19:04:45 +02005537 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005538 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005539 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005540 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005541 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005542 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005543 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005544 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005545 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005546 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005547 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005548 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005549 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005550
5551 if (ret == 0)
5552 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005553 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005554 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005555 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005556 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005557 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005558 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005559 /* we're in MSG_CHUNK_SIZE now */
5560 }
5561 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005562 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005563
5564 if (ret == 0)
5565 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005566 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005567 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005568 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005569 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005570 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005571 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005572 /* we're in HTTP_MSG_DONE now */
5573 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005574 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005575 int old_state = msg->msg_state;
5576
Willy Tarreau610ecce2010-01-04 21:15:02 +01005577 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005578
5579 /* we may have some pending data starting at req->buf->p
5580 * such as last chunk of data or trailers.
5581 */
5582 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005583 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005584 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005585 msg->next = 0;
5586
Willy Tarreau4fe41902010-06-07 22:27:41 +02005587 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005588 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5589 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005590 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005591 if (http_resync_states(s)) {
5592 /* some state changes occurred, maybe the analyser
5593 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005594 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005595 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005596 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005597 /* request errors are most likely due to
5598 * the server aborting the transfer.
5599 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005600 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005601 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005602 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005603 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005604 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005605 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005606 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005607 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005608
5609 /* If "option abortonclose" is set on the backend, we
5610 * want to monitor the client's connection and forward
5611 * any shutdown notification to the server, which will
5612 * decide whether to close or to go on processing the
5613 * request.
5614 */
5615 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005616 channel_auto_read(req);
5617 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005618 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005619 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005620 /* POST requests may require to read extra CRLF
5621 * sent by broken browsers and which could cause
5622 * an RST to be sent upon close on some systems
5623 * (eg: Linux).
5624 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005625 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005626 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005627
Willy Tarreau610ecce2010-01-04 21:15:02 +01005628 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005629 }
5630 }
5631
Willy Tarreaud98cf932009-12-27 22:54:55 +01005632 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005633 /* we may have some pending data starting at req->buf->p */
5634 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005635 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005636 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5637
Willy Tarreaubed410e2014-04-22 08:19:34 +02005638 msg->next = 0;
5639 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5640
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005641 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005642 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005643 if (!(s->flags & SF_ERR_MASK))
5644 s->flags |= SF_ERR_CLICL;
5645 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005646 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005647 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005648 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005649 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005650 }
5651
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005652 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005653 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005654 if (objt_server(s->target))
5655 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005656
5657 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005658 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005659
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005660 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005661 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005662 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005663
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005664 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005665 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005666 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005667 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005668 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005669
Willy Tarreau5c620922011-05-11 19:56:11 +02005670 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005671 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005672 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005673 * modes are already handled by the stream sock layer. We must not do
5674 * this in content-length mode because it could present the MSG_MORE
5675 * flag with the last block of forwarded data, which would cause an
5676 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005677 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005678 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005679 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005680
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005681 return 0;
5682
Willy Tarreaud98cf932009-12-27 22:54:55 +01005683 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005684 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005685 if (sess->listener->counters)
5686 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005687
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005688 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005689 /* we may have some pending data starting at req->buf->p */
5690 b_adv(req->buf, msg->next);
5691 msg->next = 0;
5692
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005693 txn->req.msg_state = HTTP_MSG_ERROR;
5694 if (txn->status) {
5695 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005696 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005697 } else {
5698 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005699 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005700 }
5701 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005702 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005703
Willy Tarreaue7dff022015-04-03 01:14:29 +02005704 if (!(s->flags & SF_ERR_MASK))
5705 s->flags |= SF_ERR_PRXCOND;
5706 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005707 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005708 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005709 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005710 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005711 }
5712 return 0;
5713
5714 aborted_xfer:
5715 txn->req.msg_state = HTTP_MSG_ERROR;
5716 if (txn->status) {
5717 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005718 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005719 } else {
5720 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005721 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005722 }
5723 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005724 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005725
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005726 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005727 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005728 if (objt_server(s->target))
5729 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005730
Willy Tarreaue7dff022015-04-03 01:14:29 +02005731 if (!(s->flags & SF_ERR_MASK))
5732 s->flags |= SF_ERR_SRVCL;
5733 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005734 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005735 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005736 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005737 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005738 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005739 return 0;
5740}
5741
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005742/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5743 * processing can continue on next analysers, or zero if it either needs more
5744 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005745 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005746 * when it has nothing left to do, and may remove any analyser when it wants to
5747 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005748 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005749int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005750{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005751 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005752 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005753 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005754 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005755 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005756 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005757 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005758
Willy Tarreau87b09662015-04-03 00:22:06 +02005759 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 +02005760 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005761 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005762 rep,
5763 rep->rex, rep->wex,
5764 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005765 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005766 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005767
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005768 /*
5769 * Now parse the partial (or complete) lines.
5770 * We will check the response syntax, and also join multi-line
5771 * headers. An index of all the lines will be elaborated while
5772 * parsing.
5773 *
5774 * For the parsing, we use a 28 states FSM.
5775 *
5776 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005777 * rep->buf->p = beginning of response
5778 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5779 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005780 * msg->eol = end of current header or line (LF or CRLF)
5781 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005782 */
5783
Willy Tarreau628c40c2014-04-24 19:11:26 +02005784 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005785 /* There's a protected area at the end of the buffer for rewriting
5786 * purposes. We don't want to start to parse the request if the
5787 * protected area is affected, because we may have to move processed
5788 * data later, which is much more complicated.
5789 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005790 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005791 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005792 /* some data has still not left the buffer, wake us once that's done */
5793 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5794 goto abort_response;
5795 channel_dont_close(rep);
5796 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005797 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005798 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005799 }
5800
Willy Tarreau379357a2013-06-08 12:55:46 +02005801 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5802 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5803 buffer_slow_realign(rep->buf);
5804
Willy Tarreau9b28e032012-10-12 23:49:43 +02005805 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005806 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005807 }
5808
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005809 /* 1: we might have to print this header in debug mode */
5810 if (unlikely((global.mode & MODE_DEBUG) &&
5811 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005812 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005813 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005814
Willy Tarreau9b28e032012-10-12 23:49:43 +02005815 sol = rep->buf->p;
5816 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005817 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005818
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005819 sol += hdr_idx_first_pos(&txn->hdr_idx);
5820 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005821
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005822 while (cur_idx) {
5823 eol = sol + txn->hdr_idx.v[cur_idx].len;
5824 debug_hdr("srvhdr", s, sol, eol);
5825 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5826 cur_idx = txn->hdr_idx.v[cur_idx].next;
5827 }
5828 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005829
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005830 /*
5831 * Now we quickly check if we have found a full valid response.
5832 * If not so, we check the FD and buffer states before leaving.
5833 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005834 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005835 * responses are checked first.
5836 *
5837 * Depending on whether the client is still there or not, we
5838 * may send an error response back or not. Note that normally
5839 * we should only check for HTTP status there, and check I/O
5840 * errors somewhere else.
5841 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005842
Willy Tarreau655dce92009-11-08 13:10:58 +01005843 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005844 /* Invalid response */
5845 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5846 /* we detected a parsing error. We want to archive this response
5847 * in the dedicated proxy area for later troubleshooting.
5848 */
5849 hdr_response_bad:
5850 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005851 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005852
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005853 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005854 if (objt_server(s->target)) {
5855 objt_server(s->target)->counters.failed_resp++;
5856 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005857 }
Willy Tarreau64648412010-03-05 10:41:54 +01005858 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005859 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005860 rep->analysers = 0;
5861 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005862 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005863 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005864 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005865
Willy Tarreaue7dff022015-04-03 01:14:29 +02005866 if (!(s->flags & SF_ERR_MASK))
5867 s->flags |= SF_ERR_PRXCOND;
5868 if (!(s->flags & SF_FINST_MASK))
5869 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005870
5871 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005872 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005873
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005874 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005875 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005876 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005877 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005878 goto hdr_response_bad;
5879 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005880
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005881 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005882 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005883 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005884 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005885 else if (txn->flags & TX_NOT_FIRST)
5886 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005887
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005888 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005889 if (objt_server(s->target)) {
5890 objt_server(s->target)->counters.failed_resp++;
5891 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005892 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005893
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005894 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005895 rep->analysers = 0;
5896 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005897 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005898 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005899 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005900
Willy Tarreaue7dff022015-04-03 01:14:29 +02005901 if (!(s->flags & SF_ERR_MASK))
5902 s->flags |= SF_ERR_SRVCL;
5903 if (!(s->flags & SF_FINST_MASK))
5904 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005905 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005906 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005907
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005908 /* read timeout : return a 504 to the client. */
5909 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005910 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005911 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005912 else if (txn->flags & TX_NOT_FIRST)
5913 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005914
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005915 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005916 if (objt_server(s->target)) {
5917 objt_server(s->target)->counters.failed_resp++;
5918 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005919 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005920
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005921 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005922 rep->analysers = 0;
5923 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005924 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005925 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005926 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005927
Willy Tarreaue7dff022015-04-03 01:14:29 +02005928 if (!(s->flags & SF_ERR_MASK))
5929 s->flags |= SF_ERR_SRVTO;
5930 if (!(s->flags & SF_FINST_MASK))
5931 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005932 return 0;
5933 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005934
Willy Tarreauf003d372012-11-26 13:35:37 +01005935 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005936 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005937 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005938 s->be->be_counters.cli_aborts++;
5939 if (objt_server(s->target))
5940 objt_server(s->target)->counters.cli_aborts++;
5941
5942 rep->analysers = 0;
5943 channel_auto_close(rep);
5944
5945 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005946 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005947 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01005948
Willy Tarreaue7dff022015-04-03 01:14:29 +02005949 if (!(s->flags & SF_ERR_MASK))
5950 s->flags |= SF_ERR_CLICL;
5951 if (!(s->flags & SF_FINST_MASK))
5952 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005953
Willy Tarreau87b09662015-04-03 00:22:06 +02005954 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005955 return 0;
5956 }
5957
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005958 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005959 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005960 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005961 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005962 else if (txn->flags & TX_NOT_FIRST)
5963 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005964
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005965 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005966 if (objt_server(s->target)) {
5967 objt_server(s->target)->counters.failed_resp++;
5968 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005969 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005970
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005971 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005972 rep->analysers = 0;
5973 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005974 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005975 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005976 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005977
Willy Tarreaue7dff022015-04-03 01:14:29 +02005978 if (!(s->flags & SF_ERR_MASK))
5979 s->flags |= SF_ERR_SRVCL;
5980 if (!(s->flags & SF_FINST_MASK))
5981 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005982 return 0;
5983 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005984
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005985 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005986 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005987 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005988 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005989 else if (txn->flags & TX_NOT_FIRST)
5990 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005991
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005992 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005993 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005994 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005995
Willy Tarreaue7dff022015-04-03 01:14:29 +02005996 if (!(s->flags & SF_ERR_MASK))
5997 s->flags |= SF_ERR_CLICL;
5998 if (!(s->flags & SF_FINST_MASK))
5999 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006000
Willy Tarreau87b09662015-04-03 00:22:06 +02006001 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006002 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006003 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006004
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006005 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006006 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006007 return 0;
6008 }
6009
6010 /* More interesting part now : we know that we have a complete
6011 * response which at least looks like HTTP. We have an indicator
6012 * of each header's length, so we can parse them quickly.
6013 */
6014
6015 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006016 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006017
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006018 /*
6019 * 1: get the status code
6020 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006021 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006022 if (n < 1 || n > 5)
6023 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006024 /* when the client triggers a 4xx from the server, it's most often due
6025 * to a missing object or permission. These events should be tracked
6026 * because if they happen often, it may indicate a brute force or a
6027 * vulnerability scan.
6028 */
6029 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006030 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006031
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006032 if (objt_server(s->target))
6033 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006034
Willy Tarreau5b154472009-12-21 20:11:07 +01006035 /* check if the response is HTTP/1.1 or above */
6036 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006037 ((rep->buf->p[5] > '1') ||
6038 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006039 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006040
6041 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006042 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 +01006043
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006044 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006045 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006046
Willy Tarreau9b28e032012-10-12 23:49:43 +02006047 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006048
Willy Tarreau39650402010-03-15 19:44:39 +01006049 /* Adjust server's health based on status code. Note: status codes 501
6050 * and 505 are triggered on demand by client request, so we must not
6051 * count them as server failures.
6052 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006053 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006054 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006055 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006056 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006057 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006058 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006059
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006060 /*
6061 * 2: check for cacheability.
6062 */
6063
6064 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006065 case 100:
6066 /*
6067 * We may be facing a 100-continue response, in which case this
6068 * is not the right response, and we're waiting for the next one.
6069 * Let's allow this response to go to the client and wait for the
6070 * next one.
6071 */
6072 hdr_idx_init(&txn->hdr_idx);
6073 msg->next -= channel_forward(rep, msg->next);
6074 msg->msg_state = HTTP_MSG_RPBEFORE;
6075 txn->status = 0;
6076 s->logs.t_data = -1; /* was not a response yet */
6077 goto next_one;
6078
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006079 case 200:
6080 case 203:
6081 case 206:
6082 case 300:
6083 case 301:
6084 case 410:
6085 /* RFC2616 @13.4:
6086 * "A response received with a status code of
6087 * 200, 203, 206, 300, 301 or 410 MAY be stored
6088 * by a cache (...) unless a cache-control
6089 * directive prohibits caching."
6090 *
6091 * RFC2616 @9.5: POST method :
6092 * "Responses to this method are not cacheable,
6093 * unless the response includes appropriate
6094 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006095 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006096 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006097 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006098 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6099 break;
6100 default:
6101 break;
6102 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006103
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006104 /*
6105 * 3: we may need to capture headers
6106 */
6107 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006108 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006109 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006110 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006111
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006112 /* 4: determine the transfer-length.
6113 * According to RFC2616 #4.4, amended by the HTTPbis working group,
6114 * the presence of a message-body in a RESPONSE and its transfer length
6115 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006116 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006117 * All responses to the HEAD request method MUST NOT include a
6118 * message-body, even though the presence of entity-header fields
6119 * might lead one to believe they do. All 1xx (informational), 204
6120 * (No Content), and 304 (Not Modified) responses MUST NOT include a
6121 * message-body. All other responses do include a message-body,
6122 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006123 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006124 * 1. Any response which "MUST NOT" include a message-body (such as the
6125 * 1xx, 204 and 304 responses and any response to a HEAD request) is
6126 * always terminated by the first empty line after the header fields,
6127 * regardless of the entity-header fields present in the message.
6128 *
6129 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
6130 * the "chunked" transfer-coding (Section 6.2) is used, the
6131 * transfer-length is defined by the use of this transfer-coding.
6132 * If a Transfer-Encoding header field is present and the "chunked"
6133 * transfer-coding is not present, the transfer-length is defined by
6134 * the sender closing the connection.
6135 *
6136 * 3. If a Content-Length header field is present, its decimal value in
6137 * OCTETs represents both the entity-length and the transfer-length.
6138 * If a message is received with both a Transfer-Encoding header
6139 * field and a Content-Length header field, the latter MUST be ignored.
6140 *
6141 * 4. If the message uses the media type "multipart/byteranges", and
6142 * the transfer-length is not otherwise specified, then this self-
6143 * delimiting media type defines the transfer-length. This media
6144 * type MUST NOT be used unless the sender knows that the recipient
6145 * can parse it; the presence in a request of a Range header with
6146 * multiple byte-range specifiers from a 1.1 client implies that the
6147 * client can parse multipart/byteranges responses.
6148 *
6149 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006150 */
6151
6152 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006153 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006154 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006155 * FIXME: should we parse anyway and return an error on chunked encoding ?
6156 */
6157 if (txn->meth == HTTP_METH_HEAD ||
6158 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006159 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006160 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006161 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006162 goto skip_content_length;
6163 }
6164
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006165 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006166 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006167 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006168 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006169 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006170 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6171 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006172 /* bad transfer-encoding (chunked followed by something else) */
6173 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006174 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006175 break;
6176 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006177 }
6178
6179 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
6180 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006181 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006182 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006183 signed long long cl;
6184
Willy Tarreauad14f752011-09-02 20:33:27 +02006185 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006186 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006187 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006188 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006189
Willy Tarreauad14f752011-09-02 20:33:27 +02006190 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006191 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006192 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006193 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006194
Willy Tarreauad14f752011-09-02 20:33:27 +02006195 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006196 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006197 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006198 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006199
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006200 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006201 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006202 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006203 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006204
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006205 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006206 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006207 }
6208
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006209 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006210 select_compression_response_header(s, rep->buf);
6211
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006212skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006213 /* Now we have to check if we need to modify the Connection header.
6214 * This is more difficult on the response than it is on the request,
6215 * because we can have two different HTTP versions and we don't know
6216 * how the client will interprete a response. For instance, let's say
6217 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6218 * HTTP/1.1 response without any header. Maybe it will bound itself to
6219 * HTTP/1.0 because it only knows about it, and will consider the lack
6220 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6221 * the lack of header as a keep-alive. Thus we will use two flags
6222 * indicating how a request MAY be understood by the client. In case
6223 * of multiple possibilities, we'll fix the header to be explicit. If
6224 * ambiguous cases such as both close and keepalive are seen, then we
6225 * will fall back to explicit close. Note that we won't take risks with
6226 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006227 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006228 */
6229
Willy Tarreaudc008c52010-02-01 16:20:08 +01006230 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6231 txn->status == 101)) {
6232 /* Either we've established an explicit tunnel, or we're
6233 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006234 * to understand the next protocols. We have to switch to tunnel
6235 * mode, so that we transfer the request and responses then let
6236 * this protocol pass unmodified. When we later implement specific
6237 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006238 * header which contains information about that protocol for
6239 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006240 */
6241 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6242 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006243 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6244 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006245 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006246 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006247 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006248
Willy Tarreau70dffda2014-01-30 03:07:23 +01006249 /* this situation happens when combining pretend-keepalive with httpclose. */
6250 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006251 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006252 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006253 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6254
Willy Tarreau60466522010-01-18 19:08:45 +01006255 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006256 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006257 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6258 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006259
Willy Tarreau60466522010-01-18 19:08:45 +01006260 /* now adjust header transformations depending on current state */
6261 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6262 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6263 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006264 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006265 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006266 }
Willy Tarreau60466522010-01-18 19:08:45 +01006267 else { /* SCL / KAL */
6268 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006269 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006270 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006271 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006272
Willy Tarreau60466522010-01-18 19:08:45 +01006273 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006274 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006275
Willy Tarreau60466522010-01-18 19:08:45 +01006276 /* Some keep-alive responses are converted to Server-close if
6277 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006278 */
Willy Tarreau60466522010-01-18 19:08:45 +01006279 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6280 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006281 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006282 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006283 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006284 }
6285
Willy Tarreau7959a552013-09-23 16:44:27 +02006286 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006287 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006288
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006289 /* end of job, return OK */
6290 rep->analysers &= ~an_bit;
6291 rep->analyse_exp = TICK_ETERNITY;
6292 channel_auto_close(rep);
6293 return 1;
6294
6295 abort_keep_alive:
6296 /* A keep-alive request to the server failed on a network error.
6297 * The client is required to retry. We need to close without returning
6298 * any other information so that the client retries.
6299 */
6300 txn->status = 0;
6301 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006302 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006303 channel_auto_close(rep);
6304 s->logs.logwait = 0;
6305 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006306 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006307 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006308 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006309 return 0;
6310}
6311
6312/* This function performs all the processing enabled for the current response.
6313 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006314 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006315 * other functions. It works like process_request (see indications above).
6316 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006317int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006318{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006319 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006320 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006321 struct http_msg *msg = &txn->rsp;
6322 struct proxy *cur_proxy;
6323 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006324 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006325
Willy Tarreau87b09662015-04-03 00:22:06 +02006326 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 +02006327 now_ms, __FUNCTION__,
6328 s,
6329 rep,
6330 rep->rex, rep->wex,
6331 rep->flags,
6332 rep->buf->i,
6333 rep->analysers);
6334
6335 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6336 return 0;
6337
Willy Tarreau70730dd2014-04-24 18:06:27 +02006338 /* The stats applet needs to adjust the Connection header but we don't
6339 * apply any filter there.
6340 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006341 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6342 rep->analysers &= ~an_bit;
6343 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006344 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006345 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006346
Willy Tarreau58975672014-04-24 21:13:57 +02006347 /*
6348 * We will have to evaluate the filters.
6349 * As opposed to version 1.2, now they will be evaluated in the
6350 * filters order and not in the header order. This means that
6351 * each filter has to be validated among all headers.
6352 *
6353 * Filters are tried with ->be first, then with ->fe if it is
6354 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006355 *
6356 * Maybe we are in resume condiion. In this case I choose the
6357 * "struct proxy" which contains the rule list matching the resume
6358 * pointer. If none of theses "struct proxy" match, I initialise
6359 * the process with the first one.
6360 *
6361 * In fact, I check only correspondance betwwen the current list
6362 * pointer and the ->fe rule list. If it doesn't match, I initialize
6363 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006364 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006365 if (s->current_rule_list == &sess->fe->http_res_rules)
6366 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006367 else
6368 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006369 while (1) {
6370 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006371
Willy Tarreau58975672014-04-24 21:13:57 +02006372 /* evaluate http-response rules */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006373 if (ret == HTTP_RULE_RES_CONT)
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006374 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006375
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006376 /* we need to be called again. */
6377 if (ret == HTTP_RULE_RES_YIELD) {
6378 channel_dont_close(rep);
6379 return 0;
6380 }
6381
Willy Tarreau58975672014-04-24 21:13:57 +02006382 /* try headers filters */
6383 if (rule_set->rsp_exp != NULL) {
6384 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6385 return_bad_resp:
6386 if (objt_server(s->target)) {
6387 objt_server(s->target)->counters.failed_resp++;
6388 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006389 }
Willy Tarreau58975672014-04-24 21:13:57 +02006390 s->be->be_counters.failed_resp++;
6391 return_srv_prx_502:
6392 rep->analysers = 0;
6393 txn->status = 502;
6394 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006395 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006396 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006397 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006398 if (!(s->flags & SF_ERR_MASK))
6399 s->flags |= SF_ERR_PRXCOND;
6400 if (!(s->flags & SF_FINST_MASK))
6401 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006402 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006403 }
Willy Tarreau58975672014-04-24 21:13:57 +02006404 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006405
Willy Tarreau58975672014-04-24 21:13:57 +02006406 /* has the response been denied ? */
6407 if (txn->flags & TX_SVDENY) {
6408 if (objt_server(s->target))
6409 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006410
Willy Tarreau58975672014-04-24 21:13:57 +02006411 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006412 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006413 if (sess->listener->counters)
6414 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006415
Willy Tarreau58975672014-04-24 21:13:57 +02006416 goto return_srv_prx_502;
6417 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006418
Willy Tarreau58975672014-04-24 21:13:57 +02006419 /* add response headers from the rule sets in the same order */
6420 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006421 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006422 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006423 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006424 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006425 ret = acl_pass(ret);
6426 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6427 ret = !ret;
6428 if (!ret)
6429 continue;
6430 }
6431 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6432 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006433 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006434
Willy Tarreau58975672014-04-24 21:13:57 +02006435 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006436 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006437 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006438 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006439 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006440
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006441 /* After this point, this anayzer can't return yield, so we can
6442 * remove the bit corresponding to this analyzer from the list.
6443 *
6444 * Note that the intermediate returns and goto found previously
6445 * reset the analyzers.
6446 */
6447 rep->analysers &= ~an_bit;
6448 rep->analyse_exp = TICK_ETERNITY;
6449
Willy Tarreau58975672014-04-24 21:13:57 +02006450 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006451 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006452 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006453
Willy Tarreau58975672014-04-24 21:13:57 +02006454 /*
6455 * Now check for a server cookie.
6456 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006457 if (s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name ||
Willy Tarreau58975672014-04-24 21:13:57 +02006458 (s->be->options & PR_O_CHK_CACHE))
6459 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006460
Willy Tarreau58975672014-04-24 21:13:57 +02006461 /*
6462 * Check for cache-control or pragma headers if required.
6463 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006464 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 +02006465 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006466
Willy Tarreau58975672014-04-24 21:13:57 +02006467 /*
6468 * Add server cookie in the response if needed
6469 */
6470 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6471 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006472 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006473 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6474 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6475 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6476 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6477 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006478 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006479 /* the server is known, it's not the one the client requested, or the
6480 * cookie's last seen date needs to be refreshed. We have to
6481 * insert a set-cookie here, except if we want to insert only on POST
6482 * requests and this one isn't. Note that servers which don't have cookies
6483 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006484 */
Willy Tarreau58975672014-04-24 21:13:57 +02006485 if (!objt_server(s->target)->cookie) {
6486 chunk_printf(&trash,
6487 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6488 s->be->cookie_name);
6489 }
6490 else {
6491 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006492
Willy Tarreau58975672014-04-24 21:13:57 +02006493 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6494 /* emit last_date, which is mandatory */
6495 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6496 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6497 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006498
Willy Tarreau58975672014-04-24 21:13:57 +02006499 if (s->be->cookie_maxlife) {
6500 /* emit first_date, which is either the original one or
6501 * the current date.
6502 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006503 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006504 s30tob64(txn->cookie_first_date ?
6505 txn->cookie_first_date >> 2 :
6506 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006507 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006508 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006509 }
Willy Tarreau58975672014-04-24 21:13:57 +02006510 chunk_appendf(&trash, "; path=/");
6511 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006512
Willy Tarreau58975672014-04-24 21:13:57 +02006513 if (s->be->cookie_domain)
6514 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006515
Willy Tarreau58975672014-04-24 21:13:57 +02006516 if (s->be->ck_opts & PR_CK_HTTPONLY)
6517 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006518
Willy Tarreau58975672014-04-24 21:13:57 +02006519 if (s->be->ck_opts & PR_CK_SECURE)
6520 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006521
Willy Tarreau58975672014-04-24 21:13:57 +02006522 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6523 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006524
Willy Tarreau58975672014-04-24 21:13:57 +02006525 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006526 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006527 /* the server did not change, only the date was updated */
6528 txn->flags |= TX_SCK_UPDATED;
6529 else
6530 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006531
Willy Tarreau58975672014-04-24 21:13:57 +02006532 /* Here, we will tell an eventual cache on the client side that we don't
6533 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6534 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6535 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006536 */
Willy Tarreau58975672014-04-24 21:13:57 +02006537 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006538
Willy Tarreau58975672014-04-24 21:13:57 +02006539 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006540
Willy Tarreau58975672014-04-24 21:13:57 +02006541 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6542 "Cache-control: private", 22) < 0))
6543 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006544 }
Willy Tarreau58975672014-04-24 21:13:57 +02006545 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006546
Willy Tarreau58975672014-04-24 21:13:57 +02006547 /*
6548 * Check if result will be cacheable with a cookie.
6549 * We'll block the response if security checks have caught
6550 * nasty things such as a cacheable cookie.
6551 */
6552 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6553 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6554 (s->be->options & PR_O_CHK_CACHE)) {
6555 /* we're in presence of a cacheable response containing
6556 * a set-cookie header. We'll block it as requested by
6557 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006558 */
Willy Tarreau58975672014-04-24 21:13:57 +02006559 if (objt_server(s->target))
6560 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006561
Willy Tarreau58975672014-04-24 21:13:57 +02006562 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006563 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006564 if (sess->listener->counters)
6565 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006566
Willy Tarreau58975672014-04-24 21:13:57 +02006567 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6568 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6569 send_log(s->be, LOG_ALERT,
6570 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6571 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6572 goto return_srv_prx_502;
6573 }
Willy Tarreau03945942009-12-22 16:50:27 +01006574
Willy Tarreau70730dd2014-04-24 18:06:27 +02006575 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006576 /*
6577 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6578 * If an "Upgrade" token is found, the header is left untouched in order
6579 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006580 * if anything but "Upgrade" is present in the Connection header. We don't
6581 * want to touch any 101 response either since it's switching to another
6582 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006583 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006584 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006585 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006586 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006587 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6588 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006589
Willy Tarreau58975672014-04-24 21:13:57 +02006590 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6591 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6592 /* we want a keep-alive response here. Keep-alive header
6593 * required if either side is not 1.1.
6594 */
6595 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6596 want_flags |= TX_CON_KAL_SET;
6597 }
6598 else {
6599 /* we want a close response here. Close header required if
6600 * the server is 1.1, regardless of the client.
6601 */
6602 if (msg->flags & HTTP_MSGF_VER_11)
6603 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006604 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006605
Willy Tarreau58975672014-04-24 21:13:57 +02006606 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6607 http_change_connection_header(txn, msg, want_flags);
6608 }
6609
6610 skip_header_mangling:
6611 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6612 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6613 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006614
Willy Tarreau58975672014-04-24 21:13:57 +02006615 /* if the user wants to log as soon as possible, without counting
6616 * bytes from the server, then this is the right moment. We have
6617 * to temporarily assign bytes_out to log what we currently have.
6618 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006619 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006620 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6621 s->logs.bytes_out = txn->rsp.eoh;
6622 s->do_log(s);
6623 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006624 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006625 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006626}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006627
Willy Tarreaud98cf932009-12-27 22:54:55 +01006628/* This function is an analyser which forwards response body (including chunk
6629 * sizes if any). It is called as soon as we must forward, even if we forward
6630 * zero byte. The only situation where it must not be called is when we're in
6631 * tunnel mode and we want to forward till the close. It's used both to forward
6632 * remaining data and to resync after end of body. It expects the msg_state to
6633 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006634 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006635 *
6636 * It is capable of compressing response data both in content-length mode and
6637 * in chunked mode. The state machines follows different flows depending on
6638 * whether content-length and chunked modes are used, since there are no
6639 * trailers in content-length :
6640 *
6641 * chk-mode cl-mode
6642 * ,----- BODY -----.
6643 * / \
6644 * V size > 0 V chk-mode
6645 * .--> SIZE -------------> DATA -------------> CRLF
6646 * | | size == 0 | last byte |
6647 * | v final crlf v inspected |
6648 * | TRAILERS -----------> DONE |
6649 * | |
6650 * `----------------------------------------------'
6651 *
6652 * Compression only happens in the DATA state, and must be flushed in final
6653 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6654 * is performed at once on final states for all bytes parsed, or when leaving
6655 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006656 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006657int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006658{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006659 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006660 struct http_txn *txn = s->txn;
6661 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006662 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006663 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006664 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006665
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006666 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6667 return 0;
6668
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006669 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006670 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006671 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006672 /* Output closed while we were sending data. We must abort and
6673 * wake the other side up.
6674 */
6675 msg->msg_state = HTTP_MSG_ERROR;
6676 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006677 return 1;
6678 }
6679
Willy Tarreau4fe41902010-06-07 22:27:41 +02006680 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006681 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006682
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006683 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006684 /* we have msg->sov which points to the first byte of message
6685 * body, and res->buf.p still points to the beginning of the
6686 * message. We forward the headers now, as we don't need them
6687 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006688 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006689 b_adv(res->buf, msg->sov);
6690 msg->next -= msg->sov;
6691 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006692
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006693 /* The previous analysers guarantee that the state is somewhere
6694 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6695 * msg->next are always correct.
6696 */
6697 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6698 if (msg->flags & HTTP_MSGF_TE_CHNK)
6699 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6700 else
6701 msg->msg_state = HTTP_MSG_DATA;
6702 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006703 }
6704
Willy Tarreauefdf0942014-04-24 20:08:57 +02006705 if (res->to_forward) {
6706 /* We can't process the buffer's contents yet */
6707 res->flags |= CF_WAKE_WRITE;
6708 goto missing_data;
6709 }
6710
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006711 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6712 /* We need a compression buffer in the DATA state to put the
6713 * output of compressed data, and in CRLF state to let the
6714 * TRAILERS state finish the job of removing the trailing CRLF.
6715 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006716 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006717 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006718 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006719 goto aborted_xfer; /* no memory */
6720 }
6721
6722 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006723 if (ret < 0) {
6724 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006725 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006726 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006727 compressing = 1;
6728 }
6729
Willy Tarreaud98cf932009-12-27 22:54:55 +01006730 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006731 switch (msg->msg_state - HTTP_MSG_DATA) {
6732 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006733 /* we may have some pending data starting at res->buf->p */
6734 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006735 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006736 if (ret < 0)
6737 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006738
Willy Tarreaud5a67832014-04-21 10:54:27 +02006739 if (msg->chunk_len) {
6740 /* input empty or output full */
6741 if (res->buf->i > msg->next)
6742 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006743 goto missing_data;
6744 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006745 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006746 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006747 if (msg->chunk_len > res->buf->i - msg->next) {
6748 /* output full */
6749 res->flags |= CF_WAKE_WRITE;
6750 goto missing_data;
6751 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006752 msg->next += msg->chunk_len;
6753 msg->chunk_len = 0;
6754 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006755
6756 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006757 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006758 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006759 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006760 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006761 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006762 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006763 /* fall through for HTTP_MSG_CHUNK_CRLF */
6764
6765 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6766 /* we want the CRLF after the data */
6767
6768 ret = http_skip_chunk_crlf(msg);
6769 if (ret == 0)
6770 goto missing_data;
6771 else if (ret < 0) {
6772 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006773 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006774 goto return_bad_res;
6775 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006776 /* we're in MSG_CHUNK_SIZE now, fall through */
6777
6778 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006779 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006780 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006781 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006782 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006783
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006784 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006785 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006786 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006787 else if (ret < 0) {
6788 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006789 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006790 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006791 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006792 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006793 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006794
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006795 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006796 if (unlikely(compressing)) {
6797 /* we need to flush output contents before syncing FSMs */
6798 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6799 compressing = 0;
6800 }
6801
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006802 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006803 if (ret == 0)
6804 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006805 else if (ret < 0) {
6806 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006807 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006808 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006809 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006810 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006811
6812 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006813 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006814 if (unlikely(compressing)) {
6815 /* we need to flush output contents before syncing FSMs */
6816 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6817 compressing = 0;
6818 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006819
Willy Tarreauc623c172014-04-18 09:53:50 +02006820 /* we may have some pending data starting at res->buf->p
6821 * such as a last chunk of data or trailers.
6822 */
6823 b_adv(res->buf, msg->next);
6824 msg->next = 0;
6825
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006826 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006827 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006828 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6829 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006830 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006831
Willy Tarreau610ecce2010-01-04 21:15:02 +01006832 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006833 /* some state changes occurred, maybe the analyser
6834 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006835 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006836 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006837 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006838 /* response errors are most likely due to
6839 * the client aborting the transfer.
6840 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006841 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006842 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006843 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006844 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006845 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006846 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006847 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006848 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006849 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006850 }
6851 }
6852
Willy Tarreaud98cf932009-12-27 22:54:55 +01006853 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006854 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006855 if (unlikely(compressing)) {
6856 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006857 compressing = 0;
6858 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006859
Willy Tarreauc623c172014-04-18 09:53:50 +02006860 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6861 b_adv(res->buf, msg->next);
6862 msg->next = 0;
6863 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6864 }
6865
Willy Tarreauf003d372012-11-26 13:35:37 +01006866 if (res->flags & CF_SHUTW)
6867 goto aborted_xfer;
6868
6869 /* stop waiting for data if the input is closed before the end. If the
6870 * client side was already closed, it means that the client has aborted,
6871 * so we don't want to count this as a server abort. Otherwise it's a
6872 * server abort.
6873 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006874 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006875 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006876 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006877 if (!(s->flags & SF_ERR_MASK))
6878 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006879 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006880 if (objt_server(s->target))
6881 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006882 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006883 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006884
Willy Tarreau40dba092010-03-04 18:14:51 +01006885 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006886 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006887 goto return_bad_res;
6888
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006889 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006890 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006891 * Similarly, with keep-alive on the client side, we don't want to forward a
6892 * close.
6893 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006894 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006895 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6896 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006897 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006898
Willy Tarreau5c620922011-05-11 19:56:11 +02006899 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006900 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006901 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006902 * modes are already handled by the stream sock layer. We must not do
6903 * this in content-length mode because it could present the MSG_MORE
6904 * flag with the last block of forwarded data, which would cause an
6905 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006906 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006907 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006908 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006909
Willy Tarreau87b09662015-04-03 00:22:06 +02006910 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006911 return 0;
6912
Willy Tarreau40dba092010-03-04 18:14:51 +01006913 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006914 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006915 if (objt_server(s->target))
6916 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006917
6918 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006919 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006920 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006921 compressing = 0;
6922 }
6923
Willy Tarreauc623c172014-04-18 09:53:50 +02006924 /* we may have some pending data starting at res->buf->p */
6925 if (s->comp_algo == NULL) {
6926 b_adv(res->buf, msg->next);
6927 msg->next = 0;
6928 }
6929
Willy Tarreaud98cf932009-12-27 22:54:55 +01006930 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006931 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006932 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006933 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006934 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006935 if (objt_server(s->target))
6936 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006937
Willy Tarreaue7dff022015-04-03 01:14:29 +02006938 if (!(s->flags & SF_ERR_MASK))
6939 s->flags |= SF_ERR_PRXCOND;
6940 if (!(s->flags & SF_FINST_MASK))
6941 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006942 return 0;
6943
6944 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006945 if (unlikely(compressing)) {
6946 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6947 compressing = 0;
6948 }
6949
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006950 txn->rsp.msg_state = HTTP_MSG_ERROR;
6951 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006952 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006953 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006954 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006955
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006956 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006957 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006958 if (objt_server(s->target))
6959 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006960
Willy Tarreaue7dff022015-04-03 01:14:29 +02006961 if (!(s->flags & SF_ERR_MASK))
6962 s->flags |= SF_ERR_CLICL;
6963 if (!(s->flags & SF_FINST_MASK))
6964 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006965 return 0;
6966}
6967
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006968/* Iterate the same filter through all request headers.
6969 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006970 * Since it can manage the switch to another backend, it updates the per-proxy
6971 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006972 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006973int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006974{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006975 char *cur_ptr, *cur_end, *cur_next;
6976 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006977 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006978 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006979 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006980
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006981 last_hdr = 0;
6982
Willy Tarreau9b28e032012-10-12 23:49:43 +02006983 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006984 old_idx = 0;
6985
6986 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006987 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006988 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006989 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006990 (exp->action == ACT_ALLOW ||
6991 exp->action == ACT_DENY ||
6992 exp->action == ACT_TARPIT))
6993 return 0;
6994
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006995 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006996 if (!cur_idx)
6997 break;
6998
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006999 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007000 cur_ptr = cur_next;
7001 cur_end = cur_ptr + cur_hdr->len;
7002 cur_next = cur_end + cur_hdr->cr + 1;
7003
7004 /* Now we have one header between cur_ptr and cur_end,
7005 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007006 */
7007
Willy Tarreau15a53a42015-01-21 13:39:42 +01007008 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007009 switch (exp->action) {
7010 case ACT_SETBE:
7011 /* It is not possible to jump a second time.
7012 * FIXME: should we return an HTTP/500 here so that
7013 * the admin knows there's a problem ?
7014 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007015 if (s->be != strm_fe(s))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007016 break;
7017
7018 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02007019 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007020 last_hdr = 1;
7021 break;
7022
7023 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007024 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007025 last_hdr = 1;
7026 break;
7027
7028 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007029 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007030 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007031 break;
7032
7033 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007034 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007035 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007036 break;
7037
7038 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007039 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7040 if (trash.len < 0)
7041 return -1;
7042
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007043 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007044 /* FIXME: if the user adds a newline in the replacement, the
7045 * index will not be recalculated for now, and the new line
7046 * will not be counted as a new header.
7047 */
7048
7049 cur_end += delta;
7050 cur_next += delta;
7051 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007052 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007053 break;
7054
7055 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007056 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007057 cur_next += delta;
7058
Willy Tarreaufa355d42009-11-29 18:12:29 +01007059 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007060 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7061 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007062 cur_hdr->len = 0;
7063 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007064 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007065 break;
7066
7067 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007068 }
7069
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007070 /* keep the link from this header to next one in case of later
7071 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007072 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007073 old_idx = cur_idx;
7074 }
7075 return 0;
7076}
7077
7078
7079/* Apply the filter to the request line.
7080 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7081 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007082 * Since it can manage the switch to another backend, it updates the per-proxy
7083 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007084 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007085int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007086{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007087 char *cur_ptr, *cur_end;
7088 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007089 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007090 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007091
Willy Tarreau3d300592007-03-18 18:34:41 +01007092 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007093 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007094 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007095 (exp->action == ACT_ALLOW ||
7096 exp->action == ACT_DENY ||
7097 exp->action == ACT_TARPIT))
7098 return 0;
7099 else if (exp->action == ACT_REMOVE)
7100 return 0;
7101
7102 done = 0;
7103
Willy Tarreau9b28e032012-10-12 23:49:43 +02007104 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007105 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007106
7107 /* Now we have the request line between cur_ptr and cur_end */
7108
Willy Tarreau15a53a42015-01-21 13:39:42 +01007109 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007110 switch (exp->action) {
7111 case ACT_SETBE:
7112 /* It is not possible to jump a second time.
7113 * FIXME: should we return an HTTP/500 here so that
7114 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007115 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007116 if (s->be != strm_fe(s))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007117 break;
7118
7119 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02007120 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007121 done = 1;
7122 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007123
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007124 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007125 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007126 done = 1;
7127 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007128
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007129 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007130 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007131 done = 1;
7132 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007133
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007134 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007135 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007136 done = 1;
7137 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007138
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007139 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007140 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7141 if (trash.len < 0)
7142 return -1;
7143
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007144 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007145 /* FIXME: if the user adds a newline in the replacement, the
7146 * index will not be recalculated for now, and the new line
7147 * will not be counted as a new header.
7148 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007149
Willy Tarreaufa355d42009-11-29 18:12:29 +01007150 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007151 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007152 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007153 HTTP_MSG_RQMETH,
7154 cur_ptr, cur_end + 1,
7155 NULL, NULL);
7156 if (unlikely(!cur_end))
7157 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007158
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007159 /* we have a full request and we know that we have either a CR
7160 * or an LF at <ptr>.
7161 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007162 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7163 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007164 /* there is no point trying this regex on headers */
7165 return 1;
7166 }
7167 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007168 return done;
7169}
Willy Tarreau97de6242006-12-27 17:18:38 +01007170
Willy Tarreau58f10d72006-12-04 02:26:12 +01007171
Willy Tarreau58f10d72006-12-04 02:26:12 +01007172
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007173/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007174 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007175 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176 * unparsable request. Since it can manage the switch to another backend, it
7177 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007178 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007179int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007180{
Willy Tarreau192252e2015-04-04 01:47:55 +02007181 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007182 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007183 struct hdr_exp *exp;
7184
7185 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007186 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007187
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007188 /*
7189 * The interleaving of transformations and verdicts
7190 * makes it difficult to decide to continue or stop
7191 * the evaluation.
7192 */
7193
Willy Tarreau6c123b12010-01-28 20:22:06 +01007194 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7195 break;
7196
Willy Tarreau3d300592007-03-18 18:34:41 +01007197 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007198 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007199 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007200 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007201
7202 /* if this filter had a condition, evaluate it now and skip to
7203 * next filter if the condition does not match.
7204 */
7205 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007206 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007207 ret = acl_pass(ret);
7208 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7209 ret = !ret;
7210
7211 if (!ret)
7212 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007213 }
7214
7215 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007216 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007217 if (unlikely(ret < 0))
7218 return -1;
7219
7220 if (likely(ret == 0)) {
7221 /* The filter did not match the request, it can be
7222 * iterated through all headers.
7223 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007224 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7225 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007226 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007227 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007228 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007229}
7230
7231
Willy Tarreaua15645d2007-03-18 16:22:39 +01007232
Willy Tarreau58f10d72006-12-04 02:26:12 +01007233/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007234 * Try to retrieve the server associated to the appsession.
Willy Tarreau87b09662015-04-03 00:22:06 +02007235 * If the server is found, it's assigned to the stream.
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007236 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007237void manage_client_side_appsession(struct stream *s, const char *buf, int len) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02007238 struct http_txn *txn = s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007239 appsess *asession = NULL;
7240 char *sessid_temp = NULL;
7241
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007242 if (len > s->be->appsession_len) {
7243 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007244 }
7245
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007246 if (s->be->options2 & PR_O2_AS_REQL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007247 /* request-learn option is enabled : store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007248 if (txn->sessid != NULL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007249 /* free previously allocated memory as we don't need the stream id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007250 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007251 }
7252
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007253 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007254 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007255 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007256 return;
7257 }
7258
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007259 memcpy(txn->sessid, buf, len);
7260 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007261 }
7262
7263 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7264 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007265 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007266 return;
7267 }
7268
Cyril Bontéb21570a2009-11-29 20:04:48 +01007269 memcpy(sessid_temp, buf, len);
7270 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007271
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007272 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007273 /* free previously allocated memory */
7274 pool_free2(apools.sessid, sessid_temp);
7275
7276 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007277 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7278 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007279 asession->request_count++;
7280
7281 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007282 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007283
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007284 while (srv) {
7285 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007286 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007287 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007288 (s->flags & SF_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007289 /* we found the server and it's usable */
7290 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007291 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007292 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007293 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007294
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007295 break;
7296 } else {
7297 txn->flags &= ~TX_CK_MASK;
7298 txn->flags |= TX_CK_DOWN;
7299 }
7300 }
7301 srv = srv->next;
7302 }
7303 }
7304 }
7305}
7306
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007307/* Find the end of a cookie value contained between <s> and <e>. It works the
7308 * same way as with headers above except that the semi-colon also ends a token.
7309 * See RFC2965 for more information. Note that it requires a valid header to
7310 * return a valid result.
7311 */
7312char *find_cookie_value_end(char *s, const char *e)
7313{
7314 int quoted, qdpair;
7315
7316 quoted = qdpair = 0;
7317 for (; s < e; s++) {
7318 if (qdpair) qdpair = 0;
7319 else if (quoted) {
7320 if (*s == '\\') qdpair = 1;
7321 else if (*s == '"') quoted = 0;
7322 }
7323 else if (*s == '"') quoted = 1;
7324 else if (*s == ',' || *s == ';') return s;
7325 }
7326 return s;
7327}
7328
7329/* Delete a value in a header between delimiters <from> and <next> in buffer
7330 * <buf>. The number of characters displaced is returned, and the pointer to
7331 * the first delimiter is updated if required. The function tries as much as
7332 * possible to respect the following principles :
7333 * - replace <from> delimiter by the <next> one unless <from> points to a
7334 * colon, in which case <next> is simply removed
7335 * - set exactly one space character after the new first delimiter, unless
7336 * there are not enough characters in the block being moved to do so.
7337 * - remove unneeded spaces before the previous delimiter and after the new
7338 * one.
7339 *
7340 * It is the caller's responsibility to ensure that :
7341 * - <from> points to a valid delimiter or the colon ;
7342 * - <next> points to a valid delimiter or the final CR/LF ;
7343 * - there are non-space chars before <from> ;
7344 * - there is a CR/LF at or after <next>.
7345 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007346int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007347{
7348 char *prev = *from;
7349
7350 if (*prev == ':') {
7351 /* We're removing the first value, preserve the colon and add a
7352 * space if possible.
7353 */
7354 if (!http_is_crlf[(unsigned char)*next])
7355 next++;
7356 prev++;
7357 if (prev < next)
7358 *prev++ = ' ';
7359
7360 while (http_is_spht[(unsigned char)*next])
7361 next++;
7362 } else {
7363 /* Remove useless spaces before the old delimiter. */
7364 while (http_is_spht[(unsigned char)*(prev-1)])
7365 prev--;
7366 *from = prev;
7367
7368 /* copy the delimiter and if possible a space if we're
7369 * not at the end of the line.
7370 */
7371 if (!http_is_crlf[(unsigned char)*next]) {
7372 *prev++ = *next++;
7373 if (prev + 1 < next)
7374 *prev++ = ' ';
7375 while (http_is_spht[(unsigned char)*next])
7376 next++;
7377 }
7378 }
7379 return buffer_replace2(buf, prev, next, NULL, 0);
7380}
7381
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007382/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007383 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007384 * desirable to call it only when needed. This code is quite complex because
7385 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7386 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007387 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007388void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007389{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007390 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007391 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007392 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007393 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007394 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7395 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007396
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007397 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007398 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007399 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007400
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007401 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007402 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007403 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007404
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007405 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007406 hdr_beg = hdr_next;
7407 hdr_end = hdr_beg + cur_hdr->len;
7408 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007409
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007410 /* We have one full header between hdr_beg and hdr_end, and the
7411 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007412 * "Cookie:" headers.
7413 */
7414
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007415 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007416 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007417 old_idx = cur_idx;
7418 continue;
7419 }
7420
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007421 del_from = NULL; /* nothing to be deleted */
7422 preserve_hdr = 0; /* assume we may kill the whole header */
7423
Willy Tarreau58f10d72006-12-04 02:26:12 +01007424 /* Now look for cookies. Conforming to RFC2109, we have to support
7425 * attributes whose name begin with a '$', and associate them with
7426 * the right cookie, if we want to delete this cookie.
7427 * So there are 3 cases for each cookie read :
7428 * 1) it's a special attribute, beginning with a '$' : ignore it.
7429 * 2) it's a server id cookie that we *MAY* want to delete : save
7430 * some pointers on it (last semi-colon, beginning of cookie...)
7431 * 3) it's an application cookie : we *MAY* have to delete a previous
7432 * "special" cookie.
7433 * At the end of loop, if a "special" cookie remains, we may have to
7434 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007435 * *MUST* delete it.
7436 *
7437 * Note: RFC2965 is unclear about the processing of spaces around
7438 * the equal sign in the ATTR=VALUE form. A careful inspection of
7439 * the RFC explicitly allows spaces before it, and not within the
7440 * tokens (attrs or values). An inspection of RFC2109 allows that
7441 * too but section 10.1.3 lets one think that spaces may be allowed
7442 * after the equal sign too, resulting in some (rare) buggy
7443 * implementations trying to do that. So let's do what servers do.
7444 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7445 * allowed quoted strings in values, with any possible character
7446 * after a backslash, including control chars and delimitors, which
7447 * causes parsing to become ambiguous. Browsers also allow spaces
7448 * within values even without quotes.
7449 *
7450 * We have to keep multiple pointers in order to support cookie
7451 * removal at the beginning, middle or end of header without
7452 * corrupting the header. All of these headers are valid :
7453 *
7454 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7455 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7456 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7457 * | | | | | | | | |
7458 * | | | | | | | | hdr_end <--+
7459 * | | | | | | | +--> next
7460 * | | | | | | +----> val_end
7461 * | | | | | +-----------> val_beg
7462 * | | | | +--------------> equal
7463 * | | | +----------------> att_end
7464 * | | +---------------------> att_beg
7465 * | +--------------------------> prev
7466 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007467 */
7468
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007469 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7470 /* Iterate through all cookies on this line */
7471
7472 /* find att_beg */
7473 att_beg = prev + 1;
7474 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7475 att_beg++;
7476
7477 /* find att_end : this is the first character after the last non
7478 * space before the equal. It may be equal to hdr_end.
7479 */
7480 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007481
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007482 while (equal < hdr_end) {
7483 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007484 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007485 if (http_is_spht[(unsigned char)*equal++])
7486 continue;
7487 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007488 }
7489
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007490 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7491 * is between <att_beg> and <equal>, both may be identical.
7492 */
7493
7494 /* look for end of cookie if there is an equal sign */
7495 if (equal < hdr_end && *equal == '=') {
7496 /* look for the beginning of the value */
7497 val_beg = equal + 1;
7498 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7499 val_beg++;
7500
7501 /* find the end of the value, respecting quotes */
7502 next = find_cookie_value_end(val_beg, hdr_end);
7503
7504 /* make val_end point to the first white space or delimitor after the value */
7505 val_end = next;
7506 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7507 val_end--;
7508 } else {
7509 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007510 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007511
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007512 /* We have nothing to do with attributes beginning with '$'. However,
7513 * they will automatically be removed if a header before them is removed,
7514 * since they're supposed to be linked together.
7515 */
7516 if (*att_beg == '$')
7517 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007518
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007519 /* Ignore cookies with no equal sign */
7520 if (equal == next) {
7521 /* This is not our cookie, so we must preserve it. But if we already
7522 * scheduled another cookie for removal, we cannot remove the
7523 * complete header, but we can remove the previous block itself.
7524 */
7525 preserve_hdr = 1;
7526 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007527 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007528 val_end += delta;
7529 next += delta;
7530 hdr_end += delta;
7531 hdr_next += delta;
7532 cur_hdr->len += delta;
7533 http_msg_move_end(&txn->req, delta);
7534 prev = del_from;
7535 del_from = NULL;
7536 }
7537 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007538 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007539
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007540 /* if there are spaces around the equal sign, we need to
7541 * strip them otherwise we'll get trouble for cookie captures,
7542 * or even for rewrites. Since this happens extremely rarely,
7543 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007544 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007545 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7546 int stripped_before = 0;
7547 int stripped_after = 0;
7548
7549 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007550 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007551 equal += stripped_before;
7552 val_beg += stripped_before;
7553 }
7554
7555 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007556 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007557 val_beg += stripped_after;
7558 stripped_before += stripped_after;
7559 }
7560
7561 val_end += stripped_before;
7562 next += stripped_before;
7563 hdr_end += stripped_before;
7564 hdr_next += stripped_before;
7565 cur_hdr->len += stripped_before;
7566 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007567 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007568 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007569
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007570 /* First, let's see if we want to capture this cookie. We check
7571 * that we don't already have a client side cookie, because we
7572 * can only capture one. Also as an optimisation, we ignore
7573 * cookies shorter than the declared name.
7574 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007575 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7576 (val_end - att_beg >= sess->fe->capture_namelen) &&
7577 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007578 int log_len = val_end - att_beg;
7579
7580 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7581 Alert("HTTP logging : out of memory.\n");
7582 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007583 if (log_len > sess->fe->capture_len)
7584 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007585 memcpy(txn->cli_cookie, att_beg, log_len);
7586 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007587 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007588 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007589
Willy Tarreaubca99692010-10-06 19:25:55 +02007590 /* Persistence cookies in passive, rewrite or insert mode have the
7591 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007592 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007593 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007594 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007595 * For cookies in prefix mode, the form is :
7596 *
7597 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007598 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007599 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7600 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7601 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007602 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007603
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007604 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7605 * have the server ID between val_beg and delim, and the original cookie between
7606 * delim+1 and val_end. Otherwise, delim==val_end :
7607 *
7608 * Cookie: NAME=SRV; # in all but prefix modes
7609 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7610 * | || || | |+-> next
7611 * | || || | +--> val_end
7612 * | || || +---------> delim
7613 * | || |+------------> val_beg
7614 * | || +-------------> att_end = equal
7615 * | |+-----------------> att_beg
7616 * | +------------------> prev
7617 * +-------------------------> hdr_beg
7618 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007619
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007620 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007621 for (delim = val_beg; delim < val_end; delim++)
7622 if (*delim == COOKIE_DELIM)
7623 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007624 } else {
7625 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007626 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007627 /* Now check if the cookie contains a date field, which would
7628 * appear after a vertical bar ('|') just after the server name
7629 * and before the delimiter.
7630 */
7631 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7632 if (vbar1) {
7633 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007634 * right is the last seen date. It is a base64 encoded
7635 * 30-bit value representing the UNIX date since the
7636 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007637 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007638 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007639 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007640 if (val_end - vbar1 >= 5) {
7641 val = b64tos30(vbar1);
7642 if (val > 0)
7643 txn->cookie_last_date = val << 2;
7644 }
7645 /* look for a second vertical bar */
7646 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7647 if (vbar1 && (val_end - vbar1 > 5)) {
7648 val = b64tos30(vbar1 + 1);
7649 if (val > 0)
7650 txn->cookie_first_date = val << 2;
7651 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007652 }
7653 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007654
Willy Tarreauf64d1412010-10-07 20:06:11 +02007655 /* if the cookie has an expiration date and the proxy wants to check
7656 * it, then we do that now. We first check if the cookie is too old,
7657 * then only if it has expired. We detect strict overflow because the
7658 * time resolution here is not great (4 seconds). Cookies with dates
7659 * in the future are ignored if their offset is beyond one day. This
7660 * allows an admin to fix timezone issues without expiring everyone
7661 * and at the same time avoids keeping unwanted side effects for too
7662 * long.
7663 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007664 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7665 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007666 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007667 txn->flags &= ~TX_CK_MASK;
7668 txn->flags |= TX_CK_OLD;
7669 delim = val_beg; // let's pretend we have not found the cookie
7670 txn->cookie_first_date = 0;
7671 txn->cookie_last_date = 0;
7672 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007673 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7674 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007675 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007676 txn->flags &= ~TX_CK_MASK;
7677 txn->flags |= TX_CK_EXPIRED;
7678 delim = val_beg; // let's pretend we have not found the cookie
7679 txn->cookie_first_date = 0;
7680 txn->cookie_last_date = 0;
7681 }
7682
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007683 /* Here, we'll look for the first running server which supports the cookie.
7684 * This allows to share a same cookie between several servers, for example
7685 * to dedicate backup servers to specific servers only.
7686 * However, to prevent clients from sticking to cookie-less backup server
7687 * when they have incidentely learned an empty cookie, we simply ignore
7688 * empty cookies and mark them as invalid.
7689 * The same behaviour is applied when persistence must be ignored.
7690 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007691 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007692 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007693
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007694 while (srv) {
7695 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7696 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007697 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007698 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007699 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007700 /* we found the server and we can use it */
7701 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007702 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007703 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007704 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007705 break;
7706 } else {
7707 /* we found a server, but it's down,
7708 * mark it as such and go on in case
7709 * another one is available.
7710 */
7711 txn->flags &= ~TX_CK_MASK;
7712 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007713 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007714 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007715 srv = srv->next;
7716 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007717
Willy Tarreauf64d1412010-10-07 20:06:11 +02007718 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007719 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007720 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007721 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007722 txn->flags |= TX_CK_UNUSED;
7723 else
7724 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007725 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007726
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007727 /* depending on the cookie mode, we may have to either :
7728 * - delete the complete cookie if we're in insert+indirect mode, so that
7729 * the server never sees it ;
7730 * - remove the server id from the cookie value, and tag the cookie as an
7731 * application cookie so that it does not get accidentely removed later,
7732 * if we're in cookie prefix mode
7733 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007734 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007735 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007736
Willy Tarreau9b28e032012-10-12 23:49:43 +02007737 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007738 val_end += delta;
7739 next += delta;
7740 hdr_end += delta;
7741 hdr_next += delta;
7742 cur_hdr->len += delta;
7743 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007744
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007745 del_from = NULL;
7746 preserve_hdr = 1; /* we want to keep this cookie */
7747 }
7748 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007749 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007750 del_from = prev;
7751 }
7752 } else {
7753 /* This is not our cookie, so we must preserve it. But if we already
7754 * scheduled another cookie for removal, we cannot remove the
7755 * complete header, but we can remove the previous block itself.
7756 */
7757 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007758
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007760 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007761 if (att_beg >= del_from)
7762 att_beg += delta;
7763 if (att_end >= del_from)
7764 att_end += delta;
7765 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007766 val_end += delta;
7767 next += delta;
7768 hdr_end += delta;
7769 hdr_next += delta;
7770 cur_hdr->len += delta;
7771 http_msg_move_end(&txn->req, delta);
7772 prev = del_from;
7773 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007774 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007775 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007776
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007777 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007778 if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007779 int cmp_len, value_len;
7780 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007781
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007782 if (s->be->options2 & PR_O2_AS_PFX) {
7783 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7784 value_begin = att_beg + s->be->appsession_name_len;
7785 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007786 } else {
7787 cmp_len = att_end - att_beg;
7788 value_begin = val_beg;
7789 value_len = val_end - val_beg;
7790 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007791
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007792 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007793 if (cmp_len == s->be->appsession_name_len &&
7794 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7795 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007796 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007797 }
7798
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007799 /* continue with next cookie on this header line */
7800 att_beg = next;
7801 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007802
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007803 /* There are no more cookies on this line.
7804 * We may still have one (or several) marked for deletion at the
7805 * end of the line. We must do this now in two ways :
7806 * - if some cookies must be preserved, we only delete from the
7807 * mark to the end of line ;
7808 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007809 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007810 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007811 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007812 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007813 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007814 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007815 cur_hdr->len += delta;
7816 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007817 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007818
7819 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007820 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7821 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007822 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007823 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007824 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007825 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007826 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007827 }
7828
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007829 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007830 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007831 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007832}
7833
7834
Willy Tarreaua15645d2007-03-18 16:22:39 +01007835/* Iterate the same filter through all response headers contained in <rtr>.
7836 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7837 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007838int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007839{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007840 char *cur_ptr, *cur_end, *cur_next;
7841 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007842 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007843 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007844 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007845
7846 last_hdr = 0;
7847
Willy Tarreau9b28e032012-10-12 23:49:43 +02007848 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007849 old_idx = 0;
7850
7851 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007852 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007853 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007854 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007855 (exp->action == ACT_ALLOW ||
7856 exp->action == ACT_DENY))
7857 return 0;
7858
7859 cur_idx = txn->hdr_idx.v[old_idx].next;
7860 if (!cur_idx)
7861 break;
7862
7863 cur_hdr = &txn->hdr_idx.v[cur_idx];
7864 cur_ptr = cur_next;
7865 cur_end = cur_ptr + cur_hdr->len;
7866 cur_next = cur_end + cur_hdr->cr + 1;
7867
7868 /* Now we have one header between cur_ptr and cur_end,
7869 * and the next header starts at cur_next.
7870 */
7871
Willy Tarreau15a53a42015-01-21 13:39:42 +01007872 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007873 switch (exp->action) {
7874 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007875 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007876 last_hdr = 1;
7877 break;
7878
7879 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007880 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007881 last_hdr = 1;
7882 break;
7883
7884 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007885 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7886 if (trash.len < 0)
7887 return -1;
7888
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007889 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007890 /* FIXME: if the user adds a newline in the replacement, the
7891 * index will not be recalculated for now, and the new line
7892 * will not be counted as a new header.
7893 */
7894
7895 cur_end += delta;
7896 cur_next += delta;
7897 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007898 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007899 break;
7900
7901 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007902 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007903 cur_next += delta;
7904
Willy Tarreaufa355d42009-11-29 18:12:29 +01007905 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007906 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7907 txn->hdr_idx.used--;
7908 cur_hdr->len = 0;
7909 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007910 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007911 break;
7912
7913 }
7914 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007915
7916 /* keep the link from this header to next one in case of later
7917 * removal of next header.
7918 */
7919 old_idx = cur_idx;
7920 }
7921 return 0;
7922}
7923
7924
7925/* Apply the filter to the status line in the response buffer <rtr>.
7926 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7927 * or -1 if a replacement resulted in an invalid status line.
7928 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007929int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007930{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007931 char *cur_ptr, *cur_end;
7932 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007933 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007934 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007935
7936
Willy Tarreau3d300592007-03-18 18:34:41 +01007937 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007938 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007939 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007940 (exp->action == ACT_ALLOW ||
7941 exp->action == ACT_DENY))
7942 return 0;
7943 else if (exp->action == ACT_REMOVE)
7944 return 0;
7945
7946 done = 0;
7947
Willy Tarreau9b28e032012-10-12 23:49:43 +02007948 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007949 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007950
7951 /* Now we have the status line between cur_ptr and cur_end */
7952
Willy Tarreau15a53a42015-01-21 13:39:42 +01007953 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007954 switch (exp->action) {
7955 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007956 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007957 done = 1;
7958 break;
7959
7960 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007961 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007962 done = 1;
7963 break;
7964
7965 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007966 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7967 if (trash.len < 0)
7968 return -1;
7969
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007970 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007971 /* FIXME: if the user adds a newline in the replacement, the
7972 * index will not be recalculated for now, and the new line
7973 * will not be counted as a new header.
7974 */
7975
Willy Tarreaufa355d42009-11-29 18:12:29 +01007976 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007977 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007978 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007979 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007980 cur_ptr, cur_end + 1,
7981 NULL, NULL);
7982 if (unlikely(!cur_end))
7983 return -1;
7984
7985 /* we have a full respnse and we know that we have either a CR
7986 * or an LF at <ptr>.
7987 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007988 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007989 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007990 /* there is no point trying this regex on headers */
7991 return 1;
7992 }
7993 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007994 return done;
7995}
7996
7997
7998
7999/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008000 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008001 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8002 * unparsable response.
8003 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008004int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008005{
Willy Tarreau192252e2015-04-04 01:47:55 +02008006 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008007 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008008 struct hdr_exp *exp;
8009
8010 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008011 int ret;
8012
8013 /*
8014 * The interleaving of transformations and verdicts
8015 * makes it difficult to decide to continue or stop
8016 * the evaluation.
8017 */
8018
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008019 if (txn->flags & TX_SVDENY)
8020 break;
8021
Willy Tarreau3d300592007-03-18 18:34:41 +01008022 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8024 exp->action == ACT_PASS)) {
8025 exp = exp->next;
8026 continue;
8027 }
8028
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008029 /* if this filter had a condition, evaluate it now and skip to
8030 * next filter if the condition does not match.
8031 */
8032 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008033 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008034 ret = acl_pass(ret);
8035 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8036 ret = !ret;
8037 if (!ret)
8038 continue;
8039 }
8040
Willy Tarreaua15645d2007-03-18 16:22:39 +01008041 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008042 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008043 if (unlikely(ret < 0))
8044 return -1;
8045
8046 if (likely(ret == 0)) {
8047 /* The filter did not match the response, it can be
8048 * iterated through all headers.
8049 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008050 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8051 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008052 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008053 }
8054 return 0;
8055}
8056
8057
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008059 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008060 * desirable to call it only when needed. This function is also used when we
8061 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008062 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008063void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008064{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008065 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008066 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008067 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008068 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008069 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008070 char *hdr_beg, *hdr_end, *hdr_next;
8071 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008072
Willy Tarreaua15645d2007-03-18 16:22:39 +01008073 /* Iterate through the headers.
8074 * we start with the start line.
8075 */
8076 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008077 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008078
8079 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8080 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008081 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008082
8083 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008084 hdr_beg = hdr_next;
8085 hdr_end = hdr_beg + cur_hdr->len;
8086 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008087
Willy Tarreau24581ba2010-08-31 22:39:35 +02008088 /* We have one full header between hdr_beg and hdr_end, and the
8089 * next header starts at hdr_next. We're only interested in
8090 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008091 */
8092
Willy Tarreau24581ba2010-08-31 22:39:35 +02008093 is_cookie2 = 0;
8094 prev = hdr_beg + 10;
8095 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008096 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008097 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8098 if (!val) {
8099 old_idx = cur_idx;
8100 continue;
8101 }
8102 is_cookie2 = 1;
8103 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008104 }
8105
Willy Tarreau24581ba2010-08-31 22:39:35 +02008106 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8107 * <prev> points to the colon.
8108 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008109 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008110
Willy Tarreau24581ba2010-08-31 22:39:35 +02008111 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8112 * check-cache is enabled) and we are not interested in checking
8113 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008114 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008115 if (s->be->cookie_name == NULL &&
8116 s->be->appsession_name == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008117 sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008118 return;
8119
Willy Tarreau24581ba2010-08-31 22:39:35 +02008120 /* OK so now we know we have to process this response cookie.
8121 * The format of the Set-Cookie header is slightly different
8122 * from the format of the Cookie header in that it does not
8123 * support the comma as a cookie delimiter (thus the header
8124 * cannot be folded) because the Expires attribute described in
8125 * the original Netscape's spec may contain an unquoted date
8126 * with a comma inside. We have to live with this because
8127 * many browsers don't support Max-Age and some browsers don't
8128 * support quoted strings. However the Set-Cookie2 header is
8129 * clean.
8130 *
8131 * We have to keep multiple pointers in order to support cookie
8132 * removal at the beginning, middle or end of header without
8133 * corrupting the header (in case of set-cookie2). A special
8134 * pointer, <scav> points to the beginning of the set-cookie-av
8135 * fields after the first semi-colon. The <next> pointer points
8136 * either to the end of line (set-cookie) or next unquoted comma
8137 * (set-cookie2). All of these headers are valid :
8138 *
8139 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8140 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8141 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8142 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8143 * | | | | | | | | | |
8144 * | | | | | | | | +-> next hdr_end <--+
8145 * | | | | | | | +------------> scav
8146 * | | | | | | +--------------> val_end
8147 * | | | | | +--------------------> val_beg
8148 * | | | | +----------------------> equal
8149 * | | | +------------------------> att_end
8150 * | | +----------------------------> att_beg
8151 * | +------------------------------> prev
8152 * +-----------------------------------------> hdr_beg
8153 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008154
Willy Tarreau24581ba2010-08-31 22:39:35 +02008155 for (; prev < hdr_end; prev = next) {
8156 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008157
Willy Tarreau24581ba2010-08-31 22:39:35 +02008158 /* find att_beg */
8159 att_beg = prev + 1;
8160 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8161 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008162
Willy Tarreau24581ba2010-08-31 22:39:35 +02008163 /* find att_end : this is the first character after the last non
8164 * space before the equal. It may be equal to hdr_end.
8165 */
8166 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008167
Willy Tarreau24581ba2010-08-31 22:39:35 +02008168 while (equal < hdr_end) {
8169 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8170 break;
8171 if (http_is_spht[(unsigned char)*equal++])
8172 continue;
8173 att_end = equal;
8174 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008175
Willy Tarreau24581ba2010-08-31 22:39:35 +02008176 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8177 * is between <att_beg> and <equal>, both may be identical.
8178 */
8179
8180 /* look for end of cookie if there is an equal sign */
8181 if (equal < hdr_end && *equal == '=') {
8182 /* look for the beginning of the value */
8183 val_beg = equal + 1;
8184 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8185 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008186
Willy Tarreau24581ba2010-08-31 22:39:35 +02008187 /* find the end of the value, respecting quotes */
8188 next = find_cookie_value_end(val_beg, hdr_end);
8189
8190 /* make val_end point to the first white space or delimitor after the value */
8191 val_end = next;
8192 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8193 val_end--;
8194 } else {
8195 /* <equal> points to next comma, semi-colon or EOL */
8196 val_beg = val_end = next = equal;
8197 }
8198
8199 if (next < hdr_end) {
8200 /* Set-Cookie2 supports multiple cookies, and <next> points to
8201 * a colon or semi-colon before the end. So skip all attr-value
8202 * pairs and look for the next comma. For Set-Cookie, since
8203 * commas are permitted in values, skip to the end.
8204 */
8205 if (is_cookie2)
8206 next = find_hdr_value_end(next, hdr_end);
8207 else
8208 next = hdr_end;
8209 }
8210
8211 /* Now everything is as on the diagram above */
8212
8213 /* Ignore cookies with no equal sign */
8214 if (equal == val_end)
8215 continue;
8216
8217 /* If there are spaces around the equal sign, we need to
8218 * strip them otherwise we'll get trouble for cookie captures,
8219 * or even for rewrites. Since this happens extremely rarely,
8220 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008221 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008222 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8223 int stripped_before = 0;
8224 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008225
Willy Tarreau24581ba2010-08-31 22:39:35 +02008226 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008227 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008228 equal += stripped_before;
8229 val_beg += stripped_before;
8230 }
8231
8232 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008233 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008234 val_beg += stripped_after;
8235 stripped_before += stripped_after;
8236 }
8237
8238 val_end += stripped_before;
8239 next += stripped_before;
8240 hdr_end += stripped_before;
8241 hdr_next += stripped_before;
8242 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008243 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008244 }
8245
8246 /* First, let's see if we want to capture this cookie. We check
8247 * that we don't already have a server side cookie, because we
8248 * can only capture one. Also as an optimisation, we ignore
8249 * cookies shorter than the declared name.
8250 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008251 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008252 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008253 (val_end - att_beg >= sess->fe->capture_namelen) &&
8254 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008255 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008256 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008257 Alert("HTTP logging : out of memory.\n");
8258 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008259 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008260 if (log_len > sess->fe->capture_len)
8261 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008262 memcpy(txn->srv_cookie, att_beg, log_len);
8263 txn->srv_cookie[log_len] = 0;
8264 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008265 }
8266
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008267 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008268 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008269 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008270 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8271 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008272 /* assume passive cookie by default */
8273 txn->flags &= ~TX_SCK_MASK;
8274 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008275
8276 /* If the cookie is in insert mode on a known server, we'll delete
8277 * this occurrence because we'll insert another one later.
8278 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008279 * a direct access.
8280 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008281 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008282 /* The "preserve" flag was set, we don't want to touch the
8283 * server's cookie.
8284 */
8285 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008286 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008287 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008288 /* this cookie must be deleted */
8289 if (*prev == ':' && next == hdr_end) {
8290 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008291 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008292 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8293 txn->hdr_idx.used--;
8294 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008295 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008296 hdr_next += delta;
8297 http_msg_move_end(&txn->rsp, delta);
8298 /* note: while both invalid now, <next> and <hdr_end>
8299 * are still equal, so the for() will stop as expected.
8300 */
8301 } else {
8302 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008303 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008304 next = prev;
8305 hdr_end += delta;
8306 hdr_next += delta;
8307 cur_hdr->len += delta;
8308 http_msg_move_end(&txn->rsp, delta);
8309 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008310 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008311 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008312 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008313 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008314 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008315 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008316 * with this server since we know it.
8317 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008318 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008319 next += delta;
8320 hdr_end += delta;
8321 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008322 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008323 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008324
Willy Tarreauf1348312010-10-07 15:54:11 +02008325 txn->flags &= ~TX_SCK_MASK;
8326 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008327 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008328 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008329 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008330 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008331 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008332 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008333 next += delta;
8334 hdr_end += delta;
8335 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008336 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008337 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008338
Willy Tarreau827aee92011-03-10 16:55:02 +01008339 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008340 txn->flags &= ~TX_SCK_MASK;
8341 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008342 }
8343 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008344 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008345 else if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008346 int cmp_len, value_len;
8347 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008348
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008349 if (s->be->options2 & PR_O2_AS_PFX) {
8350 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8351 value_begin = att_beg + s->be->appsession_name_len;
8352 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008353 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008354 cmp_len = att_end - att_beg;
8355 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008356 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008357 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008358
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008359 if ((cmp_len == s->be->appsession_name_len) &&
8360 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008361 /* free a possibly previously allocated memory */
8362 pool_free2(apools.sessid, txn->sessid);
8363
Willy Tarreau87b09662015-04-03 00:22:06 +02008364 /* Store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008365 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008366 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008367 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008368 return;
8369 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008370 memcpy(txn->sessid, value_begin, value_len);
8371 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008372 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008373 }
8374 /* that's done for this cookie, check the next one on the same
8375 * line when next != hdr_end (only if is_cookie2).
8376 */
8377 }
8378 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008379 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008380 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008381
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008382 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008383 appsess *asession = NULL;
8384 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008385 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008386 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008387 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008388 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8389 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008390 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008391 return;
8392 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008393 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8394
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008395 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8396 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008397 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8398 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008399 return;
8400 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008401 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8402 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008403
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008404 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008405 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008406 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008407 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8408 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008409 return;
8410 }
8411 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008412 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008413
8414 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008415 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008416 }
8417
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008418 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008419 asession->request_count++;
8420 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008421}
8422
8423
Willy Tarreaua15645d2007-03-18 16:22:39 +01008424/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008425 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008426 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008427void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008428{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008429 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008430 char *p1, *p2;
8431
8432 char *cur_ptr, *cur_end, *cur_next;
8433 int cur_idx;
8434
Willy Tarreau5df51872007-11-25 16:20:08 +01008435 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008436 return;
8437
8438 /* Iterate through the headers.
8439 * we start with the start line.
8440 */
8441 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008442 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008443
8444 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8445 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008446 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008447
8448 cur_hdr = &txn->hdr_idx.v[cur_idx];
8449 cur_ptr = cur_next;
8450 cur_end = cur_ptr + cur_hdr->len;
8451 cur_next = cur_end + cur_hdr->cr + 1;
8452
8453 /* We have one full header between cur_ptr and cur_end, and the
8454 * next header starts at cur_next. We're only interested in
8455 * "Cookie:" headers.
8456 */
8457
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008458 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8459 if (val) {
8460 if ((cur_end - (cur_ptr + val) >= 8) &&
8461 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8462 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8463 return;
8464 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008465 }
8466
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008467 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8468 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008469 continue;
8470
8471 /* OK, right now we know we have a cache-control header at cur_ptr */
8472
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008473 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008474
8475 if (p1 >= cur_end) /* no more info */
8476 continue;
8477
8478 /* p1 is at the beginning of the value */
8479 p2 = p1;
8480
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008481 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008482 p2++;
8483
8484 /* we have a complete value between p1 and p2 */
8485 if (p2 < cur_end && *p2 == '=') {
8486 /* we have something of the form no-cache="set-cookie" */
8487 if ((cur_end - p1 >= 21) &&
8488 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8489 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008490 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008491 continue;
8492 }
8493
8494 /* OK, so we know that either p2 points to the end of string or to a comma */
8495 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008496 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008497 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8498 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8499 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008500 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008501 return;
8502 }
8503
8504 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008505 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008506 continue;
8507 }
8508 }
8509}
8510
8511
Willy Tarreau58f10d72006-12-04 02:26:12 +01008512/*
8513 * Try to retrieve a known appsession in the URI, then the associated server.
Willy Tarreau87b09662015-04-03 00:22:06 +02008514 * If the server is found, it's assigned to the stream.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008515 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008516void get_srv_from_appsession(struct stream *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008517{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008518 char *end_params, *first_param, *cur_param, *next_param;
8519 char separator;
8520 int value_len;
8521
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008522 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008523
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008524 if (s->be->appsession_name == NULL ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02008525 (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 +01008526 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008527 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008528
Cyril Bontéb21570a2009-11-29 20:04:48 +01008529 first_param = NULL;
8530 switch (mode) {
8531 case PR_O2_AS_M_PP:
8532 first_param = memchr(begin, ';', len);
8533 break;
8534 case PR_O2_AS_M_QS:
8535 first_param = memchr(begin, '?', len);
8536 break;
8537 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008538
Cyril Bontéb21570a2009-11-29 20:04:48 +01008539 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008540 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008541 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008542
Cyril Bontéb21570a2009-11-29 20:04:48 +01008543 switch (mode) {
8544 case PR_O2_AS_M_PP:
8545 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8546 end_params = (char *) begin + len;
8547 }
8548 separator = ';';
8549 break;
8550 case PR_O2_AS_M_QS:
8551 end_params = (char *) begin + len;
8552 separator = '&';
8553 break;
8554 default:
8555 /* unknown mode, shouldn't happen */
8556 return;
8557 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008558
Cyril Bontéb21570a2009-11-29 20:04:48 +01008559 cur_param = next_param = end_params;
8560 while (cur_param > first_param) {
8561 cur_param--;
8562 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8563 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008564 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8565 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8566 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008567 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008568 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8569 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008570 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008571 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008572 }
8573 break;
8574 }
8575 next_param = cur_param;
8576 }
8577 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008578#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008579 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008580 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008581#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008582}
8583
Willy Tarreaub2513902006-12-17 14:52:38 +01008584/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008585 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008586 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008587 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008588 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008589 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008590 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008591 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008592 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008593int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008594{
8595 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008596 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008597 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008598
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008599 if (!uri_auth)
8600 return 0;
8601
Cyril Bonté70be45d2010-10-12 00:14:35 +02008602 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008603 return 0;
8604
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008605 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008606 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008607 return 0;
8608
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008609 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008610 return 0;
8611
Willy Tarreaub2513902006-12-17 14:52:38 +01008612 return 1;
8613}
8614
Willy Tarreau4076a152009-04-02 15:18:36 +02008615/*
8616 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008617 * By default it tries to report the error position as msg->err_pos. However if
8618 * this one is not set, it will then report msg->next, which is the last known
8619 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008620 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008621 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008622void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008623 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008624 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008625{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008626 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008627 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008628 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008629
Willy Tarreau9b28e032012-10-12 23:49:43 +02008630 es->len = MIN(chn->buf->i, sizeof(es->buf));
8631 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008632 len1 = MIN(len1, es->len);
8633 len2 = es->len - len1; /* remaining data if buffer wraps */
8634
Willy Tarreau9b28e032012-10-12 23:49:43 +02008635 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008636 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008637 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008638
Willy Tarreau4076a152009-04-02 15:18:36 +02008639 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008640 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008641 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008642 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008643
Willy Tarreau4076a152009-04-02 15:18:36 +02008644 es->when = date; // user-visible date
8645 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008646 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008647 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008648 if (objt_conn(sess->origin))
8649 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008650 else
8651 memset(&es->src, 0, sizeof(es->src));
8652
Willy Tarreau078272e2010-12-12 12:46:33 +01008653 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008654 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008655 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008656 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008657 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008658 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008659 es->b_out = chn->buf->o;
8660 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008661 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008662 es->m_clen = msg->chunk_len;
8663 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008664}
Willy Tarreaub2513902006-12-17 14:52:38 +01008665
Willy Tarreau294c4732011-12-16 21:35:50 +01008666/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8667 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8668 * performed over the whole headers. Otherwise it must contain a valid header
8669 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8670 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8671 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8672 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008673 * -1. The value fetch stops at commas, so this function is suited for use with
8674 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008675 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008676 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008677unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008678 struct hdr_idx *idx, int occ,
8679 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008680{
Willy Tarreau294c4732011-12-16 21:35:50 +01008681 struct hdr_ctx local_ctx;
8682 char *ptr_hist[MAX_HDR_HISTORY];
8683 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008684 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008685 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008686
Willy Tarreau294c4732011-12-16 21:35:50 +01008687 if (!ctx) {
8688 local_ctx.idx = 0;
8689 ctx = &local_ctx;
8690 }
8691
Willy Tarreaubce70882009-09-07 11:51:47 +02008692 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008693 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008694 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008695 occ--;
8696 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008697 *vptr = ctx->line + ctx->val;
8698 *vlen = ctx->vlen;
8699 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008700 }
8701 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008702 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008703 }
8704
8705 /* negative occurrence, we scan all the list then walk back */
8706 if (-occ > MAX_HDR_HISTORY)
8707 return 0;
8708
Willy Tarreau294c4732011-12-16 21:35:50 +01008709 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008710 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008711 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8712 len_hist[hist_ptr] = ctx->vlen;
8713 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008714 hist_ptr = 0;
8715 found++;
8716 }
8717 if (-occ > found)
8718 return 0;
8719 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008720 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8721 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8722 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008723 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008724 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008725 if (hist_ptr >= MAX_HDR_HISTORY)
8726 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008727 *vptr = ptr_hist[hist_ptr];
8728 *vlen = len_hist[hist_ptr];
8729 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008730}
8731
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008732/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8733 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8734 * performed over the whole headers. Otherwise it must contain a valid header
8735 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8736 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8737 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8738 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8739 * -1. This function differs from http_get_hdr() in that it only returns full
8740 * line header values and does not stop at commas.
8741 * The return value is 0 if nothing was found, or non-zero otherwise.
8742 */
8743unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8744 struct hdr_idx *idx, int occ,
8745 struct hdr_ctx *ctx, char **vptr, int *vlen)
8746{
8747 struct hdr_ctx local_ctx;
8748 char *ptr_hist[MAX_HDR_HISTORY];
8749 int len_hist[MAX_HDR_HISTORY];
8750 unsigned int hist_ptr;
8751 int found;
8752
8753 if (!ctx) {
8754 local_ctx.idx = 0;
8755 ctx = &local_ctx;
8756 }
8757
8758 if (occ >= 0) {
8759 /* search from the beginning */
8760 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8761 occ--;
8762 if (occ <= 0) {
8763 *vptr = ctx->line + ctx->val;
8764 *vlen = ctx->vlen;
8765 return 1;
8766 }
8767 }
8768 return 0;
8769 }
8770
8771 /* negative occurrence, we scan all the list then walk back */
8772 if (-occ > MAX_HDR_HISTORY)
8773 return 0;
8774
8775 found = hist_ptr = 0;
8776 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8777 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8778 len_hist[hist_ptr] = ctx->vlen;
8779 if (++hist_ptr >= MAX_HDR_HISTORY)
8780 hist_ptr = 0;
8781 found++;
8782 }
8783 if (-occ > found)
8784 return 0;
8785 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8786 * find occurrence -occ, so we have to check [hist_ptr+occ].
8787 */
8788 hist_ptr += occ;
8789 if (hist_ptr >= MAX_HDR_HISTORY)
8790 hist_ptr -= MAX_HDR_HISTORY;
8791 *vptr = ptr_hist[hist_ptr];
8792 *vlen = len_hist[hist_ptr];
8793 return 1;
8794}
8795
Willy Tarreaubaaee002006-06-26 02:48:02 +02008796/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008797 * Print a debug line with a header. Always stop at the first CR or LF char,
8798 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8799 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008800 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008801void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008802{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008803 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008804 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008805
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008806 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008807 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008808 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008809 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 +02008810
8811 for (max = 0; start + max < end; max++)
8812 if (start[max] == '\r' || start[max] == '\n')
8813 break;
8814
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008815 UBOUND(max, trash.size - trash.len - 3);
8816 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8817 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008818 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008819}
8820
Willy Tarreaueee5b512015-04-03 23:46:31 +02008821
8822/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8823 * The hdr_idx is allocated as well. In case of allocation failure, everything
8824 * allocated is freed and NULL is returned. Otherwise the new transaction is
8825 * assigned to the stream and returned.
8826 */
8827struct http_txn *http_alloc_txn(struct stream *s)
8828{
8829 struct http_txn *txn = s->txn;
8830
8831 if (txn)
8832 return txn;
8833
8834 txn = pool_alloc2(pool2_http_txn);
8835 if (!txn)
8836 return txn;
8837
8838 txn->hdr_idx.size = global.tune.max_http_hdr;
8839 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8840 if (!txn->hdr_idx.v) {
8841 pool_free2(pool2_http_txn, txn);
8842 return NULL;
8843 }
8844
8845 s->txn = txn;
8846 return txn;
8847}
8848
Willy Tarreau0937bc42009-12-22 15:03:09 +01008849/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008850 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008851 * the required fields are properly allocated and that we only need to (re)init
8852 * them. This should be used before processing any new request.
8853 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008854void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008855{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008856 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008857 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008858
8859 txn->flags = 0;
8860 txn->status = -1;
8861
Willy Tarreauf64d1412010-10-07 20:06:11 +02008862 txn->cookie_first_date = 0;
8863 txn->cookie_last_date = 0;
8864
Willy Tarreaueee5b512015-04-03 23:46:31 +02008865 txn->sessid = NULL;
8866 txn->srv_cookie = NULL;
8867 txn->cli_cookie = NULL;
8868 txn->uri = NULL;
8869
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008870 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008871 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008872 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008873 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008874 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008875 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008876 txn->req.chunk_len = 0LL;
8877 txn->req.body_len = 0LL;
8878 txn->rsp.chunk_len = 0LL;
8879 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008880 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8881 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008882 txn->req.chn = &s->req;
8883 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008884
8885 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008886
8887 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8888 if (fe->options2 & PR_O2_REQBUG_OK)
8889 txn->req.err_pos = -1; /* let buggy requests pass */
8890
Willy Tarreau0937bc42009-12-22 15:03:09 +01008891 if (txn->hdr_idx.v)
8892 hdr_idx_init(&txn->hdr_idx);
8893}
8894
8895/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008896void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008897{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008898 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008899 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008900
Willy Tarreau75195602014-03-11 15:48:55 +01008901 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008902 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008903 s->comp_algo->end(&s->comp_ctx);
8904 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008905 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008906
Willy Tarreau0937bc42009-12-22 15:03:09 +01008907 /* these ones will have been dynamically allocated */
8908 pool_free2(pool2_requri, txn->uri);
8909 pool_free2(pool2_capture, txn->cli_cookie);
8910 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008911 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008912 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008913
William Lallemanda73203e2012-03-12 12:48:57 +01008914 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008915 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008916 txn->uri = NULL;
8917 txn->srv_cookie = NULL;
8918 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008919
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008920 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008921 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008922 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008923 pool_free2(h->pool, s->req_cap[h->index]);
8924 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008925 }
8926
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008927 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008928 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008929 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008930 pool_free2(h->pool, s->res_cap[h->index]);
8931 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008932 }
8933
Willy Tarreau0937bc42009-12-22 15:03:09 +01008934}
8935
8936/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008937void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008938{
8939 http_end_txn(s);
8940 http_init_txn(s);
8941
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008942 /* reinitialise the current rule list pointer to NULL. We are sure that
8943 * any rulelist match the NULL pointer.
8944 */
8945 s->current_rule_list = NULL;
8946
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008947 s->be = strm_fe(s);
8948 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008949 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008950 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008951 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008952 /* re-init store persistence */
8953 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008954 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008955
Willy Tarreau0937bc42009-12-22 15:03:09 +01008956 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008957
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008958 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008959
Willy Tarreau739cfba2010-01-25 23:11:14 +01008960 /* We must trim any excess data from the response buffer, because we
8961 * may have blocked an invalid response from a server that we don't
8962 * want to accidentely forward once we disable the analysers, nor do
8963 * we want those data to come along with next response. A typical
8964 * example of such data would be from a buggy server responding to
8965 * a HEAD with some data, or sending more than the advertised
8966 * content-length.
8967 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008968 if (unlikely(s->res.buf->i))
8969 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008970
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008971 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008972 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008973
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008974 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008975 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008976
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008977 s->req.rex = TICK_ETERNITY;
8978 s->req.wex = TICK_ETERNITY;
8979 s->req.analyse_exp = TICK_ETERNITY;
8980 s->res.rex = TICK_ETERNITY;
8981 s->res.wex = TICK_ETERNITY;
8982 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008983}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008984
Sasha Pachev218f0642014-06-16 12:05:59 -06008985void free_http_res_rules(struct list *r)
8986{
8987 struct http_res_rule *tr, *pr;
8988
8989 list_for_each_entry_safe(pr, tr, r, list) {
8990 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008991 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008992 free(pr);
8993 }
8994}
8995
8996void free_http_req_rules(struct list *r)
8997{
Willy Tarreauff011f22011-01-06 17:51:27 +01008998 struct http_req_rule *tr, *pr;
8999
9000 list_for_each_entry_safe(pr, tr, r, list) {
9001 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01009002 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009003 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009004
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009005 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009006 free(pr);
9007 }
9008}
9009
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009010/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01009011struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9012{
9013 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009014 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009015 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009016 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009017
9018 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
9019 if (!rule) {
9020 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009021 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009022 }
9023
CJ Ess108b1dd2015-04-07 12:03:37 -04009024 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009025 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01009026 rule->action = HTTP_REQ_ACT_ALLOW;
9027 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009028 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009029 int code;
9030 int hc;
9031
Willy Tarreauff011f22011-01-06 17:51:27 +01009032 rule->action = HTTP_REQ_ACT_DENY;
9033 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009034 if (strcmp(args[cur_arg], "deny_status") == 0) {
9035 cur_arg++;
9036 if (!args[cur_arg]) {
9037 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9038 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9039 goto out_err;
9040 }
9041
9042 code = atol(args[cur_arg]);
9043 cur_arg++;
9044 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9045 if (http_err_codes[hc] == code) {
9046 rule->deny_status = hc;
9047 break;
9048 }
9049 }
9050
9051 if (hc >= HTTP_ERR_SIZE) {
9052 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9053 file, linenum, code);
9054 }
9055 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009056 } else if (!strcmp(args[0], "tarpit")) {
9057 rule->action = HTTP_REQ_ACT_TARPIT;
9058 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009059 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01009060 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009061 cur_arg = 1;
9062
9063 while(*args[cur_arg]) {
9064 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009065 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009066 cur_arg+=2;
9067 continue;
9068 } else
9069 break;
9070 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009071 } else if (!strcmp(args[0], "set-nice")) {
9072 rule->action = HTTP_REQ_ACT_SET_NICE;
9073 cur_arg = 1;
9074
9075 if (!*args[cur_arg] ||
9076 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9077 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9078 file, linenum, args[0]);
9079 goto out_err;
9080 }
9081 rule->arg.nice = atoi(args[cur_arg]);
9082 if (rule->arg.nice < -1024)
9083 rule->arg.nice = -1024;
9084 else if (rule->arg.nice > 1024)
9085 rule->arg.nice = 1024;
9086 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009087 } else if (!strcmp(args[0], "set-tos")) {
9088#ifdef IP_TOS
9089 char *err;
9090 rule->action = HTTP_REQ_ACT_SET_TOS;
9091 cur_arg = 1;
9092
9093 if (!*args[cur_arg] ||
9094 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9095 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9096 file, linenum, args[0]);
9097 goto out_err;
9098 }
9099
9100 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9101 if (err && *err != '\0') {
9102 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9103 file, linenum, err, args[0]);
9104 goto out_err;
9105 }
9106 cur_arg++;
9107#else
9108 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9109 goto out_err;
9110#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009111 } else if (!strcmp(args[0], "set-mark")) {
9112#ifdef SO_MARK
9113 char *err;
9114 rule->action = HTTP_REQ_ACT_SET_MARK;
9115 cur_arg = 1;
9116
9117 if (!*args[cur_arg] ||
9118 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9119 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9120 file, linenum, args[0]);
9121 goto out_err;
9122 }
9123
9124 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9125 if (err && *err != '\0') {
9126 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9127 file, linenum, err, args[0]);
9128 goto out_err;
9129 }
9130 cur_arg++;
9131 global.last_checks |= LSTCHK_NETADM;
9132#else
9133 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9134 goto out_err;
9135#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009136 } else if (!strcmp(args[0], "set-log-level")) {
9137 rule->action = HTTP_REQ_ACT_SET_LOGL;
9138 cur_arg = 1;
9139
9140 if (!*args[cur_arg] ||
9141 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9142 bad_log_level:
9143 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9144 file, linenum, args[0]);
9145 goto out_err;
9146 }
9147 if (strcmp(args[cur_arg], "silent") == 0)
9148 rule->arg.loglevel = -1;
9149 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9150 goto bad_log_level;
9151 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009152 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9153 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9154 cur_arg = 1;
9155
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009156 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9157 (*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 +01009158 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9159 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009160 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009161 }
9162
9163 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9164 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9165 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009166
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009167 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009168 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009169 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9170 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009171 free(proxy->conf.lfs_file);
9172 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9173 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009174 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009175 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9176 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009177 cur_arg = 1;
9178
9179 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009180 (*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 -06009181 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9182 file, linenum, args[0]);
9183 goto out_err;
9184 }
9185
9186 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9187 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9188 LIST_INIT(&rule->arg.hdr_add.fmt);
9189
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009190 error = NULL;
9191 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9192 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9193 args[cur_arg + 1], error);
9194 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009195 goto out_err;
9196 }
9197
9198 proxy->conf.args.ctx = ARGC_HRQ;
9199 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9200 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9201 file, linenum);
9202
9203 free(proxy->conf.lfs_file);
9204 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9205 proxy->conf.lfs_line = proxy->conf.args.line;
9206 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009207 } else if (strcmp(args[0], "del-header") == 0) {
9208 rule->action = HTTP_REQ_ACT_DEL_HDR;
9209 cur_arg = 1;
9210
9211 if (!*args[cur_arg] ||
9212 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9213 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9214 file, linenum, args[0]);
9215 goto out_err;
9216 }
9217
9218 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9219 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9220
9221 proxy->conf.args.ctx = ARGC_HRQ;
9222 free(proxy->conf.lfs_file);
9223 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9224 proxy->conf.lfs_line = proxy->conf.args.line;
9225 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009226 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9227 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009228 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009229 struct sample_expr *expr;
9230 unsigned int where;
9231 char *err = NULL;
9232
9233 cur_arg = 1;
9234 proxy->conf.args.ctx = ARGC_TRK;
9235
9236 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9237 if (!expr) {
9238 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9239 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9240 free(err);
9241 goto out_err;
9242 }
9243
9244 where = 0;
9245 if (proxy->cap & PR_CAP_FE)
9246 where |= SMP_VAL_FE_HRQ_HDR;
9247 if (proxy->cap & PR_CAP_BE)
9248 where |= SMP_VAL_BE_HRQ_HDR;
9249
9250 if (!(expr->fetch->val & where)) {
9251 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9252 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9253 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9254 args[cur_arg-1], sample_src_names(expr->fetch->use));
9255 free(expr);
9256 goto out_err;
9257 }
9258
9259 if (strcmp(args[cur_arg], "table") == 0) {
9260 cur_arg++;
9261 if (!args[cur_arg]) {
9262 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9263 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9264 free(expr);
9265 goto out_err;
9266 }
9267 /* we copy the table name for now, it will be resolved later */
9268 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9269 cur_arg++;
9270 }
9271 rule->act_prm.trk_ctr.expr = expr;
9272 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009273 } else if (strcmp(args[0], "redirect") == 0) {
9274 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009275 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009276
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009277 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009278 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9279 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9280 goto out_err;
9281 }
9282
9283 /* this redirect rule might already contain a parsed condition which
9284 * we'll pass to the http-request rule.
9285 */
9286 rule->action = HTTP_REQ_ACT_REDIR;
9287 rule->arg.redir = redir;
9288 rule->cond = redir->cond;
9289 redir->cond = NULL;
9290 cur_arg = 2;
9291 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009292 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9293 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9294 rule->action = HTTP_REQ_ACT_ADD_ACL;
9295 /*
9296 * '+ 8' for 'add-acl('
9297 * '- 9' for 'add-acl(' + trailing ')'
9298 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009299 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009300
9301 cur_arg = 1;
9302
9303 if (!*args[cur_arg] ||
9304 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9305 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9306 file, linenum, args[0]);
9307 goto out_err;
9308 }
9309
9310 LIST_INIT(&rule->arg.map.key);
9311 proxy->conf.args.ctx = ARGC_HRQ;
9312 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9313 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9314 file, linenum);
9315 free(proxy->conf.lfs_file);
9316 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9317 proxy->conf.lfs_line = proxy->conf.args.line;
9318 cur_arg += 1;
9319 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9320 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9321 rule->action = HTTP_REQ_ACT_DEL_ACL;
9322 /*
9323 * '+ 8' for 'del-acl('
9324 * '- 9' for 'del-acl(' + trailing ')'
9325 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009326 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009327
9328 cur_arg = 1;
9329
9330 if (!*args[cur_arg] ||
9331 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9332 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9333 file, linenum, args[0]);
9334 goto out_err;
9335 }
9336
9337 LIST_INIT(&rule->arg.map.key);
9338 proxy->conf.args.ctx = ARGC_HRQ;
9339 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9340 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9341 file, linenum);
9342 free(proxy->conf.lfs_file);
9343 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9344 proxy->conf.lfs_line = proxy->conf.args.line;
9345 cur_arg += 1;
9346 } else if (strncmp(args[0], "del-map", 7) == 0) {
9347 /* http-request del-map(<reference (map name)>) <key pattern> */
9348 rule->action = HTTP_REQ_ACT_DEL_MAP;
9349 /*
9350 * '+ 8' for 'del-map('
9351 * '- 9' for 'del-map(' + trailing ')'
9352 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009353 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009354
9355 cur_arg = 1;
9356
9357 if (!*args[cur_arg] ||
9358 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9359 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9360 file, linenum, args[0]);
9361 goto out_err;
9362 }
9363
9364 LIST_INIT(&rule->arg.map.key);
9365 proxy->conf.args.ctx = ARGC_HRQ;
9366 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9367 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9368 file, linenum);
9369 free(proxy->conf.lfs_file);
9370 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9371 proxy->conf.lfs_line = proxy->conf.args.line;
9372 cur_arg += 1;
9373 } else if (strncmp(args[0], "set-map", 7) == 0) {
9374 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9375 rule->action = HTTP_REQ_ACT_SET_MAP;
9376 /*
9377 * '+ 8' for 'set-map('
9378 * '- 9' for 'set-map(' + trailing ')'
9379 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009380 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009381
9382 cur_arg = 1;
9383
9384 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9385 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9386 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9387 file, linenum, args[0]);
9388 goto out_err;
9389 }
9390
9391 LIST_INIT(&rule->arg.map.key);
9392 LIST_INIT(&rule->arg.map.value);
9393 proxy->conf.args.ctx = ARGC_HRQ;
9394
9395 /* key pattern */
9396 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9397 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9398 file, linenum);
9399
9400 /* value pattern */
9401 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9402 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9403 file, linenum);
9404 free(proxy->conf.lfs_file);
9405 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9406 proxy->conf.lfs_line = proxy->conf.args.line;
9407
9408 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009409 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9410 char *errmsg = NULL;
9411 cur_arg = 1;
9412 /* try in the module list */
9413 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9414 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9415 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9416 free(errmsg);
9417 goto out_err;
9418 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009419 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009420 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 +01009421 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009422 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009423 }
9424
9425 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9426 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009427 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009428
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009429 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9430 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9431 file, linenum, args[0], errmsg);
9432 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009433 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009434 }
9435 rule->cond = cond;
9436 }
9437 else if (*args[cur_arg]) {
9438 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9439 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9440 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009441 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009442 }
9443
9444 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009445 out_err:
9446 free(rule);
9447 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009448}
9449
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009450/* parse an "http-respose" rule */
9451struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9452{
9453 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009454 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009455 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009456 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009457
9458 rule = calloc(1, sizeof(*rule));
9459 if (!rule) {
9460 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9461 goto out_err;
9462 }
9463
9464 if (!strcmp(args[0], "allow")) {
9465 rule->action = HTTP_RES_ACT_ALLOW;
9466 cur_arg = 1;
9467 } else if (!strcmp(args[0], "deny")) {
9468 rule->action = HTTP_RES_ACT_DENY;
9469 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009470 } else if (!strcmp(args[0], "set-nice")) {
9471 rule->action = HTTP_RES_ACT_SET_NICE;
9472 cur_arg = 1;
9473
9474 if (!*args[cur_arg] ||
9475 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9476 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9477 file, linenum, args[0]);
9478 goto out_err;
9479 }
9480 rule->arg.nice = atoi(args[cur_arg]);
9481 if (rule->arg.nice < -1024)
9482 rule->arg.nice = -1024;
9483 else if (rule->arg.nice > 1024)
9484 rule->arg.nice = 1024;
9485 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009486 } else if (!strcmp(args[0], "set-tos")) {
9487#ifdef IP_TOS
9488 char *err;
9489 rule->action = HTTP_RES_ACT_SET_TOS;
9490 cur_arg = 1;
9491
9492 if (!*args[cur_arg] ||
9493 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9494 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9495 file, linenum, args[0]);
9496 goto out_err;
9497 }
9498
9499 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9500 if (err && *err != '\0') {
9501 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9502 file, linenum, err, args[0]);
9503 goto out_err;
9504 }
9505 cur_arg++;
9506#else
9507 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9508 goto out_err;
9509#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009510 } else if (!strcmp(args[0], "set-mark")) {
9511#ifdef SO_MARK
9512 char *err;
9513 rule->action = HTTP_RES_ACT_SET_MARK;
9514 cur_arg = 1;
9515
9516 if (!*args[cur_arg] ||
9517 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9518 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9519 file, linenum, args[0]);
9520 goto out_err;
9521 }
9522
9523 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9524 if (err && *err != '\0') {
9525 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9526 file, linenum, err, args[0]);
9527 goto out_err;
9528 }
9529 cur_arg++;
9530 global.last_checks |= LSTCHK_NETADM;
9531#else
9532 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9533 goto out_err;
9534#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009535 } else if (!strcmp(args[0], "set-log-level")) {
9536 rule->action = HTTP_RES_ACT_SET_LOGL;
9537 cur_arg = 1;
9538
9539 if (!*args[cur_arg] ||
9540 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9541 bad_log_level:
9542 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9543 file, linenum, args[0]);
9544 goto out_err;
9545 }
9546 if (strcmp(args[cur_arg], "silent") == 0)
9547 rule->arg.loglevel = -1;
9548 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9549 goto bad_log_level;
9550 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009551 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9552 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9553 cur_arg = 1;
9554
9555 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9556 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9557 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9558 file, linenum, args[0]);
9559 goto out_err;
9560 }
9561
9562 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9563 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9564 LIST_INIT(&rule->arg.hdr_add.fmt);
9565
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009566 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009567 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009568 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9569 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009570 free(proxy->conf.lfs_file);
9571 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9572 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009573 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009574 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009575 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009576 cur_arg = 1;
9577
9578 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009579 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9580 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009581 file, linenum, args[0]);
9582 goto out_err;
9583 }
9584
9585 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9586 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9587 LIST_INIT(&rule->arg.hdr_add.fmt);
9588
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009589 error = NULL;
9590 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9591 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9592 args[cur_arg + 1], error);
9593 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009594 goto out_err;
9595 }
9596
9597 proxy->conf.args.ctx = ARGC_HRQ;
9598 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9599 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9600 file, linenum);
9601
9602 free(proxy->conf.lfs_file);
9603 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9604 proxy->conf.lfs_line = proxy->conf.args.line;
9605 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009606 } else if (strcmp(args[0], "del-header") == 0) {
9607 rule->action = HTTP_RES_ACT_DEL_HDR;
9608 cur_arg = 1;
9609
9610 if (!*args[cur_arg] ||
9611 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9612 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9613 file, linenum, args[0]);
9614 goto out_err;
9615 }
9616
9617 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9618 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9619
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009620 proxy->conf.args.ctx = ARGC_HRS;
9621 free(proxy->conf.lfs_file);
9622 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9623 proxy->conf.lfs_line = proxy->conf.args.line;
9624 cur_arg += 1;
9625 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9626 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9627 rule->action = HTTP_RES_ACT_ADD_ACL;
9628 /*
9629 * '+ 8' for 'add-acl('
9630 * '- 9' for 'add-acl(' + trailing ')'
9631 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009632 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009633
9634 cur_arg = 1;
9635
9636 if (!*args[cur_arg] ||
9637 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9638 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9639 file, linenum, args[0]);
9640 goto out_err;
9641 }
9642
9643 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009644 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009645 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9646 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9647 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009648 free(proxy->conf.lfs_file);
9649 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9650 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009651
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009652 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009653 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9654 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9655 rule->action = HTTP_RES_ACT_DEL_ACL;
9656 /*
9657 * '+ 8' for 'del-acl('
9658 * '- 9' for 'del-acl(' + trailing ')'
9659 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009660 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009661
9662 cur_arg = 1;
9663
9664 if (!*args[cur_arg] ||
9665 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9666 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9667 file, linenum, args[0]);
9668 goto out_err;
9669 }
9670
9671 LIST_INIT(&rule->arg.map.key);
9672 proxy->conf.args.ctx = ARGC_HRS;
9673 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9674 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9675 file, linenum);
9676 free(proxy->conf.lfs_file);
9677 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9678 proxy->conf.lfs_line = proxy->conf.args.line;
9679 cur_arg += 1;
9680 } else if (strncmp(args[0], "del-map", 7) == 0) {
9681 /* http-response del-map(<reference (map name)>) <key pattern> */
9682 rule->action = HTTP_RES_ACT_DEL_MAP;
9683 /*
9684 * '+ 8' for 'del-map('
9685 * '- 9' for 'del-map(' + trailing ')'
9686 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009687 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009688
9689 cur_arg = 1;
9690
9691 if (!*args[cur_arg] ||
9692 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9693 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9694 file, linenum, args[0]);
9695 goto out_err;
9696 }
9697
9698 LIST_INIT(&rule->arg.map.key);
9699 proxy->conf.args.ctx = ARGC_HRS;
9700 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9701 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9702 file, linenum);
9703 free(proxy->conf.lfs_file);
9704 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9705 proxy->conf.lfs_line = proxy->conf.args.line;
9706 cur_arg += 1;
9707 } else if (strncmp(args[0], "set-map", 7) == 0) {
9708 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9709 rule->action = HTTP_RES_ACT_SET_MAP;
9710 /*
9711 * '+ 8' for 'set-map('
9712 * '- 9' for 'set-map(' + trailing ')'
9713 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009714 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009715
9716 cur_arg = 1;
9717
9718 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9719 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9720 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9721 file, linenum, args[0]);
9722 goto out_err;
9723 }
9724
9725 LIST_INIT(&rule->arg.map.key);
9726 LIST_INIT(&rule->arg.map.value);
9727
9728 proxy->conf.args.ctx = ARGC_HRS;
9729
9730 /* key pattern */
9731 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9732 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9733 file, linenum);
9734
9735 /* value pattern */
9736 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9737 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9738 file, linenum);
9739
9740 free(proxy->conf.lfs_file);
9741 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9742 proxy->conf.lfs_line = proxy->conf.args.line;
9743
9744 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009745 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9746 char *errmsg = NULL;
9747 cur_arg = 1;
9748 /* try in the module list */
9749 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9750 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9751 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9752 free(errmsg);
9753 goto out_err;
9754 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009755 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009756 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 +02009757 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9758 goto out_err;
9759 }
9760
9761 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9762 struct acl_cond *cond;
9763 char *errmsg = NULL;
9764
9765 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9766 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9767 file, linenum, args[0], errmsg);
9768 free(errmsg);
9769 goto out_err;
9770 }
9771 rule->cond = cond;
9772 }
9773 else if (*args[cur_arg]) {
9774 Alert("parsing [%s:%d]: 'http-response %s' expects"
9775 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9776 file, linenum, args[0], args[cur_arg]);
9777 goto out_err;
9778 }
9779
9780 return rule;
9781 out_err:
9782 free(rule);
9783 return NULL;
9784}
9785
Willy Tarreau4baae242012-12-27 12:00:31 +01009786/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009787 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9788 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009789 */
9790struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009791 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009792{
9793 struct redirect_rule *rule;
9794 int cur_arg;
9795 int type = REDIRECT_TYPE_NONE;
9796 int code = 302;
9797 const char *destination = NULL;
9798 const char *cookie = NULL;
9799 int cookie_set = 0;
9800 unsigned int flags = REDIRECT_FLAG_NONE;
9801 struct acl_cond *cond = NULL;
9802
9803 cur_arg = 0;
9804 while (*(args[cur_arg])) {
9805 if (strcmp(args[cur_arg], "location") == 0) {
9806 if (!*args[cur_arg + 1])
9807 goto missing_arg;
9808
9809 type = REDIRECT_TYPE_LOCATION;
9810 cur_arg++;
9811 destination = args[cur_arg];
9812 }
9813 else if (strcmp(args[cur_arg], "prefix") == 0) {
9814 if (!*args[cur_arg + 1])
9815 goto missing_arg;
9816
9817 type = REDIRECT_TYPE_PREFIX;
9818 cur_arg++;
9819 destination = args[cur_arg];
9820 }
9821 else if (strcmp(args[cur_arg], "scheme") == 0) {
9822 if (!*args[cur_arg + 1])
9823 goto missing_arg;
9824
9825 type = REDIRECT_TYPE_SCHEME;
9826 cur_arg++;
9827 destination = args[cur_arg];
9828 }
9829 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9830 if (!*args[cur_arg + 1])
9831 goto missing_arg;
9832
9833 cur_arg++;
9834 cookie = args[cur_arg];
9835 cookie_set = 1;
9836 }
9837 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9838 if (!*args[cur_arg + 1])
9839 goto missing_arg;
9840
9841 cur_arg++;
9842 cookie = args[cur_arg];
9843 cookie_set = 0;
9844 }
9845 else if (strcmp(args[cur_arg], "code") == 0) {
9846 if (!*args[cur_arg + 1])
9847 goto missing_arg;
9848
9849 cur_arg++;
9850 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009851 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009852 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009853 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009854 args[cur_arg - 1], args[cur_arg]);
9855 return NULL;
9856 }
9857 }
9858 else if (!strcmp(args[cur_arg],"drop-query")) {
9859 flags |= REDIRECT_FLAG_DROP_QS;
9860 }
9861 else if (!strcmp(args[cur_arg],"append-slash")) {
9862 flags |= REDIRECT_FLAG_APPEND_SLASH;
9863 }
9864 else if (strcmp(args[cur_arg], "if") == 0 ||
9865 strcmp(args[cur_arg], "unless") == 0) {
9866 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9867 if (!cond) {
9868 memprintf(errmsg, "error in condition: %s", *errmsg);
9869 return NULL;
9870 }
9871 break;
9872 }
9873 else {
9874 memprintf(errmsg,
9875 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9876 args[cur_arg]);
9877 return NULL;
9878 }
9879 cur_arg++;
9880 }
9881
9882 if (type == REDIRECT_TYPE_NONE) {
9883 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9884 return NULL;
9885 }
9886
9887 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9888 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009889 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009890
9891 if (!use_fmt) {
9892 /* old-style static redirect rule */
9893 rule->rdr_str = strdup(destination);
9894 rule->rdr_len = strlen(destination);
9895 }
9896 else {
9897 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009898
9899 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9900 * if prefix == "/", we don't want to add anything, otherwise it
9901 * makes it hard for the user to configure a self-redirection.
9902 */
Godbachd9722032014-12-18 15:44:58 +08009903 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009904 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009905 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009906 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9907 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009908 free(curproxy->conf.lfs_file);
9909 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9910 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009911 }
9912 }
9913
Willy Tarreau4baae242012-12-27 12:00:31 +01009914 if (cookie) {
9915 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9916 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9917 */
9918 rule->cookie_len = strlen(cookie);
9919 if (cookie_set) {
9920 rule->cookie_str = malloc(rule->cookie_len + 10);
9921 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9922 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9923 rule->cookie_len += 9;
9924 } else {
9925 rule->cookie_str = malloc(rule->cookie_len + 21);
9926 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9927 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9928 rule->cookie_len += 20;
9929 }
9930 }
9931 rule->type = type;
9932 rule->code = code;
9933 rule->flags = flags;
9934 LIST_INIT(&rule->list);
9935 return rule;
9936
9937 missing_arg:
9938 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9939 return NULL;
9940}
9941
Willy Tarreau8797c062007-05-07 00:55:35 +02009942/************************************************************************/
9943/* The code below is dedicated to ACL parsing and matching */
9944/************************************************************************/
9945
9946
Willy Tarreau14174bc2012-04-16 14:34:04 +02009947/* This function ensures that the prerequisites for an L7 fetch are ready,
9948 * which means that a request or response is ready. If some data is missing,
9949 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009950 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9951 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009952 *
9953 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009954 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9955 * decide whether or not an HTTP message is present ;
9956 * 0 if the requested data cannot be fetched or if it is certain that
9957 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009958 * 1 if an HTTP message is ready
9959 */
9960static int
Willy Tarreau15e91e12015-04-04 00:52:09 +02009961smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009962 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009963{
Willy Tarreau192252e2015-04-04 01:47:55 +02009964 struct http_txn *txn;
9965 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009966
Willy Tarreaueee5b512015-04-03 23:46:31 +02009967 /* Note: this function may only be used from places where
9968 * http_init_txn() has already been done, and implies that <s>,
9969 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
9970 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009971 */
9972
Willy Tarreau192252e2015-04-04 01:47:55 +02009973 if (!s)
9974 return 0;
9975 txn = s->txn;
9976
9977 if (!txn)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009978 return 0;
Willy Tarreau192252e2015-04-04 01:47:55 +02009979 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009980
9981 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009982 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009983
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009984 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009985 /* If the buffer does not leave enough free space at the end,
9986 * we must first realign it.
9987 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009988 if (s->req.buf->p > s->req.buf->data &&
9989 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9990 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009991
Willy Tarreau14174bc2012-04-16 14:34:04 +02009992 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009993 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009994 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009995
9996 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009997 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009998 http_msg_analyzer(msg, &txn->hdr_idx);
9999
10000 /* Still no valid request ? */
10001 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010002 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010003 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010004 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010005 }
10006 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010007 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010008 return 0;
10009 }
10010
10011 /* OK we just got a valid HTTP request. We have some minor
10012 * preparation to perform so that further checks can rely
10013 * on HTTP tests.
10014 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010015
10016 /* If the request was parsed but was too large, we must absolutely
10017 * return an error so that it is not processed. At the moment this
10018 * cannot happen, but if the parsers are to change in the future,
10019 * we want this check to be maintained.
10020 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010021 if (unlikely(s->req.buf->i + s->req.buf->p >
10022 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010023 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +020010024 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010025 return 1;
10026 }
10027
Willy Tarreau9b28e032012-10-12 23:49:43 +020010028 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010029 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010030 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010031
Willy Tarreau506d0502013-07-06 13:29:24 +020010032 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10033 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010034 }
10035
Willy Tarreau506d0502013-07-06 13:29:24 +020010036 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010037 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010038 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010039
10040 /* otherwise everything's ready for the request */
10041 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010042 else {
10043 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010044 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10045 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010046 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010047 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010048 }
10049
10050 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +020010051 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010052 return 1;
10053}
Willy Tarreau8797c062007-05-07 00:55:35 +020010054
Willy Tarreau6c616e02014-06-25 16:56:41 +020010055/* Note: these functinos *do* modify the sample. Even in case of success, at
10056 * least the type and uint value are modified.
10057 */
Willy Tarreauc0239e02012-04-16 14:42:55 +020010058#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau15e91e12015-04-04 00:52:09 +020010059 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 +020010060
Willy Tarreau24e32d82012-04-23 23:55:44 +020010061#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau15e91e12015-04-04 00:52:09 +020010062 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 +020010063
Willy Tarreau8797c062007-05-07 00:55:35 +020010064
10065/* 1. Check on METHOD
10066 * We use the pre-parsed method if it is known, and store its number as an
10067 * integer. If it is unknown, we use the pointer and the length.
10068 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010069static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010070{
10071 int len, meth;
10072
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010073 len = strlen(text);
10074 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010075
10076 pattern->val.i = meth;
10077 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010078 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010079 pattern->len = len;
10080 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010081 else {
10082 pattern->ptr.str = NULL;
10083 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010084 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010085 return 1;
10086}
10087
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010088/* This function fetches the method of current HTTP request and stores
10089 * it in the global pattern struct as a chunk. There are two possibilities :
10090 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10091 * in <len> and <ptr> is NULL ;
10092 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10093 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010094 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010095 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010096static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010097smp_fetch_meth(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010098 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010099{
10100 int meth;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010101 struct http_txn *txn = strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010102
Willy Tarreau24e32d82012-04-23 23:55:44 +020010103 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010104
Willy Tarreau8797c062007-05-07 00:55:35 +020010105 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010106 smp->type = SMP_T_METH;
10107 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010108 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010109 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10110 /* ensure the indexes are not affected */
10111 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010112 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010113 smp->data.meth.str.len = txn->req.sl.rq.m_l;
10114 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010115 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010116 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010117 return 1;
10118}
10119
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010120/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010121static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010122{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010123 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010124 struct pattern_list *lst;
10125 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010126
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010127 list_for_each_entry(lst, &expr->patterns, list) {
10128 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010129
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010130 /* well-known method */
10131 if (pattern->val.i != HTTP_METH_OTHER) {
10132 if (smp->data.meth.meth == pattern->val.i)
10133 return pattern;
10134 else
10135 continue;
10136 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010137
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010138 /* Other method, we must compare the strings */
10139 if (pattern->len != smp->data.meth.str.len)
10140 continue;
10141
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010142 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010143 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10144 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010145 return pattern;
10146 }
10147 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010148}
10149
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010150static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010151smp_fetch_rqver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010152 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010153{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010154 struct http_txn *txn = strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010155 char *ptr;
10156 int len;
10157
Willy Tarreauc0239e02012-04-16 14:42:55 +020010158 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010159
Willy Tarreau8797c062007-05-07 00:55:35 +020010160 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010161 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010162
10163 while ((len-- > 0) && (*ptr++ != '/'));
10164 if (len <= 0)
10165 return 0;
10166
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010167 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010168 smp->data.str.str = ptr;
10169 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010170
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010171 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010172 return 1;
10173}
10174
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010175static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010176smp_fetch_stver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010177 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010178{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010179 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010180 char *ptr;
10181 int len;
10182
Willy Tarreauc0239e02012-04-16 14:42:55 +020010183 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010184
Willy Tarreau15e91e12015-04-04 00:52:09 +020010185 txn = strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010186 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10187 return 0;
10188
Willy Tarreau8797c062007-05-07 00:55:35 +020010189 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010190 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010191
10192 while ((len-- > 0) && (*ptr++ != '/'));
10193 if (len <= 0)
10194 return 0;
10195
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010196 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010197 smp->data.str.str = ptr;
10198 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010199
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010200 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010201 return 1;
10202}
10203
10204/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010205static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010206smp_fetch_stcode(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010207 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010208{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010209 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010210 char *ptr;
10211 int len;
10212
Willy Tarreauc0239e02012-04-16 14:42:55 +020010213 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010214
Willy Tarreau15e91e12015-04-04 00:52:09 +020010215 txn = strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010216 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10217 return 0;
10218
Willy Tarreau8797c062007-05-07 00:55:35 +020010219 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010220 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010221
Willy Tarreauf853c462012-04-23 18:53:56 +020010222 smp->type = SMP_T_UINT;
10223 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010224 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010225 return 1;
10226}
10227
10228/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010229static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010230smp_fetch_url(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010231 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010232{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010233 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010234
Willy Tarreauc0239e02012-04-16 14:42:55 +020010235 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010236
Willy Tarreau15e91e12015-04-04 00:52:09 +020010237 txn = strm->txn;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010238 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010239 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010240 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010241 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010242 return 1;
10243}
10244
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010245static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010246smp_fetch_url_ip(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010247 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010248{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010249 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010250 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010251
Willy Tarreauc0239e02012-04-16 14:42:55 +020010252 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010253
Willy Tarreau15e91e12015-04-04 00:52:09 +020010254 txn = strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010255 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 +020010256 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010257 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010258
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010259 smp->type = SMP_T_IPV4;
10260 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010261 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010262 return 1;
10263}
10264
10265static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010266smp_fetch_url_port(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010267 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010268{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010269 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010270 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010271
Willy Tarreauc0239e02012-04-16 14:42:55 +020010272 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010273
Willy Tarreau15e91e12015-04-04 00:52:09 +020010274 txn = strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010275 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 +020010276 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10277 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010278
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010279 smp->type = SMP_T_UINT;
10280 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010281 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010282 return 1;
10283}
10284
Willy Tarreau185b5c42012-04-26 15:11:51 +020010285/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10286 * Accepts an optional argument of type string containing the header field name,
10287 * and an optional argument of type signed or unsigned integer to request an
10288 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010289 * headers are considered from the first one. It does not stop on commas and
10290 * returns full lines instead (useful for User-Agent or Date for example).
10291 */
10292static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010293smp_fetch_fhdr(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010294 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010295{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010296 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010297 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010298 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010299 int occ = 0;
10300 const char *name_str = NULL;
10301 int name_len = 0;
10302
10303 if (!ctx) {
10304 /* first call */
10305 ctx = &static_hdr_ctx;
10306 ctx->idx = 0;
10307 smp->ctx.a[0] = ctx;
10308 }
10309
10310 if (args) {
10311 if (args[0].type != ARGT_STR)
10312 return 0;
10313 name_str = args[0].data.str.str;
10314 name_len = args[0].data.str.len;
10315
10316 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10317 occ = args[1].data.uint;
10318 }
10319
10320 CHECK_HTTP_MESSAGE_FIRST();
10321
Willy Tarreau15e91e12015-04-04 00:52:09 +020010322 idx = &strm->txn->hdr_idx;
10323 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10324
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010325 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10326 /* search for header from the beginning */
10327 ctx->idx = 0;
10328
10329 if (!occ && !(opt & SMP_OPT_ITERATE))
10330 /* no explicit occurrence and single fetch => last header by default */
10331 occ = -1;
10332
10333 if (!occ)
10334 /* prepare to report multiple occurrences for ACL fetches */
10335 smp->flags |= SMP_F_NOT_LAST;
10336
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010337 smp->type = SMP_T_STR;
10338 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010339 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10340 return 1;
10341
10342 smp->flags &= ~SMP_F_NOT_LAST;
10343 return 0;
10344}
10345
10346/* 6. Check on HTTP header count. The number of occurrences is returned.
10347 * Accepts exactly 1 argument of type string. It does not stop on commas and
10348 * returns full lines instead (useful for User-Agent or Date for example).
10349 */
10350static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010351smp_fetch_fhdr_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010352 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010353{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010354 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010355 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010356 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010357 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010358 const char *name = NULL;
10359 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010360
Willy Tarreau601a4d12015-04-01 19:16:09 +020010361 if (args && args->type == ARGT_STR) {
10362 name = args->data.str.str;
10363 len = args->data.str.len;
10364 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010365
10366 CHECK_HTTP_MESSAGE_FIRST();
10367
Willy Tarreau15e91e12015-04-04 00:52:09 +020010368 idx = &strm->txn->hdr_idx;
10369 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10370
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010371 ctx.idx = 0;
10372 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010373 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010374 cnt++;
10375
10376 smp->type = SMP_T_UINT;
10377 smp->data.uint = cnt;
10378 smp->flags = SMP_F_VOL_HDR;
10379 return 1;
10380}
10381
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010382static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010383smp_fetch_hdr_names(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010384 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010385{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010386 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010387 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010388 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010389 struct chunk *temp;
10390 char del = ',';
10391
10392 if (args && args->type == ARGT_STR)
10393 del = *args[0].data.str.str;
10394
10395 CHECK_HTTP_MESSAGE_FIRST();
10396
Willy Tarreau15e91e12015-04-04 00:52:09 +020010397 idx = &strm->txn->hdr_idx;
10398 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10399
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010400 temp = get_trash_chunk();
10401
10402 ctx.idx = 0;
10403 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10404 if (temp->len)
10405 temp->str[temp->len++] = del;
10406 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10407 temp->len += ctx.del;
10408 }
10409
10410 smp->type = SMP_T_STR;
10411 smp->data.str.str = temp->str;
10412 smp->data.str.len = temp->len;
10413 smp->flags = SMP_F_VOL_HDR;
10414 return 1;
10415}
10416
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010417/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10418 * Accepts an optional argument of type string containing the header field name,
10419 * and an optional argument of type signed or unsigned integer to request an
10420 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010421 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010422 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010423static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010424smp_fetch_hdr(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010425 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010426{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010427 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010428 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010429 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010430 int occ = 0;
10431 const char *name_str = NULL;
10432 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010433
Willy Tarreaua890d072013-04-02 12:01:06 +020010434 if (!ctx) {
10435 /* first call */
10436 ctx = &static_hdr_ctx;
10437 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010438 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010439 }
10440
Willy Tarreau185b5c42012-04-26 15:11:51 +020010441 if (args) {
10442 if (args[0].type != ARGT_STR)
10443 return 0;
10444 name_str = args[0].data.str.str;
10445 name_len = args[0].data.str.len;
10446
10447 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10448 occ = args[1].data.uint;
10449 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010450
Willy Tarreaue333ec92012-04-16 16:26:40 +020010451 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010452
Willy Tarreau15e91e12015-04-04 00:52:09 +020010453 idx = &strm->txn->hdr_idx;
10454 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10455
Willy Tarreau185b5c42012-04-26 15:11:51 +020010456 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010457 /* search for header from the beginning */
10458 ctx->idx = 0;
10459
Willy Tarreau185b5c42012-04-26 15:11:51 +020010460 if (!occ && !(opt & SMP_OPT_ITERATE))
10461 /* no explicit occurrence and single fetch => last header by default */
10462 occ = -1;
10463
10464 if (!occ)
10465 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010466 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010467
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010468 smp->type = SMP_T_STR;
10469 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010470 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 +020010471 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010472
Willy Tarreau37406352012-04-23 16:16:37 +020010473 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010474 return 0;
10475}
10476
Willy Tarreauc11416f2007-06-17 16:58:38 +020010477/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010478 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010479 */
10480static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010481smp_fetch_hdr_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010482 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010483{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010484 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010485 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010486 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010487 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010488 const char *name = NULL;
10489 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010490
Willy Tarreau601a4d12015-04-01 19:16:09 +020010491 if (args && args->type == ARGT_STR) {
10492 name = args->data.str.str;
10493 len = args->data.str.len;
10494 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010495
Willy Tarreaue333ec92012-04-16 16:26:40 +020010496 CHECK_HTTP_MESSAGE_FIRST();
10497
Willy Tarreau15e91e12015-04-04 00:52:09 +020010498 idx = &strm->txn->hdr_idx;
10499 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10500
Willy Tarreau33a7e692007-06-10 19:45:56 +020010501 ctx.idx = 0;
10502 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010503 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010504 cnt++;
10505
Willy Tarreauf853c462012-04-23 18:53:56 +020010506 smp->type = SMP_T_UINT;
10507 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010508 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010509 return 1;
10510}
10511
Willy Tarreau185b5c42012-04-26 15:11:51 +020010512/* Fetch an HTTP header's integer value. The integer value is returned. It
10513 * takes a mandatory argument of type string and an optional one of type int
10514 * to designate a specific occurrence. It returns an unsigned integer, which
10515 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010516 */
10517static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010518smp_fetch_hdr_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010519 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010520{
Willy Tarreau192252e2015-04-04 01:47:55 +020010521 int ret = smp_fetch_hdr(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010522
Willy Tarreauf853c462012-04-23 18:53:56 +020010523 if (ret > 0) {
10524 smp->type = SMP_T_UINT;
10525 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10526 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010527
Willy Tarreaud53e2422012-04-16 17:21:11 +020010528 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010529}
10530
Cyril Bonté69fa9922012-10-25 00:01:06 +020010531/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10532 * and an optional one of type int to designate a specific occurrence.
10533 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010534 */
10535static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010536smp_fetch_hdr_ip(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010537 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010538{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010539 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010540
Willy Tarreau192252e2015-04-04 01:47:55 +020010541 while ((ret = smp_fetch_hdr(px, sess, strm, opt, args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010542 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10543 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010544 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010545 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010546 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010547 if (smp->data.str.len < temp->size - 1) {
10548 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10549 temp->str[smp->data.str.len] = '\0';
10550 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10551 smp->type = SMP_T_IPV6;
10552 break;
10553 }
10554 }
10555 }
10556
Willy Tarreaud53e2422012-04-16 17:21:11 +020010557 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010558 if (!(smp->flags & SMP_F_NOT_LAST))
10559 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010560 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010561 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010562}
10563
Willy Tarreau737b0c12007-06-10 21:28:46 +020010564/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10565 * the first '/' after the possible hostname, and ends before the possible '?'.
10566 */
10567static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010568smp_fetch_path(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010569 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010570{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010571 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010572 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010573
Willy Tarreauc0239e02012-04-16 14:42:55 +020010574 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010575
Willy Tarreau15e91e12015-04-04 00:52:09 +020010576 txn = strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010577 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010578 ptr = http_get_path(txn);
10579 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010580 return 0;
10581
10582 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010583 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010584 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010585
10586 while (ptr < end && *ptr != '?')
10587 ptr++;
10588
Willy Tarreauf853c462012-04-23 18:53:56 +020010589 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010590 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010591 return 1;
10592}
10593
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010594/* This produces a concatenation of the first occurrence of the Host header
10595 * followed by the path component if it begins with a slash ('/'). This means
10596 * that '*' will not be added, resulting in exactly the first Host entry.
10597 * If no Host header is found, then the path is returned as-is. The returned
10598 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010599 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010600 */
10601static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010602smp_fetch_base(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010603 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010604{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010605 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010606 char *ptr, *end, *beg;
10607 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010608 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010609
10610 CHECK_HTTP_MESSAGE_FIRST();
10611
Willy Tarreau15e91e12015-04-04 00:52:09 +020010612 txn = strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010613 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010614 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreau192252e2015-04-04 01:47:55 +020010615 return smp_fetch_path(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010616
10617 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010618 temp = get_trash_chunk();
10619 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010620 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010621 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010622 smp->data.str.len = ctx.vlen;
10623
10624 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010625 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010626 beg = http_get_path(txn);
10627 if (!beg)
10628 beg = end;
10629
10630 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10631
10632 if (beg < ptr && *beg == '/') {
10633 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10634 smp->data.str.len += ptr - beg;
10635 }
10636
10637 smp->flags = SMP_F_VOL_1ST;
10638 return 1;
10639}
10640
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010641/* This produces a 32-bit hash of the concatenation of the first occurrence of
10642 * the Host header followed by the path component if it begins with a slash ('/').
10643 * This means that '*' will not be added, resulting in exactly the first Host
10644 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010645 * is hashed using the path hash followed by a full avalanche hash and provides a
10646 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010647 * high-traffic sites without having to store whole paths.
10648 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010649int
Willy Tarreau192252e2015-04-04 01:47:55 +020010650smp_fetch_base32(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010651 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010652{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010653 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010654 struct hdr_ctx ctx;
10655 unsigned int hash = 0;
10656 char *ptr, *beg, *end;
10657 int len;
10658
10659 CHECK_HTTP_MESSAGE_FIRST();
10660
Willy Tarreau15e91e12015-04-04 00:52:09 +020010661 txn = strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010662 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010663 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010664 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10665 ptr = ctx.line + ctx.val;
10666 len = ctx.vlen;
10667 while (len--)
10668 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10669 }
10670
10671 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010672 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010673 beg = http_get_path(txn);
10674 if (!beg)
10675 beg = end;
10676
10677 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10678
10679 if (beg < ptr && *beg == '/') {
10680 while (beg < ptr)
10681 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10682 }
10683 hash = full_hash(hash);
10684
10685 smp->type = SMP_T_UINT;
10686 smp->data.uint = hash;
10687 smp->flags = SMP_F_VOL_1ST;
10688 return 1;
10689}
10690
Willy Tarreau4a550602012-12-09 14:53:32 +010010691/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010692 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10693 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10694 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010695 * that in environments where IPv6 is insignificant, truncating the output to
10696 * 8 bytes would still work.
10697 */
10698static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010699smp_fetch_base32_src(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010700 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010701{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010702 struct chunk *temp;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020010703 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010704
10705 if (!cli_conn)
10706 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010707
Willy Tarreau192252e2015-04-04 01:47:55 +020010708 if (!smp_fetch_base32(px, sess, strm, opt, args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010709 return 0;
10710
Willy Tarreau47ca5452012-12-23 20:22:19 +010010711 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010712 *(unsigned int *)temp->str = htonl(smp->data.uint);
10713 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010714
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010715 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010716 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010717 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010718 temp->len += 4;
10719 break;
10720 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010721 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010722 temp->len += 16;
10723 break;
10724 default:
10725 return 0;
10726 }
10727
10728 smp->data.str = *temp;
10729 smp->type = SMP_T_BIN;
10730 return 1;
10731}
10732
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010733/* Extracts the query string, which comes after the question mark '?'. If no
10734 * question mark is found, nothing is returned. Otherwise it returns a sample
10735 * of type string carrying the whole query string.
10736 */
10737static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010738smp_fetch_query(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010739 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010740{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010741 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010742 char *ptr, *end;
10743
10744 CHECK_HTTP_MESSAGE_FIRST();
10745
Willy Tarreau15e91e12015-04-04 00:52:09 +020010746 txn = strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010747 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10748 end = ptr + txn->req.sl.rq.u_l;
10749
10750 /* look up the '?' */
10751 do {
10752 if (ptr == end)
10753 return 0;
10754 } while (*ptr++ != '?');
10755
10756 smp->type = SMP_T_STR;
10757 smp->data.str.str = ptr;
10758 smp->data.str.len = end - ptr;
10759 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10760 return 1;
10761}
10762
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010763static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010764smp_fetch_proto_http(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010765 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010766{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010767 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10768 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10769 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010770
Willy Tarreau24e32d82012-04-23 23:55:44 +020010771 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010772
Willy Tarreauf853c462012-04-23 18:53:56 +020010773 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010774 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010775 return 1;
10776}
10777
Willy Tarreau7f18e522010-10-22 20:04:13 +020010778/* return a valid test if the current request is the first one on the connection */
10779static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010780smp_fetch_http_first_req(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010781 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010782{
Willy Tarreauf853c462012-04-23 18:53:56 +020010783 smp->type = SMP_T_BOOL;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010784 smp->data.uint = !(strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010785 return 1;
10786}
10787
Willy Tarreau34db1082012-04-19 17:16:54 +020010788/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010789static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010790smp_fetch_http_auth(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010791 const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010792{
10793
Willy Tarreau24e32d82012-04-23 23:55:44 +020010794 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010795 return 0;
10796
Willy Tarreauc0239e02012-04-16 14:42:55 +020010797 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010798
Willy Tarreau15e91e12015-04-04 00:52:09 +020010799 if (!get_http_auth(strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010800 return 0;
10801
Willy Tarreauf853c462012-04-23 18:53:56 +020010802 smp->type = SMP_T_BOOL;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010803 smp->data.uint = check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010804 return 1;
10805}
Willy Tarreau8797c062007-05-07 00:55:35 +020010806
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010807/* Accepts exactly 1 argument of type userlist */
10808static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010809smp_fetch_http_auth_grp(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010810 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010811{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010812 if (!args || args->type != ARGT_USR)
10813 return 0;
10814
10815 CHECK_HTTP_MESSAGE_FIRST();
10816
Willy Tarreau15e91e12015-04-04 00:52:09 +020010817 if (!get_http_auth(strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010818 return 0;
10819
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010820 /* if the user does not belong to the userlist or has a wrong password,
10821 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010822 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010823 */
Willy Tarreau15e91e12015-04-04 00:52:09 +020010824 if (!check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010825 return 0;
10826
10827 /* pat_match_auth() will need the user list */
10828 smp->ctx.a[0] = args->data.usr;
10829
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010830 smp->type = SMP_T_STR;
10831 smp->flags = SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010832 smp->data.str.str = strm->txn->auth.user;
10833 smp->data.str.len = strlen(strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010834
10835 return 1;
10836}
10837
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010838/* Try to find the next occurrence of a cookie name in a cookie header value.
10839 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10840 * the cookie value is returned into *value and *value_l, and the function
10841 * returns a pointer to the next pointer to search from if the value was found.
10842 * Otherwise if the cookie was not found, NULL is returned and neither value
10843 * nor value_l are touched. The input <hdr> string should first point to the
10844 * header's value, and the <hdr_end> pointer must point to the first character
10845 * not part of the value. <list> must be non-zero if value may represent a list
10846 * of values (cookie headers). This makes it faster to abort parsing when no
10847 * list is expected.
10848 */
10849static char *
10850extract_cookie_value(char *hdr, const char *hdr_end,
10851 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010852 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010853{
10854 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10855 char *next;
10856
10857 /* we search at least a cookie name followed by an equal, and more
10858 * generally something like this :
10859 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10860 */
10861 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10862 /* Iterate through all cookies on this line */
10863
10864 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10865 att_beg++;
10866
10867 /* find att_end : this is the first character after the last non
10868 * space before the equal. It may be equal to hdr_end.
10869 */
10870 equal = att_end = att_beg;
10871
10872 while (equal < hdr_end) {
10873 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10874 break;
10875 if (http_is_spht[(unsigned char)*equal++])
10876 continue;
10877 att_end = equal;
10878 }
10879
10880 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10881 * is between <att_beg> and <equal>, both may be identical.
10882 */
10883
10884 /* look for end of cookie if there is an equal sign */
10885 if (equal < hdr_end && *equal == '=') {
10886 /* look for the beginning of the value */
10887 val_beg = equal + 1;
10888 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10889 val_beg++;
10890
10891 /* find the end of the value, respecting quotes */
10892 next = find_cookie_value_end(val_beg, hdr_end);
10893
10894 /* make val_end point to the first white space or delimitor after the value */
10895 val_end = next;
10896 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10897 val_end--;
10898 } else {
10899 val_beg = val_end = next = equal;
10900 }
10901
10902 /* We have nothing to do with attributes beginning with '$'. However,
10903 * they will automatically be removed if a header before them is removed,
10904 * since they're supposed to be linked together.
10905 */
10906 if (*att_beg == '$')
10907 continue;
10908
10909 /* Ignore cookies with no equal sign */
10910 if (equal == next)
10911 continue;
10912
10913 /* Now we have the cookie name between att_beg and att_end, and
10914 * its value between val_beg and val_end.
10915 */
10916
10917 if (att_end - att_beg == cookie_name_l &&
10918 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10919 /* let's return this value and indicate where to go on from */
10920 *value = val_beg;
10921 *value_l = val_end - val_beg;
10922 return next + 1;
10923 }
10924
10925 /* Set-Cookie headers only have the name in the first attr=value part */
10926 if (!list)
10927 break;
10928 }
10929
10930 return NULL;
10931}
10932
William Lallemanda43ba4e2014-01-28 18:14:25 +010010933/* Fetch a captured HTTP request header. The index is the position of
10934 * the "capture" option in the configuration file
10935 */
10936static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010937smp_fetch_capture_header_req(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010938 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010939{
Willy Tarreaud0d8da92015-04-04 02:10:38 +020010940 struct proxy *fe = strm_fe(strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010941 int idx;
10942
10943 if (!args || args->type != ARGT_UINT)
10944 return 0;
10945
10946 idx = args->data.uint;
10947
Willy Tarreau15e91e12015-04-04 00:52:09 +020010948 if (idx > (fe->nb_req_cap - 1) || strm->req_cap == NULL || strm->req_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010949 return 0;
10950
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010951 smp->type = SMP_T_STR;
10952 smp->flags |= SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010953 smp->data.str.str = strm->req_cap[idx];
10954 smp->data.str.len = strlen(strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010955
10956 return 1;
10957}
10958
10959/* Fetch a captured HTTP response header. The index is the position of
10960 * the "capture" option in the configuration file
10961 */
10962static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010963smp_fetch_capture_header_res(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010964 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010965{
Willy Tarreaud0d8da92015-04-04 02:10:38 +020010966 struct proxy *fe = strm_fe(strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010967 int idx;
10968
10969 if (!args || args->type != ARGT_UINT)
10970 return 0;
10971
10972 idx = args->data.uint;
10973
Willy Tarreau15e91e12015-04-04 00:52:09 +020010974 if (idx > (fe->nb_rsp_cap - 1) || strm->res_cap == NULL || strm->res_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010975 return 0;
10976
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010977 smp->type = SMP_T_STR;
10978 smp->flags |= SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010979 smp->data.str.str = strm->res_cap[idx];
10980 smp->data.str.len = strlen(strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010981
10982 return 1;
10983}
10984
William Lallemand65ad6e12014-01-31 15:08:02 +010010985/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10986static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010987smp_fetch_capture_req_method(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010988 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010989{
10990 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010991 struct http_txn *txn = strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010992 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010993
Willy Tarreau15e91e12015-04-04 00:52:09 +020010994 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010995 return 0;
10996
William Lallemand96a77852014-02-05 00:30:02 +010010997 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010998
William Lallemand96a77852014-02-05 00:30:02 +010010999 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11000 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011001
William Lallemand96a77852014-02-05 00:30:02 +010011002 temp = get_trash_chunk();
11003 temp->str = txn->uri;
11004 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011005 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011006 smp->type = SMP_T_STR;
11007 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011008
11009 return 1;
11010
11011}
11012
11013/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11014static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011015smp_fetch_capture_req_uri(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011016 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011017{
11018 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011019 struct http_txn *txn = strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011020 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011021
Willy Tarreau15e91e12015-04-04 00:52:09 +020011022 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011023 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011024
William Lallemand65ad6e12014-01-31 15:08:02 +010011025 ptr = txn->uri;
11026
11027 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11028 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011029
William Lallemand65ad6e12014-01-31 15:08:02 +010011030 if (!*ptr)
11031 return 0;
11032
11033 ptr++; /* skip the space */
11034
11035 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011036 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011037 if (!ptr)
11038 return 0;
11039 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11040 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011041
11042 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010011043 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011044 smp->type = SMP_T_STR;
11045 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011046
11047 return 1;
11048}
11049
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011050/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11051 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11052 */
11053static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011054smp_fetch_capture_req_ver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011055 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011056{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011057 struct http_txn *txn = strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011058
Willy Tarreau15e91e12015-04-04 00:52:09 +020011059 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011060 return 0;
11061
11062 if (txn->req.flags & HTTP_MSGF_VER_11)
11063 smp->data.str.str = "HTTP/1.1";
11064 else
11065 smp->data.str.str = "HTTP/1.0";
11066
11067 smp->data.str.len = 8;
11068 smp->type = SMP_T_STR;
11069 smp->flags = SMP_F_CONST;
11070 return 1;
11071
11072}
11073
11074/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11075 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11076 */
11077static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011078smp_fetch_capture_res_ver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011079 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011080{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011081 struct http_txn *txn = strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011082
Willy Tarreau15e91e12015-04-04 00:52:09 +020011083 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011084 return 0;
11085
11086 if (txn->rsp.flags & HTTP_MSGF_VER_11)
11087 smp->data.str.str = "HTTP/1.1";
11088 else
11089 smp->data.str.str = "HTTP/1.0";
11090
11091 smp->data.str.len = 8;
11092 smp->type = SMP_T_STR;
11093 smp->flags = SMP_F_CONST;
11094 return 1;
11095
11096}
11097
William Lallemand65ad6e12014-01-31 15:08:02 +010011098
Willy Tarreaue333ec92012-04-16 16:26:40 +020011099/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011100 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011101 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011102 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011103 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011104 * Accepts exactly 1 argument of type string. If the input options indicate
11105 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011106 * The returned sample is of type CSTR. Can be used to parse cookies in other
11107 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011108 */
Willy Tarreau192252e2015-04-04 01:47:55 +020011109int smp_fetch_cookie(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011110 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011111{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011112 struct http_txn *txn;
11113 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011114 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011115 const struct http_msg *msg;
11116 const char *hdr_name;
11117 int hdr_name_len;
11118 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011119 int occ = 0;
11120 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011121
Willy Tarreau24e32d82012-04-23 23:55:44 +020011122 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011123 return 0;
11124
Willy Tarreaua890d072013-04-02 12:01:06 +020011125 if (!ctx) {
11126 /* first call */
11127 ctx = &static_hdr_ctx;
11128 ctx->idx = 0;
11129 smp->ctx.a[2] = ctx;
11130 }
11131
Willy Tarreaue333ec92012-04-16 16:26:40 +020011132 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011133
Willy Tarreau15e91e12015-04-04 00:52:09 +020011134 txn = strm->txn;
11135 idx = &strm->txn->hdr_idx;
11136
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011137 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011138 msg = &txn->req;
11139 hdr_name = "Cookie";
11140 hdr_name_len = 6;
11141 } else {
11142 msg = &txn->rsp;
11143 hdr_name = "Set-Cookie";
11144 hdr_name_len = 10;
11145 }
11146
Willy Tarreau28376d62012-04-26 21:26:10 +020011147 if (!occ && !(opt & SMP_OPT_ITERATE))
11148 /* no explicit occurrence and single fetch => last cookie by default */
11149 occ = -1;
11150
11151 /* OK so basically here, either we want only one value and it's the
11152 * last one, or we want to iterate over all of them and we fetch the
11153 * next one.
11154 */
11155
Willy Tarreau9b28e032012-10-12 23:49:43 +020011156 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011157 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011158 /* search for the header from the beginning, we must first initialize
11159 * the search parameters.
11160 */
Willy Tarreau37406352012-04-23 16:16:37 +020011161 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011162 ctx->idx = 0;
11163 }
11164
Willy Tarreau28376d62012-04-26 21:26:10 +020011165 smp->flags |= SMP_F_VOL_HDR;
11166
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011167 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011168 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11169 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011170 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11171 goto out;
11172
Willy Tarreau24e32d82012-04-23 23:55:44 +020011173 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011174 continue;
11175
Willy Tarreau37406352012-04-23 16:16:37 +020011176 smp->ctx.a[0] = ctx->line + ctx->val;
11177 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011178 }
11179
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011180 smp->type = SMP_T_STR;
11181 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011182 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011183 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011184 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011185 &smp->data.str.str,
11186 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011187 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011188 found = 1;
11189 if (occ >= 0) {
11190 /* one value was returned into smp->data.str.{str,len} */
11191 smp->flags |= SMP_F_NOT_LAST;
11192 return 1;
11193 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011194 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011195 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011196 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011197 /* all cookie headers and values were scanned. If we're looking for the
11198 * last occurrence, we may return it now.
11199 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011200 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011201 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011202 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011203}
11204
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011205/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011206 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011207 * multiple cookies may be parsed on the same line. The returned sample is of
11208 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011209 */
11210static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011211smp_fetch_cookie_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011212 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011213{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011214 struct http_txn *txn;
11215 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011216 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011217 const struct http_msg *msg;
11218 const char *hdr_name;
11219 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011220 int cnt;
11221 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011222 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011223
Willy Tarreau24e32d82012-04-23 23:55:44 +020011224 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011225 return 0;
11226
Willy Tarreaue333ec92012-04-16 16:26:40 +020011227 CHECK_HTTP_MESSAGE_FIRST();
11228
Willy Tarreau15e91e12015-04-04 00:52:09 +020011229 txn = strm->txn;
11230 idx = &strm->txn->hdr_idx;
11231
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011232 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011233 msg = &txn->req;
11234 hdr_name = "Cookie";
11235 hdr_name_len = 6;
11236 } else {
11237 msg = &txn->rsp;
11238 hdr_name = "Set-Cookie";
11239 hdr_name_len = 10;
11240 }
11241
Willy Tarreau9b28e032012-10-12 23:49:43 +020011242 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011243 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011244 ctx.idx = 0;
11245 cnt = 0;
11246
11247 while (1) {
11248 /* Note: val_beg == NULL every time we need to fetch a new header */
11249 if (!val_beg) {
11250 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11251 break;
11252
Willy Tarreau24e32d82012-04-23 23:55:44 +020011253 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011254 continue;
11255
11256 val_beg = ctx.line + ctx.val;
11257 val_end = val_beg + ctx.vlen;
11258 }
11259
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011260 smp->type = SMP_T_STR;
11261 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011262 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011263 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011264 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011265 &smp->data.str.str,
11266 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011267 cnt++;
11268 }
11269 }
11270
Willy Tarreaub169eba2013-12-16 15:14:43 +010011271 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011272 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011273 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011274 return 1;
11275}
11276
Willy Tarreau51539362012-05-08 12:46:28 +020011277/* Fetch an cookie's integer value. The integer value is returned. It
11278 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11279 */
11280static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011281smp_fetch_cookie_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011282 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011283{
Willy Tarreau192252e2015-04-04 01:47:55 +020011284 int ret = smp_fetch_cookie(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011285
11286 if (ret > 0) {
11287 smp->type = SMP_T_UINT;
11288 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11289 }
11290
11291 return ret;
11292}
11293
Willy Tarreau8797c062007-05-07 00:55:35 +020011294/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011295/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011296/************************************************************************/
11297
David Cournapeau16023ee2010-12-23 20:55:41 +090011298/*
11299 * Given a path string and its length, find the position of beginning of the
11300 * query string. Returns NULL if no query string is found in the path.
11301 *
11302 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11303 *
11304 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11305 */
bedis4c75cca2012-10-05 08:38:24 +020011306static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011307{
11308 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011309
bedis4c75cca2012-10-05 08:38:24 +020011310 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011311 return p ? p + 1 : NULL;
11312}
11313
bedis4c75cca2012-10-05 08:38:24 +020011314static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011315{
bedis4c75cca2012-10-05 08:38:24 +020011316 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011317}
11318
11319/*
11320 * Given a url parameter, find the starting position of the first occurence,
11321 * or NULL if the parameter is not found.
11322 *
11323 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11324 * the function will return query_string+8.
11325 */
11326static char*
11327find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011328 char* url_param_name, size_t url_param_name_l,
11329 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011330{
11331 char *pos, *last;
11332
11333 pos = query_string;
11334 last = query_string + query_string_l - url_param_name_l - 1;
11335
11336 while (pos <= last) {
11337 if (pos[url_param_name_l] == '=') {
11338 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11339 return pos;
11340 pos += url_param_name_l + 1;
11341 }
bedis4c75cca2012-10-05 08:38:24 +020011342 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011343 pos++;
11344 pos++;
11345 }
11346 return NULL;
11347}
11348
11349/*
11350 * Given a url parameter name, returns its value and size into *value and
11351 * *value_l respectively, and returns non-zero. If the parameter is not found,
11352 * zero is returned and value/value_l are not touched.
11353 */
11354static int
11355find_url_param_value(char* path, size_t path_l,
11356 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011357 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011358{
11359 char *query_string, *qs_end;
11360 char *arg_start;
11361 char *value_start, *value_end;
11362
bedis4c75cca2012-10-05 08:38:24 +020011363 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011364 if (!query_string)
11365 return 0;
11366
11367 qs_end = path + path_l;
11368 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011369 url_param_name, url_param_name_l,
11370 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011371 if (!arg_start)
11372 return 0;
11373
11374 value_start = arg_start + url_param_name_l + 1;
11375 value_end = value_start;
11376
bedis4c75cca2012-10-05 08:38:24 +020011377 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011378 value_end++;
11379
11380 *value = value_start;
11381 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011382 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011383}
11384
11385static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011386smp_fetch_url_param(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011387 const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011388{
bedis4c75cca2012-10-05 08:38:24 +020011389 char delim = '?';
Willy Tarreau15e91e12015-04-04 00:52:09 +020011390 struct http_msg *msg;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011391
bedis4c75cca2012-10-05 08:38:24 +020011392 if (!args || args[0].type != ARGT_STR ||
11393 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011394 return 0;
11395
11396 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011397
Willy Tarreau15e91e12015-04-04 00:52:09 +020011398 msg = &strm->txn->req;
11399
bedis4c75cca2012-10-05 08:38:24 +020011400 if (args[1].type)
11401 delim = *args[1].data.str.str;
11402
Willy Tarreau9b28e032012-10-12 23:49:43 +020011403 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011404 args->data.str.str, args->data.str.len,
11405 &smp->data.str.str, &smp->data.str.len,
11406 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011407 return 0;
11408
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011409 smp->type = SMP_T_STR;
11410 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011411 return 1;
11412}
11413
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011414/* Return the signed integer value for the specified url parameter (see url_param
11415 * above).
11416 */
11417static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011418smp_fetch_url_param_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011419 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011420{
Willy Tarreau192252e2015-04-04 01:47:55 +020011421 int ret = smp_fetch_url_param(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011422
11423 if (ret > 0) {
11424 smp->type = SMP_T_UINT;
11425 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11426 }
11427
11428 return ret;
11429}
11430
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011431/* This produces a 32-bit hash of the concatenation of the first occurrence of
11432 * the Host header followed by the path component if it begins with a slash ('/').
11433 * This means that '*' will not be added, resulting in exactly the first Host
11434 * entry. If no Host header is found, then the path is used. The resulting value
11435 * is hashed using the url hash followed by a full avalanche hash and provides a
11436 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11437 * high-traffic sites without having to store whole paths.
11438 * this differs from the base32 functions in that it includes the url parameters
11439 * as well as the path
11440 */
11441static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011442smp_fetch_url32(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011443 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011444{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011445 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011446 struct hdr_ctx ctx;
11447 unsigned int hash = 0;
11448 char *ptr, *beg, *end;
11449 int len;
11450
11451 CHECK_HTTP_MESSAGE_FIRST();
11452
Willy Tarreau15e91e12015-04-04 00:52:09 +020011453 txn = strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011454 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011455 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011456 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11457 ptr = ctx.line + ctx.val;
11458 len = ctx.vlen;
11459 while (len--)
11460 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11461 }
11462
11463 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011464 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 +000011465 beg = http_get_path(txn);
11466 if (!beg)
11467 beg = end;
11468
11469 for (ptr = beg; ptr < end ; ptr++);
11470
11471 if (beg < ptr && *beg == '/') {
11472 while (beg < ptr)
11473 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11474 }
11475 hash = full_hash(hash);
11476
11477 smp->type = SMP_T_UINT;
11478 smp->data.uint = hash;
11479 smp->flags = SMP_F_VOL_1ST;
11480 return 1;
11481}
11482
11483/* This concatenates the source address with the 32-bit hash of the Host and
11484 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11485 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11486 * on the source address length. The URL hash is stored before the address so
11487 * that in environments where IPv6 is insignificant, truncating the output to
11488 * 8 bytes would still work.
11489 */
11490static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011491smp_fetch_url32_src(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011492 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011493{
11494 struct chunk *temp;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020011495 struct connection *cli_conn = objt_conn(sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011496
Willy Tarreau192252e2015-04-04 01:47:55 +020011497 if (!smp_fetch_url32(px, sess, strm, opt, args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011498 return 0;
11499
11500 temp = get_trash_chunk();
11501 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11502 temp->len += sizeof(smp->data.uint);
11503
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011504 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011505 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011506 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011507 temp->len += 4;
11508 break;
11509 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011510 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011511 temp->len += 16;
11512 break;
11513 default:
11514 return 0;
11515 }
11516
11517 smp->data.str = *temp;
11518 smp->type = SMP_T_BIN;
11519 return 1;
11520}
11521
Willy Tarreau185b5c42012-04-26 15:11:51 +020011522/* This function is used to validate the arguments passed to any "hdr" fetch
11523 * keyword. These keywords support an optional positive or negative occurrence
11524 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11525 * is assumed that the types are already the correct ones. Returns 0 on error,
11526 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11527 * error message in case of error, that the caller is responsible for freeing.
11528 * The initial location must either be freeable or NULL.
11529 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011530int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011531{
11532 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011533 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011534 return 0;
11535 }
11536 return 1;
11537}
11538
Willy Tarreau276fae92013-07-25 14:36:01 +020011539/* takes an UINT value on input supposed to represent the time since EPOCH,
11540 * adds an optional offset found in args[0] and emits a string representing
11541 * the date in RFC-1123/5322 format.
11542 */
Willy Tarreau87b09662015-04-03 00:22:06 +020011543static int sample_conv_http_date(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011544 struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011545{
11546 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11547 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11548 struct chunk *temp;
11549 struct tm *tm;
11550 time_t curr_date = smp->data.uint;
11551
11552 /* add offset */
11553 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11554 curr_date += args[0].data.sint;
11555
11556 tm = gmtime(&curr_date);
11557
11558 temp = get_trash_chunk();
11559 temp->len = snprintf(temp->str, temp->size - temp->len,
11560 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11561 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11562 tm->tm_hour, tm->tm_min, tm->tm_sec);
11563
11564 smp->data.str = *temp;
11565 smp->type = SMP_T_STR;
11566 return 1;
11567}
11568
Thierry FOURNIERad903512014-04-11 17:51:01 +020011569/* Match language range with language tag. RFC2616 14.4:
11570 *
11571 * A language-range matches a language-tag if it exactly equals
11572 * the tag, or if it exactly equals a prefix of the tag such
11573 * that the first tag character following the prefix is "-".
11574 *
11575 * Return 1 if the strings match, else return 0.
11576 */
11577static inline int language_range_match(const char *range, int range_len,
11578 const char *tag, int tag_len)
11579{
11580 const char *end = range + range_len;
11581 const char *tend = tag + tag_len;
11582 while (range < end) {
11583 if (*range == '-' && tag == tend)
11584 return 1;
11585 if (*range != *tag || tag == tend)
11586 return 0;
11587 range++;
11588 tag++;
11589 }
11590 /* Return true only if the last char of the tag is matched. */
11591 return tag == tend;
11592}
11593
11594/* Arguments: The list of expected value, the number of parts returned and the separator */
Willy Tarreau87b09662015-04-03 00:22:06 +020011595static int sample_conv_q_prefered(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011596 struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011597{
11598 const char *al = smp->data.str.str;
11599 const char *end = al + smp->data.str.len;
11600 const char *token;
11601 int toklen;
11602 int qvalue;
11603 const char *str;
11604 const char *w;
11605 int best_q = 0;
11606
11607 /* Set the constant to the sample, because the output of the
11608 * function will be peek in the constant configuration string.
11609 */
11610 smp->flags |= SMP_F_CONST;
11611 smp->data.str.size = 0;
11612 smp->data.str.str = "";
11613 smp->data.str.len = 0;
11614
11615 /* Parse the accept language */
11616 while (1) {
11617
11618 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011619 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011620 al++;
11621 if (al >= end)
11622 break;
11623
11624 /* Start of the fisrt word. */
11625 token = al;
11626
11627 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011628 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011629 al++;
11630 if (al == token)
11631 goto expect_comma;
11632
11633 /* Length of the token. */
11634 toklen = al - token;
11635 qvalue = 1000;
11636
11637 /* Check if the token exists in the list. If the token not exists,
11638 * jump to the next token.
11639 */
11640 str = args[0].data.str.str;
11641 w = str;
11642 while (1) {
11643 if (*str == ';' || *str == '\0') {
11644 if (language_range_match(token, toklen, w, str-w))
11645 goto look_for_q;
11646 if (*str == '\0')
11647 goto expect_comma;
11648 w = str + 1;
11649 }
11650 str++;
11651 }
11652 goto expect_comma;
11653
11654look_for_q:
11655
11656 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011657 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011658 al++;
11659 if (al >= end)
11660 goto process_value;
11661
11662 /* If ',' is found, process the result */
11663 if (*al == ',')
11664 goto process_value;
11665
11666 /* If the character is different from ';', look
11667 * for the end of the header part in best effort.
11668 */
11669 if (*al != ';')
11670 goto expect_comma;
11671
11672 /* Assumes that the char is ';', now expect "q=". */
11673 al++;
11674
11675 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011676 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011677 al++;
11678 if (al >= end)
11679 goto process_value;
11680
11681 /* Expect 'q'. If no 'q', continue in best effort */
11682 if (*al != 'q')
11683 goto process_value;
11684 al++;
11685
11686 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011687 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011688 al++;
11689 if (al >= end)
11690 goto process_value;
11691
11692 /* Expect '='. If no '=', continue in best effort */
11693 if (*al != '=')
11694 goto process_value;
11695 al++;
11696
11697 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011698 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011699 al++;
11700 if (al >= end)
11701 goto process_value;
11702
11703 /* Parse the q value. */
11704 qvalue = parse_qvalue(al, &al);
11705
11706process_value:
11707
11708 /* If the new q value is the best q value, then store the associated
11709 * language in the response. If qvalue is the biggest value (1000),
11710 * break the process.
11711 */
11712 if (qvalue > best_q) {
11713 smp->data.str.str = (char *)w;
11714 smp->data.str.len = str - w;
11715 if (qvalue >= 1000)
11716 break;
11717 best_q = qvalue;
11718 }
11719
11720expect_comma:
11721
11722 /* Expect comma or end. If the end is detected, quit the loop. */
11723 while (al < end && *al != ',')
11724 al++;
11725 if (al >= end)
11726 break;
11727
11728 /* Comma is found, jump it and restart the analyzer. */
11729 al++;
11730 }
11731
11732 /* Set default value if required. */
11733 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11734 smp->data.str.str = args[1].data.str.str;
11735 smp->data.str.len = args[1].data.str.len;
11736 }
11737
11738 /* Return true only if a matching language was found. */
11739 return smp->data.str.len != 0;
11740}
11741
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011742/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011743 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011744 * the relevant part of the request line accordingly. Then it updates various
11745 * pointers to the next elements which were moved, and the total buffer length.
11746 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011747 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11748 * error, though this can be revisited when this code is finally exploited.
11749 *
11750 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11751 * query string and 3 to replace uri.
11752 *
11753 * In query string case, the mark question '?' must be set at the start of the
11754 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011755 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011756int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011757 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011758{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011759 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011760 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011761 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011762 int delta;
11763
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011764 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011765 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011766 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011767 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11768
11769 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011770 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011771 txn->req.sl.rq.m_l += delta;
11772 txn->req.sl.rq.u += delta;
11773 txn->req.sl.rq.v += delta;
11774 break;
11775
11776 case 1: // path
11777 cur_ptr = http_get_path(txn);
11778 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011779 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011780
11781 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011782 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 +010011783 cur_end++;
11784
11785 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011786 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011787 txn->req.sl.rq.u_l += delta;
11788 txn->req.sl.rq.v += delta;
11789 break;
11790
11791 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011792 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011793 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011794 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11795 while (cur_ptr < cur_end && *cur_ptr != '?')
11796 cur_ptr++;
11797
11798 /* skip the question mark or indicate that we must insert it
11799 * (but only if the format string is not empty then).
11800 */
11801 if (cur_ptr < cur_end)
11802 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011803 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011804 offset = 0;
11805
11806 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011807 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011808 txn->req.sl.rq.u_l += delta;
11809 txn->req.sl.rq.v += delta;
11810 break;
11811
11812 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011813 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011814 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11815
11816 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011817 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011818 txn->req.sl.rq.u_l += delta;
11819 txn->req.sl.rq.v += delta;
11820 break;
11821
11822 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011823 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011824 }
11825
11826 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011827 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011828 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011829 return 0;
11830}
11831
11832/* This function executes one of the set-{method,path,query,uri} actions. It
11833 * builds a string in the trash from the specified format string. It finds
11834 * the action to be performed in p[2], previously filled by function
11835 * parse_set_req_line(). The replacement action is excuted by the function
11836 * http_action_set_req_line_exec(). It always returns 1. If an error occurs
11837 * the action is canceled, but the rule processing continue.
11838 */
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011839int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011840{
11841 chunk_reset(&trash);
11842
11843 /* If we have to create a query string, prepare a '?'. */
11844 if (*(int *)&rule->arg.act.p[2] == 2)
11845 trash.str[trash.len++] = '?';
11846 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
11847
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011848 http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s);
Thierry FOURNIER01c30122015-03-14 14:14:47 +010011849 return 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011850}
11851
11852/* parse an http-request action among :
11853 * set-method
11854 * set-path
11855 * set-query
11856 * set-uri
11857 *
11858 * All of them accept a single argument of type string representing a log-format.
11859 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11860 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
11861 * It returns 0 on success, < 0 on error.
11862 */
11863int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
11864{
11865 int cur_arg = *orig_arg;
11866
11867 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
11868
11869 switch (args[0][4]) {
11870 case 'm' :
11871 *(int *)&rule->arg.act.p[2] = 0;
11872 rule->action_ptr = http_action_set_req_line;
11873 break;
11874 case 'p' :
11875 *(int *)&rule->arg.act.p[2] = 1;
11876 rule->action_ptr = http_action_set_req_line;
11877 break;
11878 case 'q' :
11879 *(int *)&rule->arg.act.p[2] = 2;
11880 rule->action_ptr = http_action_set_req_line;
11881 break;
11882 case 'u' :
11883 *(int *)&rule->arg.act.p[2] = 3;
11884 rule->action_ptr = http_action_set_req_line;
11885 break;
11886 default:
11887 memprintf(err, "internal error: unhandled action '%s'", args[0]);
11888 return -1;
11889 }
11890
11891 if (!*args[cur_arg] ||
11892 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11893 memprintf(err, "expects exactly 1 argument <format>");
11894 return -1;
11895 }
11896
11897 LIST_INIT((struct list *)&rule->arg.act.p[0]);
11898 proxy->conf.args.ctx = ARGC_HRQ;
11899 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
11900 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
11901 proxy->conf.args.file, proxy->conf.args.line);
11902
11903 (*orig_arg)++;
11904 return 0;
11905}
11906
William Lallemand73025dd2014-04-24 14:38:37 +020011907/*
11908 * Return the struct http_req_action_kw associated to a keyword.
11909 */
11910struct http_req_action_kw *action_http_req_custom(const char *kw)
11911{
11912 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11913 struct http_req_action_kw_list *kw_list;
11914 int i;
11915
11916 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11917 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11918 if (!strcmp(kw, kw_list->kw[i].kw))
11919 return &kw_list->kw[i];
11920 }
11921 }
11922 }
11923 return NULL;
11924}
11925
11926/*
11927 * Return the struct http_res_action_kw associated to a keyword.
11928 */
11929struct http_res_action_kw *action_http_res_custom(const char *kw)
11930{
11931 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11932 struct http_res_action_kw_list *kw_list;
11933 int i;
11934
11935 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11936 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11937 if (!strcmp(kw, kw_list->kw[i].kw))
11938 return &kw_list->kw[i];
11939 }
11940 }
11941 }
11942 return NULL;
11943}
11944
Willy Tarreau4a568972010-05-12 08:08:50 +020011945/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011946/* All supported ACL keywords must be declared here. */
11947/************************************************************************/
11948
11949/* Note: must not be declared <const> as its list will be overwritten.
11950 * Please take care of keeping this list alphabetically sorted.
11951 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011952static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011953 { "base", "base", PAT_MATCH_STR },
11954 { "base_beg", "base", PAT_MATCH_BEG },
11955 { "base_dir", "base", PAT_MATCH_DIR },
11956 { "base_dom", "base", PAT_MATCH_DOM },
11957 { "base_end", "base", PAT_MATCH_END },
11958 { "base_len", "base", PAT_MATCH_LEN },
11959 { "base_reg", "base", PAT_MATCH_REG },
11960 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011961
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011962 { "cook", "req.cook", PAT_MATCH_STR },
11963 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11964 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11965 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11966 { "cook_end", "req.cook", PAT_MATCH_END },
11967 { "cook_len", "req.cook", PAT_MATCH_LEN },
11968 { "cook_reg", "req.cook", PAT_MATCH_REG },
11969 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011970
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011971 { "hdr", "req.hdr", PAT_MATCH_STR },
11972 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11973 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11974 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11975 { "hdr_end", "req.hdr", PAT_MATCH_END },
11976 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11977 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11978 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011979
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011980 /* these two declarations uses strings with list storage (in place
11981 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11982 * and delete functions are relative to the list management. The parse
11983 * and match method are related to the corresponding fetch methods. This
11984 * is very particular ACL declaration mode.
11985 */
11986 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11987 { "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 +020011988
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011989 { "path", "path", PAT_MATCH_STR },
11990 { "path_beg", "path", PAT_MATCH_BEG },
11991 { "path_dir", "path", PAT_MATCH_DIR },
11992 { "path_dom", "path", PAT_MATCH_DOM },
11993 { "path_end", "path", PAT_MATCH_END },
11994 { "path_len", "path", PAT_MATCH_LEN },
11995 { "path_reg", "path", PAT_MATCH_REG },
11996 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011997
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011998 { "req_ver", "req.ver", PAT_MATCH_STR },
11999 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012000
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012001 { "scook", "res.cook", PAT_MATCH_STR },
12002 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12003 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12004 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12005 { "scook_end", "res.cook", PAT_MATCH_END },
12006 { "scook_len", "res.cook", PAT_MATCH_LEN },
12007 { "scook_reg", "res.cook", PAT_MATCH_REG },
12008 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012009
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012010 { "shdr", "res.hdr", PAT_MATCH_STR },
12011 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12012 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12013 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12014 { "shdr_end", "res.hdr", PAT_MATCH_END },
12015 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12016 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12017 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012018
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012019 { "url", "url", PAT_MATCH_STR },
12020 { "url_beg", "url", PAT_MATCH_BEG },
12021 { "url_dir", "url", PAT_MATCH_DIR },
12022 { "url_dom", "url", PAT_MATCH_DOM },
12023 { "url_end", "url", PAT_MATCH_END },
12024 { "url_len", "url", PAT_MATCH_LEN },
12025 { "url_reg", "url", PAT_MATCH_REG },
12026 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012027
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012028 { "urlp", "urlp", PAT_MATCH_STR },
12029 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12030 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12031 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12032 { "urlp_end", "urlp", PAT_MATCH_END },
12033 { "urlp_len", "urlp", PAT_MATCH_LEN },
12034 { "urlp_reg", "urlp", PAT_MATCH_REG },
12035 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012036
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012037 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012038}};
12039
12040/************************************************************************/
12041/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012042/************************************************************************/
12043/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012044static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012045 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012046 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12047 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12048
Willy Tarreau87b09662015-04-03 00:22:06 +020012049 /* capture are allocated and are permanent in the stream */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012050 { "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 +020012051
12052 /* retrieve these captures from the HTTP logs */
12053 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12054 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12055 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12056
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012057 { "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 +020012058 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012059
Willy Tarreau409bcde2013-01-08 00:31:00 +010012060 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12061 * are only here to match the ACL's name, are request-only and are used
12062 * for ACL compatibility only.
12063 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012064 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12065 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012066 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12067 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12068
12069 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12070 * only here to match the ACL's name, are request-only and are used for
12071 * ACL compatibility only.
12072 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012073 { "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 +010012074 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12075 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
12076 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12077
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012078 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012079 { "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 +010012080 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012081 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012082 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012083 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012084
12085 /* HTTP protocol on the request path */
12086 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012087 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012088
12089 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012090 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12091 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012092
12093 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012094 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12095 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012096
Willy Tarreau18ed2562013-01-14 15:56:36 +010012097 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012098 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012099 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12100 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12101
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012102 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012103 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012104 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012105 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12106 { "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 +010012107 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012108 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12109
12110 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012111 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012112 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12113 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12114
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012115 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012116 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012117 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012118 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12119 { "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 +010012120 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012121 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12122
Willy Tarreau409bcde2013-01-08 00:31:00 +010012123 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012124 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012125 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12126 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012127 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012128
12129 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012130 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012131 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12132 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
12133 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12134
12135 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012136 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012137 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12138 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012139 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
12140 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012141 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12142 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012143 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12144 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012145}};
12146
Willy Tarreau8797c062007-05-07 00:55:35 +020012147
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012148/************************************************************************/
12149/* All supported converter keywords must be declared here. */
12150/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012151/* Note: must not be declared <const> as its list will be overwritten */
12152static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020012153 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
12154 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012155 { NULL, NULL, 0, 0, 0 },
12156}};
12157
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012158/************************************************************************/
12159/* All supported http-request action keywords must be declared here. */
12160/************************************************************************/
12161struct http_req_action_kw_list http_req_actions = {
12162 .scope = "http",
12163 .kw = {
12164 { "set-method", parse_set_req_line },
12165 { "set-path", parse_set_req_line },
12166 { "set-query", parse_set_req_line },
12167 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012168 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012169 }
12170};
12171
Willy Tarreau8797c062007-05-07 00:55:35 +020012172__attribute__((constructor))
12173static void __http_protocol_init(void)
12174{
12175 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012176 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012177 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012178 http_req_keywords_register(&http_req_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012179}
12180
12181
Willy Tarreau58f10d72006-12-04 02:26:12 +010012182/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012183 * Local variables:
12184 * c-indent-level: 8
12185 * c-basic-offset: 8
12186 * End:
12187 */