blob: 48dbc43cc79b9e44002f35784bc6c9fbc6866a83 [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 Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.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"
127 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
128
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,
134 [HTTP_ERR_408] = 408,
135 [HTTP_ERR_500] = 500,
136 [HTTP_ERR_502] = 502,
137 [HTTP_ERR_503] = 503,
138 [HTTP_ERR_504] = 504,
139};
140
Willy Tarreau80587432006-12-24 17:47:20 +0100141static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200142 [HTTP_ERR_200] =
143 "HTTP/1.0 200 OK\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
149
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100151 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
157
158 [HTTP_ERR_403] =
159 "HTTP/1.0 403 Forbidden\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
165
166 [HTTP_ERR_408] =
167 "HTTP/1.0 408 Request Time-out\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
173
174 [HTTP_ERR_500] =
175 "HTTP/1.0 500 Server Error\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
181
182 [HTTP_ERR_502] =
183 "HTTP/1.0 502 Bad Gateway\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
189
190 [HTTP_ERR_503] =
191 "HTTP/1.0 503 Service Unavailable\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
197
198 [HTTP_ERR_504] =
199 "HTTP/1.0 504 Gateway Time-out\r\n"
200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
204 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
205
206};
207
Cyril Bonté19979e12012-04-04 12:57:21 +0200208/* status codes available for the stats admin page (strictly 4 chars length) */
209const char *stat_status_codes[STAT_STATUS_SIZE] = {
210 [STAT_STATUS_DENY] = "DENY",
211 [STAT_STATUS_DONE] = "DONE",
212 [STAT_STATUS_ERRP] = "ERRP",
213 [STAT_STATUS_EXCD] = "EXCD",
214 [STAT_STATUS_NONE] = "NONE",
215 [STAT_STATUS_PART] = "PART",
216 [STAT_STATUS_UNKN] = "UNKN",
217};
218
219
William Lallemand73025dd2014-04-24 14:38:37 +0200220/* List head of all known action keywords for "http-request" */
221struct http_req_action_kw_list http_req_keywords = {
222 .list = LIST_HEAD_INIT(http_req_keywords.list)
223};
224
225/* List head of all known action keywords for "http-response" */
226struct http_res_action_kw_list http_res_keywords = {
227 .list = LIST_HEAD_INIT(http_res_keywords.list)
228};
229
Willy Tarreau80587432006-12-24 17:47:20 +0100230/* We must put the messages here since GCC cannot initialize consts depending
231 * on strlen().
232 */
233struct chunk http_err_chunks[HTTP_ERR_SIZE];
234
Willy Tarreaua890d072013-04-02 12:01:06 +0200235/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
236static struct hdr_ctx static_hdr_ctx;
237
Willy Tarreau42250582007-04-01 01:30:43 +0200238#define FD_SETS_ARE_BITFIELDS
239#ifdef FD_SETS_ARE_BITFIELDS
240/*
241 * This map is used with all the FD_* macros to check whether a particular bit
242 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
243 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
244 * byte should be encoded. Be careful to always pass bytes from 0 to 255
245 * exclusively to the macros.
246 */
247fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
248fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100249fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200250
251#else
252#error "Check if your OS uses bitfields for fd_sets"
253#endif
254
Willy Tarreau0b748332014-04-29 00:13:29 +0200255static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn);
256
Willy Tarreau80587432006-12-24 17:47:20 +0100257void init_proto_http()
258{
Willy Tarreau42250582007-04-01 01:30:43 +0200259 int i;
260 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100261 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200262
Willy Tarreau80587432006-12-24 17:47:20 +0100263 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
264 if (!http_err_msgs[msg]) {
265 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
266 abort();
267 }
268
269 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
270 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
271 }
Willy Tarreau42250582007-04-01 01:30:43 +0200272
273 /* initialize the log header encoding map : '{|}"#' should be encoded with
274 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
275 * URL encoding only requires '"', '#' to be encoded as well as non-
276 * printable characters above.
277 */
278 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
279 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100280 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200281 for (i = 0; i < 32; i++) {
282 FD_SET(i, hdr_encode_map);
283 FD_SET(i, url_encode_map);
284 }
285 for (i = 127; i < 256; i++) {
286 FD_SET(i, hdr_encode_map);
287 FD_SET(i, url_encode_map);
288 }
289
290 tmp = "\"#{|}";
291 while (*tmp) {
292 FD_SET(*tmp, hdr_encode_map);
293 tmp++;
294 }
295
296 tmp = "\"#";
297 while (*tmp) {
298 FD_SET(*tmp, url_encode_map);
299 tmp++;
300 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200301
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100302 /* initialize the http header encoding map. The draft httpbis define the
303 * header content as:
304 *
305 * HTTP-message = start-line
306 * *( header-field CRLF )
307 * CRLF
308 * [ message-body ]
309 * header-field = field-name ":" OWS field-value OWS
310 * field-value = *( field-content / obs-fold )
311 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
312 * obs-fold = CRLF 1*( SP / HTAB )
313 * field-vchar = VCHAR / obs-text
314 * VCHAR = %x21-7E
315 * obs-text = %x80-FF
316 *
317 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
318 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
319 * "obs-fold" is volontary forgotten because haproxy remove this.
320 */
321 memset(http_encode_map, 0, sizeof(http_encode_map));
322 for (i = 0x00; i <= 0x08; i++)
323 FD_SET(i, http_encode_map);
324 for (i = 0x0a; i <= 0x1f; i++)
325 FD_SET(i, http_encode_map);
326 FD_SET(0x7f, http_encode_map);
327
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200328 /* memory allocations */
329 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100330 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100331}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332
Willy Tarreau53b6c742006-12-17 13:37:46 +0100333/*
334 * We have 26 list of methods (1 per first letter), each of which can have
335 * up to 3 entries (2 valid, 1 null).
336 */
337struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100338 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100339 int len;
340 const char text[8];
341};
342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100343const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100344 ['C' - 'A'] = {
345 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
346 },
347 ['D' - 'A'] = {
348 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
349 },
350 ['G' - 'A'] = {
351 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
352 },
353 ['H' - 'A'] = {
354 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
355 },
356 ['P' - 'A'] = {
357 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
358 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
359 },
360 ['T' - 'A'] = {
361 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
362 },
363 /* rest is empty like this :
364 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
365 */
366};
367
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100368const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
369 [HTTP_METH_NONE] = { "", 0 },
370 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
371 [HTTP_METH_GET] = { "GET", 3 },
372 [HTTP_METH_HEAD] = { "HEAD", 4 },
373 [HTTP_METH_POST] = { "POST", 4 },
374 [HTTP_METH_PUT] = { "PUT", 3 },
375 [HTTP_METH_DELETE] = { "DELETE", 6 },
376 [HTTP_METH_TRACE] = { "TRACE", 5 },
377 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
378};
379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100380/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200381 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100382 * RFC2616 for those chars.
383 */
384
385const char http_is_spht[256] = {
386 [' '] = 1, ['\t'] = 1,
387};
388
389const char http_is_crlf[256] = {
390 ['\r'] = 1, ['\n'] = 1,
391};
392
393const char http_is_lws[256] = {
394 [' '] = 1, ['\t'] = 1,
395 ['\r'] = 1, ['\n'] = 1,
396};
397
398const char http_is_sep[256] = {
399 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
400 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
401 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
402 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
403 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
404};
405
406const char http_is_ctl[256] = {
407 [0 ... 31] = 1,
408 [127] = 1,
409};
410
411/*
412 * A token is any ASCII char that is neither a separator nor a CTL char.
413 * Do not overwrite values in assignment since gcc-2.95 will not handle
414 * them correctly. Instead, define every non-CTL char's status.
415 */
416const char http_is_token[256] = {
417 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
418 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
419 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
420 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
421 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
422 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
423 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
424 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
425 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
426 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
427 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
428 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
429 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
430 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
431 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
432 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
433 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
434 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
435 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
436 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
437 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
438 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
439 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
440 ['|'] = 1, ['}'] = 0, ['~'] = 1,
441};
442
443
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100444/*
445 * An http ver_token is any ASCII which can be found in an HTTP version,
446 * which includes 'H', 'T', 'P', '/', '.' and any digit.
447 */
448const char http_is_ver_token[256] = {
449 ['.'] = 1, ['/'] = 1,
450 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
451 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
452 ['H'] = 1, ['P'] = 1, ['T'] = 1,
453};
454
455
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100457 * Adds a header and its CRLF at the tail of the message's buffer, just before
458 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100459 * The header is also automatically added to the index <hdr_idx>, and the end
460 * of headers is automatically adjusted. The number of bytes added is returned
461 * on success, otherwise <0 is returned indicating an error.
462 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100463int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100464{
465 int bytes, len;
466
467 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200468 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100469 if (!bytes)
470 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100471 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100472 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
473}
474
475/*
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. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100478 * the buffer is only opened and the space reserved, but nothing is copied.
479 * The header is also automatically added to the index <hdr_idx>, and the end
480 * of headers is automatically adjusted. The number of bytes added is returned
481 * on success, otherwise <0 is returned indicating an error.
482 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100483int http_header_add_tail2(struct http_msg *msg,
484 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100485{
486 int bytes;
487
Willy Tarreau9b28e032012-10-12 23:49:43 +0200488 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100489 if (!bytes)
490 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100491 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100492 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
493}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200494
495/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100496 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
497 * If so, returns the position of the first non-space character relative to
498 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
499 * to return a pointer to the place after the first space. Returns 0 if the
500 * header name does not match. Checks are case-insensitive.
501 */
502int http_header_match2(const char *hdr, const char *end,
503 const char *name, int len)
504{
505 const char *val;
506
507 if (hdr + len >= end)
508 return 0;
509 if (hdr[len] != ':')
510 return 0;
511 if (strncasecmp(hdr, name, len) != 0)
512 return 0;
513 val = hdr + len + 1;
514 while (val < end && HTTP_IS_SPHT(*val))
515 val++;
516 if ((val >= end) && (len + 2 <= end - hdr))
517 return len + 2; /* we may replace starting from second space */
518 return val - hdr;
519}
520
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200521/* Find the first or next occurrence of header <name> in message buffer <sol>
522 * using headers index <idx>, and return it in the <ctx> structure. This
523 * structure holds everything necessary to use the header and find next
524 * occurrence. If its <idx> member is 0, the header is searched from the
525 * beginning. Otherwise, the next occurrence is returned. The function returns
526 * 1 when it finds a value, and 0 when there is no more. It is very similar to
527 * http_find_header2() except that it is designed to work with full-line headers
528 * whose comma is not a delimiter but is part of the syntax. As a special case,
529 * if ctx->val is NULL when searching for a new values of a header, the current
530 * header is rescanned. This allows rescanning after a header deletion.
531 */
532int http_find_full_header2(const char *name, int len,
533 char *sol, struct hdr_idx *idx,
534 struct hdr_ctx *ctx)
535{
536 char *eol, *sov;
537 int cur_idx, old_idx;
538
539 cur_idx = ctx->idx;
540 if (cur_idx) {
541 /* We have previously returned a header, let's search another one */
542 sol = ctx->line;
543 eol = sol + idx->v[cur_idx].len;
544 goto next_hdr;
545 }
546
547 /* first request for this header */
548 sol += hdr_idx_first_pos(idx);
549 old_idx = 0;
550 cur_idx = hdr_idx_first_idx(idx);
551 while (cur_idx) {
552 eol = sol + idx->v[cur_idx].len;
553
554 if (len == 0) {
555 /* No argument was passed, we want any header.
556 * To achieve this, we simply build a fake request. */
557 while (sol + len < eol && sol[len] != ':')
558 len++;
559 name = sol;
560 }
561
562 if ((len < eol - sol) &&
563 (sol[len] == ':') &&
564 (strncasecmp(sol, name, len) == 0)) {
565 ctx->del = len;
566 sov = sol + len + 1;
567 while (sov < eol && http_is_lws[(unsigned char)*sov])
568 sov++;
569
570 ctx->line = sol;
571 ctx->prev = old_idx;
572 ctx->idx = cur_idx;
573 ctx->val = sov - sol;
574 ctx->tws = 0;
575 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
576 eol--;
577 ctx->tws++;
578 }
579 ctx->vlen = eol - sov;
580 return 1;
581 }
582 next_hdr:
583 sol = eol + idx->v[cur_idx].cr + 1;
584 old_idx = cur_idx;
585 cur_idx = idx->v[cur_idx].next;
586 }
587 return 0;
588}
589
Willy Tarreau68085d82010-01-18 14:54:04 +0100590/* Find the end of the header value contained between <s> and <e>. See RFC2616,
591 * par 2.2 for more information. Note that it requires a valid header to return
592 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200593 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100594char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200595{
596 int quoted, qdpair;
597
598 quoted = qdpair = 0;
599 for (; s < e; s++) {
600 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200601 else if (quoted) {
602 if (*s == '\\') qdpair = 1;
603 else if (*s == '"') quoted = 0;
604 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200605 else if (*s == '"') quoted = 1;
606 else if (*s == ',') return s;
607 }
608 return s;
609}
610
611/* Find the first or next occurrence of header <name> in message buffer <sol>
612 * using headers index <idx>, and return it in the <ctx> structure. This
613 * structure holds everything necessary to use the header and find next
614 * occurrence. If its <idx> member is 0, the header is searched from the
615 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100616 * 1 when it finds a value, and 0 when there is no more. It is designed to work
617 * with headers defined as comma-separated lists. As a special case, if ctx->val
618 * is NULL when searching for a new values of a header, the current header is
619 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200620 */
621int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100622 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200623 struct hdr_ctx *ctx)
624{
Willy Tarreau68085d82010-01-18 14:54:04 +0100625 char *eol, *sov;
626 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200627
Willy Tarreau68085d82010-01-18 14:54:04 +0100628 cur_idx = ctx->idx;
629 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200630 /* We have previously returned a value, let's search
631 * another one on the same line.
632 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200633 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200634 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100635 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200636 eol = sol + idx->v[cur_idx].len;
637
638 if (sov >= eol)
639 /* no more values in this header */
640 goto next_hdr;
641
Willy Tarreau68085d82010-01-18 14:54:04 +0100642 /* values remaining for this header, skip the comma but save it
643 * for later use (eg: for header deletion).
644 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200645 sov++;
646 while (sov < eol && http_is_lws[(unsigned char)*sov])
647 sov++;
648
649 goto return_hdr;
650 }
651
652 /* first request for this header */
653 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100654 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200655 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200656 while (cur_idx) {
657 eol = sol + idx->v[cur_idx].len;
658
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200659 if (len == 0) {
660 /* No argument was passed, we want any header.
661 * To achieve this, we simply build a fake request. */
662 while (sol + len < eol && sol[len] != ':')
663 len++;
664 name = sol;
665 }
666
Willy Tarreau33a7e692007-06-10 19:45:56 +0200667 if ((len < eol - sol) &&
668 (sol[len] == ':') &&
669 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100670 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200671 sov = sol + len + 1;
672 while (sov < eol && http_is_lws[(unsigned char)*sov])
673 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100674
Willy Tarreau33a7e692007-06-10 19:45:56 +0200675 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100676 ctx->prev = old_idx;
677 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200678 ctx->idx = cur_idx;
679 ctx->val = sov - sol;
680
681 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200682 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200683 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200684 eol--;
685 ctx->tws++;
686 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200687 ctx->vlen = eol - sov;
688 return 1;
689 }
690 next_hdr:
691 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100692 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200693 cur_idx = idx->v[cur_idx].next;
694 }
695 return 0;
696}
697
698int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100699 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200700 struct hdr_ctx *ctx)
701{
702 return http_find_header2(name, strlen(name), sol, idx, ctx);
703}
704
Willy Tarreau68085d82010-01-18 14:54:04 +0100705/* Remove one value of a header. This only works on a <ctx> returned by one of
706 * the http_find_header functions. The value is removed, as well as surrounding
707 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100708 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100709 * message <msg>. The new index is returned. If it is zero, it means there is
710 * no more header, so any processing may stop. The ctx is always left in a form
711 * that can be handled by http_find_header2() to find next occurrence.
712 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100713int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100714{
715 int cur_idx = ctx->idx;
716 char *sol = ctx->line;
717 struct hdr_idx_elem *hdr;
718 int delta, skip_comma;
719
720 if (!cur_idx)
721 return 0;
722
723 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200724 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100725 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200726 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100727 http_msg_move_end(msg, delta);
728 idx->used--;
729 hdr->len = 0; /* unused entry */
730 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100731 if (idx->tail == ctx->idx)
732 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100733 ctx->idx = ctx->prev; /* walk back to the end of previous header */
734 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
735 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200736 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100737 return ctx->idx;
738 }
739
740 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200741 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
742 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100743 */
744
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200745 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200746 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200747 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100748 NULL, 0);
749 hdr->len += delta;
750 http_msg_move_end(msg, delta);
751 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200752 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100753 return ctx->idx;
754}
755
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100756/* This function handles a server error at the stream interface level. The
757 * stream interface is assumed to be already in a closed state. An optional
758 * message is copied into the input buffer, and an HTTP status code stored.
759 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100760 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200761 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200762static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100763 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200765 channel_auto_read(si->ob);
766 channel_abort(si->ob);
767 channel_auto_close(si->ob);
768 channel_erase(si->ob);
769 channel_auto_close(si->ib);
770 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100771 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200772 s->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200773 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200775 if (!(s->flags & SN_ERR_MASK))
776 s->flags |= err;
777 if (!(s->flags & SN_FINST_MASK))
778 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200779}
780
Willy Tarreau80587432006-12-24 17:47:20 +0100781/* This function returns the appropriate error location for the given session
782 * and message.
783 */
784
Willy Tarreau783f2582012-09-04 12:19:04 +0200785struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100786{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200787 if (s->be->errmsg[msgnum].str)
788 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100789 else if (s->fe->errmsg[msgnum].str)
790 return &s->fe->errmsg[msgnum];
791 else
792 return &http_err_chunks[msgnum];
793}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794
Willy Tarreau53b6c742006-12-17 13:37:46 +0100795/*
796 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
797 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
798 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100799enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100800{
801 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100802 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100803
804 m = ((unsigned)*str - 'A');
805
806 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100807 for (h = http_methods[m]; h->len > 0; h++) {
808 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100809 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100810 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100811 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100812 };
813 return HTTP_METH_OTHER;
814 }
815 return HTTP_METH_NONE;
816
817}
818
Willy Tarreau21d2af32008-02-14 20:25:24 +0100819/* Parse the URI from the given transaction (which is assumed to be in request
820 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
821 * It is returned otherwise.
822 */
823static char *
824http_get_path(struct http_txn *txn)
825{
826 char *ptr, *end;
827
Willy Tarreau9b28e032012-10-12 23:49:43 +0200828 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100829 end = ptr + txn->req.sl.rq.u_l;
830
831 if (ptr >= end)
832 return NULL;
833
834 /* RFC2616, par. 5.1.2 :
835 * Request-URI = "*" | absuri | abspath | authority
836 */
837
838 if (*ptr == '*')
839 return NULL;
840
841 if (isalpha((unsigned char)*ptr)) {
842 /* this is a scheme as described by RFC3986, par. 3.1 */
843 ptr++;
844 while (ptr < end &&
845 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
846 ptr++;
847 /* skip '://' */
848 if (ptr == end || *ptr++ != ':')
849 return NULL;
850 if (ptr == end || *ptr++ != '/')
851 return NULL;
852 if (ptr == end || *ptr++ != '/')
853 return NULL;
854 }
855 /* skip [user[:passwd]@]host[:[port]] */
856
857 while (ptr < end && *ptr != '/')
858 ptr++;
859
860 if (ptr == end)
861 return NULL;
862
863 /* OK, we got the '/' ! */
864 return ptr;
865}
866
William Lallemand65ad6e12014-01-31 15:08:02 +0100867/* Parse the URI from the given string and look for the "/" beginning the PATH.
868 * If not found, return NULL. It is returned otherwise.
869 */
870static char *
871http_get_path_from_string(char *str)
872{
873 char *ptr = str;
874
875 /* RFC2616, par. 5.1.2 :
876 * Request-URI = "*" | absuri | abspath | authority
877 */
878
879 if (*ptr == '*')
880 return NULL;
881
882 if (isalpha((unsigned char)*ptr)) {
883 /* this is a scheme as described by RFC3986, par. 3.1 */
884 ptr++;
885 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
886 ptr++;
887 /* skip '://' */
888 if (*ptr == '\0' || *ptr++ != ':')
889 return NULL;
890 if (*ptr == '\0' || *ptr++ != '/')
891 return NULL;
892 if (*ptr == '\0' || *ptr++ != '/')
893 return NULL;
894 }
895 /* skip [user[:passwd]@]host[:[port]] */
896
897 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
898 ptr++;
899
900 if (*ptr == '\0' || *ptr == ' ')
901 return NULL;
902
903 /* OK, we got the '/' ! */
904 return ptr;
905}
906
Willy Tarreau71241ab2012-12-27 11:30:54 +0100907/* Returns a 302 for a redirectable request that reaches a server working in
908 * in redirect mode. This may only be called just after the stream interface
909 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
910 * follow normal proxy processing. NOTE: this function is designed to support
911 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100912 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100913void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100914{
915 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100916 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100917 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200918 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100919
920 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100921 trash.len = strlen(HTTP_302);
922 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100923
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100924 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100925
Willy Tarreauefb453c2008-10-26 20:49:47 +0100926 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100927 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100928 return;
929
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100930 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100931 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100932 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
933 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100934 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100935
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200936 /* 3: add the request URI. Since it was already forwarded, we need
937 * to temporarily rewind the buffer.
938 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 txn = &s->txn;
Willy Tarreau211cdec2014-04-17 20:18:08 +0200940 b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200941
Willy Tarreauefb453c2008-10-26 20:49:47 +0100942 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200943 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 +0200944
Willy Tarreau9b28e032012-10-12 23:49:43 +0200945 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200946
Willy Tarreauefb453c2008-10-26 20:49:47 +0100947 if (!path)
948 return;
949
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100950 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100951 return;
952
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100953 memcpy(trash.str + trash.len, path, len);
954 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100955
956 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100957 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
958 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100959 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100960 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
961 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100962 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100963
964 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200965 si_shutr(si);
966 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100967 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100968 si->state = SI_ST_CLO;
969
970 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200971 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100972
973 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100974 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500975 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100976}
977
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100978/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100979 * that the server side is closed. Note that err_type is actually a
980 * bitmask, where almost only aborts may be cumulated with other
981 * values. We consider that aborted operations are more important
982 * than timeouts or errors due to the fact that nobody else in the
983 * logs might explain incomplete retries. All others should avoid
984 * being cumulated. It should normally not be possible to have multiple
985 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100986 * Note that connection errors appearing on the second request of a keep-alive
987 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100988 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100989void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100991 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100992
993 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100994 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200995 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100996 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100997 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100998 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
999 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001000 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001001 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001002 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001004 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001005 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001007 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001008 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1009 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001010 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001011 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001012 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001013 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001014 else if (err_type & SI_ET_CONN_RES)
1015 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001016 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1017 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001018 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001019 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001020 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001021}
1022
Willy Tarreau42250582007-04-01 01:30:43 +02001023extern const char sess_term_cond[8];
1024extern const char sess_fin_state[8];
1025extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001026struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001027struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001028struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001029
Willy Tarreau117f59e2007-03-04 18:17:17 +01001030/*
1031 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001032 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001033 */
1034void capture_headers(char *som, struct hdr_idx *idx,
1035 char **cap, struct cap_hdr *cap_hdr)
1036{
1037 char *eol, *sol, *col, *sov;
1038 int cur_idx;
1039 struct cap_hdr *h;
1040 int len;
1041
1042 sol = som + hdr_idx_first_pos(idx);
1043 cur_idx = hdr_idx_first_idx(idx);
1044
1045 while (cur_idx) {
1046 eol = sol + idx->v[cur_idx].len;
1047
1048 col = sol;
1049 while (col < eol && *col != ':')
1050 col++;
1051
1052 sov = col + 1;
1053 while (sov < eol && http_is_lws[(unsigned char)*sov])
1054 sov++;
1055
1056 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001057 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001058 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1059 if (cap[h->index] == NULL)
1060 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001061 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001062
1063 if (cap[h->index] == NULL) {
1064 Alert("HTTP capture : out of memory.\n");
1065 continue;
1066 }
1067
1068 len = eol - sov;
1069 if (len > h->len)
1070 len = h->len;
1071
1072 memcpy(cap[h->index], sov, len);
1073 cap[h->index][len]=0;
1074 }
1075 }
1076 sol = eol + idx->v[cur_idx].cr + 1;
1077 cur_idx = idx->v[cur_idx].next;
1078 }
1079}
1080
1081
Willy Tarreau42250582007-04-01 01:30:43 +02001082/* either we find an LF at <ptr> or we jump to <bad>.
1083 */
1084#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1085
1086/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1087 * otherwise to <http_msg_ood> with <state> set to <st>.
1088 */
1089#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1090 ptr++; \
1091 if (likely(ptr < end)) \
1092 goto good; \
1093 else { \
1094 state = (st); \
1095 goto http_msg_ood; \
1096 } \
1097 } while (0)
1098
1099
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001101 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001102 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1103 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1104 * will give undefined results.
1105 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1106 * and that msg->sol points to the beginning of the response.
1107 * If a complete line is found (which implies that at least one CR or LF is
1108 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1109 * returned indicating an incomplete line (which does not mean that parts have
1110 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1111 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1112 * upon next call.
1113 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001114 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001115 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1116 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001117 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001118 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001119const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001120 enum ht_state state, const char *ptr, const char *end,
1121 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001122{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001123 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001124
Willy Tarreau8973c702007-01-21 23:58:29 +01001125 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001126 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001127 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001128 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001129 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1130
1131 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001132 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001133 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1134 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001135 state = HTTP_MSG_ERROR;
1136 break;
1137
Willy Tarreau8973c702007-01-21 23:58:29 +01001138 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001139 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001141 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001142 goto http_msg_rpcode;
1143 }
1144 if (likely(HTTP_IS_SPHT(*ptr)))
1145 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1146 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001147 state = HTTP_MSG_ERROR;
1148 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001149
Willy Tarreau8973c702007-01-21 23:58:29 +01001150 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001151 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001152 if (likely(!HTTP_IS_LWS(*ptr)))
1153 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1154
1155 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001156 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001157 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1158 }
1159
1160 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001161 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001162 http_msg_rsp_reason:
1163 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001164 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001165 msg->sl.st.r_l = 0;
1166 goto http_msg_rpline_eol;
1167
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001169 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001170 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001171 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001172 goto http_msg_rpreason;
1173 }
1174 if (likely(HTTP_IS_SPHT(*ptr)))
1175 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1176 /* so it's a CR/LF, so there is no reason phrase */
1177 goto http_msg_rsp_reason;
1178
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001180 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 if (likely(!HTTP_IS_CRLF(*ptr)))
1182 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001183 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001184 http_msg_rpline_eol:
1185 /* We have seen the end of line. Note that we do not
1186 * necessarily have the \n yet, but at least we know that we
1187 * have EITHER \r OR \n, otherwise the response would not be
1188 * complete. We can then record the response length and return
1189 * to the caller which will be able to register it.
1190 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001191 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001192 return ptr;
1193
Willy Tarreau8973c702007-01-21 23:58:29 +01001194 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001195#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001196 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1197 exit(1);
1198#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001199 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001200 }
1201
1202 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001203 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001204 if (ret_state)
1205 *ret_state = state;
1206 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001207 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001208 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001209}
1210
Willy Tarreau8973c702007-01-21 23:58:29 +01001211/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001212 * This function parses a request line between <ptr> and <end>, starting with
1213 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1214 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1215 * will give undefined results.
1216 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1217 * and that msg->sol points to the beginning of the request.
1218 * If a complete line is found (which implies that at least one CR or LF is
1219 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1220 * returned indicating an incomplete line (which does not mean that parts have
1221 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1222 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1223 * upon next call.
1224 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001225 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1227 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001228 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001229 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001230const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001231 enum ht_state state, const char *ptr, const char *end,
1232 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001234 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001238 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 if (likely(HTTP_IS_TOKEN(*ptr)))
1240 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001241
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001242 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001243 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1245 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 if (likely(HTTP_IS_CRLF(*ptr))) {
1248 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001249 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001251 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001252 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001253 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001254 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001255 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001256 msg->sl.rq.v_l = 0;
1257 goto http_msg_rqline_eol;
1258 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001259 state = HTTP_MSG_ERROR;
1260 break;
1261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001262 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001263 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001265 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001266 goto http_msg_rquri;
1267 }
1268 if (likely(HTTP_IS_SPHT(*ptr)))
1269 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1270 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1271 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001274 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001275 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001277
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001279 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001280 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1281 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001282
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001283 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001284 /* non-ASCII chars are forbidden unless option
1285 * accept-invalid-http-request is enabled in the frontend.
1286 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001287 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001288 if (msg->err_pos < -1)
1289 goto invalid_char;
1290 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001291 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001292 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1293 }
1294
1295 if (likely(HTTP_IS_CRLF(*ptr))) {
1296 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1297 goto http_msg_req09_uri_e;
1298 }
1299
1300 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001301 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001302 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001303 state = HTTP_MSG_ERROR;
1304 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001307 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001309 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 goto http_msg_rqver;
1311 }
1312 if (likely(HTTP_IS_SPHT(*ptr)))
1313 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1314 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1315 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001318 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001319 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001321
1322 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001323 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001324 http_msg_rqline_eol:
1325 /* We have seen the end of line. Note that we do not
1326 * necessarily have the \n yet, but at least we know that we
1327 * have EITHER \r OR \n, otherwise the request would not be
1328 * complete. We can then record the request length and return
1329 * to the caller which will be able to register it.
1330 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001331 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001332 return ptr;
1333 }
1334
1335 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001336 state = HTTP_MSG_ERROR;
1337 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001338
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001340#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1342 exit(1);
1343#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001344 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001348 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001349 if (ret_state)
1350 *ret_state = state;
1351 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001352 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001353 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001355
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001356/*
1357 * Returns the data from Authorization header. Function may be called more
1358 * than once so data is stored in txn->auth_data. When no header is found
1359 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001360 * searching again for something we are unable to find anyway. However, if
1361 * the result if valid, the cache is not reused because we would risk to
1362 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001363 */
1364
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001365/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1366 * set according to global.tune.bufsize.
1367 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001368char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001369
1370int
1371get_http_auth(struct session *s)
1372{
1373
1374 struct http_txn *txn = &s->txn;
1375 struct chunk auth_method;
1376 struct hdr_ctx ctx;
1377 char *h, *p;
1378 int len;
1379
1380#ifdef DEBUG_AUTH
1381 printf("Auth for session %p: %d\n", s, txn->auth.method);
1382#endif
1383
1384 if (txn->auth.method == HTTP_AUTH_WRONG)
1385 return 0;
1386
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001387 txn->auth.method = HTTP_AUTH_WRONG;
1388
1389 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001390
1391 if (txn->flags & TX_USE_PX_CONN) {
1392 h = "Proxy-Authorization";
1393 len = strlen(h);
1394 } else {
1395 h = "Authorization";
1396 len = strlen(h);
1397 }
1398
Willy Tarreau9b28e032012-10-12 23:49:43 +02001399 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001400 return 0;
1401
1402 h = ctx.line + ctx.val;
1403
1404 p = memchr(h, ' ', ctx.vlen);
1405 if (!p || p == h)
1406 return 0;
1407
1408 chunk_initlen(&auth_method, h, 0, p-h);
1409 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1410
1411 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1412
1413 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001414 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001415
1416 if (len < 0)
1417 return 0;
1418
1419
1420 get_http_auth_buff[len] = '\0';
1421
1422 p = strchr(get_http_auth_buff, ':');
1423
1424 if (!p)
1425 return 0;
1426
1427 txn->auth.user = get_http_auth_buff;
1428 *p = '\0';
1429 txn->auth.pass = p+1;
1430
1431 txn->auth.method = HTTP_AUTH_BASIC;
1432 return 1;
1433 }
1434
1435 return 0;
1436}
1437
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8973c702007-01-21 23:58:29 +01001439/*
1440 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001441 * depending on the initial msg->msg_state. The caller is responsible for
1442 * ensuring that the message does not wrap. The function can be preempted
1443 * everywhere when data are missing and recalled at the exact same location
1444 * with no information loss. The message may even be realigned between two
1445 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001446 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001447 * fields. Note that msg->sol will be initialized after completing the first
1448 * state, so that none of the msg pointers has to be initialized prior to the
1449 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001451void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452{
Willy Tarreau3770f232013-12-07 00:01:53 +01001453 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001455 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001457 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001458 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001459 ptr = buf->p + msg->next;
1460 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 if (unlikely(ptr >= end))
1463 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001466 /*
1467 * First, states that are specific to the response only.
1468 * We check them first so that request and headers are
1469 * closer to each other (accessed more often).
1470 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001471 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001472 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001473 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001474 /* we have a start of message, but we have to check
1475 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001476 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001477 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001478 if (unlikely(ptr != buf->p)) {
1479 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001480 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001481 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001482 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001483 }
Willy Tarreau26927362012-05-18 23:22:52 +02001484 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001485 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001486 hdr_idx_init(idx);
1487 state = HTTP_MSG_RPVER;
1488 goto http_msg_rpver;
1489 }
1490
1491 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1492 goto http_msg_invalid;
1493
1494 if (unlikely(*ptr == '\n'))
1495 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1496 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1497 /* stop here */
1498
Willy Tarreau8973c702007-01-21 23:58:29 +01001499 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001500 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001501 EXPECT_LF_HERE(ptr, http_msg_invalid);
1502 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1503 /* stop here */
1504
Willy Tarreau8973c702007-01-21 23:58:29 +01001505 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001506 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001507 case HTTP_MSG_RPVER_SP:
1508 case HTTP_MSG_RPCODE:
1509 case HTTP_MSG_RPCODE_SP:
1510 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001511 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001512 state, ptr, end,
1513 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001514 if (unlikely(!ptr))
1515 return;
1516
1517 /* we have a full response and we know that we have either a CR
1518 * or an LF at <ptr>.
1519 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001520 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1521
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001522 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001523 if (likely(*ptr == '\r'))
1524 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1525 goto http_msg_rpline_end;
1526
Willy Tarreau8973c702007-01-21 23:58:29 +01001527 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001528 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001529 /* msg->sol must point to the first of CR or LF. */
1530 EXPECT_LF_HERE(ptr, http_msg_invalid);
1531 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1532 /* stop here */
1533
1534 /*
1535 * Second, states that are specific to the request only
1536 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001538 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001540 /* we have a start of message, but we have to check
1541 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001542 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001543 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001544 if (likely(ptr != buf->p)) {
1545 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001546 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001547 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001548 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 }
Willy Tarreau26927362012-05-18 23:22:52 +02001550 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001551 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001552 state = HTTP_MSG_RQMETH;
1553 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001555
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1557 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 if (unlikely(*ptr == '\n'))
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1561 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001562 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001565 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 EXPECT_LF_HERE(ptr, http_msg_invalid);
1567 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001568 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001571 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 case HTTP_MSG_RQMETH_SP:
1573 case HTTP_MSG_RQURI:
1574 case HTTP_MSG_RQURI_SP:
1575 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001576 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001577 state, ptr, end,
1578 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 if (unlikely(!ptr))
1580 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001581
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 /* we have a full request and we know that we have either a CR
1583 * or an LF at <ptr>.
1584 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001586
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001587 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 if (likely(*ptr == '\r'))
1589 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001593 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 /* check for HTTP/0.9 request : no version information available.
1595 * msg->sol must point to the first of CR or LF.
1596 */
1597 if (unlikely(msg->sl.rq.v_l == 0))
1598 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 EXPECT_LF_HERE(ptr, http_msg_invalid);
1601 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001602 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001603
Willy Tarreau8973c702007-01-21 23:58:29 +01001604 /*
1605 * Common states below
1606 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001608 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001609 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 if (likely(!HTTP_IS_CRLF(*ptr))) {
1611 goto http_msg_hdr_name;
1612 }
1613
1614 if (likely(*ptr == '\r'))
1615 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1616 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001617
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001619 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 /* assumes msg->sol points to the first char */
1621 if (likely(HTTP_IS_TOKEN(*ptr)))
1622 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001623
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001624 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001626
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001627 if (likely(msg->err_pos < -1) || *ptr == '\n')
1628 goto http_msg_invalid;
1629
1630 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001631 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001632
1633 /* and we still accept this non-token character */
1634 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001637 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001638 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 if (likely(HTTP_IS_SPHT(*ptr)))
1640 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001643 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 if (likely(!HTTP_IS_CRLF(*ptr))) {
1646 goto http_msg_hdr_val;
1647 }
1648
1649 if (likely(*ptr == '\r'))
1650 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1651 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001654 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655 EXPECT_LF_HERE(ptr, http_msg_invalid);
1656 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001657
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001659 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 if (likely(HTTP_IS_SPHT(*ptr))) {
1661 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001662 for (; buf->p + msg->sov < ptr; msg->sov++)
1663 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 goto http_msg_hdr_l1_sp;
1665 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001666 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001667 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 goto http_msg_complete_header;
1669
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001671 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001672 /* assumes msg->sol points to the first char, and msg->sov
1673 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 */
1675 if (likely(!HTTP_IS_CRLF(*ptr)))
1676 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001677
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001678 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 /* Note: we could also copy eol into ->eoh so that we have the
1680 * real header end in case it ends with lots of LWS, but is this
1681 * really needed ?
1682 */
1683 if (likely(*ptr == '\r'))
1684 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1685 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001686
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001687 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001688 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 EXPECT_LF_HERE(ptr, http_msg_invalid);
1690 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001691
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001693 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1695 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001696 for (; buf->p + msg->eol < ptr; msg->eol++)
1697 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 goto http_msg_hdr_val;
1699 }
1700 http_msg_complete_header:
1701 /*
1702 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001703 * Assumes msg->sol points to the first char, msg->sov points
1704 * to the first character of the value and msg->eol to the
1705 * first CR or LF so we know how the line ends. We insert last
1706 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001708 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 idx, idx->tail) < 0))
1710 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001711
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001712 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 if (likely(!HTTP_IS_CRLF(*ptr))) {
1714 goto http_msg_hdr_name;
1715 }
1716
1717 if (likely(*ptr == '\r'))
1718 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1719 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001720
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001722 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001723 /* Assumes msg->sol points to the first of either CR or LF.
1724 * Sets ->sov and ->next to the total header length, ->eoh to
1725 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1726 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 EXPECT_LF_HERE(ptr, http_msg_invalid);
1728 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001729 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001730 msg->eoh = msg->sol;
1731 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001732 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001733 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001735
1736 case HTTP_MSG_ERROR:
1737 /* this may only happen if we call http_msg_analyser() twice with an error */
1738 break;
1739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001741#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1743 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001744#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001745 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 }
1747 http_msg_ood:
1748 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001749 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001750 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001752
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 http_msg_invalid:
1754 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001755 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001756 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 return;
1758}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001759
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001760/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1761 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1762 * nothing is done and 1 is returned.
1763 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001764static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001765{
1766 int delta;
1767 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001768 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001769
1770 if (msg->sl.rq.v_l != 0)
1771 return 1;
1772
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001773 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1774 if (txn->meth != HTTP_METH_GET)
1775 return 0;
1776
Willy Tarreau9b28e032012-10-12 23:49:43 +02001777 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001778 delta = 0;
1779
1780 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001781 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1782 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001783 }
1784 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001785 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001786 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001787 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001788 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001789 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001790 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001791 NULL, NULL);
1792 if (unlikely(!cur_end))
1793 return 0;
1794
1795 /* we have a full HTTP/1.0 request now and we know that
1796 * we have either a CR or an LF at <ptr>.
1797 */
1798 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1799 return 1;
1800}
1801
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001802/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001803 * and "keep-alive" values. If we already know that some headers may safely
1804 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001805 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1806 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001807 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001808 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1809 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1810 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001811 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001812 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001813void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001814{
Willy Tarreau5b154472009-12-21 20:11:07 +01001815 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001816 const char *hdr_val = "Connection";
1817 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001818
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001819 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001820 return;
1821
Willy Tarreau88d349d2010-01-25 12:15:43 +01001822 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1823 hdr_val = "Proxy-Connection";
1824 hdr_len = 16;
1825 }
1826
Willy Tarreau5b154472009-12-21 20:11:07 +01001827 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001828 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001829 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001830 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1831 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001832 if (to_del & 2)
1833 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001834 else
1835 txn->flags |= TX_CON_KAL_SET;
1836 }
1837 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1838 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001839 if (to_del & 1)
1840 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001841 else
1842 txn->flags |= TX_CON_CLO_SET;
1843 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001844 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1845 txn->flags |= TX_HDR_CONN_UPG;
1846 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001847 }
1848
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001849 txn->flags |= TX_HDR_CONN_PRS;
1850 return;
1851}
Willy Tarreau5b154472009-12-21 20:11:07 +01001852
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001853/* Apply desired changes on the Connection: header. Values may be removed and/or
1854 * added depending on the <wanted> flags, which are exclusively composed of
1855 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1856 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1857 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001858void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001859{
1860 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001861 const char *hdr_val = "Connection";
1862 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001863
1864 ctx.idx = 0;
1865
Willy Tarreau88d349d2010-01-25 12:15:43 +01001866
1867 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1868 hdr_val = "Proxy-Connection";
1869 hdr_len = 16;
1870 }
1871
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001873 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001874 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1875 if (wanted & TX_CON_KAL_SET)
1876 txn->flags |= TX_CON_KAL_SET;
1877 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001878 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001879 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001880 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1881 if (wanted & TX_CON_CLO_SET)
1882 txn->flags |= TX_CON_CLO_SET;
1883 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001884 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001885 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001886 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001887
1888 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1889 return;
1890
1891 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1892 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001893 hdr_val = "Connection: close";
1894 hdr_len = 17;
1895 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1896 hdr_val = "Proxy-Connection: close";
1897 hdr_len = 23;
1898 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001899 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001900 }
1901
1902 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1903 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001904 hdr_val = "Connection: keep-alive";
1905 hdr_len = 22;
1906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection: keep-alive";
1908 hdr_len = 28;
1909 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001910 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001911 }
1912 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001913}
1914
Willy Tarreauc24715e2014-04-17 15:21:20 +02001915/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1916 * the first byte of data after the chunk size, so that we know we can forward
1917 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1918 * the chunk size. That way it is always possible to differentiate between the
1919 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001920 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001922 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001923static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001924{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001925 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001926 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001927 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001928 const char *end = buf->data + buf->size;
1929 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001930 unsigned int chunk = 0;
1931
1932 /* The chunk size is in the following form, though we are only
1933 * interested in the size and CRLF :
1934 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1935 */
1936 while (1) {
1937 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001938 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001939 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001941 if (c < 0) /* not a hex digit anymore */
1942 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001943 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001944 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001945 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001946 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001947 chunk = (chunk << 4) + c;
1948 }
1949
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001951 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001952 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953
1954 while (http_is_spht[(unsigned char)*ptr]) {
1955 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001956 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001957 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001958 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001959 }
1960
Willy Tarreaud98cf932009-12-27 22:54:55 +01001961 /* Up to there, we know that at least one byte is present at *ptr. Check
1962 * for the end of chunk size.
1963 */
1964 while (1) {
1965 if (likely(HTTP_IS_CRLF(*ptr))) {
1966 /* we now have a CR or an LF at ptr */
1967 if (likely(*ptr == '\r')) {
1968 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001969 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001970 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001971 return 0;
1972 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001973
Willy Tarreaud98cf932009-12-27 22:54:55 +01001974 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001975 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001976 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001977 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001978 /* done */
1979 break;
1980 }
1981 else if (*ptr == ';') {
1982 /* chunk extension, ends at next CRLF */
1983 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001984 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001985 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001986 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001987
1988 while (!HTTP_IS_CRLF(*ptr)) {
1989 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001991 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001992 return 0;
1993 }
1994 /* we have a CRLF now, loop above */
1995 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001996 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001997 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001998 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001999 }
2000
Willy Tarreaud98cf932009-12-27 22:54:55 +01002001 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002002 * which may or may not be present. We save that into ->next,
2003 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002004 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002005 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002006 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002007 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002008 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002009 msg->chunk_len = chunk;
2010 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002011 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002012 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002013 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002014 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002015 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002016}
2017
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002018/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002019 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020 * the trailers is found, it is automatically scheduled to be forwarded,
2021 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2022 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002023 * except maybe msg->next if it could parse some lines, and returns zero.
2024 * If a parse error is encountered, the function returns < 0 and does not
2025 * change anything except maybe msg->next. Note that the message must
2026 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002027 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002028 * forwarding, and that msg->next exactly matches the length of trailers
2029 * already parsed and not forwarded. It is also important to note that this
2030 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002031 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002032static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002034 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002035
Willy Tarreaua458b672012-03-05 11:17:50 +01002036 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002038 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002039 const char *ptr = b_ptr(buf, msg->next);
2040 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002041 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042
2043 /* scan current line and stop at LF or CRLF */
2044 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002045 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002046 return 0;
2047
2048 if (*ptr == '\n') {
2049 if (!p1)
2050 p1 = ptr;
2051 p2 = ptr;
2052 break;
2053 }
2054
2055 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002056 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002057 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002059 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060 p1 = ptr;
2061 }
2062
2063 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002064 if (ptr >= buf->data + buf->size)
2065 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002066 }
2067
2068 /* after LF; point to beginning of next line */
2069 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002070 if (p2 >= buf->data + buf->size)
2071 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002072
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002073 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002074 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002075 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002076
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002077 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002078 /* LF/CRLF at beginning of line => end of trailers at p2.
2079 * Everything was scheduled for forwarding, there's nothing
2080 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002081 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002082 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002083 msg->msg_state = HTTP_MSG_DONE;
2084 return 1;
2085 }
2086 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002087 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002088 }
2089}
2090
Willy Tarreauc24715e2014-04-17 15:21:20 +02002091/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2092 * or a possible LF alone at the end of a chunk. It automatically adjusts
2093 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002094 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2096 * not enough data are available, the function does not change anything and
2097 * returns zero. If a parse error is encountered, the function returns < 0 and
2098 * does not change anything. Note: this function is designed to parse wrapped
2099 * CRLF at the end of the buffer.
2100 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002101static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002102{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002103 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002104 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002105 int bytes;
2106
2107 /* NB: we'll check data availabilty at the end. It's not a
2108 * problem because whatever we match first will be checked
2109 * against the correct length.
2110 */
2111 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002112 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002113 if (*ptr == '\r') {
2114 bytes++;
2115 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002116 if (ptr >= buf->data + buf->size)
2117 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002118 }
2119
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002120 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002121 return 0;
2122
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002123 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002124 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002125 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002126 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002127
2128 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002129 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002130 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002131 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002132 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002133 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2134 return 1;
2135}
Willy Tarreau5b154472009-12-21 20:11:07 +01002136
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002137/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2138 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2139 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2140 * Unparsable qvalues return 1000 as "q=1.000".
2141 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002142int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002143{
2144 int q = 1000;
2145
2146 if (!isdigit(*qvalue))
2147 goto out;
2148 q = (*qvalue++ - '0') * 1000;
2149
2150 if (*qvalue++ != '.')
2151 goto out;
2152
2153 if (!isdigit(*qvalue))
2154 goto out;
2155 q += (*qvalue++ - '0') * 100;
2156
2157 if (!isdigit(*qvalue))
2158 goto out;
2159 q += (*qvalue++ - '0') * 10;
2160
2161 if (!isdigit(*qvalue))
2162 goto out;
2163 q += (*qvalue++ - '0') * 1;
2164 out:
2165 if (q > 1000)
2166 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002167 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002168 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002169 return q;
2170}
William Lallemand82fe75c2012-10-23 10:25:10 +02002171
2172/*
2173 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002174 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002175int select_compression_request_header(struct session *s, struct buffer *req)
2176{
2177 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002178 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002179 struct hdr_ctx ctx;
2180 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002181 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002182
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002183 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2184 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002185 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2186 */
2187 ctx.idx = 0;
2188 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2189 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002190 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2191 (ctx.vlen < 31 ||
2192 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2193 ctx.line[ctx.val + 30] < '6' ||
2194 (ctx.line[ctx.val + 30] == '6' &&
2195 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2196 s->comp_algo = NULL;
2197 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002198 }
2199
William Lallemand82fe75c2012-10-23 10:25:10 +02002200 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002201 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002202 int best_q = 0;
2203
William Lallemand82fe75c2012-10-23 10:25:10 +02002204 ctx.idx = 0;
2205 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002206 const char *qval;
2207 int q;
2208 int toklen;
2209
2210 /* try to isolate the token from the optional q-value */
2211 toklen = 0;
2212 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2213 toklen++;
2214
2215 qval = ctx.line + ctx.val + toklen;
2216 while (1) {
2217 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2218 qval++;
2219
2220 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2221 qval = NULL;
2222 break;
2223 }
2224 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002225
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002226 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2227 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002228
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002229 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2230 qval = NULL;
2231 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002232 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002233 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2234 break;
2235
2236 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2237 qval++;
2238 }
2239
2240 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002241 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002242
2243 if (q <= best_q)
2244 continue;
2245
2246 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2247 if (*(ctx.line + ctx.val) == '*' ||
2248 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2249 s->comp_algo = comp_algo;
2250 best_q = q;
2251 break;
2252 }
2253 }
2254 }
2255 }
2256
2257 /* remove all occurrences of the header when "compression offload" is set */
2258 if (s->comp_algo) {
2259 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2260 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2261 ctx.idx = 0;
2262 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2263 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002264 }
2265 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002266 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002267 }
2268
2269 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002270 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2271 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002272 if (comp_algo->add_data == identity_add_data) {
2273 s->comp_algo = comp_algo;
2274 return 1;
2275 }
2276 }
2277 }
2278
2279 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002280 return 0;
2281}
2282
2283/*
2284 * Selects a comression algorithm depending of the server response.
2285 */
2286int select_compression_response_header(struct session *s, struct buffer *res)
2287{
2288 struct http_txn *txn = &s->txn;
2289 struct http_msg *msg = &txn->rsp;
2290 struct hdr_ctx ctx;
2291 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002292
2293 /* no common compression algorithm was found in request header */
2294 if (s->comp_algo == NULL)
2295 goto fail;
2296
2297 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002298 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002299 goto fail;
2300
William Lallemandd3002612012-11-26 14:34:47 +01002301 /* 200 only */
2302 if (txn->status != 200)
2303 goto fail;
2304
William Lallemand82fe75c2012-10-23 10:25:10 +02002305 /* Content-Length is null */
2306 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2307 goto fail;
2308
2309 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002310 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002311 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2312 goto fail;
2313
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002314 /* no compression when Cache-Control: no-transform is present in the message */
2315 ctx.idx = 0;
2316 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2317 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2318 goto fail;
2319 }
2320
William Lallemand82fe75c2012-10-23 10:25:10 +02002321 comp_type = NULL;
2322
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002323 /* we don't want to compress multipart content-types, nor content-types that are
2324 * not listed in the "compression type" directive if any. If no content-type was
2325 * found but configuration requires one, we don't compress either. Backend has
2326 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002327 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002328 ctx.idx = 0;
2329 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2330 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2331 goto fail;
2332
2333 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2334 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002335 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002336 if (ctx.vlen >= comp_type->name_len &&
2337 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002338 /* this Content-Type should be compressed */
2339 break;
2340 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002341 /* this Content-Type should not be compressed */
2342 if (comp_type == NULL)
2343 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002344 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002345 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002346 else { /* no content-type header */
2347 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2348 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002349 }
2350
William Lallemandd85f9172012-11-09 17:05:39 +01002351 /* limit compression rate */
2352 if (global.comp_rate_lim > 0)
2353 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2354 goto fail;
2355
William Lallemand072a2bf2012-11-20 17:01:01 +01002356 /* limit cpu usage */
2357 if (idle_pct < compress_min_idle)
2358 goto fail;
2359
William Lallemand4c49fae2012-11-07 15:00:23 +01002360 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002361 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002362 goto fail;
2363
William Lallemandec3e3892012-11-12 17:02:18 +01002364 s->flags |= SN_COMP_READY;
2365
William Lallemand82fe75c2012-10-23 10:25:10 +02002366 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002367 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002368 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2369 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2370
2371 /* add Transfer-Encoding header */
2372 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2373 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2374
2375 /*
2376 * Add Content-Encoding header when it's not identity encoding.
2377 * RFC 2616 : Identity encoding: This content-coding is used only in the
2378 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2379 * header.
2380 */
2381 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002382 trash.len = 18;
2383 memcpy(trash.str, "Content-Encoding: ", trash.len);
2384 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2385 trash.len += s->comp_algo->name_len;
2386 trash.str[trash.len] = '\0';
2387 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002388 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002389 return 1;
2390
2391fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002392 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002393 return 0;
2394}
2395
2396
Willy Tarreaud787e662009-07-07 10:14:51 +02002397/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2398 * processing can continue on next analysers, or zero if it either needs more
2399 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2400 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2401 * when it has nothing left to do, and may remove any analyser when it wants to
2402 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002403 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002404int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002405{
Willy Tarreau59234e92008-11-30 23:51:27 +01002406 /*
2407 * We will parse the partial (or complete) lines.
2408 * We will check the request syntax, and also join multi-line
2409 * headers. An index of all the lines will be elaborated while
2410 * parsing.
2411 *
2412 * For the parsing, we use a 28 states FSM.
2413 *
2414 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002415 * req->buf->p = beginning of request
2416 * req->buf->p + msg->eoh = end of processed headers / start of current one
2417 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002418 * msg->eol = end of current header or line (LF or CRLF)
2419 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002420 *
2421 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002422 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002423 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2424 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002425 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002426
Willy Tarreau59234e92008-11-30 23:51:27 +01002427 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002428 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002429 struct http_txn *txn = &s->txn;
2430 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002431 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002432
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002433 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002434 now_ms, __FUNCTION__,
2435 s,
2436 req,
2437 req->rex, req->wex,
2438 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002439 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002440 req->analysers);
2441
Willy Tarreau52a0c602009-08-16 22:45:38 +02002442 /* we're speaking HTTP here, so let's speak HTTP to the client */
2443 s->srv_error = http_return_srv_error;
2444
Willy Tarreau83e3af02009-12-28 17:39:57 +01002445 /* There's a protected area at the end of the buffer for rewriting
2446 * purposes. We don't want to start to parse the request if the
2447 * protected area is affected, because we may have to move processed
2448 * data later, which is much more complicated.
2449 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002450 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002451 if (txn->flags & TX_NOT_FIRST) {
2452 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002453 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002454 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002455 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002456 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002457 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002458 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002459 return 0;
2460 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002461 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2462 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2463 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002464 }
2465
Willy Tarreau065e8332010-01-08 00:30:20 +01002466 /* Note that we have the same problem with the response ; we
2467 * may want to send a redirect, error or anything which requires
2468 * some spare space. So we'll ensure that we have at least
2469 * maxrewrite bytes available in the response buffer before
2470 * processing that one. This will only affect pipelined
2471 * keep-alive requests.
2472 */
2473 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002474 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002475 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2476 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2477 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002478 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002479 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002480 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002481 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002482 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002483 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002484 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002485 return 0;
2486 }
2487 }
2488
Willy Tarreau9b28e032012-10-12 23:49:43 +02002489 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002490 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002491 }
2492
Willy Tarreau59234e92008-11-30 23:51:27 +01002493 /* 1: we might have to print this header in debug mode */
2494 if (unlikely((global.mode & MODE_DEBUG) &&
2495 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002496 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002497 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002498
Willy Tarreau9b28e032012-10-12 23:49:43 +02002499 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002500 /* this is a bit complex : in case of error on the request line,
2501 * we know that rq.l is still zero, so we display only the part
2502 * up to the end of the line (truncated by debug_hdr).
2503 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002504 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002505 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002506
Willy Tarreau59234e92008-11-30 23:51:27 +01002507 sol += hdr_idx_first_pos(&txn->hdr_idx);
2508 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002509
Willy Tarreau59234e92008-11-30 23:51:27 +01002510 while (cur_idx) {
2511 eol = sol + txn->hdr_idx.v[cur_idx].len;
2512 debug_hdr("clihdr", s, sol, eol);
2513 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2514 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002515 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002516 }
2517
Willy Tarreau58f10d72006-12-04 02:26:12 +01002518
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 /*
2520 * Now we quickly check if we have found a full valid request.
2521 * If not so, we check the FD and buffer states before leaving.
2522 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002523 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002524 * requests are checked first. When waiting for a second request
2525 * on a keep-alive session, if we encounter and error, close, t/o,
2526 * we note the error in the session flags but don't set any state.
2527 * Since the error will be noted there, it will not be counted by
2528 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002529 * Last, we may increase some tracked counters' http request errors on
2530 * the cases that are deliberately the client's fault. For instance,
2531 * a timeout or connection reset is not counted as an error. However
2532 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002533 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002534
Willy Tarreau655dce92009-11-08 13:10:58 +01002535 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002536 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002537 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002538 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002539 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002540 session_inc_http_req_ctr(s);
2541 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002542 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002543 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002544 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002545
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 /* 1: Since we are in header mode, if there's no space
2547 * left for headers, we won't be able to free more
2548 * later, so the session will never terminate. We
2549 * must terminate it now.
2550 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002551 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002552 /* FIXME: check if URI is set and return Status
2553 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002554 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002555 session_inc_http_req_ctr(s);
2556 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002557 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002558 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002559 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002560 goto return_bad_req;
2561 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002562
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002564 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002565 if (!(s->flags & SN_ERR_MASK))
2566 s->flags |= SN_ERR_CLICL;
2567
Willy Tarreaufcffa692010-01-10 14:21:19 +01002568 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002569 goto failed_keep_alive;
2570
Willy Tarreau59234e92008-11-30 23:51:27 +01002571 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002572 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002573 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002574 session_inc_http_err_ctr(s);
2575 }
2576
Willy Tarreaudc979f22012-12-04 10:39:01 +01002577 txn->status = 400;
2578 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002579 msg->msg_state = HTTP_MSG_ERROR;
2580 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002581
Willy Tarreauda7ff642010-06-23 11:44:09 +02002582 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002583 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002584 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002585 if (s->listener->counters)
2586 s->listener->counters->failed_req++;
2587
Willy Tarreau59234e92008-11-30 23:51:27 +01002588 if (!(s->flags & SN_FINST_MASK))
2589 s->flags |= SN_FINST_R;
2590 return 0;
2591 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002592
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002594 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002595 if (!(s->flags & SN_ERR_MASK))
2596 s->flags |= SN_ERR_CLITO;
2597
Willy Tarreaufcffa692010-01-10 14:21:19 +01002598 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002599 goto failed_keep_alive;
2600
Willy Tarreau59234e92008-11-30 23:51:27 +01002601 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002602 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002603 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002604 session_inc_http_err_ctr(s);
2605 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002606 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002607 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002608 msg->msg_state = HTTP_MSG_ERROR;
2609 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002610
Willy Tarreauda7ff642010-06-23 11:44:09 +02002611 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002612 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002613 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002614 if (s->listener->counters)
2615 s->listener->counters->failed_req++;
2616
Willy Tarreau59234e92008-11-30 23:51:27 +01002617 if (!(s->flags & SN_FINST_MASK))
2618 s->flags |= SN_FINST_R;
2619 return 0;
2620 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002621
Willy Tarreau59234e92008-11-30 23:51:27 +01002622 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002623 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002624 if (!(s->flags & SN_ERR_MASK))
2625 s->flags |= SN_ERR_CLICL;
2626
Willy Tarreaufcffa692010-01-10 14:21:19 +01002627 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002628 goto failed_keep_alive;
2629
Willy Tarreau4076a152009-04-02 15:18:36 +02002630 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002631 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002633 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002634 msg->msg_state = HTTP_MSG_ERROR;
2635 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002636
Willy Tarreauda7ff642010-06-23 11:44:09 +02002637 session_inc_http_err_ctr(s);
2638 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002639 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002640 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002641 if (s->listener->counters)
2642 s->listener->counters->failed_req++;
2643
Willy Tarreau59234e92008-11-30 23:51:27 +01002644 if (!(s->flags & SN_FINST_MASK))
2645 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002646 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002647 }
2648
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002649 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002650 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2651 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002652#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002653 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002654 /* We need more data, we have to re-enable quick-ack in case we
2655 * previously disabled it, otherwise we might cause the client
2656 * to delay next data.
2657 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002658 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002659 }
2660#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002661
Willy Tarreaufcffa692010-01-10 14:21:19 +01002662 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2663 /* If the client starts to talk, let's fall back to
2664 * request timeout processing.
2665 */
2666 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002667 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002668 }
2669
Willy Tarreau59234e92008-11-30 23:51:27 +01002670 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002671 if (!tick_isset(req->analyse_exp)) {
2672 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2673 (txn->flags & TX_WAIT_NEXT_RQ) &&
2674 tick_isset(s->be->timeout.httpka))
2675 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2676 else
2677 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2678 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002679
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 /* we're not ready yet */
2681 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002682
2683 failed_keep_alive:
2684 /* Here we process low-level errors for keep-alive requests. In
2685 * short, if the request is not the first one and it experiences
2686 * a timeout, read error or shutdown, we just silently close so
2687 * that the client can try again.
2688 */
2689 txn->status = 0;
2690 msg->msg_state = HTTP_MSG_RQBEFORE;
2691 req->analysers = 0;
2692 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002693 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002694 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002695 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002696 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002697 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002698
Willy Tarreaud787e662009-07-07 10:14:51 +02002699 /* OK now we have a complete HTTP request with indexed headers. Let's
2700 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002701 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002702 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002703 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002704 * byte after the last LF. msg->sov points to the first byte of data.
2705 * msg->eol cannot be trusted because it may have been left uninitialized
2706 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002707 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002708
Willy Tarreauda7ff642010-06-23 11:44:09 +02002709 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002710 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2711
Willy Tarreaub16a5742010-01-10 14:46:16 +01002712 if (txn->flags & TX_WAIT_NEXT_RQ) {
2713 /* kill the pending keep-alive timeout */
2714 txn->flags &= ~TX_WAIT_NEXT_RQ;
2715 req->analyse_exp = TICK_ETERNITY;
2716 }
2717
2718
Willy Tarreaud787e662009-07-07 10:14:51 +02002719 /* Maybe we found in invalid header name while we were configured not
2720 * to block on that, so we have to capture it now.
2721 */
2722 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002723 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002724
Willy Tarreau59234e92008-11-30 23:51:27 +01002725 /*
2726 * 1: identify the method
2727 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002728 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002729
2730 /* we can make use of server redirect on GET and HEAD */
2731 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2732 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002733
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 /*
2735 * 2: check if the URI matches the monitor_uri.
2736 * We have to do this for every request which gets in, because
2737 * the monitor-uri is defined by the frontend.
2738 */
2739 if (unlikely((s->fe->monitor_uri_len != 0) &&
2740 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002741 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 s->fe->monitor_uri,
2743 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002744 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002745 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002746 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002747 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002748
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002750 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002751
Willy Tarreau59234e92008-11-30 23:51:27 +01002752 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002753 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002754 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002755
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 ret = acl_pass(ret);
2757 if (cond->pol == ACL_COND_UNLESS)
2758 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002759
Willy Tarreau59234e92008-11-30 23:51:27 +01002760 if (ret) {
2761 /* we fail this request, let's return 503 service unavail */
2762 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002763 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002764 if (!(s->flags & SN_ERR_MASK))
2765 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002766 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002767 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002768 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002769
Willy Tarreau59234e92008-11-30 23:51:27 +01002770 /* nothing to fail, let's reply normaly */
2771 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002772 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002773 if (!(s->flags & SN_ERR_MASK))
2774 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 goto return_prx_cond;
2776 }
2777
2778 /*
2779 * 3: Maybe we have to copy the original REQURI for the logs ?
2780 * Note: we cannot log anymore if the request has been
2781 * classified as invalid.
2782 */
2783 if (unlikely(s->logs.logwait & LW_REQ)) {
2784 /* we have a complete HTTP request that we must log */
2785 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2786 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002787
Willy Tarreau59234e92008-11-30 23:51:27 +01002788 if (urilen >= REQURI_LEN)
2789 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002790 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002791 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002792
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002793 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002794 s->do_log(s);
2795 } else {
2796 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002797 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002798 }
Willy Tarreau06619262006-12-17 08:37:22 +01002799
Willy Tarreau59234e92008-11-30 23:51:27 +01002800 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002801 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002802 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002803
Willy Tarreau5b154472009-12-21 20:11:07 +01002804 /* ... and check if the request is HTTP/1.1 or above */
2805 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002806 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2807 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2808 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002809 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002810
2811 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002812 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 +01002813
Willy Tarreau88d349d2010-01-25 12:15:43 +01002814 /* if the frontend has "option http-use-proxy-header", we'll check if
2815 * we have what looks like a proxied connection instead of a connection,
2816 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2817 * Note that this is *not* RFC-compliant, however browsers and proxies
2818 * happen to do that despite being non-standard :-(
2819 * We consider that a request not beginning with either '/' or '*' is
2820 * a proxied connection, which covers both "scheme://location" and
2821 * CONNECT ip:port.
2822 */
2823 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002824 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002825 txn->flags |= TX_USE_PX_CONN;
2826
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002827 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002828 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002829
Willy Tarreau59234e92008-11-30 23:51:27 +01002830 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002831 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002832 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002833 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002834
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002835 /* 6: determine the transfer-length.
2836 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2837 * the presence of a message-body in a REQUEST and its transfer length
2838 * must be determined that way (in order of precedence) :
2839 * 1. The presence of a message-body in a request is signaled by the
2840 * inclusion of a Content-Length or Transfer-Encoding header field
2841 * in the request's header fields. When a request message contains
2842 * both a message-body of non-zero length and a method that does
2843 * not define any semantics for that request message-body, then an
2844 * origin server SHOULD either ignore the message-body or respond
2845 * with an appropriate error message (e.g., 413). A proxy or
2846 * gateway, when presented the same request, SHOULD either forward
2847 * the request inbound with the message- body or ignore the
2848 * message-body when determining a response.
2849 *
2850 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2851 * and the "chunked" transfer-coding (Section 6.2) is used, the
2852 * transfer-length is defined by the use of this transfer-coding.
2853 * If a Transfer-Encoding header field is present and the "chunked"
2854 * transfer-coding is not present, the transfer-length is defined
2855 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002856 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002857 * 3. If a Content-Length header field is present, its decimal value in
2858 * OCTETs represents both the entity-length and the transfer-length.
2859 * If a message is received with both a Transfer-Encoding header
2860 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002861 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002862 * 4. By the server closing the connection. (Closing the connection
2863 * cannot be used to indicate the end of a request body, since that
2864 * would leave no possibility for the server to send back a response.)
2865 *
2866 * Whenever a transfer-coding is applied to a message-body, the set of
2867 * transfer-codings MUST include "chunked", unless the message indicates
2868 * it is terminated by closing the connection. When the "chunked"
2869 * transfer-coding is used, it MUST be the last transfer-coding applied
2870 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002871 */
2872
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002873 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002874 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002875 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002876 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002877 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002878 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002879 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2880 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002881 /* bad transfer-encoding (chunked followed by something else) */
2882 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002883 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002884 break;
2885 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002886 }
2887
Willy Tarreau32b47f42009-10-18 20:55:02 +02002888 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002889 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002890 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002891 signed long long cl;
2892
Willy Tarreauad14f752011-09-02 20:33:27 +02002893 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002894 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002895 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002896 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002897
Willy Tarreauad14f752011-09-02 20:33:27 +02002898 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002899 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002900 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002901 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002902
Willy Tarreauad14f752011-09-02 20:33:27 +02002903 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002904 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002905 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002906 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002907
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002908 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002909 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002910 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002911 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002912
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002913 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002914 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002915 }
2916
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002917 /* bodyless requests have a known length */
2918 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002919 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002920
Willy Tarreau179085c2014-04-28 16:48:56 +02002921 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2922 * only change if both the request and the config reference something else.
2923 * Option httpclose by itself sets tunnel mode where headers are mangled.
2924 * However, if another mode is set, it will affect it (eg: server-close/
2925 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2926 * if FE and BE have the same settings (common). The method consists in
2927 * checking if options changed between the two calls (implying that either
2928 * one is non-null, or one of them is non-null and we are there for the first
2929 * time.
2930 */
2931 if (!(txn->flags & TX_HDR_CONN_PRS) ||
2932 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
2933 int tmp = TX_CON_WANT_KAL;
2934
2935 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2936 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2937 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2938 tmp = TX_CON_WANT_TUN;
2939
2940 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2941 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2942 tmp = TX_CON_WANT_TUN;
2943 }
2944
2945 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2946 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2947 /* option httpclose + server_close => forceclose */
2948 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2949 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2950 tmp = TX_CON_WANT_CLO;
2951 else
2952 tmp = TX_CON_WANT_SCL;
2953 }
2954
2955 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2956 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2957 tmp = TX_CON_WANT_CLO;
2958
2959 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2960 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2961
2962 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2963 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2964 /* parse the Connection header and possibly clean it */
2965 int to_del = 0;
2966 if ((msg->flags & HTTP_MSGF_VER_11) ||
2967 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2968 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2969 to_del |= 2; /* remove "keep-alive" */
2970 if (!(msg->flags & HTTP_MSGF_VER_11))
2971 to_del |= 1; /* remove "close" */
2972 http_parse_connection_header(txn, msg, to_del);
2973 }
2974
2975 /* check if client or config asks for explicit close in KAL/SCL */
2976 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2977 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2978 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2979 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2980 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2981 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2982 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2983 }
2984
Willy Tarreaud787e662009-07-07 10:14:51 +02002985 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002986 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002987 req->analyse_exp = TICK_ETERNITY;
2988 return 1;
2989
2990 return_bad_req:
2991 /* We centralize bad requests processing here */
2992 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2993 /* we detected a parsing error. We want to archive this request
2994 * in the dedicated proxy area for later troubleshooting.
2995 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002996 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002997 }
2998
2999 txn->req.msg_state = HTTP_MSG_ERROR;
3000 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003001 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003002
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003003 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003004 if (s->listener->counters)
3005 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003006
3007 return_prx_cond:
3008 if (!(s->flags & SN_ERR_MASK))
3009 s->flags |= SN_ERR_PRXCOND;
3010 if (!(s->flags & SN_FINST_MASK))
3011 s->flags |= SN_FINST_R;
3012
3013 req->analysers = 0;
3014 req->analyse_exp = TICK_ETERNITY;
3015 return 0;
3016}
3017
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003018
Willy Tarreau347a35d2013-11-22 17:51:09 +01003019/* This function prepares an applet to handle the stats. It can deal with the
3020 * "100-continue" expectation, check that admin rules are met for POST requests,
3021 * and program a response message if something was unexpected. It cannot fail
3022 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003023 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003024 * s->target which is supposed to already point to the stats applet. The caller
3025 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003026 */
3027int http_handle_stats(struct session *s, struct channel *req)
3028{
3029 struct stats_admin_rule *stats_admin_rule;
3030 struct stream_interface *si = s->rep->prod;
3031 struct http_txn *txn = &s->txn;
3032 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003033 struct uri_auth *uri_auth = s->be->uri_auth;
3034 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003035 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003036
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003037 appctx = si_appctx(si);
3038 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3039 appctx->st1 = appctx->st2 = 0;
3040 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3041 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003042 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3043 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003044
3045 uri = msg->chn->buf->p + msg->sl.rq.u;
3046 lookup = uri + uri_auth->uri_len;
3047
3048 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3049 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003050 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003051 break;
3052 }
3053 }
3054
3055 if (uri_auth->refresh) {
3056 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3057 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003058 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003059 break;
3060 }
3061 }
3062 }
3063
3064 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3065 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003066 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003067 break;
3068 }
3069 }
3070
3071 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3072 if (memcmp(h, ";st=", 4) == 0) {
3073 int i;
3074 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003075 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003076 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3077 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003078 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003079 break;
3080 }
3081 }
3082 break;
3083 }
3084 }
3085
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003086 appctx->ctx.stats.scope_str = 0;
3087 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003088 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3089 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3090 int itx = 0;
3091 const char *h2;
3092 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3093 const char *err;
3094
3095 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3096 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003097 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003098 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3099 itx++;
3100 h++;
3101 }
3102
3103 if (itx > STAT_SCOPE_TXT_MAXLEN)
3104 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003105 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003106
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003107 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003108 memcpy(scope_txt, h2, itx);
3109 scope_txt[itx] = '\0';
3110 err = invalid_char(scope_txt);
3111 if (err) {
3112 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003113 appctx->ctx.stats.scope_str = 0;
3114 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003115 }
3116 break;
3117 }
3118 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003119
3120 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003121 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003122 int ret = 1;
3123
3124 if (stats_admin_rule->cond) {
3125 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3126 ret = acl_pass(ret);
3127 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3128 ret = !ret;
3129 }
3130
3131 if (ret) {
3132 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003133 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003134 break;
3135 }
3136 }
3137
3138 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003139 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003140 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003141 /* we'll need the request body, possibly after sending 100-continue */
3142 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003143 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003144 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003145 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003146 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3147 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003148 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003149 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003150 else {
3151 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003152 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003153 }
3154
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003155 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003156 return 1;
3157}
3158
Lukas Tribus67db8df2013-06-23 17:37:13 +02003159/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3160 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3161 */
3162static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3163{
3164#ifdef IP_TOS
3165 if (from.ss_family == AF_INET)
3166 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3167#endif
3168#ifdef IPV6_TCLASS
3169 if (from.ss_family == AF_INET6) {
3170 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3171 /* v4-mapped addresses need IP_TOS */
3172 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3173 else
3174 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3175 }
3176#endif
3177}
3178
Willy Tarreau20b0de52012-12-24 15:45:22 +01003179/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003180 * transaction <txn>. Returns the verdict of the first rule that prevents
3181 * further processing of the request (auth, deny, ...), and defaults to
3182 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3183 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3184 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003185 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003186enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003187http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003188{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003189 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003190 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003191 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003192 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003193
Willy Tarreauff011f22011-01-06 17:51:27 +01003194 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003195 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003196 continue;
3197
Willy Tarreau96257ec2012-12-27 10:46:37 +01003198 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003199 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003200 int ret;
3201
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003202 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003203 ret = acl_pass(ret);
3204
Willy Tarreauff011f22011-01-06 17:51:27 +01003205 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003206 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003207
3208 if (!ret) /* condition not matched */
3209 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003210 }
3211
Willy Tarreau20b0de52012-12-24 15:45:22 +01003212
Willy Tarreau96257ec2012-12-27 10:46:37 +01003213 switch (rule->action) {
3214 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003215 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003216
3217 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003218 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003219
Willy Tarreauccbcc372012-12-27 12:37:57 +01003220 case HTTP_REQ_ACT_TARPIT:
3221 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003222 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003223
Willy Tarreau96257ec2012-12-27 10:46:37 +01003224 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003225 /* Auth might be performed on regular http-req rules as well as on stats */
3226 auth_realm = rule->arg.auth.realm;
3227 if (!auth_realm) {
3228 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3229 auth_realm = STATS_DEFAULT_REALM;
3230 else
3231 auth_realm = px->id;
3232 }
3233 /* send 401/407 depending on whether we use a proxy or not. We still
3234 * count one error, because normal browsing won't significantly
3235 * increase the counter but brute force attempts will.
3236 */
3237 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3238 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3239 stream_int_retnclose(&s->si[0], &trash);
3240 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003241 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003242
Willy Tarreau81499eb2012-12-27 12:19:02 +01003243 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003244 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3245 return HTTP_RULE_RES_BADREQ;
3246 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003247
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003248 case HTTP_REQ_ACT_SET_NICE:
3249 s->task->nice = rule->arg.nice;
3250 break;
3251
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003252 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003253 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003254 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003255 break;
3256
Willy Tarreau51347ed2013-06-11 19:34:13 +02003257 case HTTP_REQ_ACT_SET_MARK:
3258#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003259 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003260 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003261#endif
3262 break;
3263
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003264 case HTTP_REQ_ACT_SET_LOGL:
3265 s->logs.level = rule->arg.loglevel;
3266 break;
3267
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003268 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003269 case HTTP_REQ_ACT_SET_HDR:
3270 ctx.idx = 0;
3271 /* remove all occurrences of the header */
3272 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3273 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3274 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003275 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003276 if (rule->action == HTTP_REQ_ACT_DEL_HDR)
3277 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003278 /* now fall through to header addition */
3279
3280 case HTTP_REQ_ACT_ADD_HDR:
3281 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3282 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3283 trash.len = rule->arg.hdr_add.name_len;
3284 trash.str[trash.len++] = ':';
3285 trash.str[trash.len++] = ' ';
3286 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3287 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3288 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003289
3290 case HTTP_REQ_ACT_DEL_ACL:
3291 case HTTP_REQ_ACT_DEL_MAP: {
3292 struct pat_ref *ref;
3293 char *key;
3294 int len;
3295
3296 /* collect reference */
3297 ref = pat_ref_lookup(rule->arg.map.ref);
3298 if (!ref)
3299 continue;
3300
3301 /* collect key */
3302 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3303 key = trash.str;
3304 key[len] = '\0';
3305
3306 /* perform update */
3307 /* returned code: 1=ok, 0=ko */
3308 pat_ref_delete(ref, key);
3309
3310 break;
3311 }
3312
3313 case HTTP_REQ_ACT_ADD_ACL: {
3314 struct pat_ref *ref;
3315 char *key;
3316 struct chunk *trash_key;
3317 int len;
3318
3319 trash_key = get_trash_chunk();
3320
3321 /* collect reference */
3322 ref = pat_ref_lookup(rule->arg.map.ref);
3323 if (!ref)
3324 continue;
3325
3326 /* collect key */
3327 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3328 key = trash_key->str;
3329 key[len] = '\0';
3330
3331 /* perform update */
3332 /* add entry only if it does not already exist */
3333 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003334 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003335
3336 break;
3337 }
3338
3339 case HTTP_REQ_ACT_SET_MAP: {
3340 struct pat_ref *ref;
3341 char *key, *value;
3342 struct chunk *trash_key, *trash_value;
3343 int len;
3344
3345 trash_key = get_trash_chunk();
3346 trash_value = get_trash_chunk();
3347
3348 /* collect reference */
3349 ref = pat_ref_lookup(rule->arg.map.ref);
3350 if (!ref)
3351 continue;
3352
3353 /* collect key */
3354 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3355 key = trash_key->str;
3356 key[len] = '\0';
3357
3358 /* collect value */
3359 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3360 value = trash_value->str;
3361 value[len] = '\0';
3362
3363 /* perform update */
3364 if (pat_ref_find_elt(ref, key) != NULL)
3365 /* update entry if it exists */
3366 pat_ref_set(ref, key, value, NULL);
3367 else
3368 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003369 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003370
3371 break;
3372 }
William Lallemand73025dd2014-04-24 14:38:37 +02003373
3374 case HTTP_REQ_ACT_CUSTOM_CONT:
3375 rule->action_ptr(rule, px, s, txn);
3376 break;
3377
3378 case HTTP_REQ_ACT_CUSTOM_STOP:
3379 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003380 return HTTP_RULE_RES_DONE;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003381 }
3382 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003383
3384 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003385 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003386}
3387
Willy Tarreau71241ab2012-12-27 11:30:54 +01003388
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003389/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3390 * transaction <txn>. Returns the first rule that prevents further processing
3391 * of the response (deny, ...) or NULL if it executed all rules or stopped
3392 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3393 * rule.
3394 */
3395static struct http_res_rule *
3396http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3397{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003398 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003399 struct http_res_rule *rule;
3400 struct hdr_ctx ctx;
3401
3402 list_for_each_entry(rule, rules, list) {
3403 if (rule->action >= HTTP_RES_ACT_MAX)
3404 continue;
3405
3406 /* check optional condition */
3407 if (rule->cond) {
3408 int ret;
3409
3410 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3411 ret = acl_pass(ret);
3412
3413 if (rule->cond->pol == ACL_COND_UNLESS)
3414 ret = !ret;
3415
3416 if (!ret) /* condition not matched */
3417 continue;
3418 }
3419
3420
3421 switch (rule->action) {
3422 case HTTP_RES_ACT_ALLOW:
3423 return NULL; /* "allow" rules are OK */
3424
3425 case HTTP_RES_ACT_DENY:
3426 txn->flags |= TX_SVDENY;
3427 return rule;
3428
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003429 case HTTP_RES_ACT_SET_NICE:
3430 s->task->nice = rule->arg.nice;
3431 break;
3432
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003433 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003434 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003435 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003436 break;
3437
Willy Tarreau51347ed2013-06-11 19:34:13 +02003438 case HTTP_RES_ACT_SET_MARK:
3439#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003440 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003441 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003442#endif
3443 break;
3444
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003445 case HTTP_RES_ACT_SET_LOGL:
3446 s->logs.level = rule->arg.loglevel;
3447 break;
3448
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003449 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003450 case HTTP_RES_ACT_SET_HDR:
3451 ctx.idx = 0;
3452 /* remove all occurrences of the header */
3453 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3454 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3455 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3456 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003457 if (rule->action == HTTP_RES_ACT_DEL_HDR)
3458 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003459 /* now fall through to header addition */
3460
3461 case HTTP_RES_ACT_ADD_HDR:
3462 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3463 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3464 trash.len = rule->arg.hdr_add.name_len;
3465 trash.str[trash.len++] = ':';
3466 trash.str[trash.len++] = ' ';
3467 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3468 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3469 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003470
3471 case HTTP_RES_ACT_DEL_ACL:
3472 case HTTP_RES_ACT_DEL_MAP: {
3473 struct pat_ref *ref;
3474 char *key;
3475 int len;
3476
3477 /* collect reference */
3478 ref = pat_ref_lookup(rule->arg.map.ref);
3479 if (!ref)
3480 continue;
3481
3482 /* collect key */
3483 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3484 key = trash.str;
3485 key[len] = '\0';
3486
3487 /* perform update */
3488 /* returned code: 1=ok, 0=ko */
3489 pat_ref_delete(ref, key);
3490
3491 break;
3492 }
3493
3494 case HTTP_RES_ACT_ADD_ACL: {
3495 struct pat_ref *ref;
3496 char *key;
3497 struct chunk *trash_key;
3498 int len;
3499
3500 trash_key = get_trash_chunk();
3501
3502 /* collect reference */
3503 ref = pat_ref_lookup(rule->arg.map.ref);
3504 if (!ref)
3505 continue;
3506
3507 /* collect key */
3508 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3509 key = trash_key->str;
3510 key[len] = '\0';
3511
3512 /* perform update */
3513 /* check if the entry already exists */
3514 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003515 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003516
3517 break;
3518 }
3519
3520 case HTTP_RES_ACT_SET_MAP: {
3521 struct pat_ref *ref;
3522 char *key, *value;
3523 struct chunk *trash_key, *trash_value;
3524 int len;
3525
3526 trash_key = get_trash_chunk();
3527 trash_value = get_trash_chunk();
3528
3529 /* collect reference */
3530 ref = pat_ref_lookup(rule->arg.map.ref);
3531 if (!ref)
3532 continue;
3533
3534 /* collect key */
3535 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3536 key = trash_key->str;
3537 key[len] = '\0';
3538
3539 /* collect value */
3540 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3541 value = trash_value->str;
3542 value[len] = '\0';
3543
3544 /* perform update */
3545 if (pat_ref_find_elt(ref, key) != NULL)
3546 /* update entry if it exists */
3547 pat_ref_set(ref, key, value, NULL);
3548 else
3549 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003550 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003551
3552 break;
3553 }
William Lallemand73025dd2014-04-24 14:38:37 +02003554
3555 case HTTP_RES_ACT_CUSTOM_CONT:
3556 rule->action_ptr(rule, px, s, txn);
3557 break;
3558
3559 case HTTP_RES_ACT_CUSTOM_STOP:
3560 rule->action_ptr(rule, px, s, txn);
3561 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003562 }
3563 }
3564
3565 /* we reached the end of the rules, nothing to report */
3566 return NULL;
3567}
3568
3569
Willy Tarreau71241ab2012-12-27 11:30:54 +01003570/* Perform an HTTP redirect based on the information in <rule>. The function
3571 * returns non-zero on success, or zero in case of a, irrecoverable error such
3572 * as too large a request to build a valid response.
3573 */
3574static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3575{
3576 struct http_msg *msg = &txn->req;
3577 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003578 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003579
3580 /* build redirect message */
3581 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003582 case 308:
3583 msg_fmt = HTTP_308;
3584 break;
3585 case 307:
3586 msg_fmt = HTTP_307;
3587 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003588 case 303:
3589 msg_fmt = HTTP_303;
3590 break;
3591 case 301:
3592 msg_fmt = HTTP_301;
3593 break;
3594 case 302:
3595 default:
3596 msg_fmt = HTTP_302;
3597 break;
3598 }
3599
3600 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3601 return 0;
3602
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003603 location = trash.str + trash.len;
3604
Willy Tarreau71241ab2012-12-27 11:30:54 +01003605 switch(rule->type) {
3606 case REDIRECT_TYPE_SCHEME: {
3607 const char *path;
3608 const char *host;
3609 struct hdr_ctx ctx;
3610 int pathlen;
3611 int hostlen;
3612
3613 host = "";
3614 hostlen = 0;
3615 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003616 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003617 host = ctx.line + ctx.val;
3618 hostlen = ctx.vlen;
3619 }
3620
3621 path = http_get_path(txn);
3622 /* build message using path */
3623 if (path) {
3624 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3625 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3626 int qs = 0;
3627 while (qs < pathlen) {
3628 if (path[qs] == '?') {
3629 pathlen = qs;
3630 break;
3631 }
3632 qs++;
3633 }
3634 }
3635 } else {
3636 path = "/";
3637 pathlen = 1;
3638 }
3639
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003640 if (rule->rdr_str) { /* this is an old "redirect" rule */
3641 /* check if we can add scheme + "://" + host + path */
3642 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3643 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003644
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003645 /* add scheme */
3646 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3647 trash.len += rule->rdr_len;
3648 }
3649 else {
3650 /* add scheme with executing log format */
3651 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003652
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003653 /* check if we can add scheme + "://" + host + path */
3654 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3655 return 0;
3656 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003657 /* add "://" */
3658 memcpy(trash.str + trash.len, "://", 3);
3659 trash.len += 3;
3660
3661 /* add host */
3662 memcpy(trash.str + trash.len, host, hostlen);
3663 trash.len += hostlen;
3664
3665 /* add path */
3666 memcpy(trash.str + trash.len, path, pathlen);
3667 trash.len += pathlen;
3668
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003669 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003670 if (trash.len && trash.str[trash.len - 1] != '/' &&
3671 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3672 if (trash.len > trash.size - 5)
3673 return 0;
3674 trash.str[trash.len] = '/';
3675 trash.len++;
3676 }
3677
3678 break;
3679 }
3680 case REDIRECT_TYPE_PREFIX: {
3681 const char *path;
3682 int pathlen;
3683
3684 path = http_get_path(txn);
3685 /* build message using path */
3686 if (path) {
3687 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3688 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3689 int qs = 0;
3690 while (qs < pathlen) {
3691 if (path[qs] == '?') {
3692 pathlen = qs;
3693 break;
3694 }
3695 qs++;
3696 }
3697 }
3698 } else {
3699 path = "/";
3700 pathlen = 1;
3701 }
3702
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003703 if (rule->rdr_str) { /* this is an old "redirect" rule */
3704 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3705 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003706
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003707 /* add prefix. Note that if prefix == "/", we don't want to
3708 * add anything, otherwise it makes it hard for the user to
3709 * configure a self-redirection.
3710 */
3711 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3712 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3713 trash.len += rule->rdr_len;
3714 }
3715 }
3716 else {
3717 /* add prefix with executing log format */
3718 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3719
3720 /* Check length */
3721 if (trash.len + pathlen > trash.size - 4)
3722 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003723 }
3724
3725 /* add path */
3726 memcpy(trash.str + trash.len, path, pathlen);
3727 trash.len += pathlen;
3728
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003729 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003730 if (trash.len && trash.str[trash.len - 1] != '/' &&
3731 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3732 if (trash.len > trash.size - 5)
3733 return 0;
3734 trash.str[trash.len] = '/';
3735 trash.len++;
3736 }
3737
3738 break;
3739 }
3740 case REDIRECT_TYPE_LOCATION:
3741 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003742 if (rule->rdr_str) { /* this is an old "redirect" rule */
3743 if (trash.len + rule->rdr_len > trash.size - 4)
3744 return 0;
3745
3746 /* add location */
3747 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3748 trash.len += rule->rdr_len;
3749 }
3750 else {
3751 /* add location with executing log format */
3752 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003753
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003754 /* Check left length */
3755 if (trash.len > trash.size - 4)
3756 return 0;
3757 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003758 break;
3759 }
3760
3761 if (rule->cookie_len) {
3762 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3763 trash.len += 14;
3764 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3765 trash.len += rule->cookie_len;
3766 memcpy(trash.str + trash.len, "\r\n", 2);
3767 trash.len += 2;
3768 }
3769
3770 /* add end of headers and the keep-alive/close status.
3771 * We may choose to set keep-alive if the Location begins
3772 * with a slash, because the client will come back to the
3773 * same server.
3774 */
3775 txn->status = rule->code;
3776 /* let's log the request time */
3777 s->logs.tv_request = now;
3778
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003779 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003780 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3781 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3782 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3783 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3784 /* keep-alive possible */
3785 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3786 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3787 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3788 trash.len += 30;
3789 } else {
3790 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3791 trash.len += 24;
3792 }
3793 }
3794 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3795 trash.len += 4;
3796 bo_inject(txn->rsp.chn, trash.str, trash.len);
3797 /* "eat" the request */
3798 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003799 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003800 msg->sov = 0;
3801 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3802 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3803 txn->req.msg_state = HTTP_MSG_CLOSED;
3804 txn->rsp.msg_state = HTTP_MSG_DONE;
3805 } else {
3806 /* keep-alive not possible */
3807 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3808 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3809 trash.len += 29;
3810 } else {
3811 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3812 trash.len += 23;
3813 }
3814 stream_int_retnclose(txn->req.chn->prod, &trash);
3815 txn->req.chn->analysers = 0;
3816 }
3817
3818 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003819 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003820 if (!(s->flags & SN_FINST_MASK))
3821 s->flags |= SN_FINST_R;
3822
3823 return 1;
3824}
3825
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003826/* This stream analyser runs all HTTP request processing which is common to
3827 * frontends and backends, which means blocking ACLs, filters, connection-close,
3828 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003829 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003830 * either needs more data or wants to immediately abort the request (eg: deny,
3831 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003832 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003833int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003834{
Willy Tarreaud787e662009-07-07 10:14:51 +02003835 struct http_txn *txn = &s->txn;
3836 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003837 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003838 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003839 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02003840
Willy Tarreau655dce92009-11-08 13:10:58 +01003841 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003842 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003843 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003844 return 0;
3845 }
3846
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003847 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02003848 now_ms, __FUNCTION__,
3849 s,
3850 req,
3851 req->rex, req->wex,
3852 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003853 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003854 req->analysers);
3855
Willy Tarreau65410832014-04-28 21:25:43 +02003856 /* just in case we have some per-backend tracking */
3857 session_inc_be_http_req_ctr(s);
3858
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003859 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003860 if (!LIST_ISEMPTY(&px->http_req_rules)) {
3861 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003862
Willy Tarreau0b748332014-04-29 00:13:29 +02003863 switch (verdict) {
3864 case HTTP_RULE_RES_CONT:
3865 case HTTP_RULE_RES_STOP: /* nothing to do */
3866 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003867
Willy Tarreau0b748332014-04-29 00:13:29 +02003868 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3869 if (txn->flags & TX_CLTARPIT)
3870 goto tarpit;
3871 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003872
Willy Tarreau0b748332014-04-29 00:13:29 +02003873 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3874 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003875
Willy Tarreau0b748332014-04-29 00:13:29 +02003876 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003877 goto done;
3878
Willy Tarreau0b748332014-04-29 00:13:29 +02003879 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3880 goto return_bad_req;
3881 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003882 }
3883
Willy Tarreau52542592014-04-28 18:33:26 +02003884 /* OK at this stage, we know that the request was accepted according to
3885 * the http-request rules, we can check for the stats. Note that the
3886 * URI is detected *before* the req* rules in order not to be affected
3887 * by a possible reqrep, while they are processed *after* so that a
3888 * reqdeny can still block them. This clearly needs to change in 1.6!
3889 */
3890 if (stats_check_uri(s->rep->prod, txn, px)) {
3891 s->target = &http_stats_applet.obj_type;
3892 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3893 txn->status = 500;
3894 s->logs.tv_request = now;
3895 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003896
Willy Tarreau52542592014-04-28 18:33:26 +02003897 if (!(s->flags & SN_ERR_MASK))
3898 s->flags |= SN_ERR_RESOURCE;
3899 goto return_prx_cond;
3900 }
3901
3902 /* parse the whole stats request and extract the relevant information */
3903 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02003904 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
3905 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003906
Willy Tarreau0b748332014-04-29 00:13:29 +02003907 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3908 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003909
Willy Tarreau0b748332014-04-29 00:13:29 +02003910 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3911 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003912 }
3913
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003914 /* evaluate the req* rules except reqadd */
3915 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003916 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003917 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003918
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003919 if (txn->flags & TX_CLDENY)
3920 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003921
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003922 if (txn->flags & TX_CLTARPIT)
3923 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003924 }
Willy Tarreau06619262006-12-17 08:37:22 +01003925
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003926 /* add request headers from the rule sets in the same order */
3927 list_for_each_entry(wl, &px->req_add, list) {
3928 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003929 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003930 ret = acl_pass(ret);
3931 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3932 ret = !ret;
3933 if (!ret)
3934 continue;
3935 }
3936
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003937 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003938 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003939 }
3940
Willy Tarreau52542592014-04-28 18:33:26 +02003941
3942 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003943 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003944 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003945 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3946 s->fe->fe_counters.intercepted_req++;
3947
3948 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3949 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3950 if (!(s->flags & SN_FINST_MASK))
3951 s->flags |= SN_FINST_R;
3952
Willy Tarreau70730dd2014-04-24 18:06:27 +02003953 /* we may want to compress the stats page */
3954 if (s->fe->comp || s->be->comp)
3955 select_compression_request_header(s, req->buf);
3956
3957 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003958 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) |
3959 AN_REQ_HTTP_XFER_BODY | AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE | AN_RES_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003960 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003961 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003962
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003963 /* check whether we have some ACLs set to redirect this request */
3964 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003965 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003966 int ret;
3967
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003968 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003969 ret = acl_pass(ret);
3970 if (rule->cond->pol == ACL_COND_UNLESS)
3971 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003972 if (!ret)
3973 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003974 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003975 if (!http_apply_redirect_rule(rule, s, txn))
3976 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003977 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003978 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003979
Willy Tarreau2be39392010-01-03 17:24:51 +01003980 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3981 * If this happens, then the data will not come immediately, so we must
3982 * send all what we have without waiting. Note that due to the small gain
3983 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003984 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003985 * itself once used.
3986 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003987 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003988
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003989 done: /* done with this analyser, continue with next ones that the calling
3990 * points will have set, if any.
3991 */
3992 req->analysers &= ~an_bit;
3993 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003994 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003995
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003996 tarpit:
3997 /* When a connection is tarpitted, we use the tarpit timeout,
3998 * which may be the same as the connect timeout if unspecified.
3999 * If unset, then set it to zero because we really want it to
4000 * eventually expire. We build the tarpit as an analyser.
4001 */
4002 channel_erase(s->req);
4003
4004 /* wipe the request out so that we can drop the connection early
4005 * if the client closes first.
4006 */
4007 channel_dont_connect(req);
4008 req->analysers = 0; /* remove switching rules etc... */
4009 req->analysers |= AN_REQ_HTTP_TARPIT;
4010 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4011 if (!req->analyse_exp)
4012 req->analyse_exp = tick_add(now_ms, 0);
4013 session_inc_http_err_ctr(s);
4014 s->fe->fe_counters.denied_req++;
4015 if (s->fe != s->be)
4016 s->be->be_counters.denied_req++;
4017 if (s->listener->counters)
4018 s->listener->counters->denied_req++;
4019 goto done;
4020
4021 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004022 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004023 txn->status = 403;
4024 s->logs.tv_request = now;
4025 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4026 session_inc_http_err_ctr(s);
4027 s->fe->fe_counters.denied_req++;
4028 if (s->fe != s->be)
4029 s->be->be_counters.denied_req++;
4030 if (s->listener->counters)
4031 s->listener->counters->denied_req++;
4032 goto return_prx_cond;
4033
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004034 return_bad_req:
4035 /* We centralize bad requests processing here */
4036 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4037 /* we detected a parsing error. We want to archive this request
4038 * in the dedicated proxy area for later troubleshooting.
4039 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004040 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004041 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004042
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004043 txn->req.msg_state = HTTP_MSG_ERROR;
4044 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004045 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004046
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004047 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004048 if (s->listener->counters)
4049 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004050
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004051 return_prx_cond:
4052 if (!(s->flags & SN_ERR_MASK))
4053 s->flags |= SN_ERR_PRXCOND;
4054 if (!(s->flags & SN_FINST_MASK))
4055 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004056
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004057 req->analysers = 0;
4058 req->analyse_exp = TICK_ETERNITY;
4059 return 0;
4060}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004061
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004062/* This function performs all the processing enabled for the current request.
4063 * It returns 1 if the processing can continue on next analysers, or zero if it
4064 * needs more data, encounters an error, or wants to immediately abort the
4065 * request. It relies on buffers flags, and updates s->req->analysers.
4066 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004067int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004068{
4069 struct http_txn *txn = &s->txn;
4070 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004071 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004072
Willy Tarreau655dce92009-11-08 13:10:58 +01004073 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004074 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004075 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004076 return 0;
4077 }
4078
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004079 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004080 now_ms, __FUNCTION__,
4081 s,
4082 req,
4083 req->rex, req->wex,
4084 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004085 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004086 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004087
William Lallemand82fe75c2012-10-23 10:25:10 +02004088 if (s->fe->comp || s->be->comp)
4089 select_compression_request_header(s, req->buf);
4090
Willy Tarreau59234e92008-11-30 23:51:27 +01004091 /*
4092 * Right now, we know that we have processed the entire headers
4093 * and that unwanted requests have been filtered out. We can do
4094 * whatever we want with the remaining request. Also, now we
4095 * may have separate values for ->fe, ->be.
4096 */
Willy Tarreau06619262006-12-17 08:37:22 +01004097
Willy Tarreau59234e92008-11-30 23:51:27 +01004098 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004099 * If HTTP PROXY is set we simply get remote server address parsing
4100 * incoming request. Note that this requires that a connection is
4101 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004102 */
4103 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004104 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004105 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004106
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004107 /* Note that for now we don't reuse existing proxy connections */
4108 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004109 txn->req.msg_state = HTTP_MSG_ERROR;
4110 txn->status = 500;
4111 req->analysers = 0;
4112 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4113
4114 if (!(s->flags & SN_ERR_MASK))
4115 s->flags |= SN_ERR_RESOURCE;
4116 if (!(s->flags & SN_FINST_MASK))
4117 s->flags |= SN_FINST_R;
4118
4119 return 0;
4120 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004121
4122 path = http_get_path(txn);
4123 url2sa(req->buf->p + msg->sl.rq.u,
4124 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004125 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004126 /* if the path was found, we have to remove everything between
4127 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4128 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4129 * u_l characters by a single "/".
4130 */
4131 if (path) {
4132 char *cur_ptr = req->buf->p;
4133 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4134 int delta;
4135
4136 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4137 http_msg_move_end(&txn->req, delta);
4138 cur_end += delta;
4139 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4140 goto return_bad_req;
4141 }
4142 else {
4143 char *cur_ptr = req->buf->p;
4144 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4145 int delta;
4146
4147 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4148 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4149 http_msg_move_end(&txn->req, delta);
4150 cur_end += delta;
4151 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4152 goto return_bad_req;
4153 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004154 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004155
Willy Tarreau59234e92008-11-30 23:51:27 +01004156 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004157 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004158 * Note that doing so might move headers in the request, but
4159 * the fields will stay coherent and the URI will not move.
4160 * This should only be performed in the backend.
4161 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004162 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004163 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4164 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004165
Willy Tarreau59234e92008-11-30 23:51:27 +01004166 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004167 * 8: the appsession cookie was looked up very early in 1.2,
4168 * so let's do the same now.
4169 */
4170
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004171 /* It needs to look into the URI unless persistence must be ignored */
4172 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004173 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 +01004174 }
4175
William Lallemanda73203e2012-03-12 12:48:57 +01004176 /* add unique-id if "header-unique-id" is specified */
4177
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004178 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4179 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4180 goto return_bad_req;
4181 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004182 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004183 }
William Lallemanda73203e2012-03-12 12:48:57 +01004184
4185 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004186 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4187 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004188 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004189 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004190 goto return_bad_req;
4191 }
4192
Cyril Bontéb21570a2009-11-29 20:04:48 +01004193 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004194 * 9: add X-Forwarded-For if either the frontend or the backend
4195 * asks for it.
4196 */
4197 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004198 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004199 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004200 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4201 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004202 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004203 /* The header is set to be added only if none is present
4204 * and we found it, so don't do anything.
4205 */
4206 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004207 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004208 /* Add an X-Forwarded-For header unless the source IP is
4209 * in the 'except' network range.
4210 */
4211 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004212 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004213 != s->fe->except_net.s_addr) &&
4214 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004215 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004216 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004217 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004218 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004219 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004220
4221 /* Note: we rely on the backend to get the header name to be used for
4222 * x-forwarded-for, because the header is really meant for the backends.
4223 * However, if the backend did not specify any option, we have to rely
4224 * on the frontend's header name.
4225 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004226 if (s->be->fwdfor_hdr_len) {
4227 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004228 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004229 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004230 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004231 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004232 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004233 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 +01004234
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004235 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004236 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004237 }
4238 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004239 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004240 /* FIXME: for the sake of completeness, we should also support
4241 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004242 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004243 int len;
4244 char pn[INET6_ADDRSTRLEN];
4245 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004246 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004247 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004248
Willy Tarreau59234e92008-11-30 23:51:27 +01004249 /* Note: we rely on the backend to get the header name to be used for
4250 * x-forwarded-for, because the header is really meant for the backends.
4251 * However, if the backend did not specify any option, we have to rely
4252 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004253 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004254 if (s->be->fwdfor_hdr_len) {
4255 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004256 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004257 } else {
4258 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004259 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004260 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004261 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004262
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004263 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004264 goto return_bad_req;
4265 }
4266 }
4267
4268 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004269 * 10: add X-Original-To if either the frontend or the backend
4270 * asks for it.
4271 */
4272 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4273
4274 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004275 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004276 /* Add an X-Original-To header unless the destination IP is
4277 * in the 'except' network range.
4278 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004279 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004280
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004281 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004282 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004283 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004284 != s->fe->except_to.s_addr) &&
4285 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004286 (((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 +02004287 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004288 int len;
4289 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004290 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004291
4292 /* Note: we rely on the backend to get the header name to be used for
4293 * x-original-to, because the header is really meant for the backends.
4294 * However, if the backend did not specify any option, we have to rely
4295 * on the frontend's header name.
4296 */
4297 if (s->be->orgto_hdr_len) {
4298 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004299 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004300 } else {
4301 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004302 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004303 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004304 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 +02004305
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004306 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004307 goto return_bad_req;
4308 }
4309 }
4310 }
4311
Willy Tarreau50fc7772012-11-11 22:19:57 +01004312 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4313 * If an "Upgrade" token is found, the header is left untouched in order not to have
4314 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4315 * "Upgrade" is present in the Connection header.
4316 */
4317 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4318 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004319 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4320 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004321 unsigned int want_flags = 0;
4322
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004323 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004324 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004325 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4326 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004327 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004328 want_flags |= TX_CON_CLO_SET;
4329 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004330 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004331 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4332 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004333 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004334 want_flags |= TX_CON_KAL_SET;
4335 }
4336
4337 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004338 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004339 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004340
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004341
Willy Tarreau522d6c02009-12-06 18:49:18 +01004342 /* If we have no server assigned yet and we're balancing on url_param
4343 * with a POST request, we may be interested in checking the body for
4344 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004345 */
4346 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4347 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004348 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004349 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004350 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004351 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004352
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004353 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004354 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004355#ifdef TCP_QUICKACK
4356 /* We expect some data from the client. Unless we know for sure
4357 * we already have a full request, we have to re-enable quick-ack
4358 * in case we previously disabled it, otherwise we might cause
4359 * the client to delay further data.
4360 */
4361 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004362 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004363 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004364 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004365 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004366#endif
4367 }
Willy Tarreau03945942009-12-22 16:50:27 +01004368
Willy Tarreau59234e92008-11-30 23:51:27 +01004369 /*************************************************************
4370 * OK, that's finished for the headers. We have done what we *
4371 * could. Let's switch to the DATA state. *
4372 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004373 req->analyse_exp = TICK_ETERNITY;
4374 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004375
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004376 /* if the server closes the connection, we want to immediately react
4377 * and close the socket to save packets and syscalls.
4378 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004379 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4380 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004381
Willy Tarreau59234e92008-11-30 23:51:27 +01004382 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004383 /* OK let's go on with the BODY now */
4384 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004385
Willy Tarreau59234e92008-11-30 23:51:27 +01004386 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004387 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004388 /* we detected a parsing error. We want to archive this request
4389 * in the dedicated proxy area for later troubleshooting.
4390 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004391 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004392 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004393
Willy Tarreau59234e92008-11-30 23:51:27 +01004394 txn->req.msg_state = HTTP_MSG_ERROR;
4395 txn->status = 400;
4396 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004397 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004398
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004399 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004400 if (s->listener->counters)
4401 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004402
Willy Tarreau59234e92008-11-30 23:51:27 +01004403 if (!(s->flags & SN_ERR_MASK))
4404 s->flags |= SN_ERR_PRXCOND;
4405 if (!(s->flags & SN_FINST_MASK))
4406 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004407 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004408}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004409
Willy Tarreau60b85b02008-11-30 23:28:40 +01004410/* This function is an analyser which processes the HTTP tarpit. It always
4411 * returns zero, at the beginning because it prevents any other processing
4412 * from occurring, and at the end because it terminates the request.
4413 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004414int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004415{
4416 struct http_txn *txn = &s->txn;
4417
4418 /* This connection is being tarpitted. The CLIENT side has
4419 * already set the connect expiration date to the right
4420 * timeout. We just have to check that the client is still
4421 * there and that the timeout has not expired.
4422 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004423 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004424 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004425 !tick_is_expired(req->analyse_exp, now_ms))
4426 return 0;
4427
4428 /* We will set the queue timer to the time spent, just for
4429 * logging purposes. We fake a 500 server error, so that the
4430 * attacker will not suspect his connection has been tarpitted.
4431 * It will not cause trouble to the logs because we can exclude
4432 * the tarpitted connections by filtering on the 'PT' status flags.
4433 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004434 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4435
4436 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004437 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004438 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004439
4440 req->analysers = 0;
4441 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004442
Willy Tarreau60b85b02008-11-30 23:28:40 +01004443 if (!(s->flags & SN_ERR_MASK))
4444 s->flags |= SN_ERR_PRXCOND;
4445 if (!(s->flags & SN_FINST_MASK))
4446 s->flags |= SN_FINST_T;
4447 return 0;
4448}
4449
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004450/* This function is an analyser which waits for the HTTP request body. It waits
4451 * for either the buffer to be full, or the full advertised contents to have
4452 * reached the buffer. It must only be called after the standard HTTP request
4453 * processing has occurred, because it expects the request to be parsed and will
4454 * look for the Expect header. It may send a 100-Continue interim response. It
4455 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4456 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4457 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004458 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004459int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004460{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004461 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004462 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004463
4464 /* We have to parse the HTTP request body to find any required data.
4465 * "balance url_param check_post" should have been the only way to get
4466 * into this. We were brought here after HTTP header analysis, so all
4467 * related structures are ready.
4468 */
4469
Willy Tarreau890988f2014-04-10 11:59:33 +02004470 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4471 /* This is the first call */
4472 if (msg->msg_state < HTTP_MSG_BODY)
4473 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004474
Willy Tarreau890988f2014-04-10 11:59:33 +02004475 if (msg->msg_state < HTTP_MSG_100_SENT) {
4476 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4477 * send an HTTP/1.1 100 Continue intermediate response.
4478 */
4479 if (msg->flags & HTTP_MSGF_VER_11) {
4480 struct hdr_ctx ctx;
4481 ctx.idx = 0;
4482 /* Expect is allowed in 1.1, look for it */
4483 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4484 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4485 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4486 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004487 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004488 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004489 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004490
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004491 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004492 * req->buf->p still points to the beginning of the message. We
4493 * must save the body in msg->next because it survives buffer
4494 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004495 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004496 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004497
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004498 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004499 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4500 else
4501 msg->msg_state = HTTP_MSG_DATA;
4502 }
4503
Willy Tarreau890988f2014-04-10 11:59:33 +02004504 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4505 /* We're in content-length mode, we just have to wait for enough data. */
4506 if (req->buf->i - msg->sov < msg->body_len)
4507 goto missing_data;
4508
4509 /* OK we have everything we need now */
4510 goto http_end;
4511 }
4512
4513 /* OK here we're parsing a chunked-encoded message */
4514
Willy Tarreau522d6c02009-12-06 18:49:18 +01004515 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004516 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004517 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004518 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004519 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004520 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004521
Willy Tarreau115acb92009-12-26 13:56:06 +01004522 if (!ret)
4523 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004524 else if (ret < 0) {
4525 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004526 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004527 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004528 }
4529
Willy Tarreaud98cf932009-12-27 22:54:55 +01004530 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004531 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004532 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004533 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004534 if (msg->msg_state == HTTP_MSG_TRAILERS)
4535 goto http_end;
4536
Willy Tarreau226071e2014-04-10 11:55:45 +02004537 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004538 goto http_end;
4539
4540 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004541 /* we get here if we need to wait for more data. If the buffer is full,
4542 * we have the maximum we can expect.
4543 */
4544 if (buffer_full(req->buf, global.tune.maxrewrite))
4545 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004546
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004547 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004548 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004549 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004550
4551 if (!(s->flags & SN_ERR_MASK))
4552 s->flags |= SN_ERR_CLITO;
4553 if (!(s->flags & SN_FINST_MASK))
4554 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004555 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004556 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004557
4558 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004559 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004560 /* Not enough data. We'll re-use the http-request
4561 * timeout here. Ideally, we should set the timeout
4562 * relative to the accept() date. We just set the
4563 * request timeout once at the beginning of the
4564 * request.
4565 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004566 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004567 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004568 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004569 return 0;
4570 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004571
4572 http_end:
4573 /* The situation will not evolve, so let's give up on the analysis. */
4574 s->logs.tv_request = now; /* update the request timer to reflect full request */
4575 req->analysers &= ~an_bit;
4576 req->analyse_exp = TICK_ETERNITY;
4577 return 1;
4578
4579 return_bad_req: /* let's centralize all bad requests */
4580 txn->req.msg_state = HTTP_MSG_ERROR;
4581 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004582 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004583
Willy Tarreau79ebac62010-06-07 13:47:49 +02004584 if (!(s->flags & SN_ERR_MASK))
4585 s->flags |= SN_ERR_PRXCOND;
4586 if (!(s->flags & SN_FINST_MASK))
4587 s->flags |= SN_FINST_R;
4588
Willy Tarreau522d6c02009-12-06 18:49:18 +01004589 return_err_msg:
4590 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004591 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004592 if (s->listener->counters)
4593 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004594 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004595}
4596
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004597/* send a server's name with an outgoing request over an established connection.
4598 * Note: this function is designed to be called once the request has been scheduled
4599 * for being forwarded. This is the reason why it rewinds the buffer before
4600 * proceeding.
4601 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004602int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004603
4604 struct hdr_ctx ctx;
4605
Mark Lamourinec2247f02012-01-04 13:02:01 -05004606 char *hdr_name = be->server_id_hdr_name;
4607 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004608 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004609 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004610 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004611
William Lallemandd9e90662012-01-30 17:27:17 +01004612 ctx.idx = 0;
4613
Willy Tarreau211cdec2014-04-17 20:18:08 +02004614 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004615 if (old_o) {
4616 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004617 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004618 txn->req.next += old_o;
4619 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004620 }
4621
Willy Tarreau9b28e032012-10-12 23:49:43 +02004622 old_i = chn->buf->i;
4623 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 -05004624 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004625 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004626 }
4627
4628 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004629 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004630 memcpy(hdr_val, hdr_name, hdr_name_len);
4631 hdr_val += hdr_name_len;
4632 *hdr_val++ = ':';
4633 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004634 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4635 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004636
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004637 if (old_o) {
4638 /* If this was a forwarded request, we must readjust the amount of
4639 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004640 * variations. Note that the current state is >= HTTP_MSG_BODY,
4641 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004642 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004643 old_o += chn->buf->i - old_i;
4644 b_adv(chn->buf, old_o);
4645 txn->req.next -= old_o;
4646 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004647 }
4648
Mark Lamourinec2247f02012-01-04 13:02:01 -05004649 return 0;
4650}
4651
Willy Tarreau610ecce2010-01-04 21:15:02 +01004652/* Terminate current transaction and prepare a new one. This is very tricky
4653 * right now but it works.
4654 */
4655void http_end_txn_clean_session(struct session *s)
4656{
Willy Tarreau068621e2013-12-23 15:11:25 +01004657 int prev_status = s->txn.status;
4658
Willy Tarreau610ecce2010-01-04 21:15:02 +01004659 /* FIXME: We need a more portable way of releasing a backend's and a
4660 * server's connections. We need a safer way to reinitialize buffer
4661 * flags. We also need a more accurate method for computing per-request
4662 * data.
4663 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004664
Willy Tarreau4213a112013-12-15 10:25:42 +01004665 /* unless we're doing keep-alive, we want to quickly close the connection
4666 * to the server.
4667 */
4668 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4669 !si_conn_ready(s->req->cons)) {
4670 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4671 si_shutr(s->req->cons);
4672 si_shutw(s->req->cons);
4673 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004674
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004675 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004677 if (unlikely(s->srv_conn))
4678 sess_change_server(s, NULL);
4679 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004680
4681 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4682 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004683 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004684
4685 if (s->txn.status) {
4686 int n;
4687
4688 n = s->txn.status / 100;
4689 if (n < 1 || n > 5)
4690 n = 0;
4691
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004692 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004693 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004694 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004695 s->fe->fe_counters.p.http.comp_rsp++;
4696 }
Willy Tarreau24657792010-02-26 10:30:28 +01004697 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004698 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004699 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004700 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004701 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004702 s->be->be_counters.p.http.comp_rsp++;
4703 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004704 }
4705
4706 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004707 s->logs.bytes_in -= s->req->buf->i;
4708 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004709
4710 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004711 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004712 !(s->flags & SN_MONITOR) &&
4713 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4714 s->do_log(s);
4715 }
4716
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004717 session_update_time_stats(s);
4718
Willy Tarreau610ecce2010-01-04 21:15:02 +01004719 s->logs.accept_date = date; /* user-visible date for logging */
4720 s->logs.tv_accept = now; /* corrected date for internal use */
4721 tv_zero(&s->logs.tv_request);
4722 s->logs.t_queue = -1;
4723 s->logs.t_connect = -1;
4724 s->logs.t_data = -1;
4725 s->logs.t_close = 0;
4726 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4727 s->logs.srv_queue_size = 0; /* we will get this number soon */
4728
Willy Tarreau9b28e032012-10-12 23:49:43 +02004729 s->logs.bytes_in = s->req->total = s->req->buf->i;
4730 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004731
4732 if (s->pend_pos)
4733 pendconn_free(s->pend_pos);
4734
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004735 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004736 if (s->flags & SN_CURR_SESS) {
4737 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004738 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004739 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004740 if (may_dequeue_tasks(objt_server(s->target), s->be))
4741 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004742 }
4743
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004744 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004745
Willy Tarreau4213a112013-12-15 10:25:42 +01004746 /* only release our endpoint if we don't intend to reuse the
4747 * connection.
4748 */
4749 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4750 !si_conn_ready(s->req->cons)) {
4751 si_release_endpoint(s->req->cons);
4752 }
4753
Willy Tarreau610ecce2010-01-04 21:15:02 +01004754 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004755 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004756 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004757 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004758 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau77d29022014-06-11 14:11:44 +02004759 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_READ_NOEXP);
Willy Tarreau1592d1e2014-06-11 16:49:14 +02004760 s->rep->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);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004761 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST);
Willy Tarreau36346242014-02-24 18:26:30 +01004762 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004763
Willy Tarreau610ecce2010-01-04 21:15:02 +01004764 s->txn.meth = 0;
4765 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004766 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004767
4768 if (prev_status == 401 || prev_status == 407) {
4769 /* In HTTP keep-alive mode, if we receive a 401, we still have
4770 * a chance of being able to send the visitor again to the same
4771 * server over the same connection. This is required by some
4772 * broken protocols such as NTLM, and anyway whenever there is
4773 * an opportunity for sending the challenge to the proper place,
4774 * it's better to do it (at least it helps with debugging).
4775 */
4776 s->txn.flags |= TX_PREFER_LAST;
4777 }
4778
Willy Tarreauee55dc02010-06-01 10:56:34 +02004779 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004780 s->req->cons->flags |= SI_FL_INDEP_STR;
4781
Willy Tarreau96e31212011-05-30 18:10:30 +02004782 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004783 s->req->flags |= CF_NEVER_WAIT;
4784 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004785 }
4786
Willy Tarreau610ecce2010-01-04 21:15:02 +01004787 /* if the request buffer is not empty, it means we're
4788 * about to process another request, so send pending
4789 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004790 * Just don't do this if the buffer is close to be full,
4791 * because the request will wait for it to flush a little
4792 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004793 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004794 if (s->req->buf->i) {
4795 if (s->rep->buf->o &&
4796 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4797 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004798 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004799 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004800
4801 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004802 channel_auto_read(s->req);
4803 channel_auto_close(s->req);
4804 channel_auto_read(s->rep);
4805 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004806
Willy Tarreau27375622013-12-17 00:00:28 +01004807 /* we're in keep-alive with an idle connection, monitor it */
4808 si_idle_conn(s->req->cons);
4809
Willy Tarreau342b11c2010-11-24 16:22:09 +01004810 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004811 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004812}
4813
4814
4815/* This function updates the request state machine according to the response
4816 * state machine and buffer flags. It returns 1 if it changes anything (flag
4817 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4818 * it is only used to find when a request/response couple is complete. Both
4819 * this function and its equivalent should loop until both return zero. It
4820 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4821 */
4822int http_sync_req_state(struct session *s)
4823{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004824 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004825 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004826 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004827 unsigned int old_state = txn->req.msg_state;
4828
Willy Tarreau610ecce2010-01-04 21:15:02 +01004829 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4830 return 0;
4831
4832 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004833 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004834 * We can shut the read side unless we want to abort_on_close,
4835 * or we have a POST request. The issue with POST requests is
4836 * that some browsers still send a CRLF after the request, and
4837 * this CRLF must be read so that it does not remain in the kernel
4838 * buffers, otherwise a close could cause an RST on some systems
4839 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004840 * Note that if we're using keep-alive on the client side, we'd
4841 * rather poll now and keep the polling enabled for the whole
4842 * session's life than enabling/disabling it between each
4843 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004844 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004845 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4846 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4847 !(s->be->options & PR_O_ABRT_CLOSE) &&
4848 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004849 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004850
Willy Tarreau40f151a2012-12-20 12:10:09 +01004851 /* if the server closes the connection, we want to immediately react
4852 * and close the socket to save packets and syscalls.
4853 */
4854 chn->cons->flags |= SI_FL_NOHALF;
4855
Willy Tarreau610ecce2010-01-04 21:15:02 +01004856 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4857 goto wait_other_side;
4858
4859 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4860 /* The server has not finished to respond, so we
4861 * don't want to move in order not to upset it.
4862 */
4863 goto wait_other_side;
4864 }
4865
4866 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4867 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004868 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004869 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004870 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004871 goto wait_other_side;
4872 }
4873
4874 /* When we get here, it means that both the request and the
4875 * response have finished receiving. Depending on the connection
4876 * mode, we'll have to wait for the last bytes to leave in either
4877 * direction, and sometimes for a close to be effective.
4878 */
4879
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004880 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4881 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004882 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4883 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004884 }
4885 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4886 /* Option forceclose is set, or either side wants to close,
4887 * let's enforce it now that we're not expecting any new
4888 * data to come. The caller knows the session is complete
4889 * once both states are CLOSED.
4890 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004891 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4892 channel_shutr_now(chn);
4893 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004894 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004895 }
4896 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004897 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4898 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004899 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004900 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4901 channel_auto_read(chn);
4902 txn->req.msg_state = HTTP_MSG_TUNNEL;
4903 chn->flags |= CF_NEVER_WAIT;
4904 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004905 }
4906
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004907 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004908 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004909 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004910
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004911 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004912 txn->req.msg_state = HTTP_MSG_CLOSING;
4913 goto http_msg_closing;
4914 }
4915 else {
4916 txn->req.msg_state = HTTP_MSG_CLOSED;
4917 goto http_msg_closed;
4918 }
4919 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004920 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004921 }
4922
4923 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4924 http_msg_closing:
4925 /* nothing else to forward, just waiting for the output buffer
4926 * to be empty and for the shutw_now to take effect.
4927 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004928 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004929 txn->req.msg_state = HTTP_MSG_CLOSED;
4930 goto http_msg_closed;
4931 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004932 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004933 txn->req.msg_state = HTTP_MSG_ERROR;
4934 goto wait_other_side;
4935 }
4936 }
4937
4938 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4939 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004940 /* see above in MSG_DONE why we only do this in these states */
4941 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4942 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4943 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004944 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004945 goto wait_other_side;
4946 }
4947
4948 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004949 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004950}
4951
4952
4953/* This function updates the response state machine according to the request
4954 * state machine and buffer flags. It returns 1 if it changes anything (flag
4955 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4956 * it is only used to find when a request/response couple is complete. Both
4957 * this function and its equivalent should loop until both return zero. It
4958 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4959 */
4960int http_sync_res_state(struct session *s)
4961{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004962 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004963 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004964 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004965 unsigned int old_state = txn->rsp.msg_state;
4966
Willy Tarreau610ecce2010-01-04 21:15:02 +01004967 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4968 return 0;
4969
4970 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4971 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004972 * still monitor the server connection for a possible close
4973 * while the request is being uploaded, so we don't disable
4974 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004975 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004976 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004977
4978 if (txn->req.msg_state == HTTP_MSG_ERROR)
4979 goto wait_other_side;
4980
4981 if (txn->req.msg_state < HTTP_MSG_DONE) {
4982 /* The client seems to still be sending data, probably
4983 * because we got an error response during an upload.
4984 * We have the choice of either breaking the connection
4985 * or letting it pass through. Let's do the later.
4986 */
4987 goto wait_other_side;
4988 }
4989
4990 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4991 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004992 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004993 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004994 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004995 goto wait_other_side;
4996 }
4997
4998 /* When we get here, it means that both the request and the
4999 * response have finished receiving. Depending on the connection
5000 * mode, we'll have to wait for the last bytes to leave in either
5001 * direction, and sometimes for a close to be effective.
5002 */
5003
5004 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5005 /* Server-close mode : shut read and wait for the request
5006 * side to close its output buffer. The caller will detect
5007 * when we're in DONE and the other is in CLOSED and will
5008 * catch that for the final cleanup.
5009 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005010 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5011 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005012 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005013 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5014 /* Option forceclose is set, or either side wants to close,
5015 * let's enforce it now that we're not expecting any new
5016 * data to come. The caller knows the session is complete
5017 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005018 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005019 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5020 channel_shutr_now(chn);
5021 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005022 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005023 }
5024 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005025 /* The last possible modes are keep-alive and tunnel. Tunnel will
5026 * need to forward remaining data. Keep-alive will need to monitor
5027 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005028 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005029 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005030 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005031 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5032 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005033 }
5034
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005035 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005036 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005037 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005038 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5039 goto http_msg_closing;
5040 }
5041 else {
5042 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5043 goto http_msg_closed;
5044 }
5045 }
5046 goto wait_other_side;
5047 }
5048
5049 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5050 http_msg_closing:
5051 /* nothing else to forward, just waiting for the output buffer
5052 * to be empty and for the shutw_now to take effect.
5053 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005054 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005055 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5056 goto http_msg_closed;
5057 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005058 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005059 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005060 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005061 if (objt_server(s->target))
5062 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005063 goto wait_other_side;
5064 }
5065 }
5066
5067 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5068 http_msg_closed:
5069 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005070 bi_erase(chn);
5071 channel_auto_close(chn);
5072 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005073 goto wait_other_side;
5074 }
5075
5076 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005077 /* We force the response to leave immediately if we're waiting for the
5078 * other side, since there is no pending shutdown to push it out.
5079 */
5080 if (!channel_is_empty(chn))
5081 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005082 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005083}
5084
5085
5086/* Resync the request and response state machines. Return 1 if either state
5087 * changes.
5088 */
5089int http_resync_states(struct session *s)
5090{
5091 struct http_txn *txn = &s->txn;
5092 int old_req_state = txn->req.msg_state;
5093 int old_res_state = txn->rsp.msg_state;
5094
Willy Tarreau610ecce2010-01-04 21:15:02 +01005095 http_sync_req_state(s);
5096 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005097 if (!http_sync_res_state(s))
5098 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005099 if (!http_sync_req_state(s))
5100 break;
5101 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005102
Willy Tarreau610ecce2010-01-04 21:15:02 +01005103 /* OK, both state machines agree on a compatible state.
5104 * There are a few cases we're interested in :
5105 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5106 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5107 * directions, so let's simply disable both analysers.
5108 * - HTTP_MSG_CLOSED on the response only means we must abort the
5109 * request.
5110 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5111 * with server-close mode means we've completed one request and we
5112 * must re-initialize the server connection.
5113 */
5114
5115 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5116 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5117 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5118 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5119 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005120 channel_auto_close(s->req);
5121 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005122 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005123 channel_auto_close(s->rep);
5124 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005125 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005126 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5127 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005128 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005129 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005130 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005131 channel_auto_close(s->rep);
5132 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005133 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005134 channel_abort(s->req);
5135 channel_auto_close(s->req);
5136 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005137 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005138 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005139 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5140 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005141 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005142 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5143 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5144 /* server-close/keep-alive: terminate this transaction,
5145 * possibly killing the server connection and reinitialize
5146 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005147 */
5148 http_end_txn_clean_session(s);
5149 }
5150
Willy Tarreau610ecce2010-01-04 21:15:02 +01005151 return txn->req.msg_state != old_req_state ||
5152 txn->rsp.msg_state != old_res_state;
5153}
5154
Willy Tarreaud98cf932009-12-27 22:54:55 +01005155/* This function is an analyser which forwards request body (including chunk
5156 * sizes if any). It is called as soon as we must forward, even if we forward
5157 * zero byte. The only situation where it must not be called is when we're in
5158 * tunnel mode and we want to forward till the close. It's used both to forward
5159 * remaining data and to resync after end of body. It expects the msg_state to
5160 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5161 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005162 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005163 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005164 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005165int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005166{
5167 struct http_txn *txn = &s->txn;
5168 struct http_msg *msg = &s->txn.req;
5169
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005170 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5171 return 0;
5172
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005173 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005174 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005175 /* Output closed while we were sending data. We must abort and
5176 * wake the other side up.
5177 */
5178 msg->msg_state = HTTP_MSG_ERROR;
5179 http_resync_states(s);
Willy Tarreaub9edf8f2014-05-07 14:24:16 +02005180
5181 if (req->flags & CF_READ_TIMEOUT)
5182 goto cli_timeout;
5183
5184 if (req->flags & CF_WRITE_TIMEOUT)
5185 goto srv_timeout;
5186
Willy Tarreau082b01c2010-01-02 23:58:04 +01005187 return 1;
5188 }
5189
Willy Tarreaud98cf932009-12-27 22:54:55 +01005190 /* Note that we don't have to send 100-continue back because we don't
5191 * need the data to complete our job, and it's up to the server to
5192 * decide whether to return 100, 417 or anything else in return of
5193 * an "Expect: 100-continue" header.
5194 */
5195
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005196 if (msg->sov) {
5197 /* we have msg->sov which points to the first byte of message
5198 * body, and req->buf.p still points to the beginning of the
5199 * message. We forward the headers now, as we don't need them
5200 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005201 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005202 b_adv(req->buf, msg->sov);
5203 msg->next -= msg->sov;
5204 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005205
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005206 /* The previous analysers guarantee that the state is somewhere
5207 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5208 * msg->next are always correct.
5209 */
5210 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5211 if (msg->flags & HTTP_MSGF_TE_CHNK)
5212 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5213 else
5214 msg->msg_state = HTTP_MSG_DATA;
5215 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005216 }
5217
Willy Tarreau7ba23542014-04-17 21:50:00 +02005218 /* Some post-connect processing might want us to refrain from starting to
5219 * forward data. Currently, the only reason for this is "balance url_param"
5220 * whichs need to parse/process the request after we've enabled forwarding.
5221 */
5222 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5223 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5224 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005225 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005226 goto missing_data;
5227 }
5228 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5229 }
5230
Willy Tarreau80a92c02014-03-12 10:41:13 +01005231 /* in most states, we should abort in case of early close */
5232 channel_auto_close(req);
5233
Willy Tarreauefdf0942014-04-24 20:08:57 +02005234 if (req->to_forward) {
5235 /* We can't process the buffer's contents yet */
5236 req->flags |= CF_WAKE_WRITE;
5237 goto missing_data;
5238 }
5239
Willy Tarreaud98cf932009-12-27 22:54:55 +01005240 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005241 if (msg->msg_state == HTTP_MSG_DATA) {
5242 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005243 /* we may have some pending data starting at req->buf->p */
5244 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005245 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005246 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005247 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005248 msg->next += msg->chunk_len;
5249 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005250
5251 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005252 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005253 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005254 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005255 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005256 }
5257 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005258 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005259 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005260 * TRAILERS state.
5261 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005262 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005263
Willy Tarreau54d23df2012-10-25 19:04:45 +02005264 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005265 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005266 else if (ret < 0) {
5267 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005268 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005269 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005270 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005271 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005272 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005273 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005274 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005275 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005276 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005277
5278 if (ret == 0)
5279 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005280 else if (ret < 0) {
5281 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005282 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005283 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005284 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005285 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005286 /* we're in MSG_CHUNK_SIZE now */
5287 }
5288 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005289 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005290
5291 if (ret == 0)
5292 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005293 else if (ret < 0) {
5294 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005295 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005296 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005297 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005298 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005299 /* we're in HTTP_MSG_DONE now */
5300 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005301 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005302 int old_state = msg->msg_state;
5303
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005305
5306 /* we may have some pending data starting at req->buf->p
5307 * such as last chunk of data or trailers.
5308 */
5309 b_adv(req->buf, msg->next);
5310 msg->next = 0;
5311
Willy Tarreau4fe41902010-06-07 22:27:41 +02005312 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005313 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5314 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005315 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005316 if (http_resync_states(s)) {
5317 /* some state changes occurred, maybe the analyser
5318 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005319 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005320 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005321 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005322 /* request errors are most likely due to
5323 * the server aborting the transfer.
5324 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005325 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005326 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005327 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005328 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005329 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005330 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005331 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005332 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005333
5334 /* If "option abortonclose" is set on the backend, we
5335 * want to monitor the client's connection and forward
5336 * any shutdown notification to the server, which will
5337 * decide whether to close or to go on processing the
5338 * request.
5339 */
5340 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005341 channel_auto_read(req);
5342 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005343 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005344 else if (s->txn.meth == HTTP_METH_POST) {
5345 /* POST requests may require to read extra CRLF
5346 * sent by broken browsers and which could cause
5347 * an RST to be sent upon close on some systems
5348 * (eg: Linux).
5349 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005350 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005351 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005352
Willy Tarreaub1982e22014-05-06 22:57:53 +02005353 /* if we received everything, we don't want to expire anymore */
5354 if (msg->msg_state == HTTP_MSG_DONE) {
5355 req->flags |= CF_READ_NOEXP;
5356 req->rex = TICK_ETERNITY;
5357 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005358 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005359 }
5360 }
5361
Willy Tarreaud98cf932009-12-27 22:54:55 +01005362 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005363 /* we may have some pending data starting at req->buf->p */
5364 b_adv(req->buf, msg->next);
5365 msg->next = 0;
5366 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5367
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005368 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005369 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005370 if (!(s->flags & SN_ERR_MASK))
5371 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005372 if (!(s->flags & SN_FINST_MASK)) {
5373 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5374 s->flags |= SN_FINST_H;
5375 else
5376 s->flags |= SN_FINST_D;
5377 }
5378
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005379 s->fe->fe_counters.cli_aborts++;
5380 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005381 if (objt_server(s->target))
5382 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005383
5384 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005385 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005386
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005387 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005388 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005389 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005390
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005391 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005392 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005393 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005394 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005395 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005396
Willy Tarreau5c620922011-05-11 19:56:11 +02005397 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005398 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005399 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005400 * modes are already handled by the stream sock layer. We must not do
5401 * this in content-length mode because it could present the MSG_MORE
5402 * flag with the last block of forwarded data, which would cause an
5403 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005404 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005405 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005406 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005407
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005408 return 0;
5409
Willy Tarreaud98cf932009-12-27 22:54:55 +01005410 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005411 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005412 if (s->listener->counters)
5413 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005414
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005415 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005416 /* we may have some pending data starting at req->buf->p */
5417 b_adv(req->buf, msg->next);
5418 msg->next = 0;
5419
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005420 txn->req.msg_state = HTTP_MSG_ERROR;
5421 if (txn->status) {
5422 /* Note: we don't send any error if some data were already sent */
5423 stream_int_retnclose(req->prod, NULL);
5424 } else {
5425 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005426 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005427 }
5428 req->analysers = 0;
5429 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005430
5431 if (!(s->flags & SN_ERR_MASK))
5432 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005433 if (!(s->flags & SN_FINST_MASK)) {
5434 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5435 s->flags |= SN_FINST_H;
5436 else
5437 s->flags |= SN_FINST_D;
5438 }
5439 return 0;
5440
5441 aborted_xfer:
5442 txn->req.msg_state = HTTP_MSG_ERROR;
5443 if (txn->status) {
5444 /* Note: we don't send any error if some data were already sent */
5445 stream_int_retnclose(req->prod, NULL);
5446 } else {
5447 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005448 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005449 }
5450 req->analysers = 0;
5451 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5452
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005453 s->fe->fe_counters.srv_aborts++;
5454 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005455 if (objt_server(s->target))
5456 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005457
5458 if (!(s->flags & SN_ERR_MASK))
5459 s->flags |= SN_ERR_SRVCL;
5460 if (!(s->flags & SN_FINST_MASK)) {
5461 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5462 s->flags |= SN_FINST_H;
5463 else
5464 s->flags |= SN_FINST_D;
5465 }
Willy Tarreaub9edf8f2014-05-07 14:24:16 +02005466 return 0;
5467
5468 cli_timeout:
5469 if (!(s->flags & SN_ERR_MASK))
5470 s->flags |= SN_ERR_CLITO;
5471
5472 if (!(s->flags & SN_FINST_MASK)) {
5473 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5474 s->flags |= SN_FINST_H;
5475 else
5476 s->flags |= SN_FINST_D;
5477 }
5478
5479 if (txn->status > 0) {
5480 /* Don't send any error message if something was already sent */
5481 stream_int_retnclose(req->prod, NULL);
5482 }
5483 else {
5484 txn->status = 408;
5485 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
5486 }
5487
5488 msg->msg_state = HTTP_MSG_ERROR;
5489 req->analysers = 0;
5490 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5491
5492 session_inc_http_err_ctr(s);
5493 s->fe->fe_counters.failed_req++;
5494 s->be->be_counters.failed_req++;
5495 if (s->listener->counters)
5496 s->listener->counters->failed_req++;
5497 return 0;
5498
5499 srv_timeout:
5500 if (!(s->flags & SN_ERR_MASK))
5501 s->flags |= SN_ERR_SRVTO;
5502
5503 if (!(s->flags & SN_FINST_MASK)) {
5504 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5505 s->flags |= SN_FINST_H;
5506 else
5507 s->flags |= SN_FINST_D;
5508 }
5509
5510 if (txn->status > 0) {
5511 /* Don't send any error message if something was already sent */
5512 stream_int_retnclose(req->prod, NULL);
5513 }
5514 else {
5515 txn->status = 504;
5516 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_504));
5517 }
5518
5519 msg->msg_state = HTTP_MSG_ERROR;
5520 req->analysers = 0;
5521 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5522
5523 s->be->be_counters.failed_resp++;
5524 if (objt_server(s->target)) {
5525 objt_server(s->target)->counters.failed_resp++;
5526 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
5527 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005528 return 0;
5529}
5530
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005531/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5532 * processing can continue on next analysers, or zero if it either needs more
5533 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5534 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5535 * when it has nothing left to do, and may remove any analyser when it wants to
5536 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005537 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005538int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005539{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005540 struct http_txn *txn = &s->txn;
5541 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005542 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005543 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005544 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005545 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005546
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005547 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005548 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005549 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005550 rep,
5551 rep->rex, rep->wex,
5552 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005553 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005554 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005555
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005556 /*
5557 * Now parse the partial (or complete) lines.
5558 * We will check the response syntax, and also join multi-line
5559 * headers. An index of all the lines will be elaborated while
5560 * parsing.
5561 *
5562 * For the parsing, we use a 28 states FSM.
5563 *
5564 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005565 * rep->buf->p = beginning of response
5566 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5567 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005568 * msg->eol = end of current header or line (LF or CRLF)
5569 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005570 */
5571
Willy Tarreau628c40c2014-04-24 19:11:26 +02005572 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005573 /* There's a protected area at the end of the buffer for rewriting
5574 * purposes. We don't want to start to parse the request if the
5575 * protected area is affected, because we may have to move processed
5576 * data later, which is much more complicated.
5577 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005578 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005579 if (unlikely(!channel_reserved(rep))) {
5580 /* some data has still not left the buffer, wake us once that's done */
5581 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5582 goto abort_response;
5583 channel_dont_close(rep);
5584 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005585 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005586 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005587 }
5588
Willy Tarreau379357a2013-06-08 12:55:46 +02005589 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5590 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5591 buffer_slow_realign(rep->buf);
5592
Willy Tarreau9b28e032012-10-12 23:49:43 +02005593 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005594 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005595 }
5596
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005597 /* 1: we might have to print this header in debug mode */
5598 if (unlikely((global.mode & MODE_DEBUG) &&
5599 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005600 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005601 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005602
Willy Tarreau9b28e032012-10-12 23:49:43 +02005603 sol = rep->buf->p;
5604 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005605 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005606
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005607 sol += hdr_idx_first_pos(&txn->hdr_idx);
5608 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005609
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005610 while (cur_idx) {
5611 eol = sol + txn->hdr_idx.v[cur_idx].len;
5612 debug_hdr("srvhdr", s, sol, eol);
5613 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5614 cur_idx = txn->hdr_idx.v[cur_idx].next;
5615 }
5616 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005617
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005618 /*
5619 * Now we quickly check if we have found a full valid response.
5620 * If not so, we check the FD and buffer states before leaving.
5621 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005622 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005623 * responses are checked first.
5624 *
5625 * Depending on whether the client is still there or not, we
5626 * may send an error response back or not. Note that normally
5627 * we should only check for HTTP status there, and check I/O
5628 * errors somewhere else.
5629 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005630
Willy Tarreau655dce92009-11-08 13:10:58 +01005631 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005632 /* Invalid response */
5633 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5634 /* we detected a parsing error. We want to archive this response
5635 * in the dedicated proxy area for later troubleshooting.
5636 */
5637 hdr_response_bad:
5638 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005639 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005640
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005641 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005642 if (objt_server(s->target)) {
5643 objt_server(s->target)->counters.failed_resp++;
5644 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005645 }
Willy Tarreau64648412010-03-05 10:41:54 +01005646 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005647 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005648 rep->analysers = 0;
5649 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005650 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005651 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005652 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005653
5654 if (!(s->flags & SN_ERR_MASK))
5655 s->flags |= SN_ERR_PRXCOND;
5656 if (!(s->flags & SN_FINST_MASK))
5657 s->flags |= SN_FINST_H;
5658
5659 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005660 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005661
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005662 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005663 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005664 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005665 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005666 goto hdr_response_bad;
5667 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005668
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005669 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005670 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005671 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005672 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005673 else if (txn->flags & TX_NOT_FIRST)
5674 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005675
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005676 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005677 if (objt_server(s->target)) {
5678 objt_server(s->target)->counters.failed_resp++;
5679 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005680 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005681
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005682 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005683 rep->analysers = 0;
5684 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005685 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005686 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005687 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005688
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005689 if (!(s->flags & SN_ERR_MASK))
5690 s->flags |= SN_ERR_SRVCL;
5691 if (!(s->flags & SN_FINST_MASK))
5692 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005693 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005694 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005695
Willy Tarreaub9edf8f2014-05-07 14:24:16 +02005696 /* read/write timeout : return a 504 to the client.
5697 * The write timeout may happen when we're uploading POST
5698 * data that the server is not consuming fast enough.
5699 */
5700 else if (rep->flags & (CF_READ_TIMEOUT|CF_WRITE_TIMEOUT)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005701 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005702 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005703 else if (txn->flags & TX_NOT_FIRST)
5704 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005705
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005706 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005707 if (objt_server(s->target)) {
5708 objt_server(s->target)->counters.failed_resp++;
5709 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005710 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005711
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005712 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005713 rep->analysers = 0;
5714 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005715 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005716 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005717 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005718
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005719 if (!(s->flags & SN_ERR_MASK))
5720 s->flags |= SN_ERR_SRVTO;
5721 if (!(s->flags & SN_FINST_MASK))
5722 s->flags |= SN_FINST_H;
5723 return 0;
5724 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005725
Willy Tarreauf003d372012-11-26 13:35:37 +01005726 /* client abort with an abortonclose */
5727 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5728 s->fe->fe_counters.cli_aborts++;
5729 s->be->be_counters.cli_aborts++;
5730 if (objt_server(s->target))
5731 objt_server(s->target)->counters.cli_aborts++;
5732
5733 rep->analysers = 0;
5734 channel_auto_close(rep);
5735
5736 txn->status = 400;
5737 bi_erase(rep);
5738 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5739
5740 if (!(s->flags & SN_ERR_MASK))
5741 s->flags |= SN_ERR_CLICL;
5742 if (!(s->flags & SN_FINST_MASK))
5743 s->flags |= SN_FINST_H;
5744
5745 /* process_session() will take care of the error */
5746 return 0;
5747 }
5748
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005749 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005750 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005751 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005752 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005753 else if (txn->flags & TX_NOT_FIRST)
5754 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005755
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005756 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005757 if (objt_server(s->target)) {
5758 objt_server(s->target)->counters.failed_resp++;
5759 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005760 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005761
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005762 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005763 rep->analysers = 0;
5764 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005765 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005766 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005767 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005768
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005769 if (!(s->flags & SN_ERR_MASK))
5770 s->flags |= SN_ERR_SRVCL;
5771 if (!(s->flags & SN_FINST_MASK))
5772 s->flags |= SN_FINST_H;
5773 return 0;
5774 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005775
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005776 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005777 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005778 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005779 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005780 else if (txn->flags & TX_NOT_FIRST)
5781 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005782
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005783 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005784 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005785 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005786
5787 if (!(s->flags & SN_ERR_MASK))
5788 s->flags |= SN_ERR_CLICL;
5789 if (!(s->flags & SN_FINST_MASK))
5790 s->flags |= SN_FINST_H;
5791
5792 /* process_session() will take care of the error */
5793 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005794 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005795
Willy Tarreau3bed5e92014-05-07 14:50:10 +02005796 /* we don't want to expire on the server side first until the client
5797 * has sent all the expected message body.
5798 */
5799 if (txn->req.msg_state >= HTTP_MSG_BODY && txn->req.msg_state < HTTP_MSG_DONE)
5800 rep->flags |= CF_READ_NOEXP;
5801
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005802 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005803 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005804 return 0;
5805 }
5806
5807 /* More interesting part now : we know that we have a complete
5808 * response which at least looks like HTTP. We have an indicator
5809 * of each header's length, so we can parse them quickly.
5810 */
5811
5812 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005813 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005814
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005815 /*
5816 * 1: get the status code
5817 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005818 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005819 if (n < 1 || n > 5)
5820 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005821 /* when the client triggers a 4xx from the server, it's most often due
5822 * to a missing object or permission. These events should be tracked
5823 * because if they happen often, it may indicate a brute force or a
5824 * vulnerability scan.
5825 */
5826 if (n == 4)
5827 session_inc_http_err_ctr(s);
5828
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005829 if (objt_server(s->target))
5830 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005831
Willy Tarreau5b154472009-12-21 20:11:07 +01005832 /* check if the response is HTTP/1.1 or above */
5833 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005834 ((rep->buf->p[5] > '1') ||
5835 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005836 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005837
5838 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005839 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 +01005840
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005841 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005842 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005843
Willy Tarreau9b28e032012-10-12 23:49:43 +02005844 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005845
Willy Tarreau39650402010-03-15 19:44:39 +01005846 /* Adjust server's health based on status code. Note: status codes 501
5847 * and 505 are triggered on demand by client request, so we must not
5848 * count them as server failures.
5849 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005850 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005851 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005852 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005853 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005854 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005855 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005856
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005857 /*
5858 * 2: check for cacheability.
5859 */
5860
5861 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005862 case 100:
5863 /*
5864 * We may be facing a 100-continue response, in which case this
5865 * is not the right response, and we're waiting for the next one.
5866 * Let's allow this response to go to the client and wait for the
5867 * next one.
5868 */
5869 hdr_idx_init(&txn->hdr_idx);
5870 msg->next -= channel_forward(rep, msg->next);
5871 msg->msg_state = HTTP_MSG_RPBEFORE;
5872 txn->status = 0;
5873 s->logs.t_data = -1; /* was not a response yet */
5874 goto next_one;
5875
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005876 case 200:
5877 case 203:
5878 case 206:
5879 case 300:
5880 case 301:
5881 case 410:
5882 /* RFC2616 @13.4:
5883 * "A response received with a status code of
5884 * 200, 203, 206, 300, 301 or 410 MAY be stored
5885 * by a cache (...) unless a cache-control
5886 * directive prohibits caching."
5887 *
5888 * RFC2616 @9.5: POST method :
5889 * "Responses to this method are not cacheable,
5890 * unless the response includes appropriate
5891 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005892 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005893 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005894 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005895 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5896 break;
5897 default:
5898 break;
5899 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005900
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005901 /*
5902 * 3: we may need to capture headers
5903 */
5904 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005905 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005906 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005907 txn->rsp.cap, s->fe->rsp_cap);
5908
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005909 /* 4: determine the transfer-length.
5910 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5911 * the presence of a message-body in a RESPONSE and its transfer length
5912 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005913 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005914 * All responses to the HEAD request method MUST NOT include a
5915 * message-body, even though the presence of entity-header fields
5916 * might lead one to believe they do. All 1xx (informational), 204
5917 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5918 * message-body. All other responses do include a message-body,
5919 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005920 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005921 * 1. Any response which "MUST NOT" include a message-body (such as the
5922 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5923 * always terminated by the first empty line after the header fields,
5924 * regardless of the entity-header fields present in the message.
5925 *
5926 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5927 * the "chunked" transfer-coding (Section 6.2) is used, the
5928 * transfer-length is defined by the use of this transfer-coding.
5929 * If a Transfer-Encoding header field is present and the "chunked"
5930 * transfer-coding is not present, the transfer-length is defined by
5931 * the sender closing the connection.
5932 *
5933 * 3. If a Content-Length header field is present, its decimal value in
5934 * OCTETs represents both the entity-length and the transfer-length.
5935 * If a message is received with both a Transfer-Encoding header
5936 * field and a Content-Length header field, the latter MUST be ignored.
5937 *
5938 * 4. If the message uses the media type "multipart/byteranges", and
5939 * the transfer-length is not otherwise specified, then this self-
5940 * delimiting media type defines the transfer-length. This media
5941 * type MUST NOT be used unless the sender knows that the recipient
5942 * can parse it; the presence in a request of a Range header with
5943 * multiple byte-range specifiers from a 1.1 client implies that the
5944 * client can parse multipart/byteranges responses.
5945 *
5946 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005947 */
5948
5949 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005950 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005951 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005952 * FIXME: should we parse anyway and return an error on chunked encoding ?
5953 */
5954 if (txn->meth == HTTP_METH_HEAD ||
5955 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005956 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005957 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005958 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005959 goto skip_content_length;
5960 }
5961
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005962 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005963 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005964 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005965 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005966 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005967 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5968 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005969 /* bad transfer-encoding (chunked followed by something else) */
5970 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005971 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005972 break;
5973 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005974 }
5975
5976 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5977 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005978 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005979 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005980 signed long long cl;
5981
Willy Tarreauad14f752011-09-02 20:33:27 +02005982 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005983 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005984 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005985 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005986
Willy Tarreauad14f752011-09-02 20:33:27 +02005987 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005988 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005989 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005990 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005991
Willy Tarreauad14f752011-09-02 20:33:27 +02005992 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005993 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005994 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005995 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005996
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005997 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005998 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005999 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006000 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006001
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006002 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006003 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006004 }
6005
William Lallemand82fe75c2012-10-23 10:25:10 +02006006 if (s->fe->comp || s->be->comp)
6007 select_compression_response_header(s, rep->buf);
6008
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006009skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006010 /* Now we have to check if we need to modify the Connection header.
6011 * This is more difficult on the response than it is on the request,
6012 * because we can have two different HTTP versions and we don't know
6013 * how the client will interprete a response. For instance, let's say
6014 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6015 * HTTP/1.1 response without any header. Maybe it will bound itself to
6016 * HTTP/1.0 because it only knows about it, and will consider the lack
6017 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6018 * the lack of header as a keep-alive. Thus we will use two flags
6019 * indicating how a request MAY be understood by the client. In case
6020 * of multiple possibilities, we'll fix the header to be explicit. If
6021 * ambiguous cases such as both close and keepalive are seen, then we
6022 * will fall back to explicit close. Note that we won't take risks with
6023 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006024 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006025 */
6026
Willy Tarreaudc008c52010-02-01 16:20:08 +01006027 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6028 txn->status == 101)) {
6029 /* Either we've established an explicit tunnel, or we're
6030 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006031 * to understand the next protocols. We have to switch to tunnel
6032 * mode, so that we transfer the request and responses then let
6033 * this protocol pass unmodified. When we later implement specific
6034 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006035 * header which contains information about that protocol for
6036 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006037 */
6038 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6039 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006040 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6041 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006042 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6043 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006044 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006045
Willy Tarreau70dffda2014-01-30 03:07:23 +01006046 /* this situation happens when combining pretend-keepalive with httpclose. */
6047 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006048 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6049 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006050 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6051
Willy Tarreau60466522010-01-18 19:08:45 +01006052 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006053 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006054 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6055 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006056
Willy Tarreau60466522010-01-18 19:08:45 +01006057 /* now adjust header transformations depending on current state */
6058 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6059 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6060 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006061 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006062 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006063 }
Willy Tarreau60466522010-01-18 19:08:45 +01006064 else { /* SCL / KAL */
6065 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006066 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006067 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006068 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006069
Willy Tarreau60466522010-01-18 19:08:45 +01006070 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006071 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006072
Willy Tarreau60466522010-01-18 19:08:45 +01006073 /* Some keep-alive responses are converted to Server-close if
6074 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006075 */
Willy Tarreau60466522010-01-18 19:08:45 +01006076 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6077 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006078 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006079 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006080 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006081 }
6082
Willy Tarreau7959a552013-09-23 16:44:27 +02006083 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006084 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006085
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006086 /* end of job, return OK */
6087 rep->analysers &= ~an_bit;
6088 rep->analyse_exp = TICK_ETERNITY;
6089 channel_auto_close(rep);
6090 return 1;
6091
6092 abort_keep_alive:
6093 /* A keep-alive request to the server failed on a network error.
6094 * The client is required to retry. We need to close without returning
6095 * any other information so that the client retries.
6096 */
6097 txn->status = 0;
6098 rep->analysers = 0;
6099 s->req->analysers = 0;
6100 channel_auto_close(rep);
6101 s->logs.logwait = 0;
6102 s->logs.level = 0;
6103 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6104 bi_erase(rep);
6105 stream_int_retnclose(rep->cons, NULL);
6106 return 0;
6107}
6108
6109/* This function performs all the processing enabled for the current response.
6110 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6111 * and updates s->rep->analysers. It might make sense to explode it into several
6112 * other functions. It works like process_request (see indications above).
6113 */
6114int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6115{
6116 struct http_txn *txn = &s->txn;
6117 struct http_msg *msg = &txn->rsp;
6118 struct proxy *cur_proxy;
6119 struct cond_wordlist *wl;
6120 struct http_res_rule *http_res_last_rule = NULL;
6121
6122 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6123 now_ms, __FUNCTION__,
6124 s,
6125 rep,
6126 rep->rex, rep->wex,
6127 rep->flags,
6128 rep->buf->i,
6129 rep->analysers);
6130
6131 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6132 return 0;
6133
6134 rep->analysers &= ~an_bit;
6135 rep->analyse_exp = TICK_ETERNITY;
6136
Willy Tarreau70730dd2014-04-24 18:06:27 +02006137 /* The stats applet needs to adjust the Connection header but we don't
6138 * apply any filter there.
6139 */
6140 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6141 goto skip_filters;
6142
Willy Tarreau58975672014-04-24 21:13:57 +02006143 /*
6144 * We will have to evaluate the filters.
6145 * As opposed to version 1.2, now they will be evaluated in the
6146 * filters order and not in the header order. This means that
6147 * each filter has to be validated among all headers.
6148 *
6149 * Filters are tried with ->be first, then with ->fe if it is
6150 * different from ->be.
6151 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006152
Willy Tarreau58975672014-04-24 21:13:57 +02006153 cur_proxy = s->be;
6154 while (1) {
6155 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006156
Willy Tarreau58975672014-04-24 21:13:57 +02006157 /* evaluate http-response rules */
6158 if (!http_res_last_rule)
6159 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s, txn);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006160
Willy Tarreau58975672014-04-24 21:13:57 +02006161 /* try headers filters */
6162 if (rule_set->rsp_exp != NULL) {
6163 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6164 return_bad_resp:
6165 if (objt_server(s->target)) {
6166 objt_server(s->target)->counters.failed_resp++;
6167 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006168 }
Willy Tarreau58975672014-04-24 21:13:57 +02006169 s->be->be_counters.failed_resp++;
6170 return_srv_prx_502:
6171 rep->analysers = 0;
6172 txn->status = 502;
6173 s->logs.t_data = -1; /* was not a valid response */
6174 rep->prod->flags |= SI_FL_NOLINGER;
6175 bi_erase(rep);
6176 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6177 if (!(s->flags & SN_ERR_MASK))
6178 s->flags |= SN_ERR_PRXCOND;
6179 if (!(s->flags & SN_FINST_MASK))
6180 s->flags |= SN_FINST_H;
6181 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006182 }
Willy Tarreau58975672014-04-24 21:13:57 +02006183 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006184
Willy Tarreau58975672014-04-24 21:13:57 +02006185 /* has the response been denied ? */
6186 if (txn->flags & TX_SVDENY) {
6187 if (objt_server(s->target))
6188 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006189
Willy Tarreau58975672014-04-24 21:13:57 +02006190 s->be->be_counters.denied_resp++;
6191 s->fe->fe_counters.denied_resp++;
6192 if (s->listener->counters)
6193 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006194
Willy Tarreau58975672014-04-24 21:13:57 +02006195 goto return_srv_prx_502;
6196 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006197
Willy Tarreau58975672014-04-24 21:13:57 +02006198 /* add response headers from the rule sets in the same order */
6199 list_for_each_entry(wl, &rule_set->rsp_add, list) {
6200 if (txn->status < 200)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006201 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006202 if (wl->cond) {
6203 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6204 ret = acl_pass(ret);
6205 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6206 ret = !ret;
6207 if (!ret)
6208 continue;
6209 }
6210 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6211 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006212 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006213
Willy Tarreau58975672014-04-24 21:13:57 +02006214 /* check whether we're already working on the frontend */
6215 if (cur_proxy == s->fe)
6216 break;
6217 cur_proxy = s->fe;
6218 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006219
Willy Tarreau58975672014-04-24 21:13:57 +02006220 /* OK that's all we can do for 1xx responses */
6221 if (unlikely(txn->status < 200))
6222 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006223
Willy Tarreau58975672014-04-24 21:13:57 +02006224 /*
6225 * Now check for a server cookie.
6226 */
6227 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6228 (s->be->options & PR_O_CHK_CACHE))
6229 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006230
Willy Tarreau58975672014-04-24 21:13:57 +02006231 /*
6232 * Check for cache-control or pragma headers if required.
6233 */
6234 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
6235 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006236
Willy Tarreau58975672014-04-24 21:13:57 +02006237 /*
6238 * Add server cookie in the response if needed
6239 */
6240 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6241 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6242 (!(s->flags & SN_DIRECT) ||
6243 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6244 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6245 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6246 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6247 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6248 !(s->flags & SN_IGNORE_PRST)) {
6249 /* the server is known, it's not the one the client requested, or the
6250 * cookie's last seen date needs to be refreshed. We have to
6251 * insert a set-cookie here, except if we want to insert only on POST
6252 * requests and this one isn't. Note that servers which don't have cookies
6253 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006254 */
Willy Tarreau58975672014-04-24 21:13:57 +02006255 if (!objt_server(s->target)->cookie) {
6256 chunk_printf(&trash,
6257 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6258 s->be->cookie_name);
6259 }
6260 else {
6261 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006262
Willy Tarreau58975672014-04-24 21:13:57 +02006263 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6264 /* emit last_date, which is mandatory */
6265 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6266 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6267 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006268
Willy Tarreau58975672014-04-24 21:13:57 +02006269 if (s->be->cookie_maxlife) {
6270 /* emit first_date, which is either the original one or
6271 * the current date.
6272 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006273 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006274 s30tob64(txn->cookie_first_date ?
6275 txn->cookie_first_date >> 2 :
6276 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006277 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006278 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006279 }
Willy Tarreau58975672014-04-24 21:13:57 +02006280 chunk_appendf(&trash, "; path=/");
6281 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006282
Willy Tarreau58975672014-04-24 21:13:57 +02006283 if (s->be->cookie_domain)
6284 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006285
Willy Tarreau58975672014-04-24 21:13:57 +02006286 if (s->be->ck_opts & PR_CK_HTTPONLY)
6287 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006288
Willy Tarreau58975672014-04-24 21:13:57 +02006289 if (s->be->ck_opts & PR_CK_SECURE)
6290 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006291
Willy Tarreau58975672014-04-24 21:13:57 +02006292 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6293 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006294
Willy Tarreau58975672014-04-24 21:13:57 +02006295 txn->flags &= ~TX_SCK_MASK;
6296 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6297 /* the server did not change, only the date was updated */
6298 txn->flags |= TX_SCK_UPDATED;
6299 else
6300 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006301
Willy Tarreau58975672014-04-24 21:13:57 +02006302 /* Here, we will tell an eventual cache on the client side that we don't
6303 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6304 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6305 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006306 */
Willy Tarreau58975672014-04-24 21:13:57 +02006307 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006308
Willy Tarreau58975672014-04-24 21:13:57 +02006309 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006310
Willy Tarreau58975672014-04-24 21:13:57 +02006311 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6312 "Cache-control: private", 22) < 0))
6313 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006314 }
Willy Tarreau58975672014-04-24 21:13:57 +02006315 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006316
Willy Tarreau58975672014-04-24 21:13:57 +02006317 /*
6318 * Check if result will be cacheable with a cookie.
6319 * We'll block the response if security checks have caught
6320 * nasty things such as a cacheable cookie.
6321 */
6322 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6323 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6324 (s->be->options & PR_O_CHK_CACHE)) {
6325 /* we're in presence of a cacheable response containing
6326 * a set-cookie header. We'll block it as requested by
6327 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006328 */
Willy Tarreau58975672014-04-24 21:13:57 +02006329 if (objt_server(s->target))
6330 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006331
Willy Tarreau58975672014-04-24 21:13:57 +02006332 s->be->be_counters.denied_resp++;
6333 s->fe->fe_counters.denied_resp++;
6334 if (s->listener->counters)
6335 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006336
Willy Tarreau58975672014-04-24 21:13:57 +02006337 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6338 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6339 send_log(s->be, LOG_ALERT,
6340 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6341 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6342 goto return_srv_prx_502;
6343 }
Willy Tarreau03945942009-12-22 16:50:27 +01006344
Willy Tarreau70730dd2014-04-24 18:06:27 +02006345 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006346 /*
6347 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6348 * If an "Upgrade" token is found, the header is left untouched in order
6349 * not to have to deal with some client bugs : some of them fail an upgrade
6350 * if anything but "Upgrade" is present in the Connection header.
6351 */
6352 if (!(txn->flags & TX_HDR_CONN_UPG) &&
6353 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6354 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6355 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6356 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006357
Willy Tarreau58975672014-04-24 21:13:57 +02006358 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6359 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6360 /* we want a keep-alive response here. Keep-alive header
6361 * required if either side is not 1.1.
6362 */
6363 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6364 want_flags |= TX_CON_KAL_SET;
6365 }
6366 else {
6367 /* we want a close response here. Close header required if
6368 * the server is 1.1, regardless of the client.
6369 */
6370 if (msg->flags & HTTP_MSGF_VER_11)
6371 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006372 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006373
Willy Tarreau58975672014-04-24 21:13:57 +02006374 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6375 http_change_connection_header(txn, msg, want_flags);
6376 }
6377
6378 skip_header_mangling:
6379 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6380 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6381 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006382
Willy Tarreau58975672014-04-24 21:13:57 +02006383 /* if the user wants to log as soon as possible, without counting
6384 * bytes from the server, then this is the right moment. We have
6385 * to temporarily assign bytes_out to log what we currently have.
6386 */
6387 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6388 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6389 s->logs.bytes_out = txn->rsp.eoh;
6390 s->do_log(s);
6391 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006392 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006393 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006394}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006395
Willy Tarreaud98cf932009-12-27 22:54:55 +01006396/* This function is an analyser which forwards response body (including chunk
6397 * sizes if any). It is called as soon as we must forward, even if we forward
6398 * zero byte. The only situation where it must not be called is when we're in
6399 * tunnel mode and we want to forward till the close. It's used both to forward
6400 * remaining data and to resync after end of body. It expects the msg_state to
6401 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6402 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006403 *
6404 * It is capable of compressing response data both in content-length mode and
6405 * in chunked mode. The state machines follows different flows depending on
6406 * whether content-length and chunked modes are used, since there are no
6407 * trailers in content-length :
6408 *
6409 * chk-mode cl-mode
6410 * ,----- BODY -----.
6411 * / \
6412 * V size > 0 V chk-mode
6413 * .--> SIZE -------------> DATA -------------> CRLF
6414 * | | size == 0 | last byte |
6415 * | v final crlf v inspected |
6416 * | TRAILERS -----------> DONE |
6417 * | |
6418 * `----------------------------------------------'
6419 *
6420 * Compression only happens in the DATA state, and must be flushed in final
6421 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6422 * is performed at once on final states for all bytes parsed, or when leaving
6423 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006424 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006425int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006426{
6427 struct http_txn *txn = &s->txn;
6428 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006429 static struct buffer *tmpbuf = NULL;
6430 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006431 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006432
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006433 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6434 return 0;
6435
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006436 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006437 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006438 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006439 /* Output closed while we were sending data. We must abort and
6440 * wake the other side up.
6441 */
6442 msg->msg_state = HTTP_MSG_ERROR;
6443 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006444 return 1;
6445 }
6446
Willy Tarreau4fe41902010-06-07 22:27:41 +02006447 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006448 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006449
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006450 if (msg->sov) {
6451 /* we have msg->sov which points to the first byte of message
6452 * body, and res->buf.p still points to the beginning of the
6453 * message. We forward the headers now, as we don't need them
6454 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006455 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006456 b_adv(res->buf, msg->sov);
6457 msg->next -= msg->sov;
6458 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006459
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006460 /* The previous analysers guarantee that the state is somewhere
6461 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6462 * msg->next are always correct.
6463 */
6464 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6465 if (msg->flags & HTTP_MSGF_TE_CHNK)
6466 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6467 else
6468 msg->msg_state = HTTP_MSG_DATA;
6469 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006470 }
6471
Willy Tarreauefdf0942014-04-24 20:08:57 +02006472 if (res->to_forward) {
6473 /* We can't process the buffer's contents yet */
6474 res->flags |= CF_WAKE_WRITE;
6475 goto missing_data;
6476 }
6477
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006478 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6479 /* We need a compression buffer in the DATA state to put the
6480 * output of compressed data, and in CRLF state to let the
6481 * TRAILERS state finish the job of removing the trailing CRLF.
6482 */
6483 if (unlikely(tmpbuf == NULL)) {
6484 /* this is the first time we need the compression buffer */
6485 tmpbuf = pool_alloc2(pool2_buffer);
6486 if (tmpbuf == NULL)
6487 goto aborted_xfer; /* no memory */
6488 }
6489
6490 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006491 if (ret < 0) {
6492 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006493 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006494 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006495 compressing = 1;
6496 }
6497
Willy Tarreaud98cf932009-12-27 22:54:55 +01006498 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006499 switch (msg->msg_state - HTTP_MSG_DATA) {
6500 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006501 /* we may have some pending data starting at res->buf->p */
6502 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006503 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006504 if (ret < 0)
6505 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006506
Willy Tarreaud5a67832014-04-21 10:54:27 +02006507 if (msg->chunk_len) {
6508 /* input empty or output full */
6509 if (res->buf->i > msg->next)
6510 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006511 goto missing_data;
6512 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006513 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006514 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006515 if (msg->chunk_len > res->buf->i - msg->next) {
6516 /* output full */
6517 res->flags |= CF_WAKE_WRITE;
6518 goto missing_data;
6519 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006520 msg->next += msg->chunk_len;
6521 msg->chunk_len = 0;
6522 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006523
6524 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006525 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006526 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006527 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006528 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006529 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006530 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006531 /* fall through for HTTP_MSG_CHUNK_CRLF */
6532
6533 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6534 /* we want the CRLF after the data */
6535
6536 ret = http_skip_chunk_crlf(msg);
6537 if (ret == 0)
6538 goto missing_data;
6539 else if (ret < 0) {
6540 if (msg->err_pos >= 0)
6541 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6542 goto return_bad_res;
6543 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006544 /* we're in MSG_CHUNK_SIZE now, fall through */
6545
6546 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006547 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006548 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006549 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006550 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006551
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006552 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006553 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006554 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006555 else if (ret < 0) {
6556 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006557 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006558 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006559 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006560 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006561 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006562
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006563 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006564 if (unlikely(compressing)) {
6565 /* we need to flush output contents before syncing FSMs */
6566 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6567 compressing = 0;
6568 }
6569
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006570 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006571 if (ret == 0)
6572 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006573 else if (ret < 0) {
6574 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006575 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006576 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006577 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006578 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006579
6580 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006581 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006582 if (unlikely(compressing)) {
6583 /* we need to flush output contents before syncing FSMs */
6584 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6585 compressing = 0;
6586 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006587
Willy Tarreauc623c172014-04-18 09:53:50 +02006588 /* we may have some pending data starting at res->buf->p
6589 * such as a last chunk of data or trailers.
6590 */
6591 b_adv(res->buf, msg->next);
6592 msg->next = 0;
6593
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006594 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006595 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006596 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6597 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006598 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006599
Willy Tarreau610ecce2010-01-04 21:15:02 +01006600 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006601 /* some state changes occurred, maybe the analyser
6602 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006603 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006604 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006605 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006606 /* response errors are most likely due to
6607 * the client aborting the transfer.
6608 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006609 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006610 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006611 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006612 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006613 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006614 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006615 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006616 }
Willy Tarreaub1982e22014-05-06 22:57:53 +02006617
6618 /* if we received everything, we don't want to expire anymore */
6619 if (msg->msg_state == HTTP_MSG_DONE) {
6620 res->flags |= CF_READ_NOEXP;
6621 res->rex = TICK_ETERNITY;
6622 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006623 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006624 }
6625 }
6626
Willy Tarreaud98cf932009-12-27 22:54:55 +01006627 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006628 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006629 if (unlikely(compressing)) {
6630 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006631 compressing = 0;
6632 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006633
Willy Tarreauc623c172014-04-18 09:53:50 +02006634 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6635 b_adv(res->buf, msg->next);
6636 msg->next = 0;
6637 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6638 }
6639
Willy Tarreauf003d372012-11-26 13:35:37 +01006640 if (res->flags & CF_SHUTW)
6641 goto aborted_xfer;
6642
6643 /* stop waiting for data if the input is closed before the end. If the
6644 * client side was already closed, it means that the client has aborted,
6645 * so we don't want to count this as a server abort. Otherwise it's a
6646 * server abort.
6647 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006648 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006649 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006650 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006651 if (!(s->flags & SN_ERR_MASK))
6652 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006653 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006654 if (objt_server(s->target))
6655 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006656 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006657 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006658
Willy Tarreau40dba092010-03-04 18:14:51 +01006659 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006660 if (!s->req->analysers)
6661 goto return_bad_res;
6662
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006663 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006664 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006665 * Similarly, with keep-alive on the client side, we don't want to forward a
6666 * close.
6667 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006668 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006669 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6670 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006671 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006672
Willy Tarreau5c620922011-05-11 19:56:11 +02006673 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006674 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006675 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006676 * modes are already handled by the stream sock layer. We must not do
6677 * this in content-length mode because it could present the MSG_MORE
6678 * flag with the last block of forwarded data, which would cause an
6679 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006680 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006681 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006682 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006683
Willy Tarreaud98cf932009-12-27 22:54:55 +01006684 /* the session handler will take care of timeouts and errors */
6685 return 0;
6686
Willy Tarreau40dba092010-03-04 18:14:51 +01006687 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006688 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006689 if (objt_server(s->target))
6690 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006691
6692 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006693 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006694 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006695 compressing = 0;
6696 }
6697
Willy Tarreauc623c172014-04-18 09:53:50 +02006698 /* we may have some pending data starting at res->buf->p */
6699 if (s->comp_algo == NULL) {
6700 b_adv(res->buf, msg->next);
6701 msg->next = 0;
6702 }
6703
Willy Tarreaud98cf932009-12-27 22:54:55 +01006704 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006705 /* don't send any error message as we're in the body */
6706 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006707 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006708 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006709 if (objt_server(s->target))
6710 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006711
6712 if (!(s->flags & SN_ERR_MASK))
6713 s->flags |= SN_ERR_PRXCOND;
6714 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006715 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006716 return 0;
6717
6718 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006719 if (unlikely(compressing)) {
6720 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6721 compressing = 0;
6722 }
6723
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006724 txn->rsp.msg_state = HTTP_MSG_ERROR;
6725 /* don't send any error message as we're in the body */
6726 stream_int_retnclose(res->cons, NULL);
6727 res->analysers = 0;
6728 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6729
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006730 s->fe->fe_counters.cli_aborts++;
6731 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006732 if (objt_server(s->target))
6733 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006734
6735 if (!(s->flags & SN_ERR_MASK))
6736 s->flags |= SN_ERR_CLICL;
6737 if (!(s->flags & SN_FINST_MASK))
6738 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006739 return 0;
6740}
6741
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006742/* Iterate the same filter through all request headers.
6743 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006744 * Since it can manage the switch to another backend, it updates the per-proxy
6745 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006746 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006747int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006748{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006749 char term;
6750 char *cur_ptr, *cur_end, *cur_next;
6751 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006752 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006753 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006754 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006755
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006756 last_hdr = 0;
6757
Willy Tarreau9b28e032012-10-12 23:49:43 +02006758 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006759 old_idx = 0;
6760
6761 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006762 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006763 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006764 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006765 (exp->action == ACT_ALLOW ||
6766 exp->action == ACT_DENY ||
6767 exp->action == ACT_TARPIT))
6768 return 0;
6769
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006770 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006771 if (!cur_idx)
6772 break;
6773
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006774 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006775 cur_ptr = cur_next;
6776 cur_end = cur_ptr + cur_hdr->len;
6777 cur_next = cur_end + cur_hdr->cr + 1;
6778
6779 /* Now we have one header between cur_ptr and cur_end,
6780 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006781 */
6782
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006783 /* The annoying part is that pattern matching needs
6784 * that we modify the contents to null-terminate all
6785 * strings before testing them.
6786 */
6787
6788 term = *cur_end;
6789 *cur_end = '\0';
6790
6791 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6792 switch (exp->action) {
6793 case ACT_SETBE:
6794 /* It is not possible to jump a second time.
6795 * FIXME: should we return an HTTP/500 here so that
6796 * the admin knows there's a problem ?
6797 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006798 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006799 break;
6800
6801 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006802 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006803 last_hdr = 1;
6804 break;
6805
6806 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006807 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006808 last_hdr = 1;
6809 break;
6810
6811 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006812 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006813 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006814 break;
6815
6816 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006817 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006818 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006819 break;
6820
6821 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006822 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6823 if (trash.len < 0)
6824 return -1;
6825
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006826 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006827 /* FIXME: if the user adds a newline in the replacement, the
6828 * index will not be recalculated for now, and the new line
6829 * will not be counted as a new header.
6830 */
6831
6832 cur_end += delta;
6833 cur_next += delta;
6834 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006835 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006836 break;
6837
6838 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006839 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006840 cur_next += delta;
6841
Willy Tarreaufa355d42009-11-29 18:12:29 +01006842 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006843 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6844 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006845 cur_hdr->len = 0;
6846 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006847 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006848 break;
6849
6850 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006851 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006852 if (cur_end)
6853 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006854
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006855 /* keep the link from this header to next one in case of later
6856 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006857 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006858 old_idx = cur_idx;
6859 }
6860 return 0;
6861}
6862
6863
6864/* Apply the filter to the request line.
6865 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6866 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006867 * Since it can manage the switch to another backend, it updates the per-proxy
6868 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006869 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006870int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006871{
6872 char term;
6873 char *cur_ptr, *cur_end;
6874 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006875 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006876 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006877
Willy Tarreau3d300592007-03-18 18:34:41 +01006878 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006879 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006880 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006881 (exp->action == ACT_ALLOW ||
6882 exp->action == ACT_DENY ||
6883 exp->action == ACT_TARPIT))
6884 return 0;
6885 else if (exp->action == ACT_REMOVE)
6886 return 0;
6887
6888 done = 0;
6889
Willy Tarreau9b28e032012-10-12 23:49:43 +02006890 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006891 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006892
6893 /* Now we have the request line between cur_ptr and cur_end */
6894
6895 /* The annoying part is that pattern matching needs
6896 * that we modify the contents to null-terminate all
6897 * strings before testing them.
6898 */
6899
6900 term = *cur_end;
6901 *cur_end = '\0';
6902
6903 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6904 switch (exp->action) {
6905 case ACT_SETBE:
6906 /* It is not possible to jump a second time.
6907 * FIXME: should we return an HTTP/500 here so that
6908 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006909 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006910 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006911 break;
6912
6913 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006914 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006915 done = 1;
6916 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006917
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006918 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006919 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006920 done = 1;
6921 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006922
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006923 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006924 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006925 done = 1;
6926 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006927
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006928 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006929 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006930 done = 1;
6931 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006932
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006933 case ACT_REPLACE:
6934 *cur_end = term; /* restore the string terminator */
Sasha Pachevc6002042014-05-26 12:33:48 -06006935 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6936 if (trash.len < 0)
6937 return -1;
6938
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006939 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006940 /* FIXME: if the user adds a newline in the replacement, the
6941 * index will not be recalculated for now, and the new line
6942 * will not be counted as a new header.
6943 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006944
Willy Tarreaufa355d42009-11-29 18:12:29 +01006945 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006946 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006947 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006948 HTTP_MSG_RQMETH,
6949 cur_ptr, cur_end + 1,
6950 NULL, NULL);
6951 if (unlikely(!cur_end))
6952 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006953
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006954 /* we have a full request and we know that we have either a CR
6955 * or an LF at <ptr>.
6956 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006957 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6958 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006959 /* there is no point trying this regex on headers */
6960 return 1;
6961 }
6962 }
6963 *cur_end = term; /* restore the string terminator */
6964 return done;
6965}
Willy Tarreau97de6242006-12-27 17:18:38 +01006966
Willy Tarreau58f10d72006-12-04 02:26:12 +01006967
Willy Tarreau58f10d72006-12-04 02:26:12 +01006968
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006969/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006970 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006971 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006972 * unparsable request. Since it can manage the switch to another backend, it
6973 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006974 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006975int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006976{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006977 struct http_txn *txn = &s->txn;
6978 struct hdr_exp *exp;
6979
6980 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006981 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006982
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006983 /*
6984 * The interleaving of transformations and verdicts
6985 * makes it difficult to decide to continue or stop
6986 * the evaluation.
6987 */
6988
Willy Tarreau6c123b12010-01-28 20:22:06 +01006989 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6990 break;
6991
Willy Tarreau3d300592007-03-18 18:34:41 +01006992 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006993 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006994 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006995 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006996
6997 /* if this filter had a condition, evaluate it now and skip to
6998 * next filter if the condition does not match.
6999 */
7000 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007001 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007002 ret = acl_pass(ret);
7003 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7004 ret = !ret;
7005
7006 if (!ret)
7007 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007008 }
7009
7010 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007011 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007012 if (unlikely(ret < 0))
7013 return -1;
7014
7015 if (likely(ret == 0)) {
7016 /* The filter did not match the request, it can be
7017 * iterated through all headers.
7018 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007019 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007020 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007021 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007022 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007023}
7024
7025
Willy Tarreaua15645d2007-03-18 16:22:39 +01007026
Willy Tarreau58f10d72006-12-04 02:26:12 +01007027/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007028 * Try to retrieve the server associated to the appsession.
7029 * If the server is found, it's assigned to the session.
7030 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007031void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7032 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007033 appsess *asession = NULL;
7034 char *sessid_temp = NULL;
7035
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007036 if (len > s->be->appsession_len) {
7037 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007038 }
7039
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007040 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007041 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007042 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007043 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007044 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007045 }
7046
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007047 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007048 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007049 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007050 return;
7051 }
7052
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007053 memcpy(txn->sessid, buf, len);
7054 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007055 }
7056
7057 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7058 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007059 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007060 return;
7061 }
7062
Cyril Bontéb21570a2009-11-29 20:04:48 +01007063 memcpy(sessid_temp, buf, len);
7064 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007065
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007066 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007067 /* free previously allocated memory */
7068 pool_free2(apools.sessid, sessid_temp);
7069
7070 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007071 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7072 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007073 asession->request_count++;
7074
7075 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007076 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007077
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007078 while (srv) {
7079 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007080 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007081 (s->be->options & PR_O_PERSIST) ||
7082 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007083 /* we found the server and it's usable */
7084 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007085 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007086 s->flags |= SN_DIRECT | SN_ASSIGNED;
7087 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007088
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007089 break;
7090 } else {
7091 txn->flags &= ~TX_CK_MASK;
7092 txn->flags |= TX_CK_DOWN;
7093 }
7094 }
7095 srv = srv->next;
7096 }
7097 }
7098 }
7099}
7100
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007101/* Find the end of a cookie value contained between <s> and <e>. It works the
7102 * same way as with headers above except that the semi-colon also ends a token.
7103 * See RFC2965 for more information. Note that it requires a valid header to
7104 * return a valid result.
7105 */
7106char *find_cookie_value_end(char *s, const char *e)
7107{
7108 int quoted, qdpair;
7109
7110 quoted = qdpair = 0;
7111 for (; s < e; s++) {
7112 if (qdpair) qdpair = 0;
7113 else if (quoted) {
7114 if (*s == '\\') qdpair = 1;
7115 else if (*s == '"') quoted = 0;
7116 }
7117 else if (*s == '"') quoted = 1;
7118 else if (*s == ',' || *s == ';') return s;
7119 }
7120 return s;
7121}
7122
7123/* Delete a value in a header between delimiters <from> and <next> in buffer
7124 * <buf>. The number of characters displaced is returned, and the pointer to
7125 * the first delimiter is updated if required. The function tries as much as
7126 * possible to respect the following principles :
7127 * - replace <from> delimiter by the <next> one unless <from> points to a
7128 * colon, in which case <next> is simply removed
7129 * - set exactly one space character after the new first delimiter, unless
7130 * there are not enough characters in the block being moved to do so.
7131 * - remove unneeded spaces before the previous delimiter and after the new
7132 * one.
7133 *
7134 * It is the caller's responsibility to ensure that :
7135 * - <from> points to a valid delimiter or the colon ;
7136 * - <next> points to a valid delimiter or the final CR/LF ;
7137 * - there are non-space chars before <from> ;
7138 * - there is a CR/LF at or after <next>.
7139 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007140int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007141{
7142 char *prev = *from;
7143
7144 if (*prev == ':') {
7145 /* We're removing the first value, preserve the colon and add a
7146 * space if possible.
7147 */
7148 if (!http_is_crlf[(unsigned char)*next])
7149 next++;
7150 prev++;
7151 if (prev < next)
7152 *prev++ = ' ';
7153
7154 while (http_is_spht[(unsigned char)*next])
7155 next++;
7156 } else {
7157 /* Remove useless spaces before the old delimiter. */
7158 while (http_is_spht[(unsigned char)*(prev-1)])
7159 prev--;
7160 *from = prev;
7161
7162 /* copy the delimiter and if possible a space if we're
7163 * not at the end of the line.
7164 */
7165 if (!http_is_crlf[(unsigned char)*next]) {
7166 *prev++ = *next++;
7167 if (prev + 1 < next)
7168 *prev++ = ' ';
7169 while (http_is_spht[(unsigned char)*next])
7170 next++;
7171 }
7172 }
7173 return buffer_replace2(buf, prev, next, NULL, 0);
7174}
7175
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007176/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007177 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007178 * desirable to call it only when needed. This code is quite complex because
7179 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7180 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007181 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007182void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007183{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007184 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007185 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007186 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007187 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7188 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007189
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007190 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007191 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007192 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007193
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007194 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007195 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007196 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007197
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007198 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007199 hdr_beg = hdr_next;
7200 hdr_end = hdr_beg + cur_hdr->len;
7201 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007202
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007203 /* We have one full header between hdr_beg and hdr_end, and the
7204 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007205 * "Cookie:" headers.
7206 */
7207
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007208 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007209 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007210 old_idx = cur_idx;
7211 continue;
7212 }
7213
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007214 del_from = NULL; /* nothing to be deleted */
7215 preserve_hdr = 0; /* assume we may kill the whole header */
7216
Willy Tarreau58f10d72006-12-04 02:26:12 +01007217 /* Now look for cookies. Conforming to RFC2109, we have to support
7218 * attributes whose name begin with a '$', and associate them with
7219 * the right cookie, if we want to delete this cookie.
7220 * So there are 3 cases for each cookie read :
7221 * 1) it's a special attribute, beginning with a '$' : ignore it.
7222 * 2) it's a server id cookie that we *MAY* want to delete : save
7223 * some pointers on it (last semi-colon, beginning of cookie...)
7224 * 3) it's an application cookie : we *MAY* have to delete a previous
7225 * "special" cookie.
7226 * At the end of loop, if a "special" cookie remains, we may have to
7227 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007228 * *MUST* delete it.
7229 *
7230 * Note: RFC2965 is unclear about the processing of spaces around
7231 * the equal sign in the ATTR=VALUE form. A careful inspection of
7232 * the RFC explicitly allows spaces before it, and not within the
7233 * tokens (attrs or values). An inspection of RFC2109 allows that
7234 * too but section 10.1.3 lets one think that spaces may be allowed
7235 * after the equal sign too, resulting in some (rare) buggy
7236 * implementations trying to do that. So let's do what servers do.
7237 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7238 * allowed quoted strings in values, with any possible character
7239 * after a backslash, including control chars and delimitors, which
7240 * causes parsing to become ambiguous. Browsers also allow spaces
7241 * within values even without quotes.
7242 *
7243 * We have to keep multiple pointers in order to support cookie
7244 * removal at the beginning, middle or end of header without
7245 * corrupting the header. All of these headers are valid :
7246 *
7247 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7248 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7249 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7250 * | | | | | | | | |
7251 * | | | | | | | | hdr_end <--+
7252 * | | | | | | | +--> next
7253 * | | | | | | +----> val_end
7254 * | | | | | +-----------> val_beg
7255 * | | | | +--------------> equal
7256 * | | | +----------------> att_end
7257 * | | +---------------------> att_beg
7258 * | +--------------------------> prev
7259 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007260 */
7261
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007262 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7263 /* Iterate through all cookies on this line */
7264
7265 /* find att_beg */
7266 att_beg = prev + 1;
7267 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7268 att_beg++;
7269
7270 /* find att_end : this is the first character after the last non
7271 * space before the equal. It may be equal to hdr_end.
7272 */
7273 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007274
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007275 while (equal < hdr_end) {
7276 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007277 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007278 if (http_is_spht[(unsigned char)*equal++])
7279 continue;
7280 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007281 }
7282
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007283 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7284 * is between <att_beg> and <equal>, both may be identical.
7285 */
7286
7287 /* look for end of cookie if there is an equal sign */
7288 if (equal < hdr_end && *equal == '=') {
7289 /* look for the beginning of the value */
7290 val_beg = equal + 1;
7291 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7292 val_beg++;
7293
7294 /* find the end of the value, respecting quotes */
7295 next = find_cookie_value_end(val_beg, hdr_end);
7296
7297 /* make val_end point to the first white space or delimitor after the value */
7298 val_end = next;
7299 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7300 val_end--;
7301 } else {
7302 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007303 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007304
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007305 /* We have nothing to do with attributes beginning with '$'. However,
7306 * they will automatically be removed if a header before them is removed,
7307 * since they're supposed to be linked together.
7308 */
7309 if (*att_beg == '$')
7310 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007311
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007312 /* Ignore cookies with no equal sign */
7313 if (equal == next) {
7314 /* This is not our cookie, so we must preserve it. But if we already
7315 * scheduled another cookie for removal, we cannot remove the
7316 * complete header, but we can remove the previous block itself.
7317 */
7318 preserve_hdr = 1;
7319 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007320 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007321 val_end += delta;
7322 next += delta;
7323 hdr_end += delta;
7324 hdr_next += delta;
7325 cur_hdr->len += delta;
7326 http_msg_move_end(&txn->req, delta);
7327 prev = del_from;
7328 del_from = NULL;
7329 }
7330 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007331 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007332
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007333 /* if there are spaces around the equal sign, we need to
7334 * strip them otherwise we'll get trouble for cookie captures,
7335 * or even for rewrites. Since this happens extremely rarely,
7336 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007337 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007338 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7339 int stripped_before = 0;
7340 int stripped_after = 0;
7341
7342 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007343 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007344 equal += stripped_before;
7345 val_beg += stripped_before;
7346 }
7347
7348 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007349 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007350 val_beg += stripped_after;
7351 stripped_before += stripped_after;
7352 }
7353
7354 val_end += stripped_before;
7355 next += stripped_before;
7356 hdr_end += stripped_before;
7357 hdr_next += stripped_before;
7358 cur_hdr->len += stripped_before;
7359 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007360 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007361 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007362
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007363 /* First, let's see if we want to capture this cookie. We check
7364 * that we don't already have a client side cookie, because we
7365 * can only capture one. Also as an optimisation, we ignore
7366 * cookies shorter than the declared name.
7367 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007368 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7369 (val_end - att_beg >= s->fe->capture_namelen) &&
7370 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007371 int log_len = val_end - att_beg;
7372
7373 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7374 Alert("HTTP logging : out of memory.\n");
7375 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007376 if (log_len > s->fe->capture_len)
7377 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007378 memcpy(txn->cli_cookie, att_beg, log_len);
7379 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007380 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007381 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007382
Willy Tarreaubca99692010-10-06 19:25:55 +02007383 /* Persistence cookies in passive, rewrite or insert mode have the
7384 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007385 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007386 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007387 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007388 * For cookies in prefix mode, the form is :
7389 *
7390 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007391 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007392 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7393 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7394 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007395 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007396
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007397 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7398 * have the server ID between val_beg and delim, and the original cookie between
7399 * delim+1 and val_end. Otherwise, delim==val_end :
7400 *
7401 * Cookie: NAME=SRV; # in all but prefix modes
7402 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7403 * | || || | |+-> next
7404 * | || || | +--> val_end
7405 * | || || +---------> delim
7406 * | || |+------------> val_beg
7407 * | || +-------------> att_end = equal
7408 * | |+-----------------> att_beg
7409 * | +------------------> prev
7410 * +-------------------------> hdr_beg
7411 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007412
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007413 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007414 for (delim = val_beg; delim < val_end; delim++)
7415 if (*delim == COOKIE_DELIM)
7416 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007417 } else {
7418 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007419 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007420 /* Now check if the cookie contains a date field, which would
7421 * appear after a vertical bar ('|') just after the server name
7422 * and before the delimiter.
7423 */
7424 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7425 if (vbar1) {
7426 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007427 * right is the last seen date. It is a base64 encoded
7428 * 30-bit value representing the UNIX date since the
7429 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007430 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007431 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007432 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007433 if (val_end - vbar1 >= 5) {
7434 val = b64tos30(vbar1);
7435 if (val > 0)
7436 txn->cookie_last_date = val << 2;
7437 }
7438 /* look for a second vertical bar */
7439 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7440 if (vbar1 && (val_end - vbar1 > 5)) {
7441 val = b64tos30(vbar1 + 1);
7442 if (val > 0)
7443 txn->cookie_first_date = val << 2;
7444 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007445 }
7446 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007447
Willy Tarreauf64d1412010-10-07 20:06:11 +02007448 /* if the cookie has an expiration date and the proxy wants to check
7449 * it, then we do that now. We first check if the cookie is too old,
7450 * then only if it has expired. We detect strict overflow because the
7451 * time resolution here is not great (4 seconds). Cookies with dates
7452 * in the future are ignored if their offset is beyond one day. This
7453 * allows an admin to fix timezone issues without expiring everyone
7454 * and at the same time avoids keeping unwanted side effects for too
7455 * long.
7456 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007457 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7458 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007459 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007460 txn->flags &= ~TX_CK_MASK;
7461 txn->flags |= TX_CK_OLD;
7462 delim = val_beg; // let's pretend we have not found the cookie
7463 txn->cookie_first_date = 0;
7464 txn->cookie_last_date = 0;
7465 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007466 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7467 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007468 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007469 txn->flags &= ~TX_CK_MASK;
7470 txn->flags |= TX_CK_EXPIRED;
7471 delim = val_beg; // let's pretend we have not found the cookie
7472 txn->cookie_first_date = 0;
7473 txn->cookie_last_date = 0;
7474 }
7475
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007476 /* Here, we'll look for the first running server which supports the cookie.
7477 * This allows to share a same cookie between several servers, for example
7478 * to dedicate backup servers to specific servers only.
7479 * However, to prevent clients from sticking to cookie-less backup server
7480 * when they have incidentely learned an empty cookie, we simply ignore
7481 * empty cookies and mark them as invalid.
7482 * The same behaviour is applied when persistence must be ignored.
7483 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007484 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007485 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007486
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007487 while (srv) {
7488 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7489 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007490 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007491 (s->be->options & PR_O_PERSIST) ||
7492 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007493 /* we found the server and we can use it */
7494 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007495 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007496 s->flags |= SN_DIRECT | SN_ASSIGNED;
7497 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007498 break;
7499 } else {
7500 /* we found a server, but it's down,
7501 * mark it as such and go on in case
7502 * another one is available.
7503 */
7504 txn->flags &= ~TX_CK_MASK;
7505 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007506 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007507 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007508 srv = srv->next;
7509 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007510
Willy Tarreauf64d1412010-10-07 20:06:11 +02007511 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007512 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007513 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007514 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007515 txn->flags |= TX_CK_UNUSED;
7516 else
7517 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007518 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007519
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007520 /* depending on the cookie mode, we may have to either :
7521 * - delete the complete cookie if we're in insert+indirect mode, so that
7522 * the server never sees it ;
7523 * - remove the server id from the cookie value, and tag the cookie as an
7524 * application cookie so that it does not get accidentely removed later,
7525 * if we're in cookie prefix mode
7526 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007527 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007528 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007529
Willy Tarreau9b28e032012-10-12 23:49:43 +02007530 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007531 val_end += delta;
7532 next += delta;
7533 hdr_end += delta;
7534 hdr_next += delta;
7535 cur_hdr->len += delta;
7536 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007537
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007538 del_from = NULL;
7539 preserve_hdr = 1; /* we want to keep this cookie */
7540 }
7541 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007542 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007543 del_from = prev;
7544 }
7545 } else {
7546 /* This is not our cookie, so we must preserve it. But if we already
7547 * scheduled another cookie for removal, we cannot remove the
7548 * complete header, but we can remove the previous block itself.
7549 */
7550 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007551
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007552 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007553 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007554 if (att_beg >= del_from)
7555 att_beg += delta;
7556 if (att_end >= del_from)
7557 att_end += delta;
7558 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007559 val_end += delta;
7560 next += delta;
7561 hdr_end += delta;
7562 hdr_next += delta;
7563 cur_hdr->len += delta;
7564 http_msg_move_end(&txn->req, delta);
7565 prev = del_from;
7566 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007567 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007568 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007569
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007570 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007571 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007572 int cmp_len, value_len;
7573 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007574
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007575 if (s->be->options2 & PR_O2_AS_PFX) {
7576 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7577 value_begin = att_beg + s->be->appsession_name_len;
7578 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007579 } else {
7580 cmp_len = att_end - att_beg;
7581 value_begin = val_beg;
7582 value_len = val_end - val_beg;
7583 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007584
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007585 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007586 if (cmp_len == s->be->appsession_name_len &&
7587 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7588 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007589 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007590 }
7591
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007592 /* continue with next cookie on this header line */
7593 att_beg = next;
7594 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007595
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007596 /* There are no more cookies on this line.
7597 * We may still have one (or several) marked for deletion at the
7598 * end of the line. We must do this now in two ways :
7599 * - if some cookies must be preserved, we only delete from the
7600 * mark to the end of line ;
7601 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007602 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007603 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007604 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007605 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007606 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007607 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007608 cur_hdr->len += delta;
7609 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007610 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007611
7612 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007613 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7614 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007615 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007616 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007617 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007618 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007619 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007620 }
7621
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007622 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007623 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007624 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007625}
7626
7627
Willy Tarreaua15645d2007-03-18 16:22:39 +01007628/* Iterate the same filter through all response headers contained in <rtr>.
7629 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7630 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007631int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007632{
7633 char term;
7634 char *cur_ptr, *cur_end, *cur_next;
7635 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007636 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007637 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007638 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007639
7640 last_hdr = 0;
7641
Willy Tarreau9b28e032012-10-12 23:49:43 +02007642 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007643 old_idx = 0;
7644
7645 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007646 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007647 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007648 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007649 (exp->action == ACT_ALLOW ||
7650 exp->action == ACT_DENY))
7651 return 0;
7652
7653 cur_idx = txn->hdr_idx.v[old_idx].next;
7654 if (!cur_idx)
7655 break;
7656
7657 cur_hdr = &txn->hdr_idx.v[cur_idx];
7658 cur_ptr = cur_next;
7659 cur_end = cur_ptr + cur_hdr->len;
7660 cur_next = cur_end + cur_hdr->cr + 1;
7661
7662 /* Now we have one header between cur_ptr and cur_end,
7663 * and the next header starts at cur_next.
7664 */
7665
7666 /* The annoying part is that pattern matching needs
7667 * that we modify the contents to null-terminate all
7668 * strings before testing them.
7669 */
7670
7671 term = *cur_end;
7672 *cur_end = '\0';
7673
7674 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7675 switch (exp->action) {
7676 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007677 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007678 last_hdr = 1;
7679 break;
7680
7681 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007682 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007683 last_hdr = 1;
7684 break;
7685
7686 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007687 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7688 if (trash.len < 0)
7689 return -1;
7690
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007691 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007692 /* FIXME: if the user adds a newline in the replacement, the
7693 * index will not be recalculated for now, and the new line
7694 * will not be counted as a new header.
7695 */
7696
7697 cur_end += delta;
7698 cur_next += delta;
7699 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007700 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007701 break;
7702
7703 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007704 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007705 cur_next += delta;
7706
Willy Tarreaufa355d42009-11-29 18:12:29 +01007707 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007708 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7709 txn->hdr_idx.used--;
7710 cur_hdr->len = 0;
7711 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007712 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007713 break;
7714
7715 }
7716 }
7717 if (cur_end)
7718 *cur_end = term; /* restore the string terminator */
7719
7720 /* keep the link from this header to next one in case of later
7721 * removal of next header.
7722 */
7723 old_idx = cur_idx;
7724 }
7725 return 0;
7726}
7727
7728
7729/* Apply the filter to the status line in the response buffer <rtr>.
7730 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7731 * or -1 if a replacement resulted in an invalid status line.
7732 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007733int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007734{
7735 char term;
7736 char *cur_ptr, *cur_end;
7737 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007738 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007739 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007740
7741
Willy Tarreau3d300592007-03-18 18:34:41 +01007742 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007743 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007744 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007745 (exp->action == ACT_ALLOW ||
7746 exp->action == ACT_DENY))
7747 return 0;
7748 else if (exp->action == ACT_REMOVE)
7749 return 0;
7750
7751 done = 0;
7752
Willy Tarreau9b28e032012-10-12 23:49:43 +02007753 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007754 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007755
7756 /* Now we have the status line between cur_ptr and cur_end */
7757
7758 /* The annoying part is that pattern matching needs
7759 * that we modify the contents to null-terminate all
7760 * strings before testing them.
7761 */
7762
7763 term = *cur_end;
7764 *cur_end = '\0';
7765
7766 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7767 switch (exp->action) {
7768 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007769 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007770 done = 1;
7771 break;
7772
7773 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007774 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007775 done = 1;
7776 break;
7777
7778 case ACT_REPLACE:
7779 *cur_end = term; /* restore the string terminator */
Sasha Pachevc6002042014-05-26 12:33:48 -06007780 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7781 if (trash.len < 0)
7782 return -1;
7783
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007784 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007785 /* FIXME: if the user adds a newline in the replacement, the
7786 * index will not be recalculated for now, and the new line
7787 * will not be counted as a new header.
7788 */
7789
Willy Tarreaufa355d42009-11-29 18:12:29 +01007790 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007791 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007792 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007793 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007794 cur_ptr, cur_end + 1,
7795 NULL, NULL);
7796 if (unlikely(!cur_end))
7797 return -1;
7798
7799 /* we have a full respnse and we know that we have either a CR
7800 * or an LF at <ptr>.
7801 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007802 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007803 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007804 /* there is no point trying this regex on headers */
7805 return 1;
7806 }
7807 }
7808 *cur_end = term; /* restore the string terminator */
7809 return done;
7810}
7811
7812
7813
7814/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007815 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007816 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7817 * unparsable response.
7818 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007819int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007820{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007821 struct http_txn *txn = &s->txn;
7822 struct hdr_exp *exp;
7823
7824 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007825 int ret;
7826
7827 /*
7828 * The interleaving of transformations and verdicts
7829 * makes it difficult to decide to continue or stop
7830 * the evaluation.
7831 */
7832
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007833 if (txn->flags & TX_SVDENY)
7834 break;
7835
Willy Tarreau3d300592007-03-18 18:34:41 +01007836 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007837 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7838 exp->action == ACT_PASS)) {
7839 exp = exp->next;
7840 continue;
7841 }
7842
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007843 /* if this filter had a condition, evaluate it now and skip to
7844 * next filter if the condition does not match.
7845 */
7846 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007847 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007848 ret = acl_pass(ret);
7849 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7850 ret = !ret;
7851 if (!ret)
7852 continue;
7853 }
7854
Willy Tarreaua15645d2007-03-18 16:22:39 +01007855 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007856 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007857 if (unlikely(ret < 0))
7858 return -1;
7859
7860 if (likely(ret == 0)) {
7861 /* The filter did not match the response, it can be
7862 * iterated through all headers.
7863 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007864 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7865 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007866 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007867 }
7868 return 0;
7869}
7870
7871
Willy Tarreaua15645d2007-03-18 16:22:39 +01007872/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007873 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007874 * desirable to call it only when needed. This function is also used when we
7875 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007876 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007877void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007878{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007879 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007880 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007881 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007882 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007883 char *hdr_beg, *hdr_end, *hdr_next;
7884 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007885
Willy Tarreaua15645d2007-03-18 16:22:39 +01007886 /* Iterate through the headers.
7887 * we start with the start line.
7888 */
7889 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007890 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007891
7892 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7893 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007894 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007895
7896 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007897 hdr_beg = hdr_next;
7898 hdr_end = hdr_beg + cur_hdr->len;
7899 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007900
Willy Tarreau24581ba2010-08-31 22:39:35 +02007901 /* We have one full header between hdr_beg and hdr_end, and the
7902 * next header starts at hdr_next. We're only interested in
7903 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007904 */
7905
Willy Tarreau24581ba2010-08-31 22:39:35 +02007906 is_cookie2 = 0;
7907 prev = hdr_beg + 10;
7908 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007909 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007910 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7911 if (!val) {
7912 old_idx = cur_idx;
7913 continue;
7914 }
7915 is_cookie2 = 1;
7916 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007917 }
7918
Willy Tarreau24581ba2010-08-31 22:39:35 +02007919 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7920 * <prev> points to the colon.
7921 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007922 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007923
Willy Tarreau24581ba2010-08-31 22:39:35 +02007924 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7925 * check-cache is enabled) and we are not interested in checking
7926 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007927 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007928 if (s->be->cookie_name == NULL &&
7929 s->be->appsession_name == NULL &&
7930 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007931 return;
7932
Willy Tarreau24581ba2010-08-31 22:39:35 +02007933 /* OK so now we know we have to process this response cookie.
7934 * The format of the Set-Cookie header is slightly different
7935 * from the format of the Cookie header in that it does not
7936 * support the comma as a cookie delimiter (thus the header
7937 * cannot be folded) because the Expires attribute described in
7938 * the original Netscape's spec may contain an unquoted date
7939 * with a comma inside. We have to live with this because
7940 * many browsers don't support Max-Age and some browsers don't
7941 * support quoted strings. However the Set-Cookie2 header is
7942 * clean.
7943 *
7944 * We have to keep multiple pointers in order to support cookie
7945 * removal at the beginning, middle or end of header without
7946 * corrupting the header (in case of set-cookie2). A special
7947 * pointer, <scav> points to the beginning of the set-cookie-av
7948 * fields after the first semi-colon. The <next> pointer points
7949 * either to the end of line (set-cookie) or next unquoted comma
7950 * (set-cookie2). All of these headers are valid :
7951 *
7952 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7953 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7954 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7955 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7956 * | | | | | | | | | |
7957 * | | | | | | | | +-> next hdr_end <--+
7958 * | | | | | | | +------------> scav
7959 * | | | | | | +--------------> val_end
7960 * | | | | | +--------------------> val_beg
7961 * | | | | +----------------------> equal
7962 * | | | +------------------------> att_end
7963 * | | +----------------------------> att_beg
7964 * | +------------------------------> prev
7965 * +-----------------------------------------> hdr_beg
7966 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007967
Willy Tarreau24581ba2010-08-31 22:39:35 +02007968 for (; prev < hdr_end; prev = next) {
7969 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007970
Willy Tarreau24581ba2010-08-31 22:39:35 +02007971 /* find att_beg */
7972 att_beg = prev + 1;
7973 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7974 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007975
Willy Tarreau24581ba2010-08-31 22:39:35 +02007976 /* find att_end : this is the first character after the last non
7977 * space before the equal. It may be equal to hdr_end.
7978 */
7979 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007980
Willy Tarreau24581ba2010-08-31 22:39:35 +02007981 while (equal < hdr_end) {
7982 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7983 break;
7984 if (http_is_spht[(unsigned char)*equal++])
7985 continue;
7986 att_end = equal;
7987 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007988
Willy Tarreau24581ba2010-08-31 22:39:35 +02007989 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7990 * is between <att_beg> and <equal>, both may be identical.
7991 */
7992
7993 /* look for end of cookie if there is an equal sign */
7994 if (equal < hdr_end && *equal == '=') {
7995 /* look for the beginning of the value */
7996 val_beg = equal + 1;
7997 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7998 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007999
Willy Tarreau24581ba2010-08-31 22:39:35 +02008000 /* find the end of the value, respecting quotes */
8001 next = find_cookie_value_end(val_beg, hdr_end);
8002
8003 /* make val_end point to the first white space or delimitor after the value */
8004 val_end = next;
8005 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8006 val_end--;
8007 } else {
8008 /* <equal> points to next comma, semi-colon or EOL */
8009 val_beg = val_end = next = equal;
8010 }
8011
8012 if (next < hdr_end) {
8013 /* Set-Cookie2 supports multiple cookies, and <next> points to
8014 * a colon or semi-colon before the end. So skip all attr-value
8015 * pairs and look for the next comma. For Set-Cookie, since
8016 * commas are permitted in values, skip to the end.
8017 */
8018 if (is_cookie2)
8019 next = find_hdr_value_end(next, hdr_end);
8020 else
8021 next = hdr_end;
8022 }
8023
8024 /* Now everything is as on the diagram above */
8025
8026 /* Ignore cookies with no equal sign */
8027 if (equal == val_end)
8028 continue;
8029
8030 /* If there are spaces around the equal sign, we need to
8031 * strip them otherwise we'll get trouble for cookie captures,
8032 * or even for rewrites. Since this happens extremely rarely,
8033 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008034 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008035 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8036 int stripped_before = 0;
8037 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038
Willy Tarreau24581ba2010-08-31 22:39:35 +02008039 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008040 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008041 equal += stripped_before;
8042 val_beg += stripped_before;
8043 }
8044
8045 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008046 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008047 val_beg += stripped_after;
8048 stripped_before += stripped_after;
8049 }
8050
8051 val_end += stripped_before;
8052 next += stripped_before;
8053 hdr_end += stripped_before;
8054 hdr_next += stripped_before;
8055 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008056 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008057 }
8058
8059 /* First, let's see if we want to capture this cookie. We check
8060 * that we don't already have a server side cookie, because we
8061 * can only capture one. Also as an optimisation, we ignore
8062 * cookies shorter than the declared name.
8063 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008064 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008065 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008066 (val_end - att_beg >= s->fe->capture_namelen) &&
8067 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008068 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008069 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008070 Alert("HTTP logging : out of memory.\n");
8071 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008072 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008073 if (log_len > s->fe->capture_len)
8074 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008075 memcpy(txn->srv_cookie, att_beg, log_len);
8076 txn->srv_cookie[log_len] = 0;
8077 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008078 }
8079
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008080 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008081 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008082 if (!(s->flags & SN_IGNORE_PRST) &&
8083 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8084 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008085 /* assume passive cookie by default */
8086 txn->flags &= ~TX_SCK_MASK;
8087 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008088
8089 /* If the cookie is in insert mode on a known server, we'll delete
8090 * this occurrence because we'll insert another one later.
8091 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008092 * a direct access.
8093 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008094 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008095 /* The "preserve" flag was set, we don't want to touch the
8096 * server's cookie.
8097 */
8098 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008099 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8100 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008101 /* this cookie must be deleted */
8102 if (*prev == ':' && next == hdr_end) {
8103 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008104 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008105 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8106 txn->hdr_idx.used--;
8107 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008108 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008109 hdr_next += delta;
8110 http_msg_move_end(&txn->rsp, delta);
8111 /* note: while both invalid now, <next> and <hdr_end>
8112 * are still equal, so the for() will stop as expected.
8113 */
8114 } else {
8115 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008116 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008117 next = prev;
8118 hdr_end += delta;
8119 hdr_next += delta;
8120 cur_hdr->len += delta;
8121 http_msg_move_end(&txn->rsp, delta);
8122 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008123 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008124 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008125 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008126 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008127 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008128 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008129 * with this server since we know it.
8130 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008131 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008132 next += delta;
8133 hdr_end += delta;
8134 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008135 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008136 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008137
Willy Tarreauf1348312010-10-07 15:54:11 +02008138 txn->flags &= ~TX_SCK_MASK;
8139 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008140 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008141 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008142 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008143 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008145 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008146 next += delta;
8147 hdr_end += delta;
8148 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008149 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008150 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008151
Willy Tarreau827aee92011-03-10 16:55:02 +01008152 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008153 txn->flags &= ~TX_SCK_MASK;
8154 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008155 }
8156 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008157 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008158 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008159 int cmp_len, value_len;
8160 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008161
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008162 if (s->be->options2 & PR_O2_AS_PFX) {
8163 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8164 value_begin = att_beg + s->be->appsession_name_len;
8165 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008166 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008167 cmp_len = att_end - att_beg;
8168 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008169 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008170 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008171
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008172 if ((cmp_len == s->be->appsession_name_len) &&
8173 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008174 /* free a possibly previously allocated memory */
8175 pool_free2(apools.sessid, txn->sessid);
8176
Cyril Bontéb21570a2009-11-29 20:04:48 +01008177 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008178 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008179 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008180 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008181 return;
8182 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008183 memcpy(txn->sessid, value_begin, value_len);
8184 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008185 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008186 }
8187 /* that's done for this cookie, check the next one on the same
8188 * line when next != hdr_end (only if is_cookie2).
8189 */
8190 }
8191 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008192 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008193 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008194
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008195 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008196 appsess *asession = NULL;
8197 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008198 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008199 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008200 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008201 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8202 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008203 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008204 return;
8205 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008206 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8207
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008208 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8209 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008210 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8211 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008212 return;
8213 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008214 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8215 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008216
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008217 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008218 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008219 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008220 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8221 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008222 return;
8223 }
8224 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008225 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008226
8227 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008228 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008229 }
8230
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008231 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008232 asession->request_count++;
8233 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008234}
8235
8236
Willy Tarreaua15645d2007-03-18 16:22:39 +01008237/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008238 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008239 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008240void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008241{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008242 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008243 char *p1, *p2;
8244
8245 char *cur_ptr, *cur_end, *cur_next;
8246 int cur_idx;
8247
Willy Tarreau5df51872007-11-25 16:20:08 +01008248 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008249 return;
8250
8251 /* Iterate through the headers.
8252 * we start with the start line.
8253 */
8254 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008255 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008256
8257 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8258 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008259 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008260
8261 cur_hdr = &txn->hdr_idx.v[cur_idx];
8262 cur_ptr = cur_next;
8263 cur_end = cur_ptr + cur_hdr->len;
8264 cur_next = cur_end + cur_hdr->cr + 1;
8265
8266 /* We have one full header between cur_ptr and cur_end, and the
8267 * next header starts at cur_next. We're only interested in
8268 * "Cookie:" headers.
8269 */
8270
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008271 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8272 if (val) {
8273 if ((cur_end - (cur_ptr + val) >= 8) &&
8274 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8275 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8276 return;
8277 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008278 }
8279
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008280 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8281 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008282 continue;
8283
8284 /* OK, right now we know we have a cache-control header at cur_ptr */
8285
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008286 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008287
8288 if (p1 >= cur_end) /* no more info */
8289 continue;
8290
8291 /* p1 is at the beginning of the value */
8292 p2 = p1;
8293
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008294 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008295 p2++;
8296
8297 /* we have a complete value between p1 and p2 */
8298 if (p2 < cur_end && *p2 == '=') {
8299 /* we have something of the form no-cache="set-cookie" */
8300 if ((cur_end - p1 >= 21) &&
8301 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8302 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008303 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008304 continue;
8305 }
8306
8307 /* OK, so we know that either p2 points to the end of string or to a comma */
8308 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008309 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008310 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8311 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8312 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008313 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008314 return;
8315 }
8316
8317 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008318 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008319 continue;
8320 }
8321 }
8322}
8323
8324
Willy Tarreau58f10d72006-12-04 02:26:12 +01008325/*
8326 * Try to retrieve a known appsession in the URI, then the associated server.
8327 * If the server is found, it's assigned to the session.
8328 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008329void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008330{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008331 char *end_params, *first_param, *cur_param, *next_param;
8332 char separator;
8333 int value_len;
8334
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008335 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008336
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008337 if (s->be->appsession_name == NULL ||
8338 (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 +01008339 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008340 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008341
Cyril Bontéb21570a2009-11-29 20:04:48 +01008342 first_param = NULL;
8343 switch (mode) {
8344 case PR_O2_AS_M_PP:
8345 first_param = memchr(begin, ';', len);
8346 break;
8347 case PR_O2_AS_M_QS:
8348 first_param = memchr(begin, '?', len);
8349 break;
8350 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008351
Cyril Bontéb21570a2009-11-29 20:04:48 +01008352 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008353 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008354 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008355
Cyril Bontéb21570a2009-11-29 20:04:48 +01008356 switch (mode) {
8357 case PR_O2_AS_M_PP:
8358 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8359 end_params = (char *) begin + len;
8360 }
8361 separator = ';';
8362 break;
8363 case PR_O2_AS_M_QS:
8364 end_params = (char *) begin + len;
8365 separator = '&';
8366 break;
8367 default:
8368 /* unknown mode, shouldn't happen */
8369 return;
8370 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008371
Cyril Bontéb21570a2009-11-29 20:04:48 +01008372 cur_param = next_param = end_params;
8373 while (cur_param > first_param) {
8374 cur_param--;
8375 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8376 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008377 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8378 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8379 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008380 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008381 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8382 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008383 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008384 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008385 }
8386 break;
8387 }
8388 next_param = cur_param;
8389 }
8390 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008391#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008392 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008393 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008394#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008395}
8396
Willy Tarreaub2513902006-12-17 14:52:38 +01008397/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008398 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008399 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008400 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008401 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008402 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008403 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008404 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008405 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008406int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008407{
8408 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008409 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008410 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008411
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008412 if (!uri_auth)
8413 return 0;
8414
Cyril Bonté70be45d2010-10-12 00:14:35 +02008415 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008416 return 0;
8417
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008418 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008419 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008420 return 0;
8421
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008422 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008423 return 0;
8424
Willy Tarreaub2513902006-12-17 14:52:38 +01008425 return 1;
8426}
8427
Willy Tarreau4076a152009-04-02 15:18:36 +02008428/*
8429 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008430 * By default it tries to report the error position as msg->err_pos. However if
8431 * this one is not set, it will then report msg->next, which is the last known
8432 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008433 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008434 */
8435void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008436 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008437 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008438{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008439 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008440 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008441
Willy Tarreau9b28e032012-10-12 23:49:43 +02008442 es->len = MIN(chn->buf->i, sizeof(es->buf));
8443 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008444 len1 = MIN(len1, es->len);
8445 len2 = es->len - len1; /* remaining data if buffer wraps */
8446
Willy Tarreau9b28e032012-10-12 23:49:43 +02008447 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008448 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008449 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008450
Willy Tarreau4076a152009-04-02 15:18:36 +02008451 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008452 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008453 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008454 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008455
Willy Tarreau4076a152009-04-02 15:18:36 +02008456 es->when = date; // user-visible date
8457 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008458 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008459 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008460 if (objt_conn(s->req->prod->end))
8461 es->src = __objt_conn(s->req->prod->end)->addr.from;
8462 else
8463 memset(&es->src, 0, sizeof(es->src));
8464
Willy Tarreau078272e2010-12-12 12:46:33 +01008465 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008466 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008467 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008468 es->s_flags = s->flags;
8469 es->t_flags = s->txn.flags;
8470 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008471 es->b_out = chn->buf->o;
8472 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008473 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008474 es->m_clen = msg->chunk_len;
8475 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008476}
Willy Tarreaub2513902006-12-17 14:52:38 +01008477
Willy Tarreau294c4732011-12-16 21:35:50 +01008478/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8479 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8480 * performed over the whole headers. Otherwise it must contain a valid header
8481 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8482 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8483 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8484 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008485 * -1. The value fetch stops at commas, so this function is suited for use with
8486 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008487 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008488 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008489unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008490 struct hdr_idx *idx, int occ,
8491 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008492{
Willy Tarreau294c4732011-12-16 21:35:50 +01008493 struct hdr_ctx local_ctx;
8494 char *ptr_hist[MAX_HDR_HISTORY];
8495 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008496 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008497 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008498
Willy Tarreau294c4732011-12-16 21:35:50 +01008499 if (!ctx) {
8500 local_ctx.idx = 0;
8501 ctx = &local_ctx;
8502 }
8503
Willy Tarreaubce70882009-09-07 11:51:47 +02008504 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008505 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008506 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008507 occ--;
8508 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008509 *vptr = ctx->line + ctx->val;
8510 *vlen = ctx->vlen;
8511 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008512 }
8513 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008514 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008515 }
8516
8517 /* negative occurrence, we scan all the list then walk back */
8518 if (-occ > MAX_HDR_HISTORY)
8519 return 0;
8520
Willy Tarreau294c4732011-12-16 21:35:50 +01008521 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008522 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008523 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8524 len_hist[hist_ptr] = ctx->vlen;
8525 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008526 hist_ptr = 0;
8527 found++;
8528 }
8529 if (-occ > found)
8530 return 0;
8531 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008532 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8533 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8534 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008535 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008536 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008537 if (hist_ptr >= MAX_HDR_HISTORY)
8538 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008539 *vptr = ptr_hist[hist_ptr];
8540 *vlen = len_hist[hist_ptr];
8541 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008542}
8543
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008544/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8545 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8546 * performed over the whole headers. Otherwise it must contain a valid header
8547 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8548 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8549 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8550 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8551 * -1. This function differs from http_get_hdr() in that it only returns full
8552 * line header values and does not stop at commas.
8553 * The return value is 0 if nothing was found, or non-zero otherwise.
8554 */
8555unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8556 struct hdr_idx *idx, int occ,
8557 struct hdr_ctx *ctx, char **vptr, int *vlen)
8558{
8559 struct hdr_ctx local_ctx;
8560 char *ptr_hist[MAX_HDR_HISTORY];
8561 int len_hist[MAX_HDR_HISTORY];
8562 unsigned int hist_ptr;
8563 int found;
8564
8565 if (!ctx) {
8566 local_ctx.idx = 0;
8567 ctx = &local_ctx;
8568 }
8569
8570 if (occ >= 0) {
8571 /* search from the beginning */
8572 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8573 occ--;
8574 if (occ <= 0) {
8575 *vptr = ctx->line + ctx->val;
8576 *vlen = ctx->vlen;
8577 return 1;
8578 }
8579 }
8580 return 0;
8581 }
8582
8583 /* negative occurrence, we scan all the list then walk back */
8584 if (-occ > MAX_HDR_HISTORY)
8585 return 0;
8586
8587 found = hist_ptr = 0;
8588 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8589 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8590 len_hist[hist_ptr] = ctx->vlen;
8591 if (++hist_ptr >= MAX_HDR_HISTORY)
8592 hist_ptr = 0;
8593 found++;
8594 }
8595 if (-occ > found)
8596 return 0;
8597 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8598 * find occurrence -occ, so we have to check [hist_ptr+occ].
8599 */
8600 hist_ptr += occ;
8601 if (hist_ptr >= MAX_HDR_HISTORY)
8602 hist_ptr -= MAX_HDR_HISTORY;
8603 *vptr = ptr_hist[hist_ptr];
8604 *vlen = len_hist[hist_ptr];
8605 return 1;
8606}
8607
Willy Tarreaubaaee002006-06-26 02:48:02 +02008608/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008609 * Print a debug line with a header. Always stop at the first CR or LF char,
8610 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8611 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008612 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008613void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008614{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008615 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008616 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008617 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008618 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8619 objt_conn(s->req->cons->end) ? (unsigned short)objt_conn(s->req->cons->end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008620
8621 for (max = 0; start + max < end; max++)
8622 if (start[max] == '\r' || start[max] == '\n')
8623 break;
8624
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008625 UBOUND(max, trash.size - trash.len - 3);
8626 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8627 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008628 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008629}
8630
Willy Tarreau0937bc42009-12-22 15:03:09 +01008631/*
8632 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8633 * the required fields are properly allocated and that we only need to (re)init
8634 * them. This should be used before processing any new request.
8635 */
8636void http_init_txn(struct session *s)
8637{
8638 struct http_txn *txn = &s->txn;
8639 struct proxy *fe = s->fe;
8640
8641 txn->flags = 0;
8642 txn->status = -1;
8643
Willy Tarreauf64d1412010-10-07 20:06:11 +02008644 txn->cookie_first_date = 0;
8645 txn->cookie_last_date = 0;
8646
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008647 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008648 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008649 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008650 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008651 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008652 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008653 txn->req.chunk_len = 0LL;
8654 txn->req.body_len = 0LL;
8655 txn->rsp.chunk_len = 0LL;
8656 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008657 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8658 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008659 txn->req.chn = s->req;
8660 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008661
8662 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008663
8664 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8665 if (fe->options2 & PR_O2_REQBUG_OK)
8666 txn->req.err_pos = -1; /* let buggy requests pass */
8667
Willy Tarreau46023632010-01-07 22:51:47 +01008668 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008669 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8670
Willy Tarreau46023632010-01-07 22:51:47 +01008671 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008672 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8673
8674 if (txn->hdr_idx.v)
8675 hdr_idx_init(&txn->hdr_idx);
8676}
8677
8678/* to be used at the end of a transaction */
8679void http_end_txn(struct session *s)
8680{
8681 struct http_txn *txn = &s->txn;
8682
Willy Tarreau75195602014-03-11 15:48:55 +01008683 /* release any possible compression context */
8684 if (s->flags & SN_COMP_READY)
8685 s->comp_algo->end(&s->comp_ctx);
8686 s->comp_algo = NULL;
8687 s->flags &= ~SN_COMP_READY;
8688
Willy Tarreau0937bc42009-12-22 15:03:09 +01008689 /* these ones will have been dynamically allocated */
8690 pool_free2(pool2_requri, txn->uri);
8691 pool_free2(pool2_capture, txn->cli_cookie);
8692 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008693 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008694 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008695
William Lallemanda73203e2012-03-12 12:48:57 +01008696 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008697 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008698 txn->uri = NULL;
8699 txn->srv_cookie = NULL;
8700 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008701
8702 if (txn->req.cap) {
8703 struct cap_hdr *h;
8704 for (h = s->fe->req_cap; h; h = h->next)
8705 pool_free2(h->pool, txn->req.cap[h->index]);
8706 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8707 }
8708
8709 if (txn->rsp.cap) {
8710 struct cap_hdr *h;
8711 for (h = s->fe->rsp_cap; h; h = h->next)
8712 pool_free2(h->pool, txn->rsp.cap[h->index]);
8713 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8714 }
8715
Willy Tarreau0937bc42009-12-22 15:03:09 +01008716}
8717
8718/* to be used at the end of a transaction to prepare a new one */
8719void http_reset_txn(struct session *s)
8720{
8721 http_end_txn(s);
8722 http_init_txn(s);
8723
8724 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008725 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008726 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008727 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008728 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008729 /* re-init store persistence */
8730 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008731 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008732
Willy Tarreau0937bc42009-12-22 15:03:09 +01008733 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008734
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008735 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008736
Willy Tarreau739cfba2010-01-25 23:11:14 +01008737 /* We must trim any excess data from the response buffer, because we
8738 * may have blocked an invalid response from a server that we don't
8739 * want to accidentely forward once we disable the analysers, nor do
8740 * we want those data to come along with next response. A typical
8741 * example of such data would be from a buggy server responding to
8742 * a HEAD with some data, or sending more than the advertised
8743 * content-length.
8744 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008745 if (unlikely(s->rep->buf->i))
8746 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008747
Willy Tarreau0937bc42009-12-22 15:03:09 +01008748 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008749 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008750
Willy Tarreaud04e8582010-05-31 12:31:35 +02008751 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008752 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008753
8754 s->req->rex = TICK_ETERNITY;
8755 s->req->wex = TICK_ETERNITY;
8756 s->req->analyse_exp = TICK_ETERNITY;
8757 s->rep->rex = TICK_ETERNITY;
8758 s->rep->wex = TICK_ETERNITY;
8759 s->rep->analyse_exp = TICK_ETERNITY;
8760}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008761
Willy Tarreauff011f22011-01-06 17:51:27 +01008762void free_http_req_rules(struct list *r) {
8763 struct http_req_rule *tr, *pr;
8764
8765 list_for_each_entry_safe(pr, tr, r, list) {
8766 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008767 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008768 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008769
8770 free(pr);
8771 }
8772}
8773
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008774/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008775struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8776{
8777 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008778 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008779 int cur_arg;
8780
8781 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8782 if (!rule) {
8783 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008784 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008785 }
8786
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008787 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008788 rule->action = HTTP_REQ_ACT_ALLOW;
8789 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008790 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008791 rule->action = HTTP_REQ_ACT_DENY;
8792 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008793 } else if (!strcmp(args[0], "tarpit")) {
8794 rule->action = HTTP_REQ_ACT_TARPIT;
8795 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008796 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008797 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008798 cur_arg = 1;
8799
8800 while(*args[cur_arg]) {
8801 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008802 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008803 cur_arg+=2;
8804 continue;
8805 } else
8806 break;
8807 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008808 } else if (!strcmp(args[0], "set-nice")) {
8809 rule->action = HTTP_REQ_ACT_SET_NICE;
8810 cur_arg = 1;
8811
8812 if (!*args[cur_arg] ||
8813 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8814 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8815 file, linenum, args[0]);
8816 goto out_err;
8817 }
8818 rule->arg.nice = atoi(args[cur_arg]);
8819 if (rule->arg.nice < -1024)
8820 rule->arg.nice = -1024;
8821 else if (rule->arg.nice > 1024)
8822 rule->arg.nice = 1024;
8823 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008824 } else if (!strcmp(args[0], "set-tos")) {
8825#ifdef IP_TOS
8826 char *err;
8827 rule->action = HTTP_REQ_ACT_SET_TOS;
8828 cur_arg = 1;
8829
8830 if (!*args[cur_arg] ||
8831 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8832 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8833 file, linenum, args[0]);
8834 goto out_err;
8835 }
8836
8837 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8838 if (err && *err != '\0') {
8839 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8840 file, linenum, err, args[0]);
8841 goto out_err;
8842 }
8843 cur_arg++;
8844#else
8845 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8846 goto out_err;
8847#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008848 } else if (!strcmp(args[0], "set-mark")) {
8849#ifdef SO_MARK
8850 char *err;
8851 rule->action = HTTP_REQ_ACT_SET_MARK;
8852 cur_arg = 1;
8853
8854 if (!*args[cur_arg] ||
8855 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8856 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8857 file, linenum, args[0]);
8858 goto out_err;
8859 }
8860
8861 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8862 if (err && *err != '\0') {
8863 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8864 file, linenum, err, args[0]);
8865 goto out_err;
8866 }
8867 cur_arg++;
8868 global.last_checks |= LSTCHK_NETADM;
8869#else
8870 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8871 goto out_err;
8872#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008873 } else if (!strcmp(args[0], "set-log-level")) {
8874 rule->action = HTTP_REQ_ACT_SET_LOGL;
8875 cur_arg = 1;
8876
8877 if (!*args[cur_arg] ||
8878 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8879 bad_log_level:
8880 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8881 file, linenum, args[0]);
8882 goto out_err;
8883 }
8884 if (strcmp(args[cur_arg], "silent") == 0)
8885 rule->arg.loglevel = -1;
8886 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8887 goto bad_log_level;
8888 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008889 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8890 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8891 cur_arg = 1;
8892
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008893 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8894 (*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 +01008895 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8896 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008897 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008898 }
8899
8900 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8901 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8902 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008903
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008904 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008905 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008906 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8907 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008908 free(proxy->conf.lfs_file);
8909 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8910 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008911 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008912 } else if (strcmp(args[0], "del-header") == 0) {
8913 rule->action = HTTP_REQ_ACT_DEL_HDR;
8914 cur_arg = 1;
8915
8916 if (!*args[cur_arg] ||
8917 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8918 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8919 file, linenum, args[0]);
8920 goto out_err;
8921 }
8922
8923 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8924 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8925
8926 proxy->conf.args.ctx = ARGC_HRQ;
8927 free(proxy->conf.lfs_file);
8928 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8929 proxy->conf.lfs_line = proxy->conf.args.line;
8930 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008931 } else if (strcmp(args[0], "redirect") == 0) {
8932 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008933 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008934
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008935 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008936 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8937 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8938 goto out_err;
8939 }
8940
8941 /* this redirect rule might already contain a parsed condition which
8942 * we'll pass to the http-request rule.
8943 */
8944 rule->action = HTTP_REQ_ACT_REDIR;
8945 rule->arg.redir = redir;
8946 rule->cond = redir->cond;
8947 redir->cond = NULL;
8948 cur_arg = 2;
8949 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008950 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8951 /* http-request add-acl(<reference (acl name)>) <key pattern> */
8952 rule->action = HTTP_REQ_ACT_ADD_ACL;
8953 /*
8954 * '+ 8' for 'add-acl('
8955 * '- 9' for 'add-acl(' + trailing ')'
8956 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008957 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008958
8959 cur_arg = 1;
8960
8961 if (!*args[cur_arg] ||
8962 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8963 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8964 file, linenum, args[0]);
8965 goto out_err;
8966 }
8967
8968 LIST_INIT(&rule->arg.map.key);
8969 proxy->conf.args.ctx = ARGC_HRQ;
8970 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8971 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8972 file, linenum);
8973 free(proxy->conf.lfs_file);
8974 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8975 proxy->conf.lfs_line = proxy->conf.args.line;
8976 cur_arg += 1;
8977 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8978 /* http-request del-acl(<reference (acl name)>) <key pattern> */
8979 rule->action = HTTP_REQ_ACT_DEL_ACL;
8980 /*
8981 * '+ 8' for 'del-acl('
8982 * '- 9' for 'del-acl(' + trailing ')'
8983 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008984 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008985
8986 cur_arg = 1;
8987
8988 if (!*args[cur_arg] ||
8989 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8990 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8991 file, linenum, args[0]);
8992 goto out_err;
8993 }
8994
8995 LIST_INIT(&rule->arg.map.key);
8996 proxy->conf.args.ctx = ARGC_HRQ;
8997 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8998 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8999 file, linenum);
9000 free(proxy->conf.lfs_file);
9001 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9002 proxy->conf.lfs_line = proxy->conf.args.line;
9003 cur_arg += 1;
9004 } else if (strncmp(args[0], "del-map", 7) == 0) {
9005 /* http-request del-map(<reference (map name)>) <key pattern> */
9006 rule->action = HTTP_REQ_ACT_DEL_MAP;
9007 /*
9008 * '+ 8' for 'del-map('
9009 * '- 9' for 'del-map(' + trailing ')'
9010 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009011 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009012
9013 cur_arg = 1;
9014
9015 if (!*args[cur_arg] ||
9016 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9017 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9018 file, linenum, args[0]);
9019 goto out_err;
9020 }
9021
9022 LIST_INIT(&rule->arg.map.key);
9023 proxy->conf.args.ctx = ARGC_HRQ;
9024 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9025 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9026 file, linenum);
9027 free(proxy->conf.lfs_file);
9028 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9029 proxy->conf.lfs_line = proxy->conf.args.line;
9030 cur_arg += 1;
9031 } else if (strncmp(args[0], "set-map", 7) == 0) {
9032 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9033 rule->action = HTTP_REQ_ACT_SET_MAP;
9034 /*
9035 * '+ 8' for 'set-map('
9036 * '- 9' for 'set-map(' + trailing ')'
9037 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009038 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009039
9040 cur_arg = 1;
9041
9042 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9043 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9044 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9045 file, linenum, args[0]);
9046 goto out_err;
9047 }
9048
9049 LIST_INIT(&rule->arg.map.key);
9050 LIST_INIT(&rule->arg.map.value);
9051 proxy->conf.args.ctx = ARGC_HRQ;
9052
9053 /* key pattern */
9054 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9055 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9056 file, linenum);
9057
9058 /* value pattern */
9059 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9060 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9061 file, linenum);
9062 free(proxy->conf.lfs_file);
9063 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9064 proxy->conf.lfs_line = proxy->conf.args.line;
9065
9066 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009067 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9068 char *errmsg = NULL;
9069 cur_arg = 1;
9070 /* try in the module list */
9071 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9072 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9073 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9074 free(errmsg);
9075 goto out_err;
9076 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009077 } else {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009078 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', '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 +01009079 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009080 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009081 }
9082
9083 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9084 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009085 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009086
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009087 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9088 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9089 file, linenum, args[0], errmsg);
9090 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009091 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009092 }
9093 rule->cond = cond;
9094 }
9095 else if (*args[cur_arg]) {
9096 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9097 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9098 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009099 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009100 }
9101
9102 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009103 out_err:
9104 free(rule);
9105 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009106}
9107
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009108/* parse an "http-respose" rule */
9109struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9110{
9111 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009112 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009113 int cur_arg;
9114
9115 rule = calloc(1, sizeof(*rule));
9116 if (!rule) {
9117 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9118 goto out_err;
9119 }
9120
9121 if (!strcmp(args[0], "allow")) {
9122 rule->action = HTTP_RES_ACT_ALLOW;
9123 cur_arg = 1;
9124 } else if (!strcmp(args[0], "deny")) {
9125 rule->action = HTTP_RES_ACT_DENY;
9126 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009127 } else if (!strcmp(args[0], "set-nice")) {
9128 rule->action = HTTP_RES_ACT_SET_NICE;
9129 cur_arg = 1;
9130
9131 if (!*args[cur_arg] ||
9132 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9133 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9134 file, linenum, args[0]);
9135 goto out_err;
9136 }
9137 rule->arg.nice = atoi(args[cur_arg]);
9138 if (rule->arg.nice < -1024)
9139 rule->arg.nice = -1024;
9140 else if (rule->arg.nice > 1024)
9141 rule->arg.nice = 1024;
9142 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009143 } else if (!strcmp(args[0], "set-tos")) {
9144#ifdef IP_TOS
9145 char *err;
9146 rule->action = HTTP_RES_ACT_SET_TOS;
9147 cur_arg = 1;
9148
9149 if (!*args[cur_arg] ||
9150 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9151 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9152 file, linenum, args[0]);
9153 goto out_err;
9154 }
9155
9156 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9157 if (err && *err != '\0') {
9158 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9159 file, linenum, err, args[0]);
9160 goto out_err;
9161 }
9162 cur_arg++;
9163#else
9164 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9165 goto out_err;
9166#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009167 } else if (!strcmp(args[0], "set-mark")) {
9168#ifdef SO_MARK
9169 char *err;
9170 rule->action = HTTP_RES_ACT_SET_MARK;
9171 cur_arg = 1;
9172
9173 if (!*args[cur_arg] ||
9174 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9175 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9176 file, linenum, args[0]);
9177 goto out_err;
9178 }
9179
9180 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9181 if (err && *err != '\0') {
9182 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9183 file, linenum, err, args[0]);
9184 goto out_err;
9185 }
9186 cur_arg++;
9187 global.last_checks |= LSTCHK_NETADM;
9188#else
9189 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9190 goto out_err;
9191#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009192 } else if (!strcmp(args[0], "set-log-level")) {
9193 rule->action = HTTP_RES_ACT_SET_LOGL;
9194 cur_arg = 1;
9195
9196 if (!*args[cur_arg] ||
9197 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9198 bad_log_level:
9199 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9200 file, linenum, args[0]);
9201 goto out_err;
9202 }
9203 if (strcmp(args[cur_arg], "silent") == 0)
9204 rule->arg.loglevel = -1;
9205 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9206 goto bad_log_level;
9207 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009208 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9209 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9210 cur_arg = 1;
9211
9212 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9213 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9214 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9215 file, linenum, args[0]);
9216 goto out_err;
9217 }
9218
9219 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9220 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9221 LIST_INIT(&rule->arg.hdr_add.fmt);
9222
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009223 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009224 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009225 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9226 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009227 free(proxy->conf.lfs_file);
9228 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9229 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009230 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009231 } else if (strcmp(args[0], "del-header") == 0) {
9232 rule->action = HTTP_RES_ACT_DEL_HDR;
9233 cur_arg = 1;
9234
9235 if (!*args[cur_arg] ||
9236 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9237 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9238 file, linenum, args[0]);
9239 goto out_err;
9240 }
9241
9242 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9243 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9244
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009245 proxy->conf.args.ctx = ARGC_HRS;
9246 free(proxy->conf.lfs_file);
9247 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9248 proxy->conf.lfs_line = proxy->conf.args.line;
9249 cur_arg += 1;
9250 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9251 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9252 rule->action = HTTP_RES_ACT_ADD_ACL;
9253 /*
9254 * '+ 8' for 'add-acl('
9255 * '- 9' for 'add-acl(' + trailing ')'
9256 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009257 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009258
9259 cur_arg = 1;
9260
9261 if (!*args[cur_arg] ||
9262 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9263 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9264 file, linenum, args[0]);
9265 goto out_err;
9266 }
9267
9268 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009269 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009270 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9271 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9272 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009273 free(proxy->conf.lfs_file);
9274 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9275 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009276
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009277 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009278 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9279 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9280 rule->action = HTTP_RES_ACT_DEL_ACL;
9281 /*
9282 * '+ 8' for 'del-acl('
9283 * '- 9' for 'del-acl(' + trailing ')'
9284 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009285 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009286
9287 cur_arg = 1;
9288
9289 if (!*args[cur_arg] ||
9290 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9291 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9292 file, linenum, args[0]);
9293 goto out_err;
9294 }
9295
9296 LIST_INIT(&rule->arg.map.key);
9297 proxy->conf.args.ctx = ARGC_HRS;
9298 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9299 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9300 file, linenum);
9301 free(proxy->conf.lfs_file);
9302 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9303 proxy->conf.lfs_line = proxy->conf.args.line;
9304 cur_arg += 1;
9305 } else if (strncmp(args[0], "del-map", 7) == 0) {
9306 /* http-response del-map(<reference (map name)>) <key pattern> */
9307 rule->action = HTTP_RES_ACT_DEL_MAP;
9308 /*
9309 * '+ 8' for 'del-map('
9310 * '- 9' for 'del-map(' + trailing ')'
9311 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009312 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009313
9314 cur_arg = 1;
9315
9316 if (!*args[cur_arg] ||
9317 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9318 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9319 file, linenum, args[0]);
9320 goto out_err;
9321 }
9322
9323 LIST_INIT(&rule->arg.map.key);
9324 proxy->conf.args.ctx = ARGC_HRS;
9325 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9326 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9327 file, linenum);
9328 free(proxy->conf.lfs_file);
9329 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9330 proxy->conf.lfs_line = proxy->conf.args.line;
9331 cur_arg += 1;
9332 } else if (strncmp(args[0], "set-map", 7) == 0) {
9333 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9334 rule->action = HTTP_RES_ACT_SET_MAP;
9335 /*
9336 * '+ 8' for 'set-map('
9337 * '- 9' for 'set-map(' + trailing ')'
9338 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009339 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009340
9341 cur_arg = 1;
9342
9343 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9344 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9345 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9346 file, linenum, args[0]);
9347 goto out_err;
9348 }
9349
9350 LIST_INIT(&rule->arg.map.key);
9351 LIST_INIT(&rule->arg.map.value);
9352
9353 proxy->conf.args.ctx = ARGC_HRS;
9354
9355 /* key pattern */
9356 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9357 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9358 file, linenum);
9359
9360 /* value pattern */
9361 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9362 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9363 file, linenum);
9364
9365 free(proxy->conf.lfs_file);
9366 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9367 proxy->conf.lfs_line = proxy->conf.args.line;
9368
9369 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009370 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9371 char *errmsg = NULL;
9372 cur_arg = 1;
9373 /* try in the module list */
9374 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9375 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9376 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9377 free(errmsg);
9378 goto out_err;
9379 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009380 } else {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009381 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'del-header', 'set-header', '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 +02009382 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9383 goto out_err;
9384 }
9385
9386 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9387 struct acl_cond *cond;
9388 char *errmsg = NULL;
9389
9390 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9391 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9392 file, linenum, args[0], errmsg);
9393 free(errmsg);
9394 goto out_err;
9395 }
9396 rule->cond = cond;
9397 }
9398 else if (*args[cur_arg]) {
9399 Alert("parsing [%s:%d]: 'http-response %s' expects"
9400 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9401 file, linenum, args[0], args[cur_arg]);
9402 goto out_err;
9403 }
9404
9405 return rule;
9406 out_err:
9407 free(rule);
9408 return NULL;
9409}
9410
Willy Tarreau4baae242012-12-27 12:00:31 +01009411/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009412 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9413 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009414 */
9415struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009416 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009417{
9418 struct redirect_rule *rule;
9419 int cur_arg;
9420 int type = REDIRECT_TYPE_NONE;
9421 int code = 302;
9422 const char *destination = NULL;
9423 const char *cookie = NULL;
9424 int cookie_set = 0;
9425 unsigned int flags = REDIRECT_FLAG_NONE;
9426 struct acl_cond *cond = NULL;
9427
9428 cur_arg = 0;
9429 while (*(args[cur_arg])) {
9430 if (strcmp(args[cur_arg], "location") == 0) {
9431 if (!*args[cur_arg + 1])
9432 goto missing_arg;
9433
9434 type = REDIRECT_TYPE_LOCATION;
9435 cur_arg++;
9436 destination = args[cur_arg];
9437 }
9438 else if (strcmp(args[cur_arg], "prefix") == 0) {
9439 if (!*args[cur_arg + 1])
9440 goto missing_arg;
9441
9442 type = REDIRECT_TYPE_PREFIX;
9443 cur_arg++;
9444 destination = args[cur_arg];
9445 }
9446 else if (strcmp(args[cur_arg], "scheme") == 0) {
9447 if (!*args[cur_arg + 1])
9448 goto missing_arg;
9449
9450 type = REDIRECT_TYPE_SCHEME;
9451 cur_arg++;
9452 destination = args[cur_arg];
9453 }
9454 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9455 if (!*args[cur_arg + 1])
9456 goto missing_arg;
9457
9458 cur_arg++;
9459 cookie = args[cur_arg];
9460 cookie_set = 1;
9461 }
9462 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9463 if (!*args[cur_arg + 1])
9464 goto missing_arg;
9465
9466 cur_arg++;
9467 cookie = args[cur_arg];
9468 cookie_set = 0;
9469 }
9470 else if (strcmp(args[cur_arg], "code") == 0) {
9471 if (!*args[cur_arg + 1])
9472 goto missing_arg;
9473
9474 cur_arg++;
9475 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009476 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009477 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009478 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009479 args[cur_arg - 1], args[cur_arg]);
9480 return NULL;
9481 }
9482 }
9483 else if (!strcmp(args[cur_arg],"drop-query")) {
9484 flags |= REDIRECT_FLAG_DROP_QS;
9485 }
9486 else if (!strcmp(args[cur_arg],"append-slash")) {
9487 flags |= REDIRECT_FLAG_APPEND_SLASH;
9488 }
9489 else if (strcmp(args[cur_arg], "if") == 0 ||
9490 strcmp(args[cur_arg], "unless") == 0) {
9491 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9492 if (!cond) {
9493 memprintf(errmsg, "error in condition: %s", *errmsg);
9494 return NULL;
9495 }
9496 break;
9497 }
9498 else {
9499 memprintf(errmsg,
9500 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9501 args[cur_arg]);
9502 return NULL;
9503 }
9504 cur_arg++;
9505 }
9506
9507 if (type == REDIRECT_TYPE_NONE) {
9508 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9509 return NULL;
9510 }
9511
9512 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9513 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009514 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009515
9516 if (!use_fmt) {
9517 /* old-style static redirect rule */
9518 rule->rdr_str = strdup(destination);
9519 rule->rdr_len = strlen(destination);
9520 }
9521 else {
9522 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009523
9524 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9525 * if prefix == "/", we don't want to add anything, otherwise it
9526 * makes it hard for the user to configure a self-redirection.
9527 */
9528 proxy->conf.args.ctx = ARGC_RDR;
9529 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009530 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009531 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9532 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009533 free(curproxy->conf.lfs_file);
9534 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9535 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009536 }
9537 }
9538
Willy Tarreau4baae242012-12-27 12:00:31 +01009539 if (cookie) {
9540 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9541 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9542 */
9543 rule->cookie_len = strlen(cookie);
9544 if (cookie_set) {
9545 rule->cookie_str = malloc(rule->cookie_len + 10);
9546 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9547 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9548 rule->cookie_len += 9;
9549 } else {
9550 rule->cookie_str = malloc(rule->cookie_len + 21);
9551 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9552 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9553 rule->cookie_len += 20;
9554 }
9555 }
9556 rule->type = type;
9557 rule->code = code;
9558 rule->flags = flags;
9559 LIST_INIT(&rule->list);
9560 return rule;
9561
9562 missing_arg:
9563 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9564 return NULL;
9565}
9566
Willy Tarreau8797c062007-05-07 00:55:35 +02009567/************************************************************************/
9568/* The code below is dedicated to ACL parsing and matching */
9569/************************************************************************/
9570
9571
Willy Tarreau14174bc2012-04-16 14:34:04 +02009572/* This function ensures that the prerequisites for an L7 fetch are ready,
9573 * which means that a request or response is ready. If some data is missing,
9574 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009575 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9576 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009577 *
9578 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009579 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9580 * decide whether or not an HTTP message is present ;
9581 * 0 if the requested data cannot be fetched or if it is certain that
9582 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009583 * 1 if an HTTP message is ready
9584 */
9585static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009586smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009587 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009588{
9589 struct http_txn *txn = l7;
9590 struct http_msg *msg = &txn->req;
9591
9592 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9593 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9594 */
9595
9596 if (unlikely(!s || !txn))
9597 return 0;
9598
9599 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009600 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009601
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009602 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009603 if (unlikely(!s->req))
9604 return 0;
9605
Willy Tarreauaae75e32013-03-29 12:31:49 +01009606 /* If the buffer does not leave enough free space at the end,
9607 * we must first realign it.
9608 */
9609 if (s->req->buf->p > s->req->buf->data &&
9610 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9611 buffer_slow_realign(s->req->buf);
9612
Willy Tarreau14174bc2012-04-16 14:34:04 +02009613 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009614 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009615 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009616
9617 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009618 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009619 http_msg_analyzer(msg, &txn->hdr_idx);
9620
9621 /* Still no valid request ? */
9622 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009623 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009624 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009625 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009626 }
9627 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009628 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009629 return 0;
9630 }
9631
9632 /* OK we just got a valid HTTP request. We have some minor
9633 * preparation to perform so that further checks can rely
9634 * on HTTP tests.
9635 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009636
9637 /* If the request was parsed but was too large, we must absolutely
9638 * return an error so that it is not processed. At the moment this
9639 * cannot happen, but if the parsers are to change in the future,
9640 * we want this check to be maintained.
9641 */
9642 if (unlikely(s->req->buf->i + s->req->buf->p >
9643 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9644 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009645 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009646 return 1;
9647 }
9648
Willy Tarreau9b28e032012-10-12 23:49:43 +02009649 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009650 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9651 s->flags |= SN_REDIRECTABLE;
9652
Willy Tarreau506d0502013-07-06 13:29:24 +02009653 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9654 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009655 }
9656
Willy Tarreau506d0502013-07-06 13:29:24 +02009657 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009658 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009659 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009660
9661 /* otherwise everything's ready for the request */
9662 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009663 else {
9664 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009665 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9666 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009667 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009668 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009669 }
9670
9671 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009672 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009673 return 1;
9674}
Willy Tarreau8797c062007-05-07 00:55:35 +02009675
Willy Tarreauc0239e02012-04-16 14:42:55 +02009676#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009677 do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +02009678
Willy Tarreau24e32d82012-04-23 23:55:44 +02009679#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009680 do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +02009681
Willy Tarreau8797c062007-05-07 00:55:35 +02009682
9683/* 1. Check on METHOD
9684 * We use the pre-parsed method if it is known, and store its number as an
9685 * integer. If it is unknown, we use the pointer and the length.
9686 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009687static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009688{
9689 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009690 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02009691
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009692 len = strlen(text);
9693 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009694
9695 pattern->val.i = meth;
9696 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009697 trash = get_trash_chunk();
9698 if (trash->size < len) {
9699 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
9700 len, trash->size);
9701 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009702 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009703 pattern->ptr.str = trash->str;
Willy Tarreau8797c062007-05-07 00:55:35 +02009704 pattern->len = len;
9705 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009706 else {
9707 pattern->ptr.str = NULL;
9708 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009709 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009710 return 1;
9711}
9712
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009713/* This function fetches the method of current HTTP request and stores
9714 * it in the global pattern struct as a chunk. There are two possibilities :
9715 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9716 * in <len> and <ptr> is NULL ;
9717 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9718 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009719 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009720 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009721static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009722smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009723 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009724{
9725 int meth;
9726 struct http_txn *txn = l7;
9727
Willy Tarreau24e32d82012-04-23 23:55:44 +02009728 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009729
Willy Tarreau8797c062007-05-07 00:55:35 +02009730 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009731 smp->type = SMP_T_METH;
9732 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009733 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009734 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9735 /* ensure the indexes are not affected */
9736 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009737 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009738 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9739 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009740 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009741 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009742 return 1;
9743}
9744
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009745/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009746static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009747{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009748 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009749 struct pattern_list *lst;
9750 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009751
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009752 list_for_each_entry(lst, &expr->patterns, list) {
9753 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009754
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009755 /* well-known method */
9756 if (pattern->val.i != HTTP_METH_OTHER) {
9757 if (smp->data.meth.meth == pattern->val.i)
9758 return pattern;
9759 else
9760 continue;
9761 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009762
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009763 /* Other method, we must compare the strings */
9764 if (pattern->len != smp->data.meth.str.len)
9765 continue;
9766
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009767 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009768 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) ||
9769 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0))
9770 return pattern;
9771 }
9772 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009773}
9774
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009775static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009776smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009777 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009778{
9779 struct http_txn *txn = l7;
9780 char *ptr;
9781 int len;
9782
Willy Tarreauc0239e02012-04-16 14:42:55 +02009783 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009784
Willy Tarreau8797c062007-05-07 00:55:35 +02009785 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009786 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009787
9788 while ((len-- > 0) && (*ptr++ != '/'));
9789 if (len <= 0)
9790 return 0;
9791
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009792 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009793 smp->data.str.str = ptr;
9794 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009795
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009796 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009797 return 1;
9798}
9799
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009800static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009801smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009802 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009803{
9804 struct http_txn *txn = l7;
9805 char *ptr;
9806 int len;
9807
Willy Tarreauc0239e02012-04-16 14:42:55 +02009808 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009809
Willy Tarreauf26b2522012-12-14 08:33:14 +01009810 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9811 return 0;
9812
Willy Tarreau8797c062007-05-07 00:55:35 +02009813 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009814 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009815
9816 while ((len-- > 0) && (*ptr++ != '/'));
9817 if (len <= 0)
9818 return 0;
9819
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009820 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009821 smp->data.str.str = ptr;
9822 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009823
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009824 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009825 return 1;
9826}
9827
9828/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009829static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009830smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009831 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009832{
9833 struct http_txn *txn = l7;
9834 char *ptr;
9835 int len;
9836
Willy Tarreauc0239e02012-04-16 14:42:55 +02009837 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009838
Willy Tarreauf26b2522012-12-14 08:33:14 +01009839 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9840 return 0;
9841
Willy Tarreau8797c062007-05-07 00:55:35 +02009842 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009843 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009844
Willy Tarreauf853c462012-04-23 18:53:56 +02009845 smp->type = SMP_T_UINT;
9846 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009847 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009848 return 1;
9849}
9850
9851/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009852static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009853smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009854 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009855{
9856 struct http_txn *txn = l7;
9857
Willy Tarreauc0239e02012-04-16 14:42:55 +02009858 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009859
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009860 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009861 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009862 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009863 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009864 return 1;
9865}
9866
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009867static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009868smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009869 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009870{
9871 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009872 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009873
Willy Tarreauc0239e02012-04-16 14:42:55 +02009874 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009875
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009876 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 +02009877 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009878 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009879
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009880 smp->type = SMP_T_IPV4;
9881 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009882 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009883 return 1;
9884}
9885
9886static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009887smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009888 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009889{
9890 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009891 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009892
Willy Tarreauc0239e02012-04-16 14:42:55 +02009893 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009894
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009895 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 +02009896 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9897 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009898
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009899 smp->type = SMP_T_UINT;
9900 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009901 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009902 return 1;
9903}
9904
Willy Tarreau185b5c42012-04-26 15:11:51 +02009905/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9906 * Accepts an optional argument of type string containing the header field name,
9907 * and an optional argument of type signed or unsigned integer to request an
9908 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009909 * headers are considered from the first one. It does not stop on commas and
9910 * returns full lines instead (useful for User-Agent or Date for example).
9911 */
9912static int
9913smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009914 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009915{
9916 struct http_txn *txn = l7;
9917 struct hdr_idx *idx = &txn->hdr_idx;
9918 struct hdr_ctx *ctx = smp->ctx.a[0];
9919 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9920 int occ = 0;
9921 const char *name_str = NULL;
9922 int name_len = 0;
9923
9924 if (!ctx) {
9925 /* first call */
9926 ctx = &static_hdr_ctx;
9927 ctx->idx = 0;
9928 smp->ctx.a[0] = ctx;
9929 }
9930
9931 if (args) {
9932 if (args[0].type != ARGT_STR)
9933 return 0;
9934 name_str = args[0].data.str.str;
9935 name_len = args[0].data.str.len;
9936
9937 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9938 occ = args[1].data.uint;
9939 }
9940
9941 CHECK_HTTP_MESSAGE_FIRST();
9942
9943 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9944 /* search for header from the beginning */
9945 ctx->idx = 0;
9946
9947 if (!occ && !(opt & SMP_OPT_ITERATE))
9948 /* no explicit occurrence and single fetch => last header by default */
9949 occ = -1;
9950
9951 if (!occ)
9952 /* prepare to report multiple occurrences for ACL fetches */
9953 smp->flags |= SMP_F_NOT_LAST;
9954
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009955 smp->type = SMP_T_STR;
9956 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009957 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9958 return 1;
9959
9960 smp->flags &= ~SMP_F_NOT_LAST;
9961 return 0;
9962}
9963
9964/* 6. Check on HTTP header count. The number of occurrences is returned.
9965 * Accepts exactly 1 argument of type string. It does not stop on commas and
9966 * returns full lines instead (useful for User-Agent or Date for example).
9967 */
9968static int
9969smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009970 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009971{
9972 struct http_txn *txn = l7;
9973 struct hdr_idx *idx = &txn->hdr_idx;
9974 struct hdr_ctx ctx;
9975 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9976 int cnt;
9977
9978 if (!args || args->type != ARGT_STR)
9979 return 0;
9980
9981 CHECK_HTTP_MESSAGE_FIRST();
9982
9983 ctx.idx = 0;
9984 cnt = 0;
9985 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9986 cnt++;
9987
9988 smp->type = SMP_T_UINT;
9989 smp->data.uint = cnt;
9990 smp->flags = SMP_F_VOL_HDR;
9991 return 1;
9992}
9993
9994/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9995 * Accepts an optional argument of type string containing the header field name,
9996 * and an optional argument of type signed or unsigned integer to request an
9997 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009998 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009999 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010000static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010001smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010002 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010003{
10004 struct http_txn *txn = l7;
10005 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010006 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010007 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010008 int occ = 0;
10009 const char *name_str = NULL;
10010 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010011
Willy Tarreaua890d072013-04-02 12:01:06 +020010012 if (!ctx) {
10013 /* first call */
10014 ctx = &static_hdr_ctx;
10015 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010016 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010017 }
10018
Willy Tarreau185b5c42012-04-26 15:11:51 +020010019 if (args) {
10020 if (args[0].type != ARGT_STR)
10021 return 0;
10022 name_str = args[0].data.str.str;
10023 name_len = args[0].data.str.len;
10024
10025 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10026 occ = args[1].data.uint;
10027 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010028
Willy Tarreaue333ec92012-04-16 16:26:40 +020010029 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010030
Willy Tarreau185b5c42012-04-26 15:11:51 +020010031 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010032 /* search for header from the beginning */
10033 ctx->idx = 0;
10034
Willy Tarreau185b5c42012-04-26 15:11:51 +020010035 if (!occ && !(opt & SMP_OPT_ITERATE))
10036 /* no explicit occurrence and single fetch => last header by default */
10037 occ = -1;
10038
10039 if (!occ)
10040 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010041 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010042
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010043 smp->type = SMP_T_STR;
10044 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010045 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 +020010046 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010047
Willy Tarreau37406352012-04-23 16:16:37 +020010048 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010049 return 0;
10050}
10051
Willy Tarreauc11416f2007-06-17 16:58:38 +020010052/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010053 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010054 */
10055static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010056smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010057 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010058{
10059 struct http_txn *txn = l7;
10060 struct hdr_idx *idx = &txn->hdr_idx;
10061 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010062 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010063 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +020010064
Willy Tarreau24e32d82012-04-23 23:55:44 +020010065 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010066 return 0;
10067
Willy Tarreaue333ec92012-04-16 16:26:40 +020010068 CHECK_HTTP_MESSAGE_FIRST();
10069
Willy Tarreau33a7e692007-06-10 19:45:56 +020010070 ctx.idx = 0;
10071 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010072 while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010073 cnt++;
10074
Willy Tarreauf853c462012-04-23 18:53:56 +020010075 smp->type = SMP_T_UINT;
10076 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010077 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010078 return 1;
10079}
10080
Willy Tarreau185b5c42012-04-26 15:11:51 +020010081/* Fetch an HTTP header's integer value. The integer value is returned. It
10082 * takes a mandatory argument of type string and an optional one of type int
10083 * to designate a specific occurrence. It returns an unsigned integer, which
10084 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010085 */
10086static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010087smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010088 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010089{
Willy Tarreauef38c392013-07-22 16:29:32 +020010090 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010091
Willy Tarreauf853c462012-04-23 18:53:56 +020010092 if (ret > 0) {
10093 smp->type = SMP_T_UINT;
10094 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10095 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010096
Willy Tarreaud53e2422012-04-16 17:21:11 +020010097 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010098}
10099
Cyril Bonté69fa9922012-10-25 00:01:06 +020010100/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10101 * and an optional one of type int to designate a specific occurrence.
10102 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010103 */
10104static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010105smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010106 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010107{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010108 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010109
Willy Tarreauef38c392013-07-22 16:29:32 +020010110 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010111 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10112 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010113 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010114 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010115 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010116 if (smp->data.str.len < temp->size - 1) {
10117 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10118 temp->str[smp->data.str.len] = '\0';
10119 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10120 smp->type = SMP_T_IPV6;
10121 break;
10122 }
10123 }
10124 }
10125
Willy Tarreaud53e2422012-04-16 17:21:11 +020010126 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010127 if (!(smp->flags & SMP_F_NOT_LAST))
10128 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010129 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010130 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010131}
10132
Willy Tarreau737b0c12007-06-10 21:28:46 +020010133/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10134 * the first '/' after the possible hostname, and ends before the possible '?'.
10135 */
10136static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010137smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010138 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010139{
10140 struct http_txn *txn = l7;
10141 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010142
Willy Tarreauc0239e02012-04-16 14:42:55 +020010143 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010144
Willy Tarreau9b28e032012-10-12 23:49:43 +020010145 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010146 ptr = http_get_path(txn);
10147 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010148 return 0;
10149
10150 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010151 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010152 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010153
10154 while (ptr < end && *ptr != '?')
10155 ptr++;
10156
Willy Tarreauf853c462012-04-23 18:53:56 +020010157 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010158 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010159 return 1;
10160}
10161
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010162/* This produces a concatenation of the first occurrence of the Host header
10163 * followed by the path component if it begins with a slash ('/'). This means
10164 * that '*' will not be added, resulting in exactly the first Host entry.
10165 * If no Host header is found, then the path is returned as-is. The returned
10166 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010167 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010168 */
10169static int
10170smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010171 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010172{
10173 struct http_txn *txn = l7;
10174 char *ptr, *end, *beg;
10175 struct hdr_ctx ctx;
10176
10177 CHECK_HTTP_MESSAGE_FIRST();
10178
10179 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010180 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010181 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010182
10183 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +010010184 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010185 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +010010186 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010187 smp->data.str.len = ctx.vlen;
10188
10189 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010190 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010191 beg = http_get_path(txn);
10192 if (!beg)
10193 beg = end;
10194
10195 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10196
10197 if (beg < ptr && *beg == '/') {
10198 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10199 smp->data.str.len += ptr - beg;
10200 }
10201
10202 smp->flags = SMP_F_VOL_1ST;
10203 return 1;
10204}
10205
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010206/* This produces a 32-bit hash of the concatenation of the first occurrence of
10207 * the Host header followed by the path component if it begins with a slash ('/').
10208 * This means that '*' will not be added, resulting in exactly the first Host
10209 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010210 * is hashed using the path hash followed by a full avalanche hash and provides a
10211 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010212 * high-traffic sites without having to store whole paths.
10213 */
10214static int
10215smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010216 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010217{
10218 struct http_txn *txn = l7;
10219 struct hdr_ctx ctx;
10220 unsigned int hash = 0;
10221 char *ptr, *beg, *end;
10222 int len;
10223
10224 CHECK_HTTP_MESSAGE_FIRST();
10225
10226 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010227 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010228 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10229 ptr = ctx.line + ctx.val;
10230 len = ctx.vlen;
10231 while (len--)
10232 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10233 }
10234
10235 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010236 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010237 beg = http_get_path(txn);
10238 if (!beg)
10239 beg = end;
10240
10241 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10242
10243 if (beg < ptr && *beg == '/') {
10244 while (beg < ptr)
10245 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10246 }
10247 hash = full_hash(hash);
10248
10249 smp->type = SMP_T_UINT;
10250 smp->data.uint = hash;
10251 smp->flags = SMP_F_VOL_1ST;
10252 return 1;
10253}
10254
Willy Tarreau4a550602012-12-09 14:53:32 +010010255/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010256 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10257 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10258 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010259 * that in environments where IPv6 is insignificant, truncating the output to
10260 * 8 bytes would still work.
10261 */
10262static int
10263smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010264 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010265{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010266 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010267 struct connection *cli_conn = objt_conn(l4->si[0].end);
10268
10269 if (!cli_conn)
10270 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010271
Willy Tarreauef38c392013-07-22 16:29:32 +020010272 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010273 return 0;
10274
Willy Tarreau47ca5452012-12-23 20:22:19 +010010275 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +010010276 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10277 temp->len += sizeof(smp->data.uint);
10278
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010279 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010280 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010281 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010282 temp->len += 4;
10283 break;
10284 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010285 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010286 temp->len += 16;
10287 break;
10288 default:
10289 return 0;
10290 }
10291
10292 smp->data.str = *temp;
10293 smp->type = SMP_T_BIN;
10294 return 1;
10295}
10296
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010297static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010298smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010299 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010300{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010301 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10302 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10303 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010304
Willy Tarreau24e32d82012-04-23 23:55:44 +020010305 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010306
Willy Tarreauf853c462012-04-23 18:53:56 +020010307 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010308 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010309 return 1;
10310}
10311
Willy Tarreau7f18e522010-10-22 20:04:13 +020010312/* return a valid test if the current request is the first one on the connection */
10313static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010314smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010315 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010316{
10317 if (!s)
10318 return 0;
10319
Willy Tarreauf853c462012-04-23 18:53:56 +020010320 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010321 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010322 return 1;
10323}
10324
Willy Tarreau34db1082012-04-19 17:16:54 +020010325/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010326static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010327smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010328 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010329{
10330
Willy Tarreau24e32d82012-04-23 23:55:44 +020010331 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010332 return 0;
10333
Willy Tarreauc0239e02012-04-16 14:42:55 +020010334 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010335
Willy Tarreauc0239e02012-04-16 14:42:55 +020010336 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010337 return 0;
10338
Willy Tarreauf853c462012-04-23 18:53:56 +020010339 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010340 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010341 return 1;
10342}
Willy Tarreau8797c062007-05-07 00:55:35 +020010343
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010344/* Accepts exactly 1 argument of type userlist */
10345static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010346smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010347 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010348{
10349
10350 if (!args || args->type != ARGT_USR)
10351 return 0;
10352
10353 CHECK_HTTP_MESSAGE_FIRST();
10354
10355 if (!get_http_auth(l4))
10356 return 0;
10357
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010358 /* if the user does not belong to the userlist or has a wrong password,
10359 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010360 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010361 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010362 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10363 return 0;
10364
10365 /* pat_match_auth() will need the user list */
10366 smp->ctx.a[0] = args->data.usr;
10367
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010368 smp->type = SMP_T_STR;
10369 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010370 smp->data.str.str = l4->txn.auth.user;
10371 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010372
10373 return 1;
10374}
10375
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010376/* Try to find the next occurrence of a cookie name in a cookie header value.
10377 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10378 * the cookie value is returned into *value and *value_l, and the function
10379 * returns a pointer to the next pointer to search from if the value was found.
10380 * Otherwise if the cookie was not found, NULL is returned and neither value
10381 * nor value_l are touched. The input <hdr> string should first point to the
10382 * header's value, and the <hdr_end> pointer must point to the first character
10383 * not part of the value. <list> must be non-zero if value may represent a list
10384 * of values (cookie headers). This makes it faster to abort parsing when no
10385 * list is expected.
10386 */
10387static char *
10388extract_cookie_value(char *hdr, const char *hdr_end,
10389 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010390 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010391{
10392 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10393 char *next;
10394
10395 /* we search at least a cookie name followed by an equal, and more
10396 * generally something like this :
10397 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10398 */
10399 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10400 /* Iterate through all cookies on this line */
10401
10402 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10403 att_beg++;
10404
10405 /* find att_end : this is the first character after the last non
10406 * space before the equal. It may be equal to hdr_end.
10407 */
10408 equal = att_end = att_beg;
10409
10410 while (equal < hdr_end) {
10411 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10412 break;
10413 if (http_is_spht[(unsigned char)*equal++])
10414 continue;
10415 att_end = equal;
10416 }
10417
10418 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10419 * is between <att_beg> and <equal>, both may be identical.
10420 */
10421
10422 /* look for end of cookie if there is an equal sign */
10423 if (equal < hdr_end && *equal == '=') {
10424 /* look for the beginning of the value */
10425 val_beg = equal + 1;
10426 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10427 val_beg++;
10428
10429 /* find the end of the value, respecting quotes */
10430 next = find_cookie_value_end(val_beg, hdr_end);
10431
10432 /* make val_end point to the first white space or delimitor after the value */
10433 val_end = next;
10434 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10435 val_end--;
10436 } else {
10437 val_beg = val_end = next = equal;
10438 }
10439
10440 /* We have nothing to do with attributes beginning with '$'. However,
10441 * they will automatically be removed if a header before them is removed,
10442 * since they're supposed to be linked together.
10443 */
10444 if (*att_beg == '$')
10445 continue;
10446
10447 /* Ignore cookies with no equal sign */
10448 if (equal == next)
10449 continue;
10450
10451 /* Now we have the cookie name between att_beg and att_end, and
10452 * its value between val_beg and val_end.
10453 */
10454
10455 if (att_end - att_beg == cookie_name_l &&
10456 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10457 /* let's return this value and indicate where to go on from */
10458 *value = val_beg;
10459 *value_l = val_end - val_beg;
10460 return next + 1;
10461 }
10462
10463 /* Set-Cookie headers only have the name in the first attr=value part */
10464 if (!list)
10465 break;
10466 }
10467
10468 return NULL;
10469}
10470
William Lallemanda43ba4e2014-01-28 18:14:25 +010010471/* Fetch a captured HTTP request header. The index is the position of
10472 * the "capture" option in the configuration file
10473 */
10474static int
10475smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10476 const struct arg *args, struct sample *smp, const char *kw)
10477{
10478 struct proxy *fe = l4->fe;
10479 struct http_txn *txn = l7;
10480 int idx;
10481
10482 if (!args || args->type != ARGT_UINT)
10483 return 0;
10484
10485 idx = args->data.uint;
10486
10487 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10488 return 0;
10489
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010490 smp->type = SMP_T_STR;
10491 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010492 smp->data.str.str = txn->req.cap[idx];
10493 smp->data.str.len = strlen(txn->req.cap[idx]);
10494
10495 return 1;
10496}
10497
10498/* Fetch a captured HTTP response header. The index is the position of
10499 * the "capture" option in the configuration file
10500 */
10501static int
10502smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10503 const struct arg *args, struct sample *smp, const char *kw)
10504{
10505 struct proxy *fe = l4->fe;
10506 struct http_txn *txn = l7;
10507 int idx;
10508
10509 if (!args || args->type != ARGT_UINT)
10510 return 0;
10511
10512 idx = args->data.uint;
10513
10514 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10515 return 0;
10516
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010517 smp->type = SMP_T_STR;
10518 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010519 smp->data.str.str = txn->rsp.cap[idx];
10520 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10521
10522 return 1;
10523}
10524
William Lallemand65ad6e12014-01-31 15:08:02 +010010525/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10526static int
10527smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10528 const struct arg *args, struct sample *smp, const char *kw)
10529{
10530 struct chunk *temp;
10531 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010532 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010533
10534 if (!txn->uri)
10535 return 0;
10536
William Lallemand96a77852014-02-05 00:30:02 +010010537 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010538
William Lallemand96a77852014-02-05 00:30:02 +010010539 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10540 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010541
William Lallemand96a77852014-02-05 00:30:02 +010010542 temp = get_trash_chunk();
10543 temp->str = txn->uri;
10544 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010545 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010546 smp->type = SMP_T_STR;
10547 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010548
10549 return 1;
10550
10551}
10552
10553/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10554static int
10555smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10556 const struct arg *args, struct sample *smp, const char *kw)
10557{
10558 struct chunk *temp;
10559 struct http_txn *txn = l7;
10560 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010561
10562 if (!txn->uri)
10563 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010564
William Lallemand65ad6e12014-01-31 15:08:02 +010010565 ptr = txn->uri;
10566
10567 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10568 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010569
William Lallemand65ad6e12014-01-31 15:08:02 +010010570 if (!*ptr)
10571 return 0;
10572
10573 ptr++; /* skip the space */
10574
10575 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010576 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010577 if (!ptr)
10578 return 0;
10579 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10580 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010581
10582 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010583 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010584 smp->type = SMP_T_STR;
10585 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010586
10587 return 1;
10588}
10589
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010590/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10591 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10592 */
10593static int
10594smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10595 const struct arg *args, struct sample *smp, const char *kw)
10596{
10597 struct http_txn *txn = l7;
10598
10599 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10600 return 0;
10601
10602 if (txn->req.flags & HTTP_MSGF_VER_11)
10603 smp->data.str.str = "HTTP/1.1";
10604 else
10605 smp->data.str.str = "HTTP/1.0";
10606
10607 smp->data.str.len = 8;
10608 smp->type = SMP_T_STR;
10609 smp->flags = SMP_F_CONST;
10610 return 1;
10611
10612}
10613
10614/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10615 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10616 */
10617static int
10618smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10619 const struct arg *args, struct sample *smp, const char *kw)
10620{
10621 struct http_txn *txn = l7;
10622
10623 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10624 return 0;
10625
10626 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10627 smp->data.str.str = "HTTP/1.1";
10628 else
10629 smp->data.str.str = "HTTP/1.0";
10630
10631 smp->data.str.len = 8;
10632 smp->type = SMP_T_STR;
10633 smp->flags = SMP_F_CONST;
10634 return 1;
10635
10636}
10637
William Lallemand65ad6e12014-01-31 15:08:02 +010010638
Willy Tarreaue333ec92012-04-16 16:26:40 +020010639/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010640 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010641 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010642 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010643 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010644 * Accepts exactly 1 argument of type string. If the input options indicate
10645 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010646 * The returned sample is of type CSTR. Can be used to parse cookies in other
10647 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010648 */
William Lallemand07c8b242014-05-02 17:11:07 +020010649int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010650 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010651{
10652 struct http_txn *txn = l7;
10653 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010654 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010655 const struct http_msg *msg;
10656 const char *hdr_name;
10657 int hdr_name_len;
10658 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010659 int occ = 0;
10660 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010661
Willy Tarreau24e32d82012-04-23 23:55:44 +020010662 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010663 return 0;
10664
Willy Tarreaua890d072013-04-02 12:01:06 +020010665 if (!ctx) {
10666 /* first call */
10667 ctx = &static_hdr_ctx;
10668 ctx->idx = 0;
10669 smp->ctx.a[2] = ctx;
10670 }
10671
Willy Tarreaue333ec92012-04-16 16:26:40 +020010672 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010673
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010674 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010675 msg = &txn->req;
10676 hdr_name = "Cookie";
10677 hdr_name_len = 6;
10678 } else {
10679 msg = &txn->rsp;
10680 hdr_name = "Set-Cookie";
10681 hdr_name_len = 10;
10682 }
10683
Willy Tarreau28376d62012-04-26 21:26:10 +020010684 if (!occ && !(opt & SMP_OPT_ITERATE))
10685 /* no explicit occurrence and single fetch => last cookie by default */
10686 occ = -1;
10687
10688 /* OK so basically here, either we want only one value and it's the
10689 * last one, or we want to iterate over all of them and we fetch the
10690 * next one.
10691 */
10692
Willy Tarreau9b28e032012-10-12 23:49:43 +020010693 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010694 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010695 /* search for the header from the beginning, we must first initialize
10696 * the search parameters.
10697 */
Willy Tarreau37406352012-04-23 16:16:37 +020010698 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010699 ctx->idx = 0;
10700 }
10701
Willy Tarreau28376d62012-04-26 21:26:10 +020010702 smp->flags |= SMP_F_VOL_HDR;
10703
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010704 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010705 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10706 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010707 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10708 goto out;
10709
Willy Tarreau24e32d82012-04-23 23:55:44 +020010710 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010711 continue;
10712
Willy Tarreau37406352012-04-23 16:16:37 +020010713 smp->ctx.a[0] = ctx->line + ctx->val;
10714 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010715 }
10716
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010717 smp->type = SMP_T_STR;
10718 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010719 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010720 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010721 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010722 &smp->data.str.str,
10723 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010724 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010725 found = 1;
10726 if (occ >= 0) {
10727 /* one value was returned into smp->data.str.{str,len} */
10728 smp->flags |= SMP_F_NOT_LAST;
10729 return 1;
10730 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010731 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010732 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010733 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010734 /* all cookie headers and values were scanned. If we're looking for the
10735 * last occurrence, we may return it now.
10736 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010737 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010738 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010739 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010740}
10741
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010742/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010743 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010744 * multiple cookies may be parsed on the same line. The returned sample is of
10745 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010746 */
10747static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010748smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010749 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010750{
10751 struct http_txn *txn = l7;
10752 struct hdr_idx *idx = &txn->hdr_idx;
10753 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010754 const struct http_msg *msg;
10755 const char *hdr_name;
10756 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010757 int cnt;
10758 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010759 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010760
Willy Tarreau24e32d82012-04-23 23:55:44 +020010761 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010762 return 0;
10763
Willy Tarreaue333ec92012-04-16 16:26:40 +020010764 CHECK_HTTP_MESSAGE_FIRST();
10765
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010766 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010767 msg = &txn->req;
10768 hdr_name = "Cookie";
10769 hdr_name_len = 6;
10770 } else {
10771 msg = &txn->rsp;
10772 hdr_name = "Set-Cookie";
10773 hdr_name_len = 10;
10774 }
10775
Willy Tarreau9b28e032012-10-12 23:49:43 +020010776 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010777 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010778 ctx.idx = 0;
10779 cnt = 0;
10780
10781 while (1) {
10782 /* Note: val_beg == NULL every time we need to fetch a new header */
10783 if (!val_beg) {
10784 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10785 break;
10786
Willy Tarreau24e32d82012-04-23 23:55:44 +020010787 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010788 continue;
10789
10790 val_beg = ctx.line + ctx.val;
10791 val_end = val_beg + ctx.vlen;
10792 }
10793
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010794 smp->type = SMP_T_STR;
10795 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010796 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010797 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010798 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010799 &smp->data.str.str,
10800 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010801 cnt++;
10802 }
10803 }
10804
Willy Tarreaub169eba2013-12-16 15:14:43 +010010805 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010806 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010807 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010808 return 1;
10809}
10810
Willy Tarreau51539362012-05-08 12:46:28 +020010811/* Fetch an cookie's integer value. The integer value is returned. It
10812 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10813 */
10814static int
10815smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010816 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010817{
Willy Tarreauef38c392013-07-22 16:29:32 +020010818 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010819
10820 if (ret > 0) {
10821 smp->type = SMP_T_UINT;
10822 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10823 }
10824
10825 return ret;
10826}
10827
Willy Tarreau8797c062007-05-07 00:55:35 +020010828/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010829/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010830/************************************************************************/
10831
David Cournapeau16023ee2010-12-23 20:55:41 +090010832/*
10833 * Given a path string and its length, find the position of beginning of the
10834 * query string. Returns NULL if no query string is found in the path.
10835 *
10836 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10837 *
10838 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10839 */
bedis4c75cca2012-10-05 08:38:24 +020010840static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010841{
10842 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010843
bedis4c75cca2012-10-05 08:38:24 +020010844 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010845 return p ? p + 1 : NULL;
10846}
10847
bedis4c75cca2012-10-05 08:38:24 +020010848static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010849{
bedis4c75cca2012-10-05 08:38:24 +020010850 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010851}
10852
10853/*
10854 * Given a url parameter, find the starting position of the first occurence,
10855 * or NULL if the parameter is not found.
10856 *
10857 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10858 * the function will return query_string+8.
10859 */
10860static char*
10861find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010862 char* url_param_name, size_t url_param_name_l,
10863 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010864{
10865 char *pos, *last;
10866
10867 pos = query_string;
10868 last = query_string + query_string_l - url_param_name_l - 1;
10869
10870 while (pos <= last) {
10871 if (pos[url_param_name_l] == '=') {
10872 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10873 return pos;
10874 pos += url_param_name_l + 1;
10875 }
bedis4c75cca2012-10-05 08:38:24 +020010876 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010877 pos++;
10878 pos++;
10879 }
10880 return NULL;
10881}
10882
10883/*
10884 * Given a url parameter name, returns its value and size into *value and
10885 * *value_l respectively, and returns non-zero. If the parameter is not found,
10886 * zero is returned and value/value_l are not touched.
10887 */
10888static int
10889find_url_param_value(char* path, size_t path_l,
10890 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010891 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010892{
10893 char *query_string, *qs_end;
10894 char *arg_start;
10895 char *value_start, *value_end;
10896
bedis4c75cca2012-10-05 08:38:24 +020010897 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010898 if (!query_string)
10899 return 0;
10900
10901 qs_end = path + path_l;
10902 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010903 url_param_name, url_param_name_l,
10904 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010905 if (!arg_start)
10906 return 0;
10907
10908 value_start = arg_start + url_param_name_l + 1;
10909 value_end = value_start;
10910
bedis4c75cca2012-10-05 08:38:24 +020010911 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010912 value_end++;
10913
10914 *value = value_start;
10915 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010916 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010917}
10918
10919static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010920smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010921 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010922{
bedis4c75cca2012-10-05 08:38:24 +020010923 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010924 struct http_txn *txn = l7;
10925 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010926
bedis4c75cca2012-10-05 08:38:24 +020010927 if (!args || args[0].type != ARGT_STR ||
10928 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010929 return 0;
10930
10931 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010932
bedis4c75cca2012-10-05 08:38:24 +020010933 if (args[1].type)
10934 delim = *args[1].data.str.str;
10935
Willy Tarreau9b28e032012-10-12 23:49:43 +020010936 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010937 args->data.str.str, args->data.str.len,
10938 &smp->data.str.str, &smp->data.str.len,
10939 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010940 return 0;
10941
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010942 smp->type = SMP_T_STR;
10943 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010944 return 1;
10945}
10946
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010947/* Return the signed integer value for the specified url parameter (see url_param
10948 * above).
10949 */
10950static int
10951smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010952 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010953{
Willy Tarreauef38c392013-07-22 16:29:32 +020010954 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010955
10956 if (ret > 0) {
10957 smp->type = SMP_T_UINT;
10958 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10959 }
10960
10961 return ret;
10962}
10963
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010964/* This produces a 32-bit hash of the concatenation of the first occurrence of
10965 * the Host header followed by the path component if it begins with a slash ('/').
10966 * This means that '*' will not be added, resulting in exactly the first Host
10967 * entry. If no Host header is found, then the path is used. The resulting value
10968 * is hashed using the url hash followed by a full avalanche hash and provides a
10969 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10970 * high-traffic sites without having to store whole paths.
10971 * this differs from the base32 functions in that it includes the url parameters
10972 * as well as the path
10973 */
10974static int
10975smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010976 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010977{
10978 struct http_txn *txn = l7;
10979 struct hdr_ctx ctx;
10980 unsigned int hash = 0;
10981 char *ptr, *beg, *end;
10982 int len;
10983
10984 CHECK_HTTP_MESSAGE_FIRST();
10985
10986 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010987 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010988 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10989 ptr = ctx.line + ctx.val;
10990 len = ctx.vlen;
10991 while (len--)
10992 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10993 }
10994
10995 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010996 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 +000010997 beg = http_get_path(txn);
10998 if (!beg)
10999 beg = end;
11000
11001 for (ptr = beg; ptr < end ; ptr++);
11002
11003 if (beg < ptr && *beg == '/') {
11004 while (beg < ptr)
11005 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11006 }
11007 hash = full_hash(hash);
11008
11009 smp->type = SMP_T_UINT;
11010 smp->data.uint = hash;
11011 smp->flags = SMP_F_VOL_1ST;
11012 return 1;
11013}
11014
11015/* This concatenates the source address with the 32-bit hash of the Host and
11016 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11017 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11018 * on the source address length. The URL hash is stored before the address so
11019 * that in environments where IPv6 is insignificant, truncating the output to
11020 * 8 bytes would still work.
11021 */
11022static int
11023smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011024 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011025{
11026 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011027 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011028
Willy Tarreaue155ec22013-11-18 18:33:22 +010011029 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011030 return 0;
11031
11032 temp = get_trash_chunk();
11033 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11034 temp->len += sizeof(smp->data.uint);
11035
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011036 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011037 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011038 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011039 temp->len += 4;
11040 break;
11041 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011042 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011043 temp->len += 16;
11044 break;
11045 default:
11046 return 0;
11047 }
11048
11049 smp->data.str = *temp;
11050 smp->type = SMP_T_BIN;
11051 return 1;
11052}
11053
Willy Tarreau185b5c42012-04-26 15:11:51 +020011054/* This function is used to validate the arguments passed to any "hdr" fetch
11055 * keyword. These keywords support an optional positive or negative occurrence
11056 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11057 * is assumed that the types are already the correct ones. Returns 0 on error,
11058 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11059 * error message in case of error, that the caller is responsible for freeing.
11060 * The initial location must either be freeable or NULL.
11061 */
11062static int val_hdr(struct arg *arg, char **err_msg)
11063{
11064 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011065 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011066 return 0;
11067 }
11068 return 1;
11069}
11070
Willy Tarreau276fae92013-07-25 14:36:01 +020011071/* takes an UINT value on input supposed to represent the time since EPOCH,
11072 * adds an optional offset found in args[0] and emits a string representing
11073 * the date in RFC-1123/5322 format.
11074 */
11075static int sample_conv_http_date(const struct arg *args, struct sample *smp)
11076{
11077 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11078 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11079 struct chunk *temp;
11080 struct tm *tm;
11081 time_t curr_date = smp->data.uint;
11082
11083 /* add offset */
11084 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11085 curr_date += args[0].data.sint;
11086
11087 tm = gmtime(&curr_date);
11088
11089 temp = get_trash_chunk();
11090 temp->len = snprintf(temp->str, temp->size - temp->len,
11091 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11092 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11093 tm->tm_hour, tm->tm_min, tm->tm_sec);
11094
11095 smp->data.str = *temp;
11096 smp->type = SMP_T_STR;
11097 return 1;
11098}
11099
Thierry FOURNIERad903512014-04-11 17:51:01 +020011100/* Match language range with language tag. RFC2616 14.4:
11101 *
11102 * A language-range matches a language-tag if it exactly equals
11103 * the tag, or if it exactly equals a prefix of the tag such
11104 * that the first tag character following the prefix is "-".
11105 *
11106 * Return 1 if the strings match, else return 0.
11107 */
11108static inline int language_range_match(const char *range, int range_len,
11109 const char *tag, int tag_len)
11110{
11111 const char *end = range + range_len;
11112 const char *tend = tag + tag_len;
11113 while (range < end) {
11114 if (*range == '-' && tag == tend)
11115 return 1;
11116 if (*range != *tag || tag == tend)
11117 return 0;
11118 range++;
11119 tag++;
11120 }
11121 /* Return true only if the last char of the tag is matched. */
11122 return tag == tend;
11123}
11124
11125/* Arguments: The list of expected value, the number of parts returned and the separator */
11126static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11127{
11128 const char *al = smp->data.str.str;
11129 const char *end = al + smp->data.str.len;
11130 const char *token;
11131 int toklen;
11132 int qvalue;
11133 const char *str;
11134 const char *w;
11135 int best_q = 0;
11136
11137 /* Set the constant to the sample, because the output of the
11138 * function will be peek in the constant configuration string.
11139 */
11140 smp->flags |= SMP_F_CONST;
11141 smp->data.str.size = 0;
11142 smp->data.str.str = "";
11143 smp->data.str.len = 0;
11144
11145 /* Parse the accept language */
11146 while (1) {
11147
11148 /* Jump spaces, quit if the end is detected. */
11149 while (al < end && isspace(*al))
11150 al++;
11151 if (al >= end)
11152 break;
11153
11154 /* Start of the fisrt word. */
11155 token = al;
11156
11157 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
11158 while (al < end && *al != ';' && *al != ',' && !isspace(*al))
11159 al++;
11160 if (al == token)
11161 goto expect_comma;
11162
11163 /* Length of the token. */
11164 toklen = al - token;
11165 qvalue = 1000;
11166
11167 /* Check if the token exists in the list. If the token not exists,
11168 * jump to the next token.
11169 */
11170 str = args[0].data.str.str;
11171 w = str;
11172 while (1) {
11173 if (*str == ';' || *str == '\0') {
11174 if (language_range_match(token, toklen, w, str-w))
11175 goto look_for_q;
11176 if (*str == '\0')
11177 goto expect_comma;
11178 w = str + 1;
11179 }
11180 str++;
11181 }
11182 goto expect_comma;
11183
11184look_for_q:
11185
11186 /* Jump spaces, quit if the end is detected. */
11187 while (al < end && isspace(*al))
11188 al++;
11189 if (al >= end)
11190 goto process_value;
11191
11192 /* If ',' is found, process the result */
11193 if (*al == ',')
11194 goto process_value;
11195
11196 /* If the character is different from ';', look
11197 * for the end of the header part in best effort.
11198 */
11199 if (*al != ';')
11200 goto expect_comma;
11201
11202 /* Assumes that the char is ';', now expect "q=". */
11203 al++;
11204
11205 /* Jump spaces, process value if the end is detected. */
11206 while (al < end && isspace(*al))
11207 al++;
11208 if (al >= end)
11209 goto process_value;
11210
11211 /* Expect 'q'. If no 'q', continue in best effort */
11212 if (*al != 'q')
11213 goto process_value;
11214 al++;
11215
11216 /* Jump spaces, process value if the end is detected. */
11217 while (al < end && isspace(*al))
11218 al++;
11219 if (al >= end)
11220 goto process_value;
11221
11222 /* Expect '='. If no '=', continue in best effort */
11223 if (*al != '=')
11224 goto process_value;
11225 al++;
11226
11227 /* Jump spaces, process value if the end is detected. */
11228 while (al < end && isspace(*al))
11229 al++;
11230 if (al >= end)
11231 goto process_value;
11232
11233 /* Parse the q value. */
11234 qvalue = parse_qvalue(al, &al);
11235
11236process_value:
11237
11238 /* If the new q value is the best q value, then store the associated
11239 * language in the response. If qvalue is the biggest value (1000),
11240 * break the process.
11241 */
11242 if (qvalue > best_q) {
11243 smp->data.str.str = (char *)w;
11244 smp->data.str.len = str - w;
11245 if (qvalue >= 1000)
11246 break;
11247 best_q = qvalue;
11248 }
11249
11250expect_comma:
11251
11252 /* Expect comma or end. If the end is detected, quit the loop. */
11253 while (al < end && *al != ',')
11254 al++;
11255 if (al >= end)
11256 break;
11257
11258 /* Comma is found, jump it and restart the analyzer. */
11259 al++;
11260 }
11261
11262 /* Set default value if required. */
11263 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11264 smp->data.str.str = args[1].data.str.str;
11265 smp->data.str.len = args[1].data.str.len;
11266 }
11267
11268 /* Return true only if a matching language was found. */
11269 return smp->data.str.len != 0;
11270}
11271
William Lallemand73025dd2014-04-24 14:38:37 +020011272/*
11273 * Return the struct http_req_action_kw associated to a keyword.
11274 */
11275struct http_req_action_kw *action_http_req_custom(const char *kw)
11276{
11277 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11278 struct http_req_action_kw_list *kw_list;
11279 int i;
11280
11281 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11282 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11283 if (!strcmp(kw, kw_list->kw[i].kw))
11284 return &kw_list->kw[i];
11285 }
11286 }
11287 }
11288 return NULL;
11289}
11290
11291/*
11292 * Return the struct http_res_action_kw associated to a keyword.
11293 */
11294struct http_res_action_kw *action_http_res_custom(const char *kw)
11295{
11296 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11297 struct http_res_action_kw_list *kw_list;
11298 int i;
11299
11300 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11301 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11302 if (!strcmp(kw, kw_list->kw[i].kw))
11303 return &kw_list->kw[i];
11304 }
11305 }
11306 }
11307 return NULL;
11308}
11309
Willy Tarreau4a568972010-05-12 08:08:50 +020011310/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011311/* All supported ACL keywords must be declared here. */
11312/************************************************************************/
11313
11314/* Note: must not be declared <const> as its list will be overwritten.
11315 * Please take care of keeping this list alphabetically sorted.
11316 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011317static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011318 { "base", "base", PAT_MATCH_STR },
11319 { "base_beg", "base", PAT_MATCH_BEG },
11320 { "base_dir", "base", PAT_MATCH_DIR },
11321 { "base_dom", "base", PAT_MATCH_DOM },
11322 { "base_end", "base", PAT_MATCH_END },
11323 { "base_len", "base", PAT_MATCH_LEN },
11324 { "base_reg", "base", PAT_MATCH_REG },
11325 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011326
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011327 { "cook", "req.cook", PAT_MATCH_STR },
11328 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11329 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11330 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11331 { "cook_end", "req.cook", PAT_MATCH_END },
11332 { "cook_len", "req.cook", PAT_MATCH_LEN },
11333 { "cook_reg", "req.cook", PAT_MATCH_REG },
11334 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011335
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011336 { "hdr", "req.hdr", PAT_MATCH_STR },
11337 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11338 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11339 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11340 { "hdr_end", "req.hdr", PAT_MATCH_END },
11341 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11342 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11343 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011344
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011345 /* these two declarations uses strings with list storage (in place
11346 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11347 * and delete functions are relative to the list management. The parse
11348 * and match method are related to the corresponding fetch methods. This
11349 * is very particular ACL declaration mode.
11350 */
11351 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11352 { "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 +020011353
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011354 { "path", "path", PAT_MATCH_STR },
11355 { "path_beg", "path", PAT_MATCH_BEG },
11356 { "path_dir", "path", PAT_MATCH_DIR },
11357 { "path_dom", "path", PAT_MATCH_DOM },
11358 { "path_end", "path", PAT_MATCH_END },
11359 { "path_len", "path", PAT_MATCH_LEN },
11360 { "path_reg", "path", PAT_MATCH_REG },
11361 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011362
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011363 { "req_ver", "req.ver", PAT_MATCH_STR },
11364 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011365
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011366 { "scook", "res.cook", PAT_MATCH_STR },
11367 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11368 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11369 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11370 { "scook_end", "res.cook", PAT_MATCH_END },
11371 { "scook_len", "res.cook", PAT_MATCH_LEN },
11372 { "scook_reg", "res.cook", PAT_MATCH_REG },
11373 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011374
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011375 { "shdr", "res.hdr", PAT_MATCH_STR },
11376 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11377 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11378 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11379 { "shdr_end", "res.hdr", PAT_MATCH_END },
11380 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11381 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11382 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011383
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011384 { "url", "url", PAT_MATCH_STR },
11385 { "url_beg", "url", PAT_MATCH_BEG },
11386 { "url_dir", "url", PAT_MATCH_DIR },
11387 { "url_dom", "url", PAT_MATCH_DOM },
11388 { "url_end", "url", PAT_MATCH_END },
11389 { "url_len", "url", PAT_MATCH_LEN },
11390 { "url_reg", "url", PAT_MATCH_REG },
11391 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011392
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011393 { "urlp", "urlp", PAT_MATCH_STR },
11394 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11395 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11396 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11397 { "urlp_end", "urlp", PAT_MATCH_END },
11398 { "urlp_len", "urlp", PAT_MATCH_LEN },
11399 { "urlp_reg", "urlp", PAT_MATCH_REG },
11400 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011401
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011402 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011403}};
11404
11405/************************************************************************/
11406/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011407/************************************************************************/
11408/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011409static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011410 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011411 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11412 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11413
William Lallemanda43ba4e2014-01-28 18:14:25 +010011414 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011415 { "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 +020011416
11417 /* retrieve these captures from the HTTP logs */
11418 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11419 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11420 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11421
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011422 { "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 +020011423 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011424
Willy Tarreau409bcde2013-01-08 00:31:00 +010011425 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11426 * are only here to match the ACL's name, are request-only and are used
11427 * for ACL compatibility only.
11428 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011429 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11430 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011431 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11432 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11433
11434 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11435 * only here to match the ACL's name, are request-only and are used for
11436 * ACL compatibility only.
11437 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011438 { "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 +010011439 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11440 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11441 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11442
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011443 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011444 { "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 +010011445 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011446 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011447 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011448
11449 /* HTTP protocol on the request path */
11450 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011451 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011452
11453 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011454 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11455 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011456
11457 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011458 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11459 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011460
Willy Tarreau18ed2562013-01-14 15:56:36 +010011461 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011462 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011463 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11464 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11465
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011466 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011467 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011468 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011469 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11470 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11471 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11472
11473 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011474 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011475 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11476 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11477
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011478 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011479 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011480 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011481 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11482 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11483 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11484
Willy Tarreau409bcde2013-01-08 00:31:00 +010011485 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011486 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011487 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11488 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011489 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011490
11491 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011492 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011493 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11494 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11495 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11496
11497 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011498 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011499 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11500 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011501 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11502 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011503 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11504 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011505 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11506 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011507}};
11508
Willy Tarreau8797c062007-05-07 00:55:35 +020011509
Willy Tarreau276fae92013-07-25 14:36:01 +020011510/* Note: must not be declared <const> as its list will be overwritten */
11511static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011512 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11513 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011514 { NULL, NULL, 0, 0, 0 },
11515}};
11516
Willy Tarreau8797c062007-05-07 00:55:35 +020011517__attribute__((constructor))
11518static void __http_protocol_init(void)
11519{
11520 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011521 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011522 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011523}
11524
11525
Willy Tarreau58f10d72006-12-04 02:26:12 +010011526/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011527 * Local variables:
11528 * c-indent-level: 8
11529 * c-basic-offset: 8
11530 * End:
11531 */